forgejo/tests/integration/actions_fetch_task_test.go
Mathieu Fenniak f25f4447ac
Some checks are pending
/ release (push) Waiting to run
testing-integration / test-unit (push) Waiting to run
testing-integration / test-sqlite (push) Waiting to run
testing-integration / test-mariadb (v10.6) (push) Waiting to run
testing-integration / test-mariadb (v11.8) (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
feat: provide multiple tasks to Runner in one FetchTask when requested (#10602)
Permits the Forgejo to return multiple tasks to the Runner in one API call, if requested.  Fixes #8917.

Related runner PR: https://code.forgejo.org/forgejo/runner/pulls/1245

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests

- I added test coverage for Go changes...
  - [ ] in their respective `*_test.go` for unit tests.
  - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10602
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2025-12-28 22:49:49 +01:00

89 lines
2.6 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package integration
import (
"net/url"
"strings"
"testing"
unit_model "forgejo.org/models/unit"
"forgejo.org/models/unittest"
user_model "forgejo.org/models/user"
"forgejo.org/modules/setting"
files_service "forgejo.org/services/repository/files"
"forgejo.org/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestActionFetchTask_TaskCapacity(t *testing.T) {
if !setting.Database.Type.IsSQLite3() {
// mock repo runner only supported on SQLite testing
t.Skip()
}
onApplicationRun(t, func(t *testing.T, u *url.URL) {
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
// create the repo
repo, _, f := tests.CreateDeclarativeRepo(t, user2, "repo-many-tasks",
[]unit_model.Type{unit_model.TypeActions}, nil,
[]*files_service.ChangeRepoFile{
{
Operation: "create",
TreePath: ".forgejo/workflows/matrix.yml",
ContentReader: strings.NewReader(`
on:
push:
jobs:
job1:
strategy:
# matrix creates 125 different jobs from one push...
matrix:
d1: [a, b, c, d, e]
d2: [a, b, c, d, e]
d3: [a, b, c, d, e]
runs-on: ubuntu-latest
steps:
- run: echo ${{ matrix.d1 }} ${{ matrix.d2 }} ${{ matrix.d3 }}
- run: sleep 2
`),
},
},
)
defer f()
runner := newMockRunner()
runner.registerAsRepoRunner(t, user2.Name, repo.Name, "mock-runner", []string{"ubuntu-latest"})
// Fetch with TaskCapacity undefined, set to nil, should return a single pending task
task := runner.fetchTask(t)
require.NotNil(t, task)
assert.Contains(t, string(task.GetWorkflowPayload()), "name: job1 (a, a, a)")
// After successfully fetching a task, the runner sets their next requested version to 0. This allows it to
// fetch back-to-back tasks without requiring that a server-side state change occurs. That behaviour is
// replicated here:
runner.lastTasksVersion = 0
// Fetch with TaskCapacity set to 1; additional should be nil
capacity := int64(1)
task, addt := runner.fetchMultipleTasks(t, &capacity)
require.NotNil(t, task, "task")
assert.Nil(t, addt, "addt")
assert.Contains(t, string(task.GetWorkflowPayload()), "name: job1 (a, a, b)")
runner.lastTasksVersion = 0
capacity = 10
task, addt = runner.fetchMultipleTasks(t, &capacity)
require.NotNil(t, task, "task")
require.NotNil(t, addt, "addt")
assert.Contains(t, string(task.GetWorkflowPayload()), "name: job1 (a, a, c)")
require.Len(t, addt, 9)
assert.Contains(t, string(addt[0].GetWorkflowPayload()), "name: job1 (a, a, d)")
})
}