media: deadline timer seam; backward-clock test fires both legs
Fix round 2 of plan 3 Task 1. waitDeadline takes its timer channel as a parameter; runDeadline passes a real time.NewTimer channel. The backward-clock test feeds the stepped-back reading, then fires the timer by hand, so a slow scheduler can no longer let a real 200 ms timer win before the test tick. TestDeadlineReached keeps the real timer end to end.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+22
−20
cmd/carlos-media/deadline.go+9 −8cmd/carlos-media/deadline_test.go+13 −12
CI passed — run details
diff --git a/cmd/carlos-media/deadline.go b/cmd/carlos-media/deadline.go| index a634b77d6..da755509a 100644 |
| --- a/cmd/carlos-media/deadline.go |
| +++ b/cmd/carlos-media/deadline.go |
| @@ -86,28 +86,29 @@ func runDeadline(getenv func(string) string, now func() time.Time, stop <-chan o |
| if !p.Wait { |
| return p.Exit |
| } |
| + timer := time.NewTimer(p.Sleep) |
| + defer timer.Stop() |
| tick := time.NewTicker(wallCheckEvery) |
| defer tick.Stop() |
| - return waitDeadline(p, now, tick.C, stop) |
| + return waitDeadline(p, now, timer.C, tick.C, stop) |
| } |
| // waitDeadline waits on two legs and exits DeadlineExitReached on |
| // whichever ends first: |
| // |
| -// - the monotonic leg, a time.Timer of p.Sleep. A wall-clock step |
| -// neither shortens nor lengthens it, so a backward step cannot |
| -// extend the run past start plus the deadline. |
| +// - the monotonic leg: a fire on timer, which runDeadline makes a |
| +// time.Timer of p.Sleep (tests pass a channel they fire by hand). A |
| +// wall-clock step neither shortens nor lengthens it, so a backward |
| +// step cannot extend the run past start plus the deadline. |
| // - the wall leg: on each tick, now() at or past p.WallEnd. A forward |
| // step, or a VM paused while the monotonic clock stood still, ends |
| // the run early, never late, against the grant's expiry. |
| // |
| // SIGTERM on stop exits DeadlineExitStopped. |
| -func waitDeadline(p deadlinePlan, now func() time.Time, tick <-chan time.Time, stop <-chan os.Signal) int { |
| - t := time.NewTimer(p.Sleep) |
| - defer t.Stop() |
| +func waitDeadline(p deadlinePlan, now func() time.Time, timer, tick <-chan time.Time, stop <-chan os.Signal) int { |
| for { |
| select { |
| - case <-t.C: |
| + case <-timer: |
| return media.DeadlineExitReached |
| case <-stop: |
| return media.DeadlineExitStopped |
diff --git a/cmd/carlos-media/deadline_test.go b/cmd/carlos-media/deadline_test.go| index 0e8f376b5..17e39d77a 100644 |
| --- a/cmd/carlos-media/deadline_test.go |
| +++ b/cmd/carlos-media/deadline_test.go |
| @@ -254,10 +254,11 @@ func tickOnce(t *testing.T, tick chan<- time.Time, what string) { |
| } |
| } |
| -// startWait runs waitDeadline in the background. |
| -func startWait(p deadlinePlan, now func() time.Time, tick <-chan time.Time) <-chan int { |
| +// startWait runs waitDeadline in the background with a timer channel |
| +// and a tick channel the test fires by hand. |
| +func startWait(p deadlinePlan, now func() time.Time, timer, tick <-chan time.Time) <-chan int { |
| done := make(chan int, 1) |
| - go func() { done <- waitDeadline(p, now, tick, nil) }() |
| + go func() { done <- waitDeadline(p, now, timer, tick, nil) }() |
| return done |
| } |
| @@ -280,7 +281,8 @@ func TestDeadlineWallClockJumpsForward(t *testing.T) { |
| end := fakeStart.Add(time.Hour) |
| c := make(handClock) |
| tick := make(chan time.Time) |
| - done := startWait(deadlinePlan{Wait: true, Sleep: time.Hour, WallEnd: end}, c.now, tick) |
| + // The timer channel never fires: only the wall leg can end it. |
| + done := startWait(deadlinePlan{Wait: true, Sleep: time.Hour, WallEnd: end}, c.now, make(chan time.Time), tick) |
| tickOnce(t, tick, "1 ns before the wall end") |
| c.read(t, end.Add(-time.Nanosecond)) |
| @@ -292,20 +294,19 @@ func TestDeadlineWallClockJumpsForward(t *testing.T) { |
| } |
| // TestDeadlineWallClockStepsBack: a backward step cannot extend the run; |
| -// the monotonic timer still ends it. |
| +// the monotonic timer still ends it. Both legs are fired by hand: the |
| +// tick reads a clock 24 h back (no exit), then the timer fires (exit 0). |
| +// The real timer is TestDeadlineReached's. |
| func TestDeadlineWallClockStepsBack(t *testing.T) { |
| c := make(handClock) |
| - tick := make(chan time.Time) |
| - began := time.Now() |
| - done := startWait(deadlinePlan{Wait: true, Sleep: 200 * time.Millisecond, WallEnd: fakeStart.Add(time.Hour)}, c.now, tick) |
| + timer, tick := make(chan time.Time), make(chan time.Time) |
| + done := startWait(deadlinePlan{Wait: true, Sleep: time.Hour, WallEnd: fakeStart.Add(time.Hour)}, c.now, timer, tick) |
| tickOnce(t, tick, "after the backward step") |
| c.read(t, fakeStart.Add(-24*time.Hour)) |
| - if code := awaitCode(t, done, 5*time.Second, "on the monotonic timer"); code != media.DeadlineExitReached { |
| + tickOnce(t, timer, "of the timer") // completing proves the backward tick did not end it |
| + if code := awaitCode(t, done, 5*time.Second, "on the timer"); code != media.DeadlineExitReached { |
| t.Fatalf("exit %d, want %d", code, media.DeadlineExitReached) |
| } |
| - if took := time.Since(began); took < 200*time.Millisecond { |
| - t.Fatalf("returned after %s, before the timer", took) |
| - } |
| } |
| // TestDeadlineWallLegThroughRun: runDeadline itself ticks. The fake wall |