Files
act_runner/pkg/runner/runner_max_parallel_test.go
silverwind f923badec7 Use golangci-lint fmt to format code (#163)
Use `golangci-lint fmt` to format code, upgrading `.golangci.yml` to v2 and mirroring the linter configuration used by https://github.com/go-gitea/gitea. `gci` now handles import ordering into standard, project-local, blank, and default groups.

Mirrors https://github.com/go-gitea/gitea/pull/37194.

Changes:
- Upgrade `.golangci.yml` to v2 format with the same linter set as gitea (minus `prealloc`, `unparam`, `testifylint`, `nilnil` which produced too many pre-existing issues)
- Add path-based exclusions (`bodyclose`, `gosec` in tests; `gosec:G115`/`G117` globally)
- Run lint via `make lint-go` in CI instead of `golangci/golangci-lint-action`, matching the pattern used by other Gitea repos
- Apply safe auto-fixes (`modernize`, `perfsprint`, `usetesting`, etc.)
- Add explanations to existing `//nolint` directives
- Remove dead code (unused `newRemoteReusableWorkflow` and `networkName`), duplicate imports, and shadowed `max` builtins
- Replace deprecated `docker/distribution/reference` with `distribution/reference`
- Fix `Deprecated:` comment casing and simplify nil/len checks

---
This PR was written with the help of Claude Opus 4.7

Reviewed-on: https://gitea.com/gitea/act/pulls/163
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-committed-by: silverwind <me@silverwind.io>
2026-04-18 09:10:09 +00:00

107 lines
2.5 KiB
Go

package runner
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestMaxParallelConfig tests that MaxParallel config is properly set
func TestMaxParallelConfig(t *testing.T) {
t.Run("MaxParallel set to 2", func(t *testing.T) {
config := &Config{
Workdir: "testdata",
MaxParallel: 2,
}
runner, err := New(config)
assert.NoError(t, err)
assert.NotNil(t, runner)
// Verify config is properly stored
runnerImpl, ok := runner.(*runnerImpl)
assert.True(t, ok)
assert.Equal(t, 2, runnerImpl.config.MaxParallel)
})
t.Run("MaxParallel set to 0 (no limit)", func(t *testing.T) {
config := &Config{
Workdir: "testdata",
MaxParallel: 0,
}
runner, err := New(config)
assert.NoError(t, err)
assert.NotNil(t, runner)
runnerImpl, ok := runner.(*runnerImpl)
assert.True(t, ok)
assert.Equal(t, 0, runnerImpl.config.MaxParallel)
})
t.Run("MaxParallel not set (defaults to 0)", func(t *testing.T) {
config := &Config{
Workdir: "testdata",
}
runner, err := New(config)
assert.NoError(t, err)
assert.NotNil(t, runner)
runnerImpl, ok := runner.(*runnerImpl)
assert.True(t, ok)
assert.Equal(t, 0, runnerImpl.config.MaxParallel)
})
}
// TestMaxParallelConcurrencyTracking tests that max-parallel actually limits concurrent execution
func TestMaxParallelConcurrencyTracking(t *testing.T) {
// This is a unit test for the parallel executor logic
// We test that when MaxParallel is set, it limits the number of workers
var mu sync.Mutex
var maxConcurrent int
var currentConcurrent int
// Create a function that tracks concurrent execution
trackingFunc := func() {
mu.Lock()
currentConcurrent++
if currentConcurrent > maxConcurrent {
maxConcurrent = currentConcurrent
}
mu.Unlock()
// Simulate work
time.Sleep(50 * time.Millisecond)
mu.Lock()
currentConcurrent--
mu.Unlock()
}
// Run multiple tasks with limited parallelism
maxConcurrent = 0
currentConcurrent = 0
// This simulates what NewParallelExecutor does with a semaphore
var wg sync.WaitGroup
semaphore := make(chan struct{}, 2) // Limit to 2 concurrent
for range 6 {
wg.Go(func() {
semaphore <- struct{}{} // Acquire
defer func() { <-semaphore }() // Release
trackingFunc()
})
}
wg.Wait()
// With a semaphore of 2, max concurrent should be <= 2
assert.LessOrEqual(t, maxConcurrent, 2, "Maximum concurrent executions should not exceed limit")
assert.GreaterOrEqual(t, maxConcurrent, 1, "Should have at least 1 concurrent execution")
}