|
| 1 | +package router |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/rudderlabs/rudder-server/router/throttler" |
| 11 | +) |
| 12 | + |
| 13 | +// MockPickupThrottler implements the PickupThrottler interface for testing |
| 14 | +type MockPickupThrottler struct { |
| 15 | + limitPerSecond int64 |
| 16 | + eventType string |
| 17 | +} |
| 18 | + |
| 19 | +func (m *MockPickupThrottler) CheckLimitReached(ctx context.Context, cost int64) (bool, error) { |
| 20 | + return false, nil |
| 21 | +} |
| 22 | + |
| 23 | +func (m *MockPickupThrottler) ResponseCodeReceived(code int) {} |
| 24 | + |
| 25 | +func (m *MockPickupThrottler) Shutdown() {} |
| 26 | + |
| 27 | +func (m *MockPickupThrottler) GetLimitPerSecond() int64 { |
| 28 | + return m.limitPerSecond |
| 29 | +} |
| 30 | + |
| 31 | +func (m *MockPickupThrottler) GetEventType() string { |
| 32 | + return m.eventType |
| 33 | +} |
| 34 | + |
| 35 | +func TestHandle_getAdaptedJobQueryBatchSize(t *testing.T) { |
| 36 | + h := &Handle{} |
| 37 | + |
| 38 | + t.Run("no throttlers available should return original batch size", func(t *testing.T) { |
| 39 | + input := 100 |
| 40 | + pickupThrottlers := func() []throttler.PickupThrottler { |
| 41 | + return []throttler.PickupThrottler{} |
| 42 | + } |
| 43 | + readSleep := 5 * time.Second |
| 44 | + maxLimit := 1000 |
| 45 | + |
| 46 | + result := h.getAdaptedJobQueryBatchSize(input, pickupThrottlers, readSleep, maxLimit) |
| 47 | + |
| 48 | + require.Equal(t, input, result) |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("multiple throttlers of eventType all should use first throttler limit", func(t *testing.T) { |
| 52 | + input := 100 |
| 53 | + pickupThrottlers := func() []throttler.PickupThrottler { |
| 54 | + return []throttler.PickupThrottler{ |
| 55 | + &MockPickupThrottler{limitPerSecond: 50, eventType: "all"}, |
| 56 | + &MockPickupThrottler{limitPerSecond: 30, eventType: "all"}, |
| 57 | + } |
| 58 | + } |
| 59 | + readSleep := 1 * time.Second |
| 60 | + maxLimit := 1000 |
| 61 | + |
| 62 | + result := h.getAdaptedJobQueryBatchSize(input, pickupThrottlers, readSleep, maxLimit) |
| 63 | + |
| 64 | + // Should use the first throttler's limit (50) since subsequent global throttlers are ignored |
| 65 | + require.Equal(t, 50, result) |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("multiple throttlers of eventType all returning zero as limit should return original batch size", func(t *testing.T) { |
| 69 | + input := 100 |
| 70 | + pickupThrottlers := func() []throttler.PickupThrottler { |
| 71 | + return []throttler.PickupThrottler{ |
| 72 | + &MockPickupThrottler{limitPerSecond: 0, eventType: "all"}, |
| 73 | + &MockPickupThrottler{limitPerSecond: 0, eventType: "all"}, |
| 74 | + } |
| 75 | + } |
| 76 | + readSleep := 1 * time.Second |
| 77 | + maxLimit := 1000 |
| 78 | + |
| 79 | + result := h.getAdaptedJobQueryBatchSize(input, pickupThrottlers, readSleep, maxLimit) |
| 80 | + |
| 81 | + require.Equal(t, input, result) |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("multiple throttlers of different eventTypes should return the sum", func(t *testing.T) { |
| 85 | + input := 100 |
| 86 | + pickupThrottlers := func() []throttler.PickupThrottler { |
| 87 | + return []throttler.PickupThrottler{ |
| 88 | + &MockPickupThrottler{limitPerSecond: 20, eventType: "track"}, |
| 89 | + &MockPickupThrottler{limitPerSecond: 30, eventType: "identify"}, |
| 90 | + &MockPickupThrottler{limitPerSecond: 25, eventType: "page"}, |
| 91 | + } |
| 92 | + } |
| 93 | + readSleep := 1 * time.Second |
| 94 | + maxLimit := 1000 |
| 95 | + |
| 96 | + result := h.getAdaptedJobQueryBatchSize(input, pickupThrottlers, readSleep, maxLimit) |
| 97 | + |
| 98 | + // Should sum all limits: 20 + 30 + 25 = 75 |
| 99 | + require.Equal(t, 75, result) |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("multiple throttlers of different eventTypes whose sum is greater than max limit should return max limit", func(t *testing.T) { |
| 103 | + input := 100 |
| 104 | + pickupThrottlers := func() []throttler.PickupThrottler { |
| 105 | + return []throttler.PickupThrottler{ |
| 106 | + &MockPickupThrottler{limitPerSecond: 400, eventType: "track"}, |
| 107 | + &MockPickupThrottler{limitPerSecond: 300, eventType: "identify"}, |
| 108 | + &MockPickupThrottler{limitPerSecond: 500, eventType: "page"}, |
| 109 | + } |
| 110 | + } |
| 111 | + readSleep := 1 * time.Second |
| 112 | + maxLimit := 800 |
| 113 | + |
| 114 | + result := h.getAdaptedJobQueryBatchSize(input, pickupThrottlers, readSleep, maxLimit) |
| 115 | + |
| 116 | + // Sum would be 1200, but should be capped at maxLimit |
| 117 | + require.Equal(t, maxLimit, result) |
| 118 | + }) |
| 119 | + |
| 120 | + t.Run("small readSleep less than a second should use 1 sec min", func(t *testing.T) { |
| 121 | + input := 100 |
| 122 | + pickupThrottlers := func() []throttler.PickupThrottler { |
| 123 | + return []throttler.PickupThrottler{ |
| 124 | + &MockPickupThrottler{limitPerSecond: 50, eventType: "track"}, |
| 125 | + } |
| 126 | + } |
| 127 | + readSleep := 300 * time.Millisecond // Less than 1 second |
| 128 | + maxLimit := 1000 |
| 129 | + |
| 130 | + result := h.getAdaptedJobQueryBatchSize(input, pickupThrottlers, readSleep, maxLimit) |
| 131 | + |
| 132 | + // Should use 1 second minimum: 50 * 1 = 50 |
| 133 | + require.Equal(t, 50, result) |
| 134 | + }) |
| 135 | + |
| 136 | + t.Run("readSleep 2s should adapt batch size to 2 seconds", func(t *testing.T) { |
| 137 | + input := 100 |
| 138 | + pickupThrottlers := func() []throttler.PickupThrottler { |
| 139 | + return []throttler.PickupThrottler{ |
| 140 | + &MockPickupThrottler{limitPerSecond: 30, eventType: "track"}, |
| 141 | + } |
| 142 | + } |
| 143 | + readSleep := 2 * time.Second |
| 144 | + maxLimit := 1000 |
| 145 | + |
| 146 | + result := h.getAdaptedJobQueryBatchSize(input, pickupThrottlers, readSleep, maxLimit) |
| 147 | + |
| 148 | + // Should use 2 seconds: 30 * 2 = 60 |
| 149 | + require.Equal(t, 60, result) |
| 150 | + }) |
| 151 | + |
| 152 | + t.Run("readSleep with fractional seconds should round up", func(t *testing.T) { |
| 153 | + input := 100 |
| 154 | + pickupThrottlers := func() []throttler.PickupThrottler { |
| 155 | + return []throttler.PickupThrottler{ |
| 156 | + &MockPickupThrottler{limitPerSecond: 40, eventType: "track"}, |
| 157 | + } |
| 158 | + } |
| 159 | + readSleep := 1500 * time.Millisecond // 1.5 seconds |
| 160 | + maxLimit := 1000 |
| 161 | + |
| 162 | + result := h.getAdaptedJobQueryBatchSize(input, pickupThrottlers, readSleep, maxLimit) |
| 163 | + |
| 164 | + // Should round up to 2 seconds: 40 * 2 = 80 |
| 165 | + require.Equal(t, 80, result) |
| 166 | + }) |
| 167 | +} |
0 commit comments