-
Notifications
You must be signed in to change notification settings - Fork 9
/
runner_test.go
79 lines (69 loc) · 1.4 KB
/
runner_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
package concurrent_test
import (
"errors"
"sync/atomic"
"testing"
"github.com/donutloop/toolkit/concurrent"
)
var StubErr error = errors.New("stub error")
func TestRun(t *testing.T) {
counter := int32(0)
errs := concurrent.Run(
func() error {
atomic.AddInt32(&counter, 1)
return nil
},
func() error {
atomic.AddInt32(&counter, 5)
return nil
},
)
if len(errs) != 0 {
for err := range errs {
t.Log(err)
}
t.Fatalf("unexpected errors")
}
expectedValue := int32(6)
if counter != expectedValue {
t.Errorf(`unexpected value (actual: %d, expected: %d)`, counter, expectedValue)
}
}
func TestRunFail(t *testing.T) {
counter := int32(0)
errs := concurrent.Run(
func() error {
return StubErr
},
func() error {
panic("check isolation of goroutine")
},
func() error {
atomic.AddInt32(&counter, 3)
return nil
},
)
expectedCountOfErrors := 2
if len(errs) != expectedCountOfErrors {
t.Fatalf(`unexpected count of errors (actual: %d, expected: %d)`, len(errs), expectedCountOfErrors)
}
expectedValue := int32(3)
if counter != expectedValue {
t.Errorf(`unexpected value (actual: %d, expected: %d)`, counter, expectedValue)
}
}
func BenchmarkRun(b *testing.B) {
for n := 0; n < b.N; n++ {
concurrent.Run(
func() error {
return StubErr
},
func() error {
panic("check isolation of goroutine")
},
func() error {
return nil
},
)
}
}