-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast_test.go
174 lines (135 loc) · 4.96 KB
/
broadcast_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package context
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
type testContext struct {
ApplicationContext
}
func (listener *testContext) GetContextId() ContextId {
return ""
}
func (listener *testContext) Get(key string) interface{} {
return nil
}
func (listener *testContext) Put(key string, value interface{}) {
}
var testEventId1 = GetEventId("testEventId1")
var testEventId2 = GetEventId("testEventId2")
func getTestEventId1() ApplicationEventId {
return testEventId1
}
func getTestEventId2() ApplicationEventId {
return testEventId2
}
type testEvent1 struct {
}
func (event testEvent1) GetEventId() ApplicationEventId {
return testEventId1
}
func (event testEvent1) GetParentEventId() ApplicationEventId {
return 0
}
func (event testEvent1) GetSource() interface{} {
return nil
}
func (event testEvent1) GetTimestamp() int64 {
return 0
}
type testEvent2 struct {
}
func (event testEvent2) GetEventId() ApplicationEventId {
return testEventId1
}
func (event testEvent2) GetParentEventId() ApplicationEventId {
return 0
}
func (event testEvent2) GetSource() interface{} {
return nil
}
func (event testEvent2) GetTimestamp() int64 {
return 0
}
type testApplicationListener1 struct {
mock.Mock
}
func (listener testApplicationListener1) GetApplicationListenerName() string {
return "github.com.procyon.projects.testApplicationListener1"
}
func (listener testApplicationListener1) SubscribeEvents() []ApplicationEventId {
return []ApplicationEventId{
getTestEventId1(),
getTestEventId2(),
}
}
func (listener testApplicationListener1) OnApplicationEvent(context Context, event ApplicationEvent) {
listener.Called(context, event)
}
type testApplicationListener2 struct {
mock.Mock
}
func (listener testApplicationListener2) GetApplicationListenerName() string {
return "github.com.procyon.projects.testApplicationListener2"
}
func (listener testApplicationListener2) SubscribeEvents() []ApplicationEventId {
return []ApplicationEventId{
getTestEventId1(),
getTestEventId2(),
}
}
func (listener testApplicationListener2) OnApplicationEvent(context Context, event ApplicationEvent) {
listener.Called(context, event)
}
func TestSimpleApplicationEventBroadcaster_RegisterApplicationListener(t *testing.T) {
broadcaster := NewSimpleApplicationEventBroadcaster()
broadcaster.RegisterApplicationListener(testApplicationListener1{})
assert.Equal(t, 1, len(broadcaster.eventListenerMap[getTestEventId1()]))
assert.Equal(t, 1, len(broadcaster.eventListenerMap[getTestEventId2()]))
}
func TestSimpleApplicationEventBroadcaster_RemoveAllApplicationListeners(t *testing.T) {
broadcaster := NewSimpleApplicationEventBroadcaster()
broadcaster.RegisterApplicationListener(testApplicationListener1{})
broadcaster.RemoveAllApplicationListeners()
assert.Equal(t, 0, len(broadcaster.eventListenerMap))
}
func TestSimpleApplicationEventBroadcaster_BroadcastEvent(t *testing.T) {
context := &testContext{}
broadcaster := NewSimpleApplicationEventBroadcaster()
listener := testApplicationListener1{}
testEvent1 := testEvent1{}
listener.On("OnApplicationEvent", context, testEvent1)
testEvent2 := testEvent2{}
listener.On("OnApplicationEvent", context, testEvent2)
broadcaster.RegisterApplicationListener(listener)
broadcaster.BroadcastEvent(context, testEvent1)
broadcaster.BroadcastEvent(context, testEvent2)
listener.AssertExpectations(t)
}
func TestSimpleApplicationEventBroadcaster_UnregisterApplicationListener(t *testing.T) {
broadcaster := NewSimpleApplicationEventBroadcaster()
listener := testApplicationListener1{}
broadcaster.RegisterApplicationListener(listener)
assert.Equal(t, 2, len(broadcaster.eventListenerMap))
broadcaster.UnregisterApplicationListener(listener)
assert.Equal(t, 0, len(broadcaster.eventListenerMap))
}
func TestNewSimpleApplicationEventBroadcaster_MultipleApplicationListener(t *testing.T) {
broadcaster := NewSimpleApplicationEventBroadcaster()
broadcaster.RegisterApplicationListener(testApplicationListener1{})
broadcaster.RegisterApplicationListener(testApplicationListener2{})
assert.Equal(t, 2, len(broadcaster.eventListenerMap[testEventId1]))
assert.Equal(t, 2, len(broadcaster.eventListenerMap[testEventId1]))
broadcaster.UnregisterApplicationListener(testApplicationListener2{})
assert.Equal(t, 1, len(broadcaster.eventListenerMap[testEventId1]))
assert.Equal(t, 1, len(broadcaster.eventListenerMap[testEventId1]))
}
func TestNewSimpleApplicationEventBroadcaster_RegisterApplicationListenerWithRegisteredListenerBefore(t *testing.T) {
broadcaster := NewSimpleApplicationEventBroadcaster()
broadcaster.RegisterApplicationListener(testApplicationListener1{})
assert.Equal(t, 1, len(broadcaster.eventListenerMap[testEventId1]))
assert.Equal(t, 1, len(broadcaster.eventListenerMap[testEventId1]))
broadcaster.RegisterApplicationListener(testApplicationListener1{})
assert.Equal(t, 1, len(broadcaster.eventListenerMap[testEventId1]))
assert.Equal(t, 1, len(broadcaster.eventListenerMap[testEventId1]))
}