-
Notifications
You must be signed in to change notification settings - Fork 136
/
message_test.go
111 lines (102 loc) · 2.98 KB
/
message_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package engo
import "testing"
type testMessageCounter struct {
counter, counter2 int
}
func (testMessageCounter) Type() string {
return "testMessageCounter"
}
func TestMessageCounterSimple(t *testing.T) {
mailbox := &MessageManager{}
msg := testMessageCounter{}
mailbox.Listen("testMessageCounter", func(message Message) {
m, ok := message.(*testMessageCounter)
if !ok {
t.Error("Message should be of type testMessageCounter")
}
m.counter++
})
mailbox.Dispatch(&msg)
if msg.counter != 1 {
t.Error("Message should have been received 1 times by now")
}
mailbox.Dispatch(&msg)
if msg.counter != 2 {
t.Error("Message should have been received 2 times by now")
}
}
func TestMessageCounterWithRemoval(t *testing.T) {
mailbox := &MessageManager{}
msg := testMessageCounter{}
handlerID := mailbox.Listen("testMessageCounter", func(message Message) {
m, ok := message.(*testMessageCounter)
if !ok {
t.Error("Message should be of type testMessageCounter")
}
m.counter++
})
mailbox.Dispatch(&msg)
if msg.counter != 1 {
t.Error("Message should have been received 1 times by now")
}
mailbox.StopListen("testMessageCounter", handlerID)
mailbox.Dispatch(&msg)
if msg.counter != 1 {
t.Error("Message should have been received exactly 1 times since its handler was removed from listeners")
}
}
func TestMessageListenOnce(t *testing.T) {
mailbox := &MessageManager{}
msg := testMessageCounter{}
mailbox.ListenOnce("testMessageCounter", func(message Message) {
m, ok := message.(*testMessageCounter)
if !ok {
t.Error("Message should be of type testMessageCounter")
}
m.counter++
})
mailbox.Listen("testMessageCounter", func(message Message) {
m, ok := message.(*testMessageCounter)
if !ok {
t.Error("Message should be of type TestMessageCounter")
}
m.counter2++
})
mailbox.Dispatch(&msg)
if msg.counter != 1 {
t.Error("Message should have been received 1 times by now")
}
if msg.counter2 != 1 {
t.Error("Message should have been recieved by second listener 1 time by now")
}
mailbox.Dispatch(&msg)
if msg.counter != 1 {
t.Error("Message should have been received exactly 1 times since its been added by ListenOnce()")
}
if msg.counter2 != 2 {
t.Error("Message should have been recieved exactly 2 times by second listener")
}
}
func TestMessageRemoveNotExistAndMultipleMessageTypes(t *testing.T) {
mailbox := &MessageManager{}
msg := testMessageCounter{}
resizeMsg := WindowResizeMessage{}
textMsg := TextMessage{}
mailbox.Listen("testMessageCounter", func(message Message) {
m, ok := message.(*testMessageCounter)
if !ok {
t.Error("Message should be of type TestMessageCounter")
}
m.counter++
})
mailbox.StopListen("TestingNonExistantMessage", 10)
mailbox.Dispatch(&resizeMsg)
mailbox.Dispatch(&textMsg)
if msg.counter != 0 {
t.Error("Message counter should be 0 since no messages were dispatched to it")
}
mailbox.Dispatch(&msg)
if msg.counter != 1 {
t.Error("Message counter should be 1. Only one message was dispatched to it")
}
}