|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package integration_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/alicebob/miniredis/v2" |
| 10 | + "github.com/llm-d-incubation/llm-d-async/pipeline" |
| 11 | + "github.com/llm-d-incubation/llm-d-async/pkg/async/inference/flowcontrol" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// TestGateFactory_RedisQuota_ConcurrencyParsing validates that GateFactory |
| 17 | +// correctly parses "redis-quota" params and produces a working AttributeGate. |
| 18 | +// Packages exercised: flowcontrol (GateFactory) -> pkg/redis (RedisQuotaGate). |
| 19 | +func TestGateFactory_RedisQuota_ConcurrencyParsing(t *testing.T) { |
| 20 | + s := miniredis.RunT(t) |
| 21 | + factory := flowcontrol.NewGateFactory("") |
| 22 | + |
| 23 | + gate, err := factory.CreateGate("redis-quota", map[string]string{ |
| 24 | + "address": s.Addr(), |
| 25 | + "attribute": "model", |
| 26 | + "mode": "concurrency", |
| 27 | + "limit": "2", |
| 28 | + "window": "30s", |
| 29 | + "prefix": "test:", |
| 30 | + }) |
| 31 | + require.NoError(t, err) |
| 32 | + require.NotNil(t, gate) |
| 33 | + |
| 34 | + // Budget always returns 1.0 for quota gates. |
| 35 | + assert.Equal(t, 1.0, gate.Budget(context.Background())) |
| 36 | + |
| 37 | + attrGate, ok := gate.(pipeline.AttributeGate) |
| 38 | + require.True(t, ok, "redis-quota gate should implement AttributeGate") |
| 39 | + |
| 40 | + ctx := context.Background() |
| 41 | + attrs := map[string]string{"model": "gpt-4"} |
| 42 | + |
| 43 | + // Acquire twice (limit=2) — both should succeed. |
| 44 | + ok1, rel1, err := attrGate.Acquire(ctx, attrs) |
| 45 | + require.NoError(t, err) |
| 46 | + assert.True(t, ok1) |
| 47 | + |
| 48 | + ok2, rel2, err := attrGate.Acquire(ctx, attrs) |
| 49 | + require.NoError(t, err) |
| 50 | + assert.True(t, ok2) |
| 51 | + |
| 52 | + // Third acquire — should fail. |
| 53 | + ok3, _, err := attrGate.Acquire(ctx, attrs) |
| 54 | + require.NoError(t, err) |
| 55 | + assert.False(t, ok3, "Third acquire should be denied (limit=2)") |
| 56 | + |
| 57 | + // Release one and retry. |
| 58 | + rel1() |
| 59 | + ok4, rel4, err := attrGate.Acquire(ctx, attrs) |
| 60 | + require.NoError(t, err) |
| 61 | + assert.True(t, ok4, "Should succeed after release") |
| 62 | + |
| 63 | + // Cleanup. |
| 64 | + rel2() |
| 65 | + if rel4 != nil { |
| 66 | + rel4() |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// TestGateFactory_RedisQuota_RateLimitParsing validates rate-limit mode parsing. |
| 71 | +func TestGateFactory_RedisQuota_RateLimitParsing(t *testing.T) { |
| 72 | + s := miniredis.RunT(t) |
| 73 | + factory := flowcontrol.NewGateFactory("") |
| 74 | + |
| 75 | + gate, err := factory.CreateGate("redis-quota", map[string]string{ |
| 76 | + "address": s.Addr(), |
| 77 | + "mode": "rate-limit", |
| 78 | + "limit": "3", |
| 79 | + "window": "1m", |
| 80 | + }) |
| 81 | + require.NoError(t, err) |
| 82 | + require.NotNil(t, gate) |
| 83 | + |
| 84 | + attrGate, ok := gate.(pipeline.AttributeGate) |
| 85 | + require.True(t, ok) |
| 86 | + |
| 87 | + ctx := context.Background() |
| 88 | + attrs := map[string]string{"userid": "alice"} |
| 89 | + |
| 90 | + for i := 0; i < 3; i++ { |
| 91 | + ok, _, err := attrGate.Acquire(ctx, attrs) |
| 92 | + require.NoError(t, err) |
| 93 | + assert.True(t, ok, "Request %d should be allowed", i+1) |
| 94 | + } |
| 95 | + |
| 96 | + // Fourth should be rate limited. |
| 97 | + ok4, _, err := attrGate.Acquire(ctx, attrs) |
| 98 | + require.NoError(t, err) |
| 99 | + assert.False(t, ok4, "Fourth request should be rate limited") |
| 100 | +} |
| 101 | + |
| 102 | +// TestGateFactory_RedisQuota_MissingParams validates error handling for missing |
| 103 | +// required parameters. |
| 104 | +func TestGateFactory_RedisQuota_MissingParams(t *testing.T) { |
| 105 | + factory := flowcontrol.NewGateFactory("") |
| 106 | + |
| 107 | + _, err := factory.CreateGate("redis-quota", map[string]string{ |
| 108 | + "limit": "5", |
| 109 | + }) |
| 110 | + assert.Error(t, err, "Should fail when address is missing") |
| 111 | + |
| 112 | + s := miniredis.RunT(t) |
| 113 | + _, err = factory.CreateGate("redis-quota", map[string]string{ |
| 114 | + "address": s.Addr(), |
| 115 | + }) |
| 116 | + assert.Error(t, err, "Should fail when limit is missing") |
| 117 | +} |
| 118 | + |
| 119 | +// TestGateFactory_RedisQuota_DefaultParams verifies that omitted optional params |
| 120 | +// fall back to documented defaults (attribute=userid, mode=rate-limit, etc.). |
| 121 | +func TestGateFactory_RedisQuota_DefaultParams(t *testing.T) { |
| 122 | + s := miniredis.RunT(t) |
| 123 | + factory := flowcontrol.NewGateFactory("") |
| 124 | + |
| 125 | + gate, err := factory.CreateGate("redis-quota", map[string]string{ |
| 126 | + "address": s.Addr(), |
| 127 | + "limit": "1", |
| 128 | + }) |
| 129 | + require.NoError(t, err) |
| 130 | + |
| 131 | + attrGate, ok := gate.(pipeline.AttributeGate) |
| 132 | + require.True(t, ok) |
| 133 | + |
| 134 | + // Default attribute is "userid", default mode is "rate-limit". |
| 135 | + ctx := context.Background() |
| 136 | + ok1, _, err := attrGate.Acquire(ctx, map[string]string{"userid": "bob"}) |
| 137 | + require.NoError(t, err) |
| 138 | + assert.True(t, ok1) |
| 139 | + |
| 140 | + ok2, _, err := attrGate.Acquire(ctx, map[string]string{"userid": "bob"}) |
| 141 | + require.NoError(t, err) |
| 142 | + assert.False(t, ok2, "Second acquire should be rate limited with default params") |
| 143 | +} |
0 commit comments