test(queue): cover a pacer slot given up while a later caller holds its own

- a caller that gives up must not release a slot when another caller reserved after it
- a delivery that did not get its slot is not counted in the run's stats
This commit is contained in:
tgiachi 2026-11-10 12:55:26 +01:00
commit c853ae7e61
2 changed files with 36 additions and 1 deletions

View file

@ -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)
}
}

View file

@ -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)