Files
air/runner/test_util.go
T
xiantang d29a90a122 Optimize unit test performance from 60s to 35s (42% improvement) (#843)
Reduce test execution time through smart waiting and selective parallelization.

Changes:
- Replace fixed sleep delays with condition-based waiting (20ms polling)
- Add CI-aware timeout multiplier (2x in CI environments)
- Enable parallel execution for 30+ pure function tests
- Add test and test-ci Make targets
- Update GitHub Actions workflow with CI flag and timeout

Performance:
- Before: ~60 seconds
- After: ~35 seconds
- Improvement: 42% faster (25 seconds saved)

Technical details:
- New helpers: waitForCondition(), waitForEngineState() in test_util.go
- Optimized tests: TestRebuild, TestRun, TestRebuildWhenRunCmdUsingDLV, etc.
- Parallelized: config_test.go (6 tests), flag_test.go (1 test), util_test.go (13 tests)
- Avoided parallelizing tests with global state (os.Setenv, os.Chdir, signal handlers)

Limitations:
- Some tests cannot be parallelized due to Go 1.25 restrictions on t.Parallel() + t.Setenv()
- Pre-existing race conditions in engine tests remain (not addressed in this change)
2026-01-05 21:37:23 +08:00

60 lines
1.6 KiB
Go

// Package runner …
package runner
import (
"fmt"
"os"
"testing"
"time"
)
func chdir(t *testing.T, targetDir string) {
originalDir, err := os.Getwd()
if err != nil {
t.Fatalf("failed to getwd: %v", err)
}
if err := os.Chdir(targetDir); err != nil {
t.Fatalf("failed to change working directory: %v", err)
}
t.Cleanup(func() {
if err := os.Chdir(originalDir); err != nil {
t.Fatalf("failed to restore working directory: %v", err)
}
})
}
// waitForCondition waits for a condition to be true with fast polling.
// Uses environment-aware timeout multiplier for CI compatibility.
func waitForCondition(t *testing.T, timeout time.Duration, condition func() bool, description string) error {
t.Helper()
// CI environments may be slower, use 2x timeout
timeoutMultiplier := 1.0
if os.Getenv("CI") != "" {
timeoutMultiplier = 2.0
}
adjustedTimeout := time.Duration(float64(timeout) * timeoutMultiplier)
deadline := time.Now().Add(adjustedTimeout)
ticker := time.NewTicker(20 * time.Millisecond) // Fast polling: 20ms
defer ticker.Stop()
for {
if condition() {
return nil
}
if time.Now().After(deadline) {
return fmt.Errorf("timeout waiting for: %s (timeout: %v)", description, adjustedTimeout)
}
<-ticker.C
}
}
// waitForEngineState waits for engine to reach the specified running state.
func waitForEngineState(t *testing.T, engine *Engine, running bool, timeout time.Duration) error {
t.Helper()
return waitForCondition(t, timeout, func() bool {
return engine.running.Load() == running
}, fmt.Sprintf("engine running=%v", running))
}