|
| 1 | +package ratelimit |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRateLimit_Basic(t *testing.T) { |
| 10 | + l := New() |
| 11 | + defer l.Stop() |
| 12 | + |
| 13 | + actor := []byte("user123") |
| 14 | + action := "test-action" |
| 15 | + bucket := time.Minute |
| 16 | + allowed := uint64(3) |
| 17 | + |
| 18 | + // First 3 should be allowed |
| 19 | + for i := 0; i < 3; i++ { |
| 20 | + if !l.RateLimit(action, actor, bucket, allowed) { |
| 21 | + t.Errorf("Request %d should have been allowed", i+1) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // 4th should be rate limited |
| 26 | + if l.RateLimit(action, actor, bucket, allowed) { |
| 27 | + t.Error("Request 4 should have been rate limited") |
| 28 | + } |
| 29 | + |
| 30 | + // 5th should also be rate limited |
| 31 | + if l.RateLimit(action, actor, bucket, allowed) { |
| 32 | + t.Error("Request 5 should have been rate limited") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestRateLimit_DifferentActors(t *testing.T) { |
| 37 | + l := New() |
| 38 | + defer l.Stop() |
| 39 | + |
| 40 | + actor1 := []byte("user1") |
| 41 | + actor2 := []byte("user2") |
| 42 | + action := "test-action" |
| 43 | + bucket := time.Minute |
| 44 | + allowed := uint64(2) |
| 45 | + |
| 46 | + // Actor 1 uses both requests |
| 47 | + l.RateLimit(action, actor1, bucket, allowed) |
| 48 | + l.RateLimit(action, actor1, bucket, allowed) |
| 49 | + |
| 50 | + // Actor 1 should be limited |
| 51 | + if l.RateLimit(action, actor1, bucket, allowed) { |
| 52 | + t.Error("Actor1 should be rate limited") |
| 53 | + } |
| 54 | + |
| 55 | + // Actor 2 should still be allowed |
| 56 | + if !l.RateLimit(action, actor2, bucket, allowed) { |
| 57 | + t.Error("Actor2 should be allowed") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestRateLimit_DifferentActions(t *testing.T) { |
| 62 | + l := New() |
| 63 | + defer l.Stop() |
| 64 | + |
| 65 | + actor := []byte("user123") |
| 66 | + action1 := "action1" |
| 67 | + action2 := "action2" |
| 68 | + bucket := time.Minute |
| 69 | + allowed := uint64(1) |
| 70 | + |
| 71 | + // Use up action1 limit |
| 72 | + l.RateLimit(action1, actor, bucket, allowed) |
| 73 | + |
| 74 | + // Action1 should be limited |
| 75 | + if l.RateLimit(action1, actor, bucket, allowed) { |
| 76 | + t.Error("Action1 should be rate limited") |
| 77 | + } |
| 78 | + |
| 79 | + // Action2 should still be allowed |
| 80 | + if !l.RateLimit(action2, actor, bucket, allowed) { |
| 81 | + t.Error("Action2 should be allowed") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestRateLimit_BucketReset(t *testing.T) { |
| 86 | + l := New() |
| 87 | + defer l.Stop() |
| 88 | + |
| 89 | + actor := []byte("user123") |
| 90 | + action := "test-action" |
| 91 | + bucket := 100 * time.Millisecond |
| 92 | + allowed := uint64(2) |
| 93 | + |
| 94 | + // Use up the limit |
| 95 | + l.RateLimit(action, actor, bucket, allowed) |
| 96 | + l.RateLimit(action, actor, bucket, allowed) |
| 97 | + |
| 98 | + if l.RateLimit(action, actor, bucket, allowed) { |
| 99 | + t.Error("Should be rate limited") |
| 100 | + } |
| 101 | + |
| 102 | + // Wait for next bucket |
| 103 | + time.Sleep(150 * time.Millisecond) |
| 104 | + |
| 105 | + // Should be allowed again |
| 106 | + if !l.RateLimit(action, actor, bucket, allowed) { |
| 107 | + t.Error("Should be allowed after bucket reset") |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestCount(t *testing.T) { |
| 112 | + l := New() |
| 113 | + defer l.Stop() |
| 114 | + |
| 115 | + actor := []byte("user123") |
| 116 | + action := "test-action" |
| 117 | + bucket := time.Minute |
| 118 | + |
| 119 | + if c := l.Count(action, actor, bucket); c != 0 { |
| 120 | + t.Errorf("Expected count 0, got %d", c) |
| 121 | + } |
| 122 | + |
| 123 | + l.RateLimit(action, actor, bucket, 10) |
| 124 | + l.RateLimit(action, actor, bucket, 10) |
| 125 | + |
| 126 | + if c := l.Count(action, actor, bucket); c != 2 { |
| 127 | + t.Errorf("Expected count 2, got %d", c) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestRemaining(t *testing.T) { |
| 132 | + l := New() |
| 133 | + defer l.Stop() |
| 134 | + |
| 135 | + actor := []byte("user123") |
| 136 | + action := "test-action" |
| 137 | + bucket := time.Minute |
| 138 | + allowed := uint64(5) |
| 139 | + |
| 140 | + if r := l.Remaining(action, actor, bucket, allowed); r != 5 { |
| 141 | + t.Errorf("Expected 5 remaining, got %d", r) |
| 142 | + } |
| 143 | + |
| 144 | + l.RateLimit(action, actor, bucket, allowed) |
| 145 | + l.RateLimit(action, actor, bucket, allowed) |
| 146 | + |
| 147 | + if r := l.Remaining(action, actor, bucket, allowed); r != 3 { |
| 148 | + t.Errorf("Expected 3 remaining, got %d", r) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestRateLimit_Concurrent(t *testing.T) { |
| 153 | + l := New() |
| 154 | + defer l.Stop() |
| 155 | + |
| 156 | + actor := []byte("user123") |
| 157 | + action := "test-action" |
| 158 | + bucket := time.Minute |
| 159 | + allowed := uint64(100) |
| 160 | + |
| 161 | + var wg sync.WaitGroup |
| 162 | + allowedCount := 0 |
| 163 | + var mu sync.Mutex |
| 164 | + |
| 165 | + // Run 200 concurrent requests |
| 166 | + for i := 0; i < 200; i++ { |
| 167 | + wg.Add(1) |
| 168 | + go func() { |
| 169 | + defer wg.Done() |
| 170 | + if l.RateLimit(action, actor, bucket, allowed) { |
| 171 | + mu.Lock() |
| 172 | + allowedCount++ |
| 173 | + mu.Unlock() |
| 174 | + } |
| 175 | + }() |
| 176 | + } |
| 177 | + |
| 178 | + wg.Wait() |
| 179 | + |
| 180 | + // Exactly 100 should have been allowed |
| 181 | + if allowedCount != 100 { |
| 182 | + t.Errorf("Expected 100 allowed, got %d", allowedCount) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestRateLimit_BinaryActor(t *testing.T) { |
| 187 | + l := New() |
| 188 | + defer l.Stop() |
| 189 | + |
| 190 | + // Test with binary data containing special characters |
| 191 | + actor := []byte{0x00, 0x01, 0xFF, ':', '\n'} |
| 192 | + action := "test-action" |
| 193 | + bucket := time.Minute |
| 194 | + allowed := uint64(1) |
| 195 | + |
| 196 | + if !l.RateLimit(action, actor, bucket, allowed) { |
| 197 | + t.Error("First request should be allowed") |
| 198 | + } |
| 199 | + if l.RateLimit(action, actor, bucket, allowed) { |
| 200 | + t.Error("Second request should be rate limited") |
| 201 | + } |
| 202 | +} |
0 commit comments