-
Notifications
You must be signed in to change notification settings - Fork 65
/
unlimited_pool_test.go
194 lines (151 loc) · 3.85 KB
/
unlimited_pool_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
package pool
import (
"sync"
"testing"
"time"
. "gopkg.in/go-playground/assert.v1"
)
// NOTES:
// - Run "go test" to run tests
// - Run "gocov test | gocov report" to report on test converage by file
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
//
// or
//
// -- may be a good idea to change to output path to somewherelike /tmp
// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
//
func TestUnlimitedPool(t *testing.T) {
var res []WorkUnit
pool := New()
defer pool.Close()
newFunc := func(d time.Duration) WorkFunc {
return func(WorkUnit) (interface{}, error) {
time.Sleep(d)
return nil, nil
}
}
for i := 0; i < 4; i++ {
wu := pool.Queue(newFunc(time.Second * 1))
res = append(res, wu)
}
var count int
for _, wu := range res {
wu.Wait()
Equal(t, wu.Error(), nil)
Equal(t, wu.Value(), nil)
count++
}
Equal(t, count, 4)
pool.Close() // testing no error occurs as Close will be called twice once defer pool.Close() fires
}
func TestUnlimitedCancel(t *testing.T) {
m := new(sync.RWMutex)
var closed bool
c := make(chan WorkUnit, 100)
pool := unlimitedGpool
defer pool.Close()
newFunc := func(d time.Duration) WorkFunc {
return func(WorkUnit) (interface{}, error) {
time.Sleep(d)
return 1, nil
}
}
go func(ch chan WorkUnit) {
for i := 0; i < 40; i++ {
go func(ch chan WorkUnit) {
m.RLock()
if !closed {
ch <- pool.Queue(newFunc(time.Second * 1))
}
m.RUnlock()
}(ch)
}
}(c)
time.Sleep(time.Second * 1)
pool.Cancel()
m.Lock()
closed = true
close(c)
m.Unlock()
var count int
for wu := range c {
wu.Wait()
if wu.Error() != nil {
_, ok := wu.Error().(*ErrCancelled)
if !ok {
_, ok = wu.Error().(*ErrPoolClosed)
if ok {
Equal(t, wu.Error().Error(), "ERROR: Work Unit added/run after the pool had been closed or cancelled")
}
} else {
Equal(t, wu.Error().Error(), "ERROR: Work Unit Cancelled")
}
Equal(t, ok, true)
continue
}
count += wu.Value().(int)
}
NotEqual(t, count, 40)
// reset and test again
pool.Reset()
wrk := pool.Queue(newFunc(time.Millisecond * 300))
wrk.Wait()
_, ok := wrk.Value().(int)
Equal(t, ok, true)
wrk = pool.Queue(newFunc(time.Millisecond * 300))
time.Sleep(time.Second * 1)
wrk.Cancel()
wrk.Wait() // proving we don't get stuck here after cancel
Equal(t, wrk.Error(), nil)
pool.Reset() // testing that we can do this and nothing bad will happen as it checks if pool closed
pool.Close()
wu := pool.Queue(newFunc(time.Second * 1))
wu.Wait()
NotEqual(t, wu.Error(), nil)
Equal(t, wu.Error().Error(), "ERROR: Work Unit added/run after the pool had been closed or cancelled")
}
func TestCancelFromWithin(t *testing.T) {
pool := New()
defer pool.Close()
newFunc := func(d time.Duration) WorkFunc {
return func(wu WorkUnit) (interface{}, error) {
time.Sleep(d)
if wu.IsCancelled() {
return nil, nil
}
return 1, nil
}
}
q := pool.Queue(newFunc(time.Second * 5))
time.Sleep(time.Second * 2)
pool.Cancel()
Equal(t, q.Value() == nil, true)
NotEqual(t, q.Error(), nil)
Equal(t, q.Error().Error(), "ERROR: Work Unit Cancelled")
}
func TestUnlimitedPanicRecovery(t *testing.T) {
pool := New()
defer pool.Close()
newFunc := func(d time.Duration, i int) WorkFunc {
return func(WorkUnit) (interface{}, error) {
if i == 1 {
panic("OMG OMG OMG! something bad happened!")
}
time.Sleep(d)
return 1, nil
}
}
var wrk WorkUnit
for i := 0; i < 4; i++ {
time.Sleep(time.Second * 1)
if i == 1 {
wrk = pool.Queue(newFunc(time.Second*1, i))
continue
}
pool.Queue(newFunc(time.Second*1, i))
}
wrk.Wait()
NotEqual(t, wrk.Error(), nil)
Equal(t, wrk.Error().Error()[0:90], "ERROR: Work Unit failed due to a recoverable error: 'OMG OMG OMG! something bad happened!'")
}