canopeas/discord/utils_test.go

44 lines
661 B
Go
Raw Permalink Normal View History

package discord
import (
"testing"
"time"
)
func TestRateLimit(t *testing.T) {
now := func() {
t.Logf("Time: %s", time.Now().Format("15:04:05"))
}
now()
limit := NewRateLimit(time.Second * 2)
if ok := limit.Try(); !ok {
now()
t.Log("First try: Rate limit should pass.")
t.FailNow()
}
if ok := limit.Try(); ok {
now()
t.Log("Second try: Rate limit should fail.")
t.FailNow()
}
time.Sleep(time.Second)
if ok := limit.Try(); ok {
now()
t.Log("Third try: Rate limit should fail.")
t.FailNow()
}
time.Sleep(time.Second)
if ok := limit.Try(); !ok {
now()
t.Log("Fourth try: Rate limit should pass.")
t.FailNow()
}
}