2016-01-17 07:02:29 +00:00
|
|
|
package eventmgr
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
2016-01-18 00:21:38 +00:00
|
|
|
var test2Tracker = 0
|
|
|
|
|
2016-01-18 07:13:25 +00:00
|
|
|
func handler1(event string, info InfoMap) {
|
2016-01-17 07:02:29 +00:00
|
|
|
t := info["test"].(*testing.T)
|
|
|
|
|
|
|
|
if info["k"].(int) != 3 {
|
|
|
|
t.Error(
|
|
|
|
"For", event,
|
|
|
|
"expected", 3,
|
|
|
|
"got", info["k"],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 07:13:25 +00:00
|
|
|
func handler2First(event string, info InfoMap) {
|
2016-01-18 00:21:38 +00:00
|
|
|
t := info["test"].(*testing.T)
|
|
|
|
|
|
|
|
if test2Tracker != 0 {
|
|
|
|
t.Error(
|
|
|
|
"For", event,
|
|
|
|
"expected", 0,
|
|
|
|
"got", test2Tracker,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
test2Tracker += 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 07:13:25 +00:00
|
|
|
func handler2Second(event string, info InfoMap) {
|
2016-01-18 00:21:38 +00:00
|
|
|
t := info["test"].(*testing.T)
|
|
|
|
|
|
|
|
if test2Tracker != 3 {
|
|
|
|
t.Error(
|
|
|
|
"For", event,
|
|
|
|
"expected", 3,
|
|
|
|
"got", test2Tracker,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
test2Tracker--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 07:13:25 +00:00
|
|
|
func handler2Third(event string, info InfoMap) {
|
2016-01-18 00:21:38 +00:00
|
|
|
t := info["test"].(*testing.T)
|
|
|
|
|
|
|
|
if test2Tracker != 2 {
|
|
|
|
t.Error(
|
|
|
|
"For", event,
|
|
|
|
"expected", 2,
|
|
|
|
"got", test2Tracker,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
test2Tracker += 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttachDispatch(t *testing.T) {
|
2016-01-17 07:02:29 +00:00
|
|
|
var manager EventManager
|
|
|
|
|
2016-01-18 00:21:38 +00:00
|
|
|
// test dispatching
|
2016-01-17 07:02:29 +00:00
|
|
|
manager.Attach("test1", handler1, 0)
|
2016-01-18 07:21:27 +00:00
|
|
|
info := NewInfoMap()
|
2016-01-17 07:02:29 +00:00
|
|
|
info["k"] = 3
|
|
|
|
info["test"] = t
|
|
|
|
manager.Dispatch("test1", info)
|
2016-01-18 00:21:38 +00:00
|
|
|
|
|
|
|
// test priority dispatching
|
|
|
|
manager.Attach("test2", handler2Second, 8)
|
|
|
|
manager.Attach("test2", handler2First, 3)
|
|
|
|
manager.Attach("test2", handler2Third, 16)
|
2016-01-18 07:13:25 +00:00
|
|
|
info = make(InfoMap)
|
2016-01-18 00:21:38 +00:00
|
|
|
info["test"] = t
|
|
|
|
manager.Dispatch("test2", info)
|
|
|
|
|
|
|
|
if test2Tracker != 7 {
|
|
|
|
t.Error(
|
|
|
|
"For", "test2 end",
|
|
|
|
"expected", 7,
|
|
|
|
"got", test2Tracker,
|
|
|
|
)
|
|
|
|
}
|
2016-01-17 07:02:29 +00:00
|
|
|
}
|