-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_and_status_group_test.go
442 lines (345 loc) · 11 KB
/
error_and_status_group_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package error_group
import (
"crypto/rand"
"errors"
"fmt"
"github.com/jgroeneveld/trial/assert"
"math/big"
"strconv"
"strings"
"sync"
"testing"
)
func TestErrorStatusGroupMultipleThreads(t *testing.T) {
esg := NewErrorStatusGroup()
var sAndEWG sync.WaitGroup
var eWG sync.WaitGroup
var sWG sync.WaitGroup
toAdd := 100000
maxRoutines := 1000
sandEGuard := make(chan struct{}, maxRoutines)
eGuard := make(chan struct{}, maxRoutines)
sGuard := make(chan struct{}, maxRoutines)
for i := 0; i < toAdd; i++ {
sandEGuard <- struct{}{}
sAndEWG.Add(1)
go func() {
<-sandEGuard
esg.AddStatusAndError(GenerateRandomNumber(), errors.New(generateRandomString(20)))
sAndEWG.Done()
}()
}
for i := 0; i < toAdd; i++ {
eGuard <- struct{}{}
eWG.Add(1)
go func() {
<-eGuard
esg.AddError(errors.New(generateRandomString(20)))
eWG.Done()
}()
}
for i := 0; i < toAdd; i++ {
sGuard <- struct{}{}
sWG.Add(1)
go func() {
<-sGuard
esg.AddStatus(GenerateRandomNumber())
sWG.Done()
}()
}
sWG.Wait()
eWG.Wait()
sAndEWG.Wait()
t.Run("verify the number of status values is correct", func(t *testing.T) {
assert.Equal(t, 2*toAdd, esg.LenErrors())
})
t.Run("verify the number of error values is correct", func(t *testing.T) {
assert.Equal(t, 2*toAdd, esg.LenStatuses())
})
}
func TestErrorStatusGroup_AddError(t *testing.T) {
esg := NewErrorStatusGroup()
var wg sync.WaitGroup
numToAdd := 100000
maxRoutines := 1000
guard := make(chan struct{}, maxRoutines)
for i := 0; i < numToAdd; i++ {
guard <- struct{}{}
wg.Add(1)
go func() {
<-guard
esg.AddError(errors.New(generateRandomString(20)))
wg.Done()
}()
}
wg.Wait()
t.Run("verify AddError() correctly added all errors", func(t *testing.T) {
assert.Equal(t, esg.LenErrors(), numToAdd)
})
}
func TestErrorStatusGroup_AddStatus(t *testing.T) {
esg := NewErrorStatusGroup()
var wg sync.WaitGroup
numToAdd := 100000
maxRoutines := 1000
guard := make(chan struct{}, maxRoutines)
for i := 0; i < numToAdd; i++ {
guard <- struct{}{}
wg.Add(1)
go func() {
<-guard
esg.AddStatus(GenerateRandomNumber())
wg.Done()
}()
}
wg.Wait()
t.Run("verify all status values were added via AddStatus() and check with LenStatuses()", func(t *testing.T) {
assert.Equal(t, esg.LenStatuses(), numToAdd)
})
}
func TestErrorStatusGroup_AddStatusAndError(t *testing.T) {
esg := NewErrorStatusGroup()
var wg sync.WaitGroup
numToAdd := 100000
maxRoutines := 1000
guard := make(chan struct{}, maxRoutines)
for i := 0; i < numToAdd; i++ {
guard <- struct{}{}
wg.Add(1)
go func() {
<-guard
esg.AddStatusAndError(GenerateRandomNumber(), errors.New(generateRandomString(10)))
wg.Done()
}()
}
wg.Wait()
t.Run("verify all values were added via AddStatusAndError() and check with LenErrors()", func(t *testing.T) {
assert.Equal(t, esg.LenErrors(), numToAdd)
})
t.Run("verify all values were added via AddStatusAndError() and check with LenStatuses()", func(t *testing.T) {
assert.Equal(t, esg.LenStatuses(), numToAdd)
})
}
func TestErrorStatusGroup_All(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
esg := NewErrorStatusGroup()
esg.AddStatusAndError(100, errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
esg.AddStatusAndError(200, errors.New(middleMessage))
}
esg.AddStatusAndError(300, errors.New(lastMessage))
allStatuses, allErrors := esg.All()
t.Run("verify All() returns the correct number of errors", func(t *testing.T) {
assert.Equal(t, len(allErrors), 12)
})
t.Run("verify All() returns the correct number of statuses", func(t *testing.T) {
assert.Equal(t, len(allStatuses), 12)
})
t.Run("verify slice returned by All() is not affected by more calls to AddError()", func(t *testing.T) {
esg.AddError(errors.New(generateRandomString(10)))
assert.Equal(t, len(allErrors), 12)
})
t.Run("verify slice returned by All() is not affected by more calls to AddStatus()", func(t *testing.T) {
esg.AddStatus(GenerateRandomNumber())
assert.Equal(t, len(allStatuses), 12)
})
}
func TestErrorStatusGroup_Error(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
esg := NewErrorStatusGroup()
esg.AddStatusAndError(100, errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
esg.AddStatusAndError(200, errors.New(middleMessage))
}
esg.AddStatusAndError(300, errors.New(lastMessage))
t.Run("verify output of Error() is correct", func(t *testing.T) {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("lowest status: [%d]", 100))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("highest status: [%d]", 300))
sb.WriteString("\n")
var stringsToConcat []string
stringsToConcat = append(stringsToConcat, firstMessage)
for i := 0; i < numToAdd; i++ {
stringsToConcat = append(stringsToConcat, middleMessage)
}
stringsToConcat = append(stringsToConcat, lastMessage)
sb.WriteString(strings.Join(stringsToConcat, "\n"))
assert.Equal(t, sb.String(), esg.Error())
})
t.Run("verify Error() returns the empty string when there are no errors", func(t *testing.T) {
other := NewErrorStatusGroup()
assert.Equal(t, "", other.Error())
})
}
func TestErrorStatusGroup_FirstError(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
esg := NewErrorStatusGroup()
esg.AddStatusAndError(100, errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
esg.AddStatusAndError(200, errors.New(middleMessage))
}
esg.AddStatusAndError(300, errors.New(lastMessage))
t.Run("verify FirstError() returns the correct error value", func(t *testing.T) {
assert.Equal(t, esg.FirstError().Error(), firstMessage)
})
}
func TestErrorStatusGroup_FirstStatus(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
esg := NewErrorStatusGroup()
esg.AddStatusAndError(100, errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
esg.AddStatusAndError(200, errors.New(middleMessage))
}
esg.AddStatusAndError(300, errors.New(lastMessage))
t.Run("verify FirstStatus() returns the correct status value", func(t *testing.T) {
assert.Equal(t, esg.FirstStatus(), 100)
})
}
func TestErrorStatusGroup_HighestStatus(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
esg := NewErrorStatusGroup()
esg.AddStatusAndError(100, errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
esg.AddStatusAndError(200, errors.New(middleMessage))
}
esg.AddStatusAndError(300, errors.New(lastMessage))
t.Run("verify HighestStatus() returns the correct status value", func(t *testing.T) {
assert.Equal(t, 300, esg.HighestStatus())
})
}
func TestErrorStatusGroup_LastError(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
esg := NewErrorStatusGroup()
esg.AddStatusAndError(100, errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
esg.AddStatusAndError(200, errors.New(middleMessage))
}
esg.AddStatusAndError(300, errors.New(lastMessage))
t.Run("verify LastError() returns the correct error value", func(t *testing.T) {
assert.Equal(t, lastMessage, esg.LastError().Error())
})
}
func TestErrorStatusGroup_LastStatus(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
esg := NewErrorStatusGroup()
esg.AddStatusAndError(100, errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
esg.AddStatusAndError(200, errors.New(middleMessage))
}
esg.AddStatusAndError(300, errors.New(lastMessage))
t.Run("verify LastStatus() returns the correct status value", func(t *testing.T) {
assert.Equal(t, 300, esg.LastStatus())
})
}
func TestErrorStatusGroup_LowestStatus(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
esg := NewErrorStatusGroup()
esg.AddStatusAndError(100, errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
esg.AddStatusAndError(200, errors.New(middleMessage))
}
esg.AddStatusAndError(300, errors.New(lastMessage))
t.Run("verify LowestStatus() returns the correct status value", func(t *testing.T) {
assert.Equal(t, 100, esg.LowestStatus())
})
}
func TestErrorStatusGroup_ToStatusAndError(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
esg := NewErrorStatusGroup()
esg.AddStatusAndError(100, errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
esg.AddStatusAndError(200, errors.New(middleMessage))
}
esg.AddStatusAndError(300, errors.New(lastMessage))
t.Run("verify output of ToStatusAndError() is correct", func(t *testing.T) {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("lowest status: [%d]", 100))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("highest status: [%d]", 300))
sb.WriteString("\n")
var stringsToConcat []string
stringsToConcat = append(stringsToConcat, firstMessage)
for i := 0; i < numToAdd; i++ {
stringsToConcat = append(stringsToConcat, middleMessage)
}
stringsToConcat = append(stringsToConcat, lastMessage)
sb.WriteString(strings.Join(stringsToConcat, "\n"))
statusCode, errVal := esg.ToStatusAndError()
assert.Equal(t, 300, statusCode)
assert.Equal(t, sb.String(), errVal.Error())
})
}
func TestErrorStatusGroup_ToError(t *testing.T) {
firstMessage := "first message"
lastMessage := "last message"
middleMessage := "middle message"
esg := NewErrorStatusGroup()
esg.AddStatusAndError(100, errors.New(firstMessage))
numToAdd := 10
for i := 0; i < numToAdd; i++ {
esg.AddStatusAndError(200, errors.New(middleMessage))
}
esg.AddStatusAndError(300, errors.New(lastMessage))
t.Run("verify output of ToError() is correct", func(t *testing.T) {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("lowest status: [%d]", 100))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("highest status: [%d]", 300))
sb.WriteString("\n")
var stringsToConcat []string
stringsToConcat = append(stringsToConcat, firstMessage)
for i := 0; i < numToAdd; i++ {
stringsToConcat = append(stringsToConcat, middleMessage)
}
stringsToConcat = append(stringsToConcat, lastMessage)
sb.WriteString(strings.Join(stringsToConcat, "\n"))
errString := esg.ToError().Error()
assert.Equal(t, sb.String(), errString)
})
t.Run("verify ToError() returns nil when there are no errors", func(t *testing.T) {
other := NewErrorStatusGroup()
assert.Nil(t, other.ToError())
})
}
func GenerateRandomNumber() int {
const letters = "123456789"
numLength := 3
ret := make([]byte, numLength)
for i := 0; i < numLength; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
return 0
}
ret[i] = letters[num.Int64()]
}
byteToInt, _ := strconv.Atoi(string(ret))
return byteToInt
}