1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-07-01 19:13:54 +00:00
goirc/event/registry_test.go
Alex Bramley 6e87169e2c Make API for adding/deleting handlers more coherent.
Previously, it was possible to add the same handler (as returned by NewHandler)
to multiple different event lists, but DelHandler only removed a handler from
*all* event lists it was present in. This may not be wanted behaviour, and
reduces control over the event lists.

Instead, allow both Add and DelHandler to take a variadic list of events to
add or delete handlers from.
2011-11-07 13:13:46 +00:00

103 lines
2 KiB
Go

package event
// oh hey unit tests. or functionality tests, or something.
import (
"testing"
"time"
)
func TestSimpleDispatch(t *testing.T) {
r := NewRegistry()
out := make(chan bool)
h := NewHandler(func(ev ...interface{}) {
out <- ev[0].(bool)
})
r.AddHandler(h, "send")
r.Dispatch("send", true)
if val := <-out; !val {
t.Fail()
}
r.Dispatch("send", false)
if val := <-out; val {
t.Fail()
}
}
func TestParallelDispatch(t *testing.T) {
r := NewRegistry()
// ensure we have enough of a buffer that all sends complete
out := make(chan int, 5)
// handler factory :-)
factory := func(t int) Handler {
return NewHandler(func(ev ...interface{}) {
// t * 10ms sleep
time.Sleep(int64(t * 1e7))
out <- t
})
}
// create some handlers and send an event to them
for _, t := range []int{5, 11, 2, 15, 8} {
r.AddHandler(factory(t), "send")
}
r.Dispatch("send")
// If parallel dispatch is working, results from out should be in numerical order
if val := <-out; val != 2 {
t.Fail()
}
if val := <-out; val != 5 {
t.Fail()
}
if val := <-out; val != 8 {
t.Fail()
}
if val := <-out; val != 11 {
t.Fail()
}
if val := <-out; val != 15 {
t.Fail()
}
}
func TestSerialDispatch(t *testing.T) {
r := NewRegistry()
r.(*registry).Serial()
// ensure we have enough of a buffer that all sends complete
out := make(chan int, 5)
// handler factory :-)
factory := func(t int) Handler {
return NewHandler(func(ev ...interface{}) {
// t * 10ms sleep
time.Sleep(int64(t * 1e7))
out <- t
})
}
// create some handlers and send an event to them
for _, t := range []int{5, 11, 2, 15, 8} {
r.AddHandler(factory(t), "send")
}
r.Dispatch("send")
// If serial dispatch is working, results from out should be in handler order
if val := <-out; val != 5 {
t.Fail()
}
if val := <-out; val != 11 {
t.Fail()
}
if val := <-out; val != 2 {
t.Fail()
}
if val := <-out; val != 15 {
t.Fail()
}
if val := <-out; val != 8 {
t.Fail()
}
}