|
1 | | -// internal/store/redis_test.go |
| 1 | +package store |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/alicebob/miniredis/v2" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/Pavan-Rana/rate-limiter/internal/limiter" |
| 13 | +) |
| 14 | + |
| 15 | +func newTestStore(t *testing.T) (*RedisStore, *miniredis.Miniredis) { |
| 16 | + t.Helper() |
| 17 | + mr := miniredis.RunT(t) |
| 18 | + store, err := NewRedisStore(mr.Addr()) |
| 19 | + require.NoError(t, err) |
| 20 | + return store, mr |
| 21 | +} |
| 22 | + |
| 23 | +func policy(limit int, window time.Duration) limiter.Policy { |
| 24 | + return limiter.Policy{Limit: limit, Window: window} |
| 25 | +} |
| 26 | + |
| 27 | +func TestNewRedisStore_UnreachableServer(t *testing.T) { |
| 28 | + store, err := NewRedisStore("127.0.0.1:1") |
| 29 | + assert.Error(t, err) |
| 30 | + assert.Nil(t, store) |
| 31 | +} |
| 32 | + |
| 33 | +func TestPing_AfterServerDown(t *testing.T) { |
| 34 | + store, mr := newTestStore(t) |
| 35 | + mr.Close() |
| 36 | + assert.Error(t, store.Ping(context.Background())) |
| 37 | +} |
| 38 | + |
| 39 | +func TestAllowRequest_WithinAndBeyondLimit(t *testing.T) { |
| 40 | + store, _ := newTestStore(t) |
| 41 | + p := policy(3, time.Minute) |
| 42 | + ctx := context.Background() |
| 43 | + |
| 44 | + for i := 0; i < p.Limit; i++ { |
| 45 | + allowed, err := store.AllowRequest(ctx, "client:limit", p) |
| 46 | + require.NoError(t, err) |
| 47 | + assert.True(t, allowed, "request %d should be allowed", i+1) |
| 48 | + } |
| 49 | + |
| 50 | + allowed, err := store.AllowRequest(ctx, "client:limit", p) |
| 51 | + require.NoError(t, err) |
| 52 | + assert.False(t, allowed, "request beyond limit should be denied") |
| 53 | +} |
| 54 | + |
| 55 | +func TestAllowRequest_WindowExpiry_ResetsCount(t *testing.T) { |
| 56 | + store, mr := newTestStore(t) |
| 57 | + window := 100 * time.Millisecond |
| 58 | + p := policy(1, window) |
| 59 | + ctx := context.Background() |
| 60 | + |
| 61 | + _, err := store.AllowRequest(ctx, "client:expiry", p) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + mr.FastForward(window + 10*time.Millisecond) |
| 65 | + |
| 66 | + allowed, err := store.AllowRequest(ctx, "client:expiry", p) |
| 67 | + require.NoError(t, err) |
| 68 | + assert.True(t, allowed, "should be allowed after window expiry") |
| 69 | +} |
| 70 | + |
| 71 | +func TestAllowRequest_DifferentKeysAreIsolated(t *testing.T) { |
| 72 | + store, _ := newTestStore(t) |
| 73 | + p := policy(1, time.Minute) |
| 74 | + ctx := context.Background() |
| 75 | + |
| 76 | + _, err := store.AllowRequest(ctx, "client:A", p) |
| 77 | + require.NoError(t, err) |
| 78 | + |
| 79 | + allowed, err := store.AllowRequest(ctx, "client:B", p) |
| 80 | + require.NoError(t, err) |
| 81 | + assert.True(t, allowed, "different keys should not share rate-limit state") |
| 82 | +} |
| 83 | + |
| 84 | +func TestAllowRequest_CancelledContext(t *testing.T) { |
| 85 | + store, _ := newTestStore(t) |
| 86 | + |
| 87 | + ctx, cancel := context.WithCancel(context.Background()) |
| 88 | + cancel() |
| 89 | + |
| 90 | + _, err := store.AllowRequest(ctx, "client:ctx", policy(10, time.Minute)) |
| 91 | + assert.Error(t, err) |
| 92 | +} |
0 commit comments