fallback_test.go82 lines, 2.5 KB
| 1 | package ratelimit |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // flaky is a limiter we can switch off, the way Redis is when it is stopped. |
| 11 | type flaky struct { |
| 12 | *Limiter |
| 13 | down bool |
| 14 | asked int |
| 15 | } |
| 16 | |
| 17 | func (f *flaky) Allow(ctx context.Context, key string) (Result, error) { |
| 18 | f.asked++ |
| 19 | if f.down { |
| 20 | return Result{}, errors.New("redis is away") |
| 21 | } |
| 22 | return f.Limiter.Allow(ctx, key) |
| 23 | } |
| 24 | |
| 25 | // fallback returns a Fallback from a flaky limiter to one in memory, both with a |
| 26 | // burst of two, and the clock all three of them share. |
| 27 | func fallback() (*Fallback, *flaky, *time.Time) { |
| 28 | clock := noon |
| 29 | now := func() time.Time { return clock } |
| 30 | first, spare := &flaky{Limiter: New("test", 2, 60)}, New("test", 2, 60) |
| 31 | first.now, spare.now = now, now |
| 32 | f := NewFallback(first, spare) |
| 33 | f.now = now |
| 34 | return f, first, &clock |
| 35 | } |
| 36 | |
| 37 | func TestFallback(t *testing.T) { |
| 38 | f, first, clock := fallback() |
| 39 | steps := []struct { |
| 40 | wait time.Duration |
| 41 | down bool // is the first limiter away? |
| 42 | left int // tokens left in the bucket that answered |
| 43 | asked int // how often the first limiter has been asked so far |
| 44 | }{ |
| 45 | {0, false, 1, 1}, // the shared bucket: two, minus one |
| 46 | {0, true, 1, 2}, // it fails, and the spare answers from a bucket of its own |
| 47 | {0, true, 0, 2}, // for a second, nobody asks the first limiter again |
| 48 | {time.Second, true, 0, 3}, // then one request goes and looks, and falls back again |
| 49 | {time.Second, false, 1, 4}, // it is back, and its bucket has had two seconds to fill |
| 50 | {0, false, 0, 5}, // from here on it is asked every time |
| 51 | } |
| 52 | for i, s := range steps { |
| 53 | *clock = clock.Add(s.wait) |
| 54 | first.down = s.down |
| 55 | res, err := f.Allow(context.Background(), "ann") |
| 56 | if err != nil || !res.Allowed || res.Remaining != s.left || first.asked != s.asked { |
| 57 | t.Fatalf("step %d: got %+v (%v) after asking %d times, want %d left after %d", |
| 58 | i+1, res, err, first.asked, s.left, s.asked) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // stuck is a limiter that never answers, the way Redis is when it hangs. |
| 64 | type stuck struct{ Allower } |
| 65 | |
| 66 | func (stuck) Allow(ctx context.Context, _ string) (Result, error) { |
| 67 | <-ctx.Done() |
| 68 | return Result{}, ctx.Err() |
| 69 | } |
| 70 | |
| 71 | func TestFallbackDoesNotWait(t *testing.T) { |
| 72 | f, first, _ := fallback() |
| 73 | f.Allower, f.patience = stuck{first}, time.Millisecond |
| 74 | began := time.Now() |
| 75 | res, err := f.Allow(context.Background(), "ann") |
| 76 | if err != nil || !res.Allowed { |
| 77 | t.Fatalf("got %+v (%v), want a yes from the spare", res, err) |
| 78 | } |
| 79 | if took := time.Since(began); took > time.Second { |
| 80 | t.Fatalf("the answer took %v", took) |
| 81 | } |
| 82 | } |