-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils_test.go
More file actions
284 lines (230 loc) · 5.27 KB
/
utils_test.go
File metadata and controls
284 lines (230 loc) · 5.27 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package event
import (
"errors"
"math/big"
"sync"
)
// ------Enums------//
const (
Unidentified Identifier = iota
TestEvent1
TestEvent2
TestLiteralEvent
TestErrorEvent
TestErrorEvent2
TestDynamicTopicEvent
TestOrderEvent
TestHandlerOrderEvent
TestConcurrentEvent
BenchmarkOrderedEvent
BenchmarkConcurrentEvent
)
const (
TestTopic Identifier = iota
TestTopic2
TestOrderTopic
BenchmarkOrderTopic
)
//------Events------//
type testEvent1 struct{}
func (*testEvent1) Topic() Identifier { return TestTopic }
func (*testEvent1) Identifier() Identifier {
return TestEvent1
}
type testEvent2 struct{}
func (*testEvent2) Topic() Identifier { return TestTopic2 }
func (*testEvent2) Identifier() Identifier {
return TestEvent2
}
type testEvent3 string
func (testEvent3) Topic() Identifier { return TestTopic2 }
func (testEvent3) Identifier() Identifier {
return TestLiteralEvent
}
type testEventError struct{}
func (*testEventError) Identifier() Identifier {
return TestErrorEvent
}
type testEventError2 struct{}
func (*testEventError2) Topic() Identifier { return TestTopic }
func (*testEventError2) Identifier() Identifier {
return TestErrorEvent2
}
type testEventDynamicTopic struct {
Tpc Identifier
}
func (evt *testEventDynamicTopic) Topic() Identifier {
return evt.Tpc
}
func (*testEventDynamicTopic) Identifier() Identifier {
return TestDynamicTopicEvent
}
type eventOrder interface {
Position() uint32
}
type testEventOrder struct {
position uint32
}
func (evt *testEventOrder) Topic() Identifier { return TestOrderTopic }
func (evt *testEventOrder) Position() uint32 { return evt.position }
func (*testEventOrder) Identifier() Identifier {
return TestOrderEvent
}
type testEventConcurrent struct {
position uint32
}
func (evt *testEventConcurrent) Position() uint32 { return evt.position }
func (*testEventConcurrent) Identifier() Identifier {
return TestConcurrentEvent
}
type testHandlerOrderEvent struct {
position *counter
unordered *flag
}
func (evt *testHandlerOrderEvent) HandlerPosition(position uint32) {
if !evt.position.is(position) {
evt.unordered.enable()
return
}
evt.position.increment()
}
func (*testHandlerOrderEvent) Identifier() Identifier {
return TestHandlerOrderEvent
}
type benchmarkOrderedEvent struct {
}
func (evt *benchmarkOrderedEvent) Topic() Identifier { return BenchmarkOrderTopic }
func (*benchmarkOrderedEvent) Identifier() Identifier {
return BenchmarkOrderedEvent
}
type benchmarkConcurrentEvent struct {
}
func (*benchmarkConcurrentEvent) Identifier() Identifier {
return BenchmarkConcurrentEvent
}
//------Handlers------//
type testHandler1 struct {
wg *sync.WaitGroup
}
func (hdl *testHandler1) Handle(evt Event) error {
if _, listens := evt.(*testEvent1); listens {
hdl.wg.Done()
}
return nil
}
type testHandler2 struct {
wg *sync.WaitGroup
}
func (hdl *testHandler2) Handle(evt Event) error {
switch evt.(type) {
case *testEvent2, testEvent3, *testEventDynamicTopic:
hdl.wg.Done()
}
return nil
}
type emptyHandler struct {
}
func (hdl *emptyHandler) Handle(evt Event) error {
// handles everything
return nil
}
type testHandlerWithError struct {
wg *sync.WaitGroup
}
func (hdl *testHandlerWithError) Handle(evt Event) error {
switch evt.(type) {
case *testEventError, *testEventError2:
hdl.wg.Done()
return errors.New("event failed")
}
return nil
}
type testHandlerOrder struct {
wg *sync.WaitGroup
position uint32
}
func (hdl *testHandlerOrder) Handle(evt Event) error {
if evt, listens := evt.(*testHandlerOrderEvent); listens {
evt.HandlerPosition(hdl.position)
hdl.wg.Done()
}
return nil
}
type testEventOrderHandler struct {
wg *sync.WaitGroup
position *counter
unordered *flag
}
func (hdl *testEventOrderHandler) Handle(evt Event) error {
if evt, listens := evt.(eventOrder); listens {
// delay the handling of the Event to simulate some sort of behavior.
fibonacci(1000)
if !hdl.position.is(evt.Position()) {
hdl.unordered.enable()
}
hdl.position.increment()
hdl.wg.Done()
}
return nil
}
type benchmarkOrderedEventHandler struct {
wg *sync.WaitGroup
}
func (hdl *benchmarkOrderedEventHandler) Handle(evt Event) error {
if _, listens := evt.(*benchmarkOrderedEvent); listens {
fibonacci(1000)
hdl.wg.Done()
}
return nil
}
type benchmarkConcurrentEventHandler struct {
wg *sync.WaitGroup
}
func (hdl *benchmarkConcurrentEventHandler) Handle(evt Event) error {
if _, listens := evt.(*benchmarkConcurrentEvent); listens {
fibonacci(1000)
hdl.wg.Done()
}
return nil
}
//------Error Handlers------//
type storeErrorsHandler struct {
sync.Mutex
wg *sync.WaitGroup
errs map[Identifier]error
}
func (hdl *storeErrorsHandler) Handle(evt Event, err error) {
hdl.Lock()
hdl.errs[hdl.key(evt)] = err
if hdl.wg != nil {
hdl.wg.Done()
}
hdl.Unlock()
}
func (hdl *storeErrorsHandler) Error(evt Event) error {
hdl.Lock()
defer hdl.Unlock()
if err, hasError := hdl.errs[hdl.key(evt)]; hasError {
return err
}
return nil
}
func (hdl *storeErrorsHandler) key(evt Event) Identifier {
if evt == nil {
return Unidentified
} else {
return evt.Identifier()
}
}
//------General------//
func fibonacci(n uint) *big.Int {
if n < 2 {
return big.NewInt(int64(n))
}
a, b := big.NewInt(0), big.NewInt(1)
for n--; n > 0; n-- {
a.Add(a, b)
a, b = b, a
}
return b
}