canopeas/discord/utils_test.go

44 lines
679 B
Go
Raw 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)
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.Millisecond * 500)
if ok := limit.Try(); ok {
now()
t.Log("Third try: Rate limit should fail.")
t.FailNow()
}
time.Sleep(time.Millisecond * 500)
if ok := limit.Try(); !ok {
now()
t.Log("Fourth try: Rate limit should pass.")
t.FailNow()
}
}