diff options
author | obscuren <geffobscura@gmail.com> | 2014-07-07 16:53:25 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-07-07 16:53:25 +0800 |
commit | 9dab7dcc3c99d387ad2c0a1623b574d3023bdfae (patch) | |
tree | 8f98cce69568d29a954410e18dce8c121d25592e /ethreact/reactor_test.go | |
parent | d40cba3042564f3471aa20a5cf477cafcacc2189 (diff) | |
parent | b958179263aa1fa5060dd5d5848b8bb092a4165c (diff) | |
download | dexon-9dab7dcc3c99d387ad2c0a1623b574d3023bdfae.tar.gz dexon-9dab7dcc3c99d387ad2c0a1623b574d3023bdfae.tar.zst dexon-9dab7dcc3c99d387ad2c0a1623b574d3023bdfae.zip |
Merge branch 'develop' of github.com-obscure:ethereum/eth-go into develop
Diffstat (limited to 'ethreact/reactor_test.go')
-rw-r--r-- | ethreact/reactor_test.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/ethreact/reactor_test.go b/ethreact/reactor_test.go new file mode 100644 index 000000000..801a8abd0 --- /dev/null +++ b/ethreact/reactor_test.go @@ -0,0 +1,63 @@ +package ethreact + +import ( + "fmt" + "testing" +) + +func TestReactorAdd(t *testing.T) { + reactor := New() + ch := make(chan Event) + reactor.Subscribe("test", ch) + if reactor.eventHandlers["test"] == nil { + t.Error("Expected new eventHandler to be created") + } + reactor.Unsubscribe("test", ch) + if reactor.eventHandlers["test"] != nil { + t.Error("Expected eventHandler to be removed") + } +} + +func TestReactorEvent(t *testing.T) { + var name string + reactor := New() + // Buffer the channel, so it doesn't block for this test + cap := 20 + ch := make(chan Event, cap) + reactor.Subscribe("even", ch) + reactor.Subscribe("odd", ch) + reactor.Post("even", "disappears") // should not broadcast if engine not started + reactor.Start() + for i := 0; i < cap; i++ { + if i%2 == 0 { + name = "even" + } else { + name = "odd" + } + reactor.Post(name, i) + } + reactor.Post("test", cap) // this should not block + i := 0 + reactor.Flush() + close(ch) + for event := range ch { + fmt.Printf("%d: %v", i, event) + if i%2 == 0 { + name = "even" + } else { + name = "odd" + } + if val, ok := event.Resource.(int); ok { + if i != val || event.Name != name { + t.Error("Expected event %d to be of type %s and resource %d, got ", i, name, i, val) + } + } else { + t.Error("Unable to cast") + } + i++ + } + if i != cap { + t.Error("excpected exactly %d events, got ", i) + } + reactor.Stop() +} |