ratelimit_test.go110 lines, 3.2 KB
| 1 | package ratelimit |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strconv" |
| 6 | "sync" |
| 7 | "sync/atomic" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/fiston-user/murmur/internal/platform/testdb" |
| 12 | ) |
| 13 | |
| 14 | // noon is where every test's clock starts. It stands still until the test moves it. |
| 15 | var noon = time.Date(2026, 11, 22, 12, 0, 0, 0, time.UTC) |
| 16 | |
| 17 | // limiter returns a Limiter in memory, and its clock. |
| 18 | func limiter(burst, perMinute int) (*Limiter, *time.Time) { |
| 19 | clock := noon |
| 20 | l := New("test", burst, perMinute) |
| 21 | l.now = func() time.Time { return clock } |
| 22 | return l, &clock |
| 23 | } |
| 24 | |
| 25 | // shared returns a Shared limiter on the Compose Redis (make up), and its clock. |
| 26 | func shared(t *testing.T, burst, perMinute int) (*Shared, *time.Time) { |
| 27 | clock := noon |
| 28 | s := NewShared(testdb.Redis(t), "test", burst, perMinute) |
| 29 | s.now = func() time.Time { return clock } |
| 30 | return s, &clock |
| 31 | } |
| 32 | |
| 33 | // both runs one test twice: on the limiter in memory, and on the one in Redis. |
| 34 | // Whatever a limiter promises, both of them have to keep. |
| 35 | func both(t *testing.T, burst, perMinute int, test func(t *testing.T, l Allower, clock *time.Time)) { |
| 36 | t.Run("memory", func(t *testing.T) { |
| 37 | l, clock := limiter(burst, perMinute) |
| 38 | test(t, l, clock) |
| 39 | }) |
| 40 | t.Run("redis", func(t *testing.T) { |
| 41 | l, clock := shared(t, burst, perMinute) |
| 42 | test(t, l, clock) |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | func TestAllow(t *testing.T) { |
| 47 | // three at once, and then one a second |
| 48 | both(t, 3, 60, func(t *testing.T, l Allower, clock *time.Time) { |
| 49 | steps := []struct { |
| 50 | wait time.Duration // the clock moves this far, and then key asks |
| 51 | key string |
| 52 | want Result |
| 53 | }{ |
| 54 | {0, "ann", Result{true, 2, 0}}, |
| 55 | {0, "ann", Result{true, 1, 0}}, |
| 56 | {0, "ann", Result{true, 0, 0}}, |
| 57 | {0, "ann", Result{false, 0, time.Second}}, |
| 58 | {0, "bob", Result{true, 2, 0}}, |
| 59 | {250 * time.Millisecond, "ann", Result{false, 0, 750 * time.Millisecond}}, |
| 60 | {750 * time.Millisecond, "ann", Result{true, 0, 0}}, |
| 61 | {2500 * time.Millisecond, "ann", Result{true, 1, 0}}, |
| 62 | {time.Hour, "ann", Result{true, 2, 0}}, |
| 63 | } |
| 64 | for i, s := range steps { |
| 65 | *clock = clock.Add(s.wait) |
| 66 | got, err := l.Allow(context.Background(), s.key) |
| 67 | if err != nil || got != s.want { |
| 68 | t.Fatalf("step %d: %s got %+v (%v), want %+v", i+1, s.key, got, err, s.want) |
| 69 | } |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | // A hundred goroutines ask for the same key at the same moment, ten times each. |
| 75 | // Exactly the burst gets through. |
| 76 | func TestCrowd(t *testing.T) { |
| 77 | both(t, 20, 60, func(t *testing.T, l Allower, _ *time.Time) { |
| 78 | var allowed atomic.Int64 |
| 79 | var crowd sync.WaitGroup |
| 80 | start := make(chan struct{}) |
| 81 | for range 100 { |
| 82 | crowd.Go(func() { |
| 83 | <-start |
| 84 | for range 10 { |
| 85 | if res, _ := l.Allow(context.Background(), "ann"); res.Allowed { |
| 86 | allowed.Add(1) |
| 87 | } |
| 88 | } |
| 89 | }) |
| 90 | } |
| 91 | close(start) |
| 92 | crowd.Wait() |
| 93 | if got := allowed.Load(); got != 20 { |
| 94 | t.Fatalf("allowed = %d, want the burst of 20", got) |
| 95 | } |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | // A bucket in memory that nobody has used for long enough to be full again is forgotten. |
| 100 | func TestIdleBucketsAreForgotten(t *testing.T) { |
| 101 | l, clock := limiter(3, 60) // an empty bucket is full again after three seconds |
| 102 | for i := range 1000 { |
| 103 | l.Allow(context.Background(), strconv.Itoa(i)) |
| 104 | } |
| 105 | *clock = clock.Add(3 * time.Second) |
| 106 | l.Allow(context.Background(), "late") |
| 107 | if n := len(l.buckets); n != 1 { |
| 108 | t.Fatalf("the limiter holds %d buckets, want only the one that just asked", n) |
| 109 | } |
| 110 | } |