1
0
Fork 0
golang-github-twin-g8/ratelimiter_test.go
Daniel Baumann 8274b1bf1b
Adding upstream version 3.0.0.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-05-16 22:32:21 +02:00

73 lines
2.2 KiB
Go

package g8
import (
"testing"
"time"
)
func TestNewRateLimiter(t *testing.T) {
rl := NewRateLimiter(2)
if rl.maximumExecutionsPerSecond != 2 {
t.Errorf("expected maximumExecutionsPerSecond to be %d, got %d", 2, rl.maximumExecutionsPerSecond)
}
if rl.executionsLeftInWindow != 2 {
t.Errorf("expected executionsLeftInWindow to be %d, got %d", 2, rl.executionsLeftInWindow)
}
// First execution: should not be rate limited
if notRateLimited := rl.Try(); !notRateLimited {
t.Error("expected Try to return true")
}
if rl.maximumExecutionsPerSecond != 2 {
t.Errorf("expected maximumExecutionsPerSecond to be %d, got %d", 2, rl.maximumExecutionsPerSecond)
}
if rl.executionsLeftInWindow != 1 {
t.Errorf("expected executionsLeftInWindow to be %d, got %d", 1, rl.executionsLeftInWindow)
}
// Second execution: should not be rate limited
if notRateLimited := rl.Try(); !notRateLimited {
t.Error("expected Try to return true")
}
if rl.maximumExecutionsPerSecond != 2 {
t.Errorf("expected maximumExecutionsPerSecond to be %d, got %d", 2, rl.maximumExecutionsPerSecond)
}
if rl.executionsLeftInWindow != 0 {
t.Errorf("expected executionsLeftInWindow to be %d, got %d", 0, rl.executionsLeftInWindow)
}
// Third execution: should be rate limited
if notRateLimited := rl.Try(); notRateLimited {
t.Error("expected Try to return false")
}
if rl.maximumExecutionsPerSecond != 2 {
t.Errorf("expected maximumExecutionsPerSecond to be %d, got %d", 2, rl.maximumExecutionsPerSecond)
}
if rl.executionsLeftInWindow != 0 {
t.Errorf("expected executionsLeftInWindow to be %d, got %d", 0, rl.executionsLeftInWindow)
}
}
func TestRateLimiter_Try(t *testing.T) {
rl := NewRateLimiter(5)
for i := 0; i < 20; i++ {
notRateLimited := rl.Try()
if i < 5 {
if !notRateLimited {
t.Fatal("expected to not be rate limited")
}
} else {
if notRateLimited {
t.Fatal("expected to be rate limited")
}
}
}
}
func TestRateLimiter_TryAlwaysUnderRateLimit(t *testing.T) {
rl := NewRateLimiter(20)
for i := 0; i < 45; i++ {
notRateLimited := rl.Try()
if !notRateLimited {
t.Fatal("expected to not be rate limited")
}
time.Sleep(51 * time.Millisecond)
}
}