-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_test.go
More file actions
165 lines (136 loc) · 3.36 KB
/
pool_test.go
File metadata and controls
165 lines (136 loc) · 3.36 KB
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
package ozero
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewPoolRunsOk(t *testing.T) {
c := make(chan struct{})
pool := NewPool()
defer pool.Close()
pool.SetWorkerFunc(func(data interface{}) error {
assert.IsType(t, 1, data)
assert.Equal(t, 1, data)
c <- struct{}{}
return nil
})
pool.SendJob(1)
assert.NotNil(t, <-c)
}
func TestDoubleCloseDoesNotCrash(t *testing.T) {
pool := NewPool()
pool.Close()
pool.Close()
}
func TestErrorFuncGetsCalledOnPanic(t *testing.T) {
c := make(chan struct{}, 1)
pool := NewPool()
defer pool.Close()
pool.SetWorkerFunc(func(data interface{}) error {
panic("an error")
return nil
}).SetErrorFunc(func(data interface{}, err error) {
assert.IsType(t, 1, data)
assert.Equal(t, 1, data)
c <- struct{}{}
})
pool.SendJob(1)
go func() {
<-time.After(time.Second)
assert.Fail(t, "Test got stuck")
// Force the test to end
c <- struct{}{}
}()
assert.NotNil(t, <-c)
}
func TestErrorFuncGetsOnErrorReturned(t *testing.T) {
c := make(chan struct{}, 1)
pool := NewPool()
defer pool.Close()
pool.SetWorkerFunc(func(data interface{}) error {
return fmt.Errorf("an error")
}).SetErrorFunc(func(data interface{}, err error) {
assert.IsType(t, 1, data)
assert.Equal(t, 1, data)
c <- struct{}{}
})
pool.SendJob(1)
go func() {
<-time.After(time.Second)
assert.Fail(t, "Test got stuck")
// Force the test to end
c <- struct{}{}
}()
assert.NotNil(t, <-c)
}
func TestFuncGetsRetriedExactly3Times(t *testing.T) {
const RETRY = 3
wch := make(chan struct{}, RETRY*2)
ech := make(chan struct{}, 2)
pool := NewPool()
defer pool.Close()
pool.SetWorkerFunc(func(data interface{}) error {
assert.IsType(t, 1, data)
assert.Equal(t, 1, data)
wch <- struct{}{}
panic("an error")
return nil
}).SetErrorFunc(func(data interface{}, err error) {
assert.IsType(t, 1, data)
assert.Equal(t, 1, data)
ech <- struct{}{}
}).SetTries(RETRY)
pool.SendJob(1)
go func() {
<-time.After(time.Second)
assert.Fail(t, "Test got stuck")
// Force the test to end
for i := 0; i < RETRY; i++ {
wch <- struct{}{}
}
ech <- struct{}{}
}()
// Assert function gets called RETRY times
for i := 0; i < RETRY; i++ {
assert.NotNil(t, <-wch)
}
// Assert error function got called after retries
assert.NotNil(t, <-ech)
}
func TestFuncGetsRetriedAfterADelay(t *testing.T) {
const RETRY = 2
const DELAY = 100 * time.Millisecond
wch := make(chan time.Time, RETRY*2)
ech := make(chan struct{}, 2)
pool := NewPool()
defer pool.Close()
pool.SetWorkerFunc(func(data interface{}) error {
assert.IsType(t, 1, data)
assert.Equal(t, 1, data)
wch <- time.Now()
panic("an error")
return nil
}).SetErrorFunc(func(data interface{}, err error) {
assert.IsType(t, 1, data)
assert.Equal(t, 1, data)
ech <- struct{}{}
}).SetTries(RETRY).SetRetryDelay(DELAY)
pool.SendJob(1)
go func() {
<-time.After(time.Second)
assert.Fail(t, "Test got stuck")
// Force the test to end
for i := 0; i < RETRY; i++ {
wch <- time.Now()
}
ech <- struct{}{}
}()
// Assert error function got called after retries
assert.NotNil(t, <-ech)
// Assert function gets called RETRY times
t1 := <-wch
t2 := <-wch
msg := fmt.Sprintf("Expecting retry delay of at least %dms, got %dms", DELAY/time.Millisecond, t2.Sub(t1)/time.Millisecond)
assert.True(t, t2.After(t1.Add(DELAY)), msg)
}