This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpt_test.go
471 lines (461 loc) · 13.3 KB
/
pt_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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package pt_test
import (
"testing"
"time"
"github.com/maratori/pt"
)
func TestPackageParallel(t *testing.T) {
t.Parallel()
t.Run("should panic on nil T", func(t *testing.T) {
t.Parallel()
defer assertPanic(t, "argument t *testing.T can not be nil")
pt.PackageParallel(nil)
})
t.Run("should not panic without tests", func(t *testing.T) {
t.Parallel()
pt.PackageParallel(t)
})
t.Run("should not panic with 1 test", func(t *testing.T) {
t.Parallel()
pt.PackageParallel(t, testing.InternalTest{F: func(*testing.T) {}})
})
t.Run("should not panic with 2 tests", func(t *testing.T) {
t.Parallel()
pt.PackageParallel(t,
testing.InternalTest{F: func(*testing.T) {}},
testing.InternalTest{F: func(*testing.T) {}},
)
})
t.Run("should not panic if called twice", func(t *testing.T) {
t.Parallel()
pt.PackageParallel(t, testing.InternalTest{F: func(*testing.T) {}})
pt.PackageParallel(t, testing.InternalTest{F: func(*testing.T) {}})
})
t.Run("should run 1 test", func(t *testing.T) {
t.Parallel()
called := false
t.Run("internal", func(it *testing.T) {
// internal2 is necessary because PackageParallel calls t.Parallel()
it.Run("internal2", func(it2 *testing.T) {
pt.PackageParallel(it2, testing.InternalTest{F: func(*testing.T) {
if called {
t.Error("test is called twice")
}
called = true
}})
})
})
if !called {
t.Error("test is not called")
}
})
t.Run("should run 2 tests", func(t *testing.T) {
t.Parallel()
called1 := false
called2 := false
t.Run("internal", func(it *testing.T) {
// internal2 is necessary because PackageParallel calls t.Parallel()
it.Run("internal2", func(it2 *testing.T) {
pt.PackageParallel(it2,
testing.InternalTest{F: func(*testing.T) {
if called1 {
t.Error("test1 is called twice")
}
called1 = true
}},
testing.InternalTest{F: func(*testing.T) {
if called2 {
t.Error("test2 is called twice")
}
called2 = true
}},
)
})
})
if !called1 {
t.Error("test1 is not called")
}
if !called2 {
t.Error("test2 is not called")
}
})
}
func TestPackageParallel2(t *testing.T) {
// Do not call t.Parallel() because test measures execution time
t.Run("should run 2 tests parallel", func(t *testing.T) {
singleTestDuration := 1 * time.Second
expectedMaxDuration := singleTestDuration + 300*time.Millisecond
start := time.Now()
t.Run("internal", func(it *testing.T) {
// internal2 is necessary because PackageParallel calls t.Parallel()
it.Run("internal2", func(it2 *testing.T) {
pt.PackageParallel(it2,
testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}},
testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}},
)
})
})
elapsed := time.Since(start)
if elapsed < singleTestDuration {
t.Errorf("tests execution time %s not exceeded %s", elapsed, singleTestDuration)
}
if elapsed > expectedMaxDuration {
t.Errorf("tests execution time %s exceeded %s", elapsed, expectedMaxDuration)
}
})
t.Run("should run 2 suites parallel in one test", func(t *testing.T) {
singleTestDuration := 1 * time.Second
expectedMaxDuration := singleTestDuration + 300*time.Millisecond
start := time.Now()
t.Run("internal", func(it *testing.T) {
// internal2 is necessary because PackageParallel calls t.Parallel()
it.Run("internal2", func(it2 *testing.T) {
pt.PackageParallel(it2, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
pt.PackageParallel(it2, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
})
})
elapsed := time.Since(start)
if elapsed < singleTestDuration {
t.Errorf("tests execution time %s not exceeded %s", elapsed, singleTestDuration)
}
if elapsed > expectedMaxDuration {
t.Errorf("tests execution time %s exceeded %s", elapsed, expectedMaxDuration)
}
})
t.Run("should run 2 suites parallel in different tests", func(t *testing.T) {
singleTestDuration := 1 * time.Second
expectedMaxDuration := singleTestDuration + 300*time.Millisecond
start := time.Now()
t.Run("internal", func(it *testing.T) {
// internal2 is necessary because PackageParallel calls t.Parallel()
it.Run("internal2", func(it2 *testing.T) {
pt.PackageParallel(it2, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
pt.PackageParallel(it2, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
})
it.Run("internal3", func(it3 *testing.T) {
pt.PackageParallel(it3, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
pt.PackageParallel(it3, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
})
})
elapsed := time.Since(start)
if elapsed < singleTestDuration {
t.Errorf("tests execution time %s not exceeded %s", elapsed, singleTestDuration)
}
if elapsed > expectedMaxDuration {
t.Errorf("tests execution time %s exceeded %s", elapsed, expectedMaxDuration)
}
})
}
func TestParallel(t *testing.T) {
t.Parallel()
t.Run("should panic on nil T", func(t *testing.T) {
t.Parallel()
defer assertPanic(t, "argument t *testing.T can not be nil")
pt.Parallel(nil)
})
t.Run("should not panic without tests", func(t *testing.T) {
t.Parallel()
pt.Parallel(&testing.T{})
})
t.Run("should not panic with 1 test", func(t *testing.T) {
t.Parallel()
pt.Parallel(t, testing.InternalTest{F: func(*testing.T) {}})
})
t.Run("should not panic with 2 tests", func(t *testing.T) {
t.Parallel()
pt.Parallel(t,
testing.InternalTest{F: func(*testing.T) {}},
testing.InternalTest{F: func(*testing.T) {}},
)
})
t.Run("should run 1 test", func(t *testing.T) {
t.Parallel()
called := false
t.Run("internal", func(it *testing.T) {
pt.Parallel(it, testing.InternalTest{F: func(*testing.T) {
if called {
t.Error("test is called twice")
}
called = true
}})
})
if !called {
t.Error("test is not called")
}
})
t.Run("should run 2 tests", func(t *testing.T) {
t.Parallel()
called1 := false
called2 := false
t.Run("internal", func(it *testing.T) {
pt.Parallel(it,
testing.InternalTest{F: func(*testing.T) {
if called1 {
t.Error("test1 is called twice")
}
called1 = true
}},
testing.InternalTest{F: func(*testing.T) {
if called2 {
t.Error("test2 is called twice")
}
called2 = true
}},
)
})
if !called1 {
t.Error("test1 is not called")
}
if !called2 {
t.Error("test2 is not called")
}
})
}
func TestParallel2(t *testing.T) {
// Do not call t.Parallel() because test measures execution time
t.Run("should run 2 tests parallel", func(t *testing.T) {
singleTestDuration := 1 * time.Second
expectedMaxDuration := singleTestDuration + 300*time.Millisecond
start := time.Now()
t.Run("internal", func(it *testing.T) {
pt.Parallel(it,
testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}},
testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}},
)
})
elapsed := time.Since(start)
if elapsed < singleTestDuration {
t.Errorf("tests execution time %s not exceeded %s", elapsed, singleTestDuration)
}
if elapsed > expectedMaxDuration {
t.Errorf("tests execution time %s exceeded %s", elapsed, expectedMaxDuration)
}
})
t.Run("should run 10 tests parallel", func(t *testing.T) {
singleTestDuration := 1 * time.Second
expectedMaxDuration := singleTestDuration * 10 / 2
tests := make([]testing.InternalTest, 10)
for i := range tests {
tests[i] = testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}}
}
start := time.Now()
t.Run("internal", func(it *testing.T) {
pt.Parallel(it, tests...)
})
elapsed := time.Since(start)
if elapsed < singleTestDuration {
t.Errorf("tests execution time %s not exceeded %s", elapsed, singleTestDuration)
}
if elapsed > expectedMaxDuration {
t.Errorf("tests execution time %s exceeded %s", elapsed, expectedMaxDuration)
}
})
t.Run("should run 2 suites parallel", func(t *testing.T) {
singleTestDuration := 1 * time.Second
expectedMaxDuration := singleTestDuration + 300*time.Millisecond
start := time.Now()
t.Run("internal", func(it *testing.T) {
pt.Parallel(it, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
pt.Parallel(it, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
})
elapsed := time.Since(start)
if elapsed < singleTestDuration {
t.Errorf("tests execution time %s not exceeded %s", elapsed, singleTestDuration)
}
if elapsed > expectedMaxDuration {
t.Errorf("tests execution time %s exceeded %s", elapsed, expectedMaxDuration)
}
})
t.Run("should run 10 suites parallel", func(t *testing.T) {
singleTestDuration := 1 * time.Second
expectedMaxDuration := singleTestDuration * 10 / 2
start := time.Now()
t.Run("internal", func(it *testing.T) {
for i := 0; i < 10; i++ {
pt.Parallel(it, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
}
})
elapsed := time.Since(start)
if elapsed < singleTestDuration {
t.Errorf("tests execution time %s not exceeded %s", elapsed, singleTestDuration)
}
if elapsed > expectedMaxDuration {
t.Errorf("tests execution time %s exceeded %s", elapsed, expectedMaxDuration)
}
})
t.Run("should run 2 suites sequential", func(t *testing.T) {
singleTestDuration := 1 * time.Second
expectedMinDuration := 2 * singleTestDuration
expectedMaxDuration := 3 * singleTestDuration
start := time.Now()
t.Run("internal", func(it *testing.T) {
it.Run("internal2", func(it2 *testing.T) {
pt.Parallel(it2, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
})
it.Run("internal2", func(it2 *testing.T) {
pt.Parallel(it2, testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}})
})
})
elapsed := time.Since(start)
if elapsed < expectedMinDuration {
t.Errorf("tests execution time %s not exceeded %s", elapsed, expectedMinDuration)
}
if elapsed > expectedMaxDuration {
t.Errorf("tests execution time %s exceeded %s", elapsed, expectedMaxDuration)
}
})
}
func TestGroup(t *testing.T) {
t.Parallel()
t.Run("should return right name", func(t *testing.T) {
t.Parallel()
internalTest := pt.Group("abc")
if internalTest.Name != "abc" {
t.Error("name is wrong")
}
})
t.Run("should not panic without tests", func(t *testing.T) {
t.Parallel()
internalTest := pt.Group("")
internalTest.F(&testing.T{})
})
t.Run("should run 1 test", func(t *testing.T) {
t.Parallel()
called := false
internalTest := pt.Group("", testing.InternalTest{F: func(*testing.T) {
if called {
t.Error("test is called twice")
}
called = true
}})
t.Run("internal", func(it *testing.T) {
internalTest.F(it)
})
if !called {
t.Error("test is not called")
}
})
t.Run("should run 2 tests", func(t *testing.T) {
t.Parallel()
called1 := false
called2 := false
internalTest := pt.Group("",
testing.InternalTest{F: func(*testing.T) {
if called1 {
t.Error("test1 is called twice")
}
called1 = true
}},
testing.InternalTest{F: func(*testing.T) {
if called2 {
t.Error("test2 is called twice")
}
called2 = true
}},
)
t.Run("internal", func(it *testing.T) {
internalTest.F(it)
})
if !called1 {
t.Error("test1 is not called")
}
if !called2 {
t.Error("test2 is not called")
}
})
}
func TestGroup2(t *testing.T) {
// Do not call t.Parallel() because test measures execution time
t.Run("should run 2 tests parallel", func(t *testing.T) {
singleTestDuration := 1 * time.Second
expectedMaxDuration := singleTestDuration + 300*time.Millisecond
internalTest := pt.Group("",
testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}},
testing.InternalTest{F: func(*testing.T) {
time.Sleep(singleTestDuration)
}},
)
start := time.Now()
t.Run("internal", func(it *testing.T) {
internalTest.F(it)
})
elapsed := time.Since(start)
if elapsed < singleTestDuration {
t.Errorf("tests execution time %s not exceeded %s", elapsed, singleTestDuration)
}
if elapsed > expectedMaxDuration {
t.Errorf("tests execution time %s exceeded %s", elapsed, expectedMaxDuration)
}
})
}
func TestTest(t *testing.T) {
t.Parallel()
t.Run("should panic on nil", func(t *testing.T) {
t.Parallel()
defer assertPanic(t, "argument test func(t *testing.T) can not be nil")
pt.Test("", nil)
})
t.Run("should return right name", func(t *testing.T) {
t.Parallel()
internalTest := pt.Test("abc", func(*testing.T) {})
if internalTest.Name != "abc" {
t.Error("name is wrong")
}
})
t.Run("should return right test func", func(t *testing.T) {
t.Parallel()
called := false
testFunc := func(*testing.T) {
if called {
t.Error("test is called twice")
}
called = true
}
internalTest := pt.Test("", testFunc)
internalTest.F(nil)
if !called {
t.Error("test is not called")
}
})
}
func assertPanic(t *testing.T, expected string) {
if value := recover(); value == nil {
t.Error("no panic")
} else if value != expected {
t.Errorf("unexpected panic value: %v", value)
}
}