diff --git a/internal/queue/pacer_test.go b/internal/queue/pacer_test.go index bf715a7..4d4ff8b 100644 --- a/internal/queue/pacer_test.go +++ b/internal/queue/pacer_test.go @@ -259,3 +259,35 @@ func TestSleepCtxStopsWhenTheContextIsCancelled(t *testing.T) { t.Fatal(err) } } + +func TestPacerKeepsTheSlotsOfLaterCallersWhenAnEarlierOneGivesUp(t *testing.T) { + f := &fakeTime{t: t0} + release := make(chan struct{}) + p := newPacer(10, f.now, nil) + p.sleep = func(ctx context.Context, d time.Duration) error { + f.sleep(ctx, d) + if d == 100*time.Millisecond { + <-release // B waits until C has reserved its slot, then gives up + return context.Canceled + } + return nil + } + p.Wait(context.Background()) // A: slot 0 + done := make(chan error) + go func() { done <- p.Wait(context.Background()) }() // B: slot +100ms + for len(f.durations()) < 1 { + time.Sleep(time.Millisecond) + } + p.Wait(context.Background()) // C: slot +200ms + close(release) + if err := <-done; err == nil { + t.Fatal("B should have given up") + } + f.mu.Lock() + f.slept = nil + f.mu.Unlock() + p.Wait(context.Background()) // D must come after C, not collide with it + if got := f.durations(); len(got) != 1 || got[0] != 300*time.Millisecond { + t.Fatalf("D sleeps %v, want [300ms]: it took a slot another caller holds", got) + } +} diff --git a/internal/queue/queue_test.go b/internal/queue/queue_test.go index ac6732b..24ae823 100644 --- a/internal/queue/queue_test.go +++ b/internal/queue/queue_test.go @@ -729,7 +729,10 @@ func TestPacingIsCancelledWithoutRecordingAnAttempt(t *testing.T) { cancel() // shutdown arrives while the delivery waits for its slot return ctx.Err() } - e.w.RunOnce(ctx) + st, _ := e.w.RunOnce(ctx) + if st != (Stats{Sent: 1}) { + t.Fatalf("stats %+v: the delivery that did not get its slot is not counted", st) + } list, _ := e.s.ListOutbox(10) if len(list) != 1 || list[0].Attempts != 0 || list[0].LastError != "" { t.Fatalf("a shutdown while waiting is not a delivery failure: %+v", list)