instancectl: wait for the output turn on a signal, not a sleep
turnTaken polled with time.Sleep, which the code-health ratchet counts and the record does not allow. The transfer builder now signals on a channel when it takes the turn; the test waits on it with a five-second timeout.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+13
−7
internal/instancectl/demo_build_transfer_test.go+13 −7
diff --git a/internal/instancectl/demo_build_transfer_test.go b/internal/instancectl/demo_build_transfer_test.go| index 753b5842b..63ab8fdb7 100644 |
| --- a/internal/instancectl/demo_build_transfer_test.go |
| +++ b/internal/instancectl/demo_build_transfer_test.go |
| @@ -23,6 +23,8 @@ import ( |
| type transferBuilder struct { |
| binary []byte |
| turns atomic.Int32 |
| + // took, when set, hears once from each build that takes the turn. |
| + took chan struct{} |
| // grant is how long the signed job has left; zero means a minute. |
| grant time.Duration |
| } |
| @@ -46,6 +48,9 @@ func (b *transferBuilder) Build(ctx context.Context, _ []byte, source io.Reader) |
| return demobuild.BuildOutput{}, err |
| } |
| b.turns.Add(1) |
| + if b.took != nil { |
| + b.took <- struct{}{} |
| + } |
| sum := sha256.Sum256(b.binary) |
| out.Binary, out.SHA256 = b.binary, hex.EncodeToString(sum[:]) |
| return out, nil |
| @@ -141,13 +146,14 @@ func unixControl(t *testing.T, s *Server) string { |
| return sock |
| } |
| -// turnTaken waits until b's build holds the output turn. |
| +// turnTaken waits until b's build holds the output turn. b.took must be |
| +// set, with room for the one build. |
| func turnTaken(t *testing.T, b *transferBuilder) { |
| t.Helper() |
| - for end := time.Now().Add(5 * time.Second); b.turns.Load() == 0; time.Sleep(5 * time.Millisecond) { |
| - if time.Now().After(end) { |
| - t.Fatal("the build never took the output turn") |
| - } |
| + select { |
| + case <-b.took: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("the build never took the output turn") |
| } |
| } |
| @@ -155,7 +161,7 @@ func turnTaken(t *testing.T, b *transferBuilder) { |
| // signed job's own deadline plus five seconds, not the 180 s window: a job |
| // with little time left cannot keep every other finished build waiting. |
| func TestDemoBuildStalledReaderReturnsTurnByGrantDeadline(t *testing.T) { |
| - b := &transferBuilder{binary: transferELF(4 << 20), grant: 300 * time.Millisecond} |
| + b := &transferBuilder{binary: transferELF(4 << 20), grant: 300 * time.Millisecond, took: make(chan struct{}, 1)} |
| sock := unixControl(t, &Server{DemoBox: true, DemoBuilder: b}) |
| stall := make(chan struct{}) |
| done := make(chan struct{}) |
| @@ -180,7 +186,7 @@ func TestDemoBuildStalledReaderReturnsTurnByGrantDeadline(t *testing.T) { |
| // A client that hangs up before reading gives the turn back at once. |
| func TestDemoBuildDisconnectedReaderReturnsTurn(t *testing.T) { |
| - b := &transferBuilder{binary: transferELF(4 << 20)} |
| + b := &transferBuilder{binary: transferELF(4 << 20), took: make(chan struct{}, 1)} |
| sock := unixControl(t, &Server{DemoBox: true, DemoBuilder: b}) |
| stall := make(chan struct{}) |
| conns := make(chan net.Conn, 1) |