snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
35
internal/tools/interval_limiter_test.go
Normal file
35
internal/tools/interval_limiter_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIntervalLimiterThrottles(t *testing.T) {
|
||||
l := NewIntervalLimiter(time.Hour)
|
||||
require.True(t, l.Allow(), "first call should be allowed")
|
||||
require.False(t, l.Allow(), "second call within interval should be throttled")
|
||||
require.False(t, l.Allow())
|
||||
}
|
||||
|
||||
func TestIntervalLimiterConcurrentSingleAllow(t *testing.T) {
|
||||
l := NewIntervalLimiter(time.Hour)
|
||||
const n = 200
|
||||
var allowed int64
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(n)
|
||||
for i := 0; i < n; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if l.Allow() {
|
||||
atomic.AddInt64(&allowed, 1)
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
require.Equal(t, int64(1), allowed, "exactly one concurrent caller should be allowed per interval")
|
||||
}
|
||||
Reference in New Issue
Block a user