-
Notifications
You must be signed in to change notification settings - Fork 19
/
async_test.go
132 lines (112 loc) · 2.88 KB
/
async_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
package async_test
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/StudioSol/async"
. "github.com/smartystreets/goconvey/convey"
)
var (
ctx context.Context
)
func init() {
ctx = context.Background()
}
func TestRun(t *testing.T) {
Convey("Given two AsyncFunc functions returning non error", t, func() {
var exec [2]bool
f1 := func(_ context.Context) error {
exec[0] = true
return nil
}
f2 := func(_ context.Context) error {
exec[1] = true
return nil
}
Convey("It should be executed properly", func() {
err := async.Run(context.Background(), f1, f2)
So(err, ShouldBeNil)
So(exec[0], ShouldBeTrue)
So(exec[1], ShouldBeTrue)
})
})
Convey("Given two AsyncFunc and one of them returning an error", t, func() {
var errTest = errors.New("test error")
f1 := func(_ context.Context) error {
return errTest
}
f2 := func(_ context.Context) error {
return nil
}
Convey("async.Run() should return that error", func() {
err := async.Run(context.Background(), f1, f2)
So(err, ShouldEqual, errTest)
})
})
Convey("Given two AsyncFunc and one of them executing a panic call", t, func() {
f1 := func(_ context.Context) error {
panic(errors.New("test panic"))
}
f2 := func(_ context.Context) error {
return nil
}
Convey("async.Run() should return that panic error", func() {
err := async.Run(context.Background(), f1, f2)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "async.Run: panic test panic")
})
})
Convey("Given two AsyncFunc and one of them executing a panic call", t, func() {
var mu sync.Mutex
var exec [2]bool
f1 := func(_ context.Context) error {
exec[0] = true
panic(errors.New("test panic"))
}
f2 := func(_ context.Context) error {
time.Sleep(5 * time.Millisecond)
mu.Lock()
exec[1] = true
mu.Unlock()
return nil
}
Convey("The other function should not be executed if does not need it", func() {
_ = async.Run(context.Background(), f1, f2)
mu.Lock()
So(exec[1], ShouldBeFalse)
mu.Unlock()
})
})
Convey("Given an AsyncFunc executing a panic call", t, func() {
var copyCtx context.Context
f1 := func(ctx context.Context) error {
copyCtx = ctx
panic(errors.New("test panic"))
}
Convey("It should cancel the context", func() {
err := async.Run(context.Background(), f1)
So(err, ShouldNotBeNil)
<-copyCtx.Done()
So(copyCtx.Err(), ShouldNotBeNil)
})
})
Convey("Given a cancellable context", t, func() {
ctx, cancel := context.WithCancel(context.TODO())
Convey("When cancelled", func() {
cancel()
Convey("It should cancel its children as well", func() {
var childCtx context.Context
f1 := func(ctx context.Context) error {
childCtx = ctx
return nil
}
err := async.Run(ctx, f1)
So(err, ShouldBeNil)
<-childCtx.Done()
So(childCtx.Err(), ShouldNotBeNil)
})
})
})
}