Common code for implementing Twitch bots.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.1 KiB

package ctxrl
import (
"testing"
"time"
)
type dummyPublisher struct{ f func() }
func (dw dummyPublisher) Publish() { dw.f() }
func TestRateLimit(t *testing.T) {
ival := time.Millisecond * 20
now := time.Now().Add(-ival)
last := map[string]time.Time{"test": now, "test2": now}
wrap := func(msg string) {
if v := time.Since(last[msg]); v < ival {
t.Errorf("%q => Minimum interval violated: %v < %v", msg, v, ival)
} else {
t.Logf("Called: %q (%v)", msg, v)
}
last[msg] = time.Now()
}
b := New()
b.Send(ContextMessage{
Context: "test",
Publisher: dummyPublisher{f: func() { wrap("test") }},
}, ival)
b.Send(ContextMessage{
Context: "test2",
Publisher: dummyPublisher{f: func() { wrap("test2") }},
}, ival)
b.Send(ContextMessage{
Context: "test",
Publisher: dummyPublisher{f: func() { wrap("test") }},
}, ival)
i := 0
for {
wu, e := b.outputOne()
t.Logf("wu %v e %v", wu, e)
if wu != nil {
<-wu
continue
}
if e {
if i != 3 {
t.Errorf("Expected empty at last iteration: %v", i)
}
break
}
i++
}
}