2020-07-02 20:00:22 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRateLimit(t *testing.T) {
|
|
|
|
now := func() {
|
|
|
|
t.Logf("Time: %s", time.Now().Format("15:04:05"))
|
|
|
|
}
|
|
|
|
|
|
|
|
now()
|
2021-05-05 02:55:25 +00:00
|
|
|
limit := NewRateLimit(time.Second)
|
2020-07-02 20:00:22 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:55:25 +00:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2020-07-02 20:00:22 +00:00
|
|
|
|
|
|
|
if ok := limit.Try(); ok {
|
|
|
|
now()
|
|
|
|
t.Log("Third try: Rate limit should fail.")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:55:25 +00:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2020-07-02 20:00:22 +00:00
|
|
|
|
|
|
|
if ok := limit.Try(); !ok {
|
|
|
|
now()
|
|
|
|
t.Log("Fourth try: Rate limit should pass.")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|