-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
70 lines (56 loc) · 1.09 KB
/
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
package pool
import (
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type PoolTestSuite struct {
suite.Suite
}
func TestPoolTestSuite(t *testing.T) {
suite.Run(t, new(PoolTestSuite))
}
func (s *PoolTestSuite) TestPool() {
var (
jobsQueued = 10
jobsRun uint64
jobStartEvents uint64
jobEndEvents uint64
job = func(input interface{}) {
atomic.AddUint64(&jobsRun, 1)
}
p = New(
jobsQueued,
job,
Callback{
Event: JobStart,
Func: func() {
atomic.AddUint64(&jobStartEvents, 1)
},
},
Callback{
Event: JobEnd,
Func: func() {
atomic.AddUint64(&jobEndEvents, 1)
},
},
)
)
p.RegisterCallback(Callback{
Event: JobEnd,
Func: func() {
atomic.AddUint64(&jobEndEvents, 1)
},
})
c := make(chan interface{})
go p.Work(c)
for i := 0; i < jobsQueued; i++ {
c <- struct{}{}
}
close(c)
p.Wait()
assert.Equal(s.T(), uint64(jobsQueued), jobsRun)
assert.Equal(s.T(), uint64(jobsQueued), jobStartEvents)
assert.Equal(s.T(), uint64(2*jobsQueued), jobEndEvents)
}