-
Notifications
You must be signed in to change notification settings - Fork 0
/
workpool_test.go
125 lines (107 loc) · 2.96 KB
/
workpool_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
package workerpool_test
import (
"context"
"github.com/stretchr/testify/require"
"github.com/the4thamigo-uk/workerpool"
"testing"
"time"
)
func testWork(id int, d time.Duration, ids chan int) workerpool.Work {
return func() {
time.Sleep(d)
ids <- id
}
}
func TestWorkPool_ThreeJobs(t *testing.T) {
p, err := workerpool.New(1, 3)
require.NoError(t, err)
require.NotNil(t, p)
defer p.Close()
res := make(chan int, 1)
err = p.Add(context.Background(), testWork(1, time.Second, res))
require.NoError(t, err)
err = p.Add(context.Background(), testWork(2, time.Second, res))
require.NoError(t, err)
err = p.Add(context.Background(), testWork(3, time.Second, res))
require.NoError(t, err)
id1 := <-res
require.Equal(t, 1, id1)
id2 := <-res
require.Equal(t, 2, id2)
id3 := <-res
require.Equal(t, 3, id3)
}
func TestWorkPool_AddAfterClose(t *testing.T) {
p, err := workerpool.New(1, 1)
require.NoError(t, err)
require.NotNil(t, p)
p.Close()
err = p.Add(context.Background(), func() {})
require.Error(t, err)
}
func TestWorkPool_AddContextDone(t *testing.T) {
p, err := workerpool.New(1, 1)
require.NoError(t, err)
require.NotNil(t, p)
defer p.Close()
// push a task into the pool which will be immediately started
err = p.Add(context.Background(), func() { time.Sleep(1 * time.Second) })
require.Nil(t, err)
// push a second task into the pool which will be queued
err = p.Add(context.Background(), func() { time.Sleep(1 * time.Second) })
require.Nil(t, err)
// adding another task should block as the queue is full
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
err = p.Add(ctx, func() {})
require.Equal(t, context.DeadlineExceeded, err)
}
func TestWorkPool_Cancel(t *testing.T) {
p, err := workerpool.New(1, 1)
require.NoError(t, err)
require.NotNil(t, p)
res := make(chan int, 1)
err = p.Add(context.Background(), testWork(1, 2*time.Second, res))
require.NoError(t, err)
time.Sleep(time.Second)
p.Cancel()
// after complete all the result should be sitting in the chan
select {
case id1 := <-res:
require.Equal(t, 1, id1)
default:
t.Errorf("work 1 did not complete")
}
}
func TestWorkPool_Complete(t *testing.T) {
p, err := workerpool.New(1, 1)
require.NoError(t, err)
require.NotNil(t, p)
res := make(chan int, 3)
err = p.Add(context.Background(), testWork(1, 1*time.Second, res))
require.NoError(t, err)
err = p.Add(context.Background(), testWork(2, 1*time.Second, res))
require.NoError(t, err)
err = p.Add(context.Background(), testWork(3, 1*time.Second, res))
require.NoError(t, err)
p.Complete()
// after complete all the results should be sitting in the chan
select {
case id1 := <-res:
require.Equal(t, 1, id1)
default:
t.Errorf("work 1 did not complete")
}
select {
case id2 := <-res:
require.Equal(t, 2, id2)
default:
t.Errorf("work 2 did not complete")
}
select {
case id3 := <-res:
require.Equal(t, 3, id3)
default:
t.Errorf("work 3 did not complete")
}
}