test: fix ratelimit bucket tests
Fix flakiness and incorrect assumptions in bucket tests: - Trigger lazy state update explicitly in slack refill test - Adjust burst/rate ratios to reliably trigger throttling in concurrent tests - Increase rate for buffer pool test to avoid timeouts
cognitive committed
Dec 30, 2025 at 02:41 UTC
306948f6abcf960c4696a12eaa812b7483c85aa2
1 file changed
+7
-4
portal/utils/ratelimit/bucket_test.go
+7
-4
@@ -138,6 +138,9 @@ func TestTakeSlackRefill(t *testing.T) {
138
// Wait for slack to refill (more than maxSlack duration)
139
time.Sleep(time.Duration(2*burst*int64(time.Second)/rate) + 100*time.Millisecond)
140
141
+ // Force state update since refill is lazy
142
+ b.Take(1)
143
+
144
b.mu.Lock()
145
allowAtAfterSleep := b.allowAt
146
b.mu.Unlock()
@@ -152,7 +155,7 @@ func TestTakeSlackRefill(t *testing.T) {
155
// TestTakeConcurrent tests concurrent Take calls
156
func TestTakeConcurrent(t *testing.T) {
157
rate := int64(100 * 1024) // 100 KiB/s
155
- burst := rate
158
+ burst := rate / 10
159
b := NewBucket(rate, burst)
160
if b == nil {
161
t.Fatal("NewBucket failed")
@@ -285,7 +288,7 @@ func TestCopyShortWrite(t *testing.T) {
288
// TestCopyConcurrent tests concurrent Copy operations
289
func TestCopyConcurrent(t *testing.T) {
290
rate := int64(100 * 1024) // 100 KiB/s
288
- burst := rate
291
+ burst := rate / 10
292
b := NewBucket(rate, burst)
293
if b == nil {
294
t.Fatal("NewBucket failed")
@@ -337,7 +340,7 @@ func TestCopyWriteError(t *testing.T) {
340
// TestTakeLargeBytes tests Take with very large byte counts
341
func TestTakeLargeBytes(t *testing.T) {
342
rate := int64(1024) // 1 KiB/s
340
- burst := rate * 10
343
+ burst := rate // 1 second burst
344
b := NewBucket(rate, burst)
345
if b == nil {
346
t.Fatal("NewBucket failed")
@@ -359,7 +362,7 @@ func TestBufferPool(t *testing.T) {
362
data := make([]byte, 64*1024) // Exactly buffer size
363
src := bytes.NewReader(data)
364
var dst bytes.Buffer
362
- b := NewBucket(1000, 1000)
365
+ b := NewBucket(100*1024*1024, 100*1024*1024)
366
367
n, err := Copy(&dst, src, b)
368
if err != nil {