Make worker concurrency tests deterministic

This commit is contained in:
2026-11-26 01:43:32 +00:00
parent a27c2cd89c
commit 73023f6397
3 changed files with 118 additions and 13 deletions

View File

@@ -317,6 +317,7 @@ func typedValueForLane(laneIndex int, chunkIndex int) any {
func TestRunnerBoundsExtractJobsAndStabilizesReverseCompletion(t *testing.T) {
prepared := preparedConcurrentPipeline(t, 3)
started := make(chan int, 6)
finished := make(chan int, 6)
release := make([]chan struct{}, 6)
for i := range release {
release[i] = make(chan struct{})
@@ -327,6 +328,7 @@ func TestRunnerBoundsExtractJobsAndStabilizesReverseCompletion(t *testing.T) {
lane := laneIndex
installExtractOperation(prepared, lane, func(ctx context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
jobIndex := request.Chunk.Index*2 + lane
defer func() { finished <- jobIndex }()
current := active.Add(1)
defer active.Add(-1)
for {
@@ -351,26 +353,68 @@ func TestRunnerBoundsExtractJobsAndStabilizesReverseCompletion(t *testing.T) {
err error
}
done := make(chan outcome, 1)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
joined := false
defer func() {
cancel()
if !joined {
select {
case <-done:
case <-time.After(5 * time.Second):
t.Error("runner did not stop after cancellation")
}
}
}()
go func() {
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Workers: 2})
output, err := New().Run(ctx, RunInput{Prepared: prepared, RawInput: []byte("input"), Workers: 2})
done <- outcome{output: output, err: err}
}()
waitJob := func(ch <-chan int) int {
t.Helper()
select {
case job := <-ch:
return job
case result := <-done:
joined = true
t.Fatalf("runner stopped before handshake: %v", result.err)
case <-ctx.Done():
t.Fatal("timed out waiting for worker handshake")
}
return -1
}
for pair := 0; pair < 3; pair++ {
first, second := <-started, <-started
first, second := waitJob(started), waitJob(started)
want := map[int]bool{pair * 2: true, pair*2 + 1: true}
if !want[first] || !want[second] || first == second {
t.Fatalf("started jobs = %d, %d; want round-robin pair %d", first, second, pair)
}
close(release[second])
if got := waitJob(finished); got != second {
t.Fatalf("completed job = %d, want %d", got, second)
}
close(release[first])
if pair < 2 {
if got := waitJob(finished); got != first {
t.Fatalf("completed job = %d, want %d", got, first)
}
}
}
var result outcome
select {
case result = <-done:
joined = true
case <-ctx.Done():
t.Fatal("runner did not finish after worker releases")
}
result := <-done
if result.err != nil {
t.Fatalf("Run() error = %v", result.err)
}
if got := maximum.Load(); got != 2 {
t.Fatalf("maximum concurrent extract jobs = %d, want 2", got)
}
if got := active.Load(); got != 0 {
t.Fatalf("active extraction jobs after return = %d", got)
}
if len(result.output.Diagnostics.Groups) != 2 {
t.Fatalf("diagnostic groups = %#v, want one deterministic group per lane", result.output.Diagnostics.Groups)
}
@@ -399,6 +443,10 @@ func TestRunnerBoundsConcurrentLaneContinuations(t *testing.T) {
publicLanes := make([]PreparedArtifactLane, len(lanes))
var active atomic.Int32
var maximum atomic.Int32
started := make(chan struct{}, len(lanes))
release := make(chan struct{})
var releaseOnce sync.Once
unblock := func() { releaseOnce.Do(func() { close(release) }) }
for i := range lanes {
lane := base
lane.resolved.ID = fmt.Sprintf("lane-%02d", i)
@@ -409,7 +457,7 @@ func TestRunnerBoundsConcurrentLaneContinuations(t *testing.T) {
extract: func(_ context.Context, _ any, request contracts.ExtractionInput, _ ExecutionContext[any]) (erasedTypedResult, error) {
return erasedTypedResult{Value: codecNotes{Items: []string{request.Chunk.ID}}}, nil
},
merge: func(_ context.Context, _ any, _ contracts.MergeInput[any], _ ExecutionContext[any]) (erasedTypedResult, error) {
merge: func(ctx context.Context, _ any, _ contracts.MergeInput[any], _ ExecutionContext[any]) (erasedTypedResult, error) {
current := active.Add(1)
defer active.Add(-1)
for {
@@ -418,7 +466,12 @@ func TestRunnerBoundsConcurrentLaneContinuations(t *testing.T) {
break
}
}
time.Sleep(5 * time.Millisecond)
started <- struct{}{}
select {
case <-release:
case <-ctx.Done():
return erasedTypedResult{}, ctx.Err()
}
return erasedTypedResult{Value: codecNotes{}}, nil
},
normalize: base.typed.normalize,
@@ -432,16 +485,58 @@ func TestRunnerBoundsConcurrentLaneContinuations(t *testing.T) {
prepared.resolved.Steps[0].ArtifactLanes = resolvedLanes
prepared.Steps[0].ArtifactLanes = publicLanes
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Workers: 2})
if err != nil {
t.Fatalf("Run() error = %v", err)
type outcome struct {
output RunOutput
err error
}
if len(output.normalizeOutputs) != len(lanes) {
t.Fatalf("normalize outputs = %d, want %d", len(output.normalizeOutputs), len(lanes))
done := make(chan outcome, 1)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
joined := false
defer func() {
cancel()
unblock()
if !joined {
select {
case <-done:
case <-time.After(5 * time.Second):
t.Error("runner did not stop after cancellation")
}
}
}()
go func() {
output, err := New().Run(ctx, RunInput{Prepared: prepared, RawInput: []byte("input"), Workers: 2})
done <- outcome{output, err}
}()
for i := 0; i < 2; i++ {
select {
case <-started:
case result := <-done:
joined = true
t.Fatalf("runner stopped before continuations overlapped: %v", result.err)
case <-ctx.Done():
t.Fatal("timed out waiting for overlapping continuations")
}
}
unblock()
var result outcome
select {
case result = <-done:
joined = true
case <-ctx.Done():
t.Fatal("runner did not finish after continuation release")
}
if result.err != nil {
t.Fatalf("Run() error = %v", result.err)
}
if len(result.output.normalizeOutputs) != len(lanes) {
t.Fatalf("normalize outputs = %d, want %d", len(result.output.normalizeOutputs), len(lanes))
}
if got := maximum.Load(); got != 2 {
t.Fatalf("maximum concurrent lane continuations = %d, want 2", got)
}
if got := active.Load(); got != 0 {
t.Fatalf("active continuations after return = %d", got)
}
}
func TestRunnerSelectsFrameworkErrorByStableLaneOrder(t *testing.T) {