Files
irc/handle_test.go
T
2018-08-05 11:27:57 +02:00

61 lines
1.4 KiB
Go

package irc_test
import (
"context"
"math/rand"
"strconv"
"testing"
"time"
"git.aiterp.net/gisle/irc"
)
func TestHandle(t *testing.T) {
rng := rand.NewSource(time.Now().UnixNano())
eventName := strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36)
client := irc.New(context.Background(), irc.Config{})
event := irc.NewEvent("test", eventName)
handled := false
irc.Handle(func(event *irc.Event, client *irc.Client) {
t.Log("Got:", event.Kind(), event.Verb())
if event.Kind() == "test" && event.Verb() == eventName {
handled = true
}
})
client.EmitSync(context.Background(), event)
if !handled {
t.Error("Event wasn't handled")
}
handled = false
client.EmitSync(context.Background(), event)
if handled {
t.Error("Event was handled after handler was removed")
}
}
func BenchmarkHandle(b *testing.B) {
rng := rand.NewSource(time.Now().UnixNano())
eventName := strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36)
client := irc.New(context.Background(), irc.Config{})
event := irc.NewEvent("test", eventName)
b.Run("Emit", func(b *testing.B) {
for n := 0; n < b.N; n++ {
client.Emit(event)
}
})
b.Run("EmitSync", func(b *testing.B) {
for n := 0; n < b.N; n++ {
client.EmitSync(context.Background(), event)
}
})
}