-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
162 lines (137 loc) · 3.98 KB
/
main_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
package events
import (
"fmt"
"github.com/stretchr/testify/suite"
"testing"
"time"
)
type TestSuite struct {
suite.Suite
testingHub *EventHub
}
func (s *TestSuite) SetupTest() {
ResetEventHub()
s.testingHub = GetEventHub()
}
func (s *TestSuite) TearDownTest() {
s.testingHub.Close()
}
func TestRunSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}
func (s *TestSuite) TestGetEventHub() {
testingHub := GetEventHub()
s.NotNil(testingHub, "hub should not be nil")
}
func (s *TestSuite) TestEventHub_Subscribe() {
testingHub := GetEventHub()
subscriber := &TestSubscriber{}
testingHub.Subscribe(subscriber)
s.Len(testingHub.subscribers, 1, "hub subscribers should have 1 subscriber")
s.Len(testingHub.subscribers[subscriber.GetKey()], 1, "hub subscribers should have 1 subscriber for the key")
s.Len(testingHub.subscribers[subscriber.GetKey()][subscriber.GetPriority()], 1, "hub subscribers should have 1 subscriber for the key and priority")
}
func (s *TestSuite) TestEventHub_DispatchAsync() {
testingHub := GetEventHub()
subscriber := &TestSubscriber{}
testingHub.Subscribe(subscriber)
testingHub.DispatchAsync(&TestEvent{
payload: "A",
delay: 100 * time.Millisecond,
})
testingHub.DispatchAsync(&TestEvent{
payload: "B",
delay: 0,
})
time.Sleep(120 * time.Millisecond)
s.True(subscriber.isHandled, "event should be handled")
s.Equal("BA", subscriber.buffer, "async event should not be handled in order")
}
func (s *TestSuite) TestEventHub_DispatchSync() {
testingHub := GetEventHub()
subscriber := &TestSubscriber{}
testingHub.Subscribe(subscriber)
err := testingHub.DispatchSync(&TestEvent{
payload: "A",
delay: 100 * time.Millisecond,
})
s.Nil(err, "event should be handled")
err = testingHub.DispatchSync(&TestEvent{
payload: "B",
delay: 0,
})
s.Nil(err, "event should be handled")
s.True(subscriber.isHandled, "event should be handled")
s.Equal("AB", subscriber.buffer, "sync event should be handled in order")
}
func (s *TestSuite) TestEventHub_Priority() {
testingHub := GetEventHub()
buffer := ""
subscriber1 := &TestPrioritySubscriber{buffer: &buffer, priority: 1}
subscriber2 := &TestPrioritySubscriber{buffer: &buffer, priority: 2}
subscriber_1 := &TestPrioritySubscriber{buffer: &buffer, priority: -1}
subscriber0 := &TestPrioritySubscriber{buffer: &buffer}
testingHub.Subscribe(subscriber1)
testingHub.Subscribe(subscriber2)
testingHub.Subscribe(subscriber_1)
testingHub.Subscribe(subscriber0)
err := testingHub.DispatchSync(&TestPriorityEvent{})
s.Nil(err, "event should be handled")
err = testingHub.DispatchSync(&TestPriorityEvent{})
s.Nil(err, "event should be handled")
s.Equal("-1012-1012", buffer, "sync event should be handled by priority")
}
func (s *TestSuite) TestEventHub_Close() {
testingHub := GetEventHub()
subscriber := &TestSubscriber{}
testingHub.Subscribe(subscriber)
s.Len(testingHub.channel, 0, "channel should be closed")
}
type TestEvent struct {
payload string
delay time.Duration
}
func (e *TestEvent) GetKey() EventKey {
return "test"
}
func (e *TestEvent) GetPayload() EventPayload {
return e.payload
}
type TestSubscriber struct {
isHandled bool
buffer string
priority int
}
func (s *TestSubscriber) Handle(event Event[EventPayload]) error {
time.Sleep(event.(*TestEvent).delay)
s.isHandled = true
s.buffer += event.GetPayload().(string)
return nil
}
func (s *TestSubscriber) GetKey() EventKey {
return "test"
}
func (s *TestSubscriber) GetPriority() int {
return s.priority
}
type TestPriorityEvent struct{}
func (e *TestPriorityEvent) GetKey() EventKey {
return "test-priority"
}
func (e *TestPriorityEvent) GetPayload() EventPayload {
return ""
}
type TestPrioritySubscriber struct {
buffer *string
priority int
}
func (s *TestPrioritySubscriber) Handle(event Event[EventPayload]) error {
*s.buffer += fmt.Sprintf("%d", s.priority)
return nil
}
func (s *TestPrioritySubscriber) GetKey() EventKey {
return "test-priority"
}
func (s *TestPrioritySubscriber) GetPriority() int {
return s.priority
}