Files
upstream-centrifugo/internal/tools/interval_limiter_test.go
upstream-mirror b352a9a30d
Some checks failed
test / Lint (push) Failing after 3s
test / Test with Go 1.26.8 (push) Failing after 2s
snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:17:21 +05:00

36 lines
771 B
Go

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")
}