activator: cmdAcceptGrace is a var, so cmd tests can shrink it
The fake cmd units never connect their streams, so every Drain waited the full one-second accept grace, twice per stop. The const becomes a var with the same value; the three tests that drain unconnected streams shrink it (the grace test itself still checks Drain ends within 3x it).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+15
−2
internal/activator/cmd_test.go+1 −0internal/activator/cmdunit.go+2 −2internal/activator/cmdunit_test.go+12 −0
diff --git a/internal/activator/cmd_test.go b/internal/activator/cmd_test.go| index e34e7dc6e..a11ed0604 100644 |
| --- a/internal/activator/cmd_test.go |
| +++ b/internal/activator/cmd_test.go |
| @@ -556,6 +556,7 @@ func TestALostCmdResponseStopsTheCmdRoleBeforeReplicationCloses(t *testing.T) { |
| cgroupErr error |
| }{{"cgroup empties", nil}, {"cgroup unreadable", errors.New("cgroup.events: permission denied")}} { |
| t.Run(c.name, func(t *testing.T) { |
| + shrinkCmdAcceptGrace(t, 50*time.Millisecond) // the stop drains two streams the fake unit never connected |
| a, cb, _ := testCmdActivator(t, Config{}) |
| ctl := &fakeControl{cmdErr: errors.New("502 mid-roll")} |
| unit := &fakeUnit{} |
diff --git a/internal/activator/cmdunit.go b/internal/activator/cmdunit.go| index a219649be..402813706 100644 |
| --- a/internal/activator/cmdunit.go |
| +++ b/internal/activator/cmdunit.go |
| @@ -351,8 +351,8 @@ func (s *cmdStream) copy(c net.Conn) { |
| } |
| // cmdAcceptGrace is how long Drain waits for a connection still queued in |
| -// the listener's backlog. |
| -const cmdAcceptGrace = time.Second |
| +// the listener's backlog. A var so tests can shrink it. |
| +var cmdAcceptGrace = time.Second |
| // Drain waits up to bound for the stream to reach EOF (its unit has |
| // stopped, so every writer has closed), then closes the listener. The unit |
diff --git a/internal/activator/cmdunit_test.go b/internal/activator/cmdunit_test.go| index ce15edbcd..2f87e3a18 100644 |
| --- a/internal/activator/cmdunit_test.go |
| +++ b/internal/activator/cmdunit_test.go |
| @@ -74,7 +74,18 @@ func startTestCmd(t *testing.T) (*ExecBackend, registry.Route, *fakeControl, *fa |
| return b, rt, ctl, unit, u, out, errs |
| } |
| +// shrinkCmdAcceptGrace sets how long a cmd stream's Drain waits for a |
| +// connection that never came, for one test. The fake units here never |
| +// connect their streams, so every Drain waits the whole grace. |
| +func shrinkCmdAcceptGrace(t *testing.T, d time.Duration) { |
| + t.Helper() |
| + old := cmdAcceptGrace |
| + cmdAcceptGrace = d |
| + t.Cleanup(func() { cmdAcceptGrace = old }) |
| +} |
| + |
| func TestStartCmdWritesTheLaunchFileAndStartsTheUnit(t *testing.T) { |
| + shrinkCmdAcceptGrace(t, 50*time.Millisecond) // the cleanup drains two unconnected streams |
| b, rt, ctl, _, u, _, _ := startTestCmd(t) |
| t.Cleanup(func() { u.StopAndSettle(context.Background()) }) |
| if u.Invocation() != "inv1" || !strings.Contains(ctl.joined(), "cmd "+rt.Host) { |
| @@ -205,6 +216,7 @@ func TestCmdStreamDrainTakesAConnectionStillInTheBacklog(t *testing.T) { |
| } |
| func TestCmdStreamDrainOfAnUnconnectedStreamEndsAfterTheGrace(t *testing.T) { |
| + shrinkCmdAcceptGrace(t, 100*time.Millisecond) |
| dir, err := os.MkdirTemp("", "cs") |
| if err != nil { |
| t.Fatal(err) |