package hostlimit import ( "context" "errors" "fmt" "sync" "testing" "time" ) func fetchConfig(t *testing.T, slots int) Config { t.Helper() c := testConfig(t) c.FetchSlots = slots return c } // keyOnSlot is a key whose hash slot is s among n. func keyOnSlot(t *testing.T, s, n int) string { t.Helper() for i := 0; i < 10000; i++ { k := fmt.Sprintf("v/%d", i) if fetchSlot(k, n) == s { return k } } t.Fatalf("no key on slot %d of %d", s, n) return "" } type held struct { release func() err error } // holdAsync runs HoldFetch in the background; its result arrives on the // channel. The test's cleanup ends the wait, joins it (bounded) and releases // a slot it got, whether or not the test read the result. func holdAsync(t *testing.T, l *Limiter, ctx context.Context, key string) <-chan held { t.Helper() ctx, cancel := context.WithCancel(ctx) got := make(chan held, 1) done := make(chan struct{}) var mu sync.Mutex var release func() go func() { defer close(done) r, err := l.HoldFetch(ctx, key) mu.Lock() release = r mu.Unlock() got <- held{r, err} }() t.Cleanup(func() { cancel() select { case <-done: case <-time.After(5 * time.Second): t.Error("a background fetch did not return after its context ended") return } mu.Lock() defer mu.Unlock() if release != nil { release() // releases are idempotent } }) return got } func mustHold(t *testing.T, l *Limiter, key string) func() { t.Helper() r, err := l.HoldFetch(context.Background(), key) if err != nil { t.Fatal(err) } return r } // While slots are free, a fetch takes one without waiting. func TestHoldFetchTakesAFreeSlotAtOnce(t *testing.T) { c := fetchConfig(t, 2) a, b := newTest(t, c), newTest(t, c) r1 := mustHold(t, a, keyOnSlot(t, 0, 2)) defer r1() start := time.Now() r2 := mustHold(t, b, keyOnSlot(t, 0, 2)) // its hash slot is held: it takes the other one defer r2() if d := time.Since(start); d > 200*time.Millisecond { t.Fatalf("a fetch with a free slot waited %s", d) } } // The cap holds across limiters on one pool (separate processes: see // TestHoldFetchSlotDiesWithItsProcess): with every slot held by others, a // fetch waits until one is released, and then takes it. func TestHoldFetchCapHoldsAcrossLimiters(t *testing.T) { c := fetchConfig(t, 2) a, b, w := newTest(t, c), newTest(t, c), newTest(t, c) r1 := mustHold(t, a, keyOnSlot(t, 0, 2)) r2 := mustHold(t, b, keyOnSlot(t, 1, 2)) defer r2() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() got := holdAsync(t, w, ctx, "v/third") select { case h := <-got: if h.release != nil { h.release() } t.Fatalf("a third fetch held a slot while both were held (err %v)", h.err) case <-time.After(150 * time.Millisecond): } r1() select { case h := <-got: if h.err != nil { t.Fatalf("the waiting fetch: %v", h.err) } h.release() case <-time.After(2 * time.Second): t.Fatal("the waiting fetch did not take the released slot") } } // A waiter whose context ends returns its context's error, and a slot it // gets after that is released at once: a later fetch is not held back by it. func TestHoldFetchAWaiterThatGivesUpLeavesNoSlotHeld(t *testing.T) { c := fetchConfig(t, 1) a, w, later := newTest(t, c), newTest(t, c), newTest(t, c) r1 := mustHold(t, a, "v/a") ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) defer cancel() if _, err := w.HoldFetch(ctx, "v/w"); !errors.Is(err, context.DeadlineExceeded) { t.Fatalf("a waiter past its context answered %v, want its deadline", err) } r1() // the abandoned wait now gets the slot, and must release it at once ctx2, cancel2 := context.WithTimeout(context.Background(), 2*time.Second) defer cancel2() select { case h := <-holdAsync(t, later, ctx2, "v/later"): if h.err != nil { t.Fatalf("a later fetch was held back by an abandoned wait: %v", h.err) } h.release() case <-time.After(3 * time.Second): t.Fatal("a later fetch never got the slot") } } // A waiter blocked behind a slow holder on its own hash slot is not delayed // beyond one retry interval once another slot is free: the host's slots are // one pool, not N separate queues. func TestHoldFetchTakesAnotherSlotThatFreesFirst(t *testing.T) { c := fetchConfig(t, 2) slow, other, w := newTest(t, c), newTest(t, c), newTest(t, c) key := keyOnSlot(t, 0, 2) rSlow := mustHold(t, slow, key) // the waiter's hash slot, held for the whole test defer rSlow() rOther := mustHold(t, other, keyOnSlot(t, 1, 2)) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() got := holdAsync(t, w, ctx, key) select { case h := <-got: if h.release != nil { h.release() } t.Fatalf("the waiter held a slot while both were held (err %v)", h.err) case <-time.After(150 * time.Millisecond): } freed := time.Now() rOther() select { case h := <-got: if h.err != nil { t.Fatalf("the waiter: %v", h.err) } if d := time.Since(freed); d > 300*time.Millisecond { t.Fatalf("the waiter took the freed slot %s after it was freed, behind a slow holder of its own slot", d) } h.release() case <-time.After(2 * time.Second): t.Fatal("the waiter never took the other slot while its own stayed held") } }