From 3e82bf33c3e9f6cd59d89de72a68ce4bd94708ce Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Wed, 27 Jul 2011 17:49:55 +0100 Subject: [PATCH] unit tests, sort of. --- event/registry_test.go | 104 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 event/registry_test.go diff --git a/event/registry_test.go b/event/registry_test.go new file mode 100644 index 0000000..2cd3af6 --- /dev/null +++ b/event/registry_test.go @@ -0,0 +1,104 @@ +package registry + +// 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("send", h) + + 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("send", factory(t)) + } + 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.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("send", factory(t)) + } + 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() + } +} +