-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils_test.go
273 lines (219 loc) · 5.21 KB
/
utils_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
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
package command
import (
"errors"
"fmt"
"math/big"
"sync"
"testing"
"time"
)
// ------Enums------//
const (
Unidentified Identifier = "Unidentified"
TestCommand1 Identifier = "TestCommand1"
TestCommand2 Identifier = "TestCommand2"
TestCommandSlow Identifier = "TestCommandSlow"
TestLiteralCommand Identifier = "TestLiteralCommand"
TestErrorCommand Identifier = "TestErrorCommand"
)
const (
handleTimeoutError = "timeout reached"
unexpectedDataError = "unexpected data"
commandFailedError = "command failed"
middlewareInwardError = "inward middleware failure"
middlewareOutwardError = "outward middleware failure"
)
//------Commands------//
type testCommand struct {
identifier Identifier
}
func (cmd *testCommand) Identifier() Identifier {
return cmd.identifier
}
type testCommand1 struct{}
func (*testCommand1) Identifier() Identifier {
return TestCommand1
}
type testCommand2 struct{}
func (*testCommand2) Identifier() Identifier {
return TestCommand2
}
type testCommandSlow struct{}
func (*testCommandSlow) Identifier() Identifier {
return TestCommandSlow
}
type testCommand3 string
func (testCommand3) Identifier() Identifier {
return TestLiteralCommand
}
type testCommandError struct{}
func (*testCommandError) Identifier() Identifier {
return TestErrorCommand
}
type testFakeClosureCommand struct{}
func (*testFakeClosureCommand) Identifier() Identifier {
return ClosureIdentifier
}
//------Handlers------//
type testHandler struct {
handles Identifier
}
func (hdl *testHandler) Handles() Identifier {
return hdl.handles
}
func (hdl *testHandler) Handle(cmd Command) (data any, err error) {
if _, ok := any(cmd).(*testCommandSlow); ok {
slowFunc()
} else {
fastFunc()
}
return
}
type testErrorHandler struct{}
func (hdl *testErrorHandler) Handles() Identifier {
return TestErrorCommand
}
func (hdl *testErrorHandler) Handle(cmd Command) (data any, err error) {
err = errors.New(commandFailedError)
return
}
type testAsyncHandler struct {
wg *sync.WaitGroup
handles Identifier
}
func (hdl *testAsyncHandler) Handles() Identifier {
return hdl.handles
}
func (hdl *testAsyncHandler) Handle(cmd Command) (data any, err error) {
if _, ok := any(cmd).(*testCommandSlow); ok {
slowFunc()
} else {
fastFunc()
}
hdl.wg.Done()
return
}
type testAsyncAwaitHandler struct {
identifier Identifier
}
func (hdl *testAsyncAwaitHandler) Handles() Identifier {
return hdl.identifier
}
func (hdl *testAsyncAwaitHandler) Handle(cmd Command) (data any, err error) {
data = "not ok"
switch any(cmd).(type) {
case *testCommand1:
fastFunc()
data = nil
case *testCommand2:
fastFunc()
data = "ok"
case *testCommandSlow:
slowFunc()
data = "slow ok"
}
return data, err
}
type testClosureHandler struct {
}
func (hdl *testClosureHandler) Handles() Identifier {
return ClosureIdentifier
}
func (hdl *testClosureHandler) Handle(cmd Command) (data any, err error) {
if cmd, ok := any(cmd).(Closure); ok {
data, err = cmd()
if err != nil {
return
}
data, ok := data.(int)
if !ok {
return nil, errors.New(unexpectedDataError)
}
return 2 + int(data), nil
}
return
}
//------Error Handlers------//
type storeErrorsHandler struct {
sync.Mutex
errs map[Identifier]error
}
func (hdl *storeErrorsHandler) Handle(cmd Command, err error) {
hdl.Lock()
hdl.errs[hdl.key(cmd)] = err
hdl.Unlock()
}
func (hdl *storeErrorsHandler) Error(cmd Command) error {
hdl.Lock()
defer hdl.Unlock()
if err, hasError := hdl.errs[hdl.key(cmd)]; hasError {
return err
}
return nil
}
func (hdl *storeErrorsHandler) key(cmd Command) Identifier {
if cmd == nil {
return Unidentified
}
return cmd.Identifier()
}
// ------Middlewares------//
type testLoggerMiddleware struct {
logHandler chan string
testId string
logged int
}
func newTestLoggerMiddleware(logHandler chan string, testId string) *testLoggerMiddleware {
return &testLoggerMiddleware{
logHandler: logHandler,
testId: testId,
}
}
func (hdl *testLoggerMiddleware) Handle(cmd Command, next Next) (data any, err error) {
hdl.log(fmt.Sprintf("%s|inward|%s", hdl.testId, cmd.Identifier()))
data, err = next(cmd)
hdl.log(fmt.Sprintf("%s|outward|%s", hdl.testId, cmd.Identifier()))
return
}
func (hdl *testLoggerMiddleware) log(message string) {
hdl.logHandler <- message
hdl.logged++
}
type testErrorMiddleware struct {
inwardFailure bool
outwardFailure bool
}
func (hdl *testErrorMiddleware) Handle(cmd Command, next Next) (data any, err error) {
if hdl.inwardFailure {
return nil, errors.New(middlewareInwardError)
}
data, err = next(cmd)
if hdl.outwardFailure {
return nil, errors.New(middlewareOutwardError)
}
return
}
type testMiddleware struct{}
func (hdl *testMiddleware) Handle(cmd Command, next Next) (data any, err error) {
fastFunc()
return next(cmd)
}
//------General------//
var fastFunc = func() { fibonacci(100) }
var slowFunc = func() { fibonacci(100000) }
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
}
func setupHandleTimeout(t *testing.T) *time.Timer {
return time.AfterFunc(time.Second*10, func() {
t.Fatal(handleTimeoutError)
})
}