-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
197 lines (193 loc) · 3.93 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
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
195
196
197
package pool
import (
"errors"
"runtime"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func Test_InitPool(t *testing.T) {
t.Run("default worker num", func(t *testing.T) {
pool := InitPool(0, 0, 0)
cnt := 0
pool.Workers.Range(func(k, v any) bool {
cnt++
return true
})
require.Equal(t, runtime.NumCPU(), cnt)
})
t.Run("custom worker num", func(t *testing.T) {
pool := InitPool(2, 2, 0)
cnt := 0
pool.Workers.Range(func(k, v any) bool {
cnt++
return true
})
require.Equal(t, 2, cnt)
})
t.Run("execute task panic", func(t *testing.T) {
pool := InitPool(2, 2, 0)
pool.TaskChan <- &Task{
Args: nil,
Fn: func(args any) (any, error) {
panic("test")
},
}
require.Equal(t, &TaskResult{
Error: errors.New("test"),
Result: nil,
}, <-pool.TaskResultChan)
time.Sleep(25 * time.Millisecond)
cnt := 0
pool.Workers.Range(func(_, v any) bool {
if !(v.(*Worker).Status == WorkerStatusFree) {
return false
}
cnt++
return true
})
require.Equal(t, 2, cnt)
})
t.Run("should increase worker", func(t *testing.T) {
pool := InitPool(1, 5, 0)
for i := 0; i < 5; i++ {
pool.TaskChan <- &Task{
ID: strconv.Itoa(i),
Args: nil,
Fn: func(args any) (any, error) {
time.Sleep(10 * time.Minute)
return nil, nil
},
}
}
for {
if len(pool.TaskChan) == 0 {
break
}
}
require.Equal(t, 5, SyncMapLen(pool.Workers))
})
t.Run("should recall worker", func(t *testing.T) {
pool := InitPool(1, 5, 2*time.Second)
pool.TaskChan <- &Task{
ID: "0",
Args: nil,
Fn: func(args any) (any, error) {
time.Sleep(10 * time.Minute)
return nil, nil
},
}
for i := 1; i < 5; i++ {
pool.TaskChan <- &Task{
ID: strconv.Itoa(i),
Args: nil,
Fn: func(args any) (any, error) {
return nil, nil
},
}
}
cnt := 0
go func() {
for result := range pool.TaskResultChan {
_ = result
cnt++
}
}()
for {
if len(pool.TaskChan) == 0 && cnt == 4 {
break
}
}
time.Sleep(3 * time.Second)
require.Equal(t, 1, SyncMapLen(pool.Workers))
})
t.Run("shutdown after increase worker", func(t *testing.T) {
pool := InitPool(1, 5, 0)
for i := 0; i < 5; i++ {
pool.TaskChan <- &Task{
ID: strconv.Itoa(i),
Args: "nil",
Fn: func(args any) (any, error) {
time.Sleep(1 * time.Second)
return nil, nil
},
}
}
cnt := 0
go func() {
for result := range pool.TaskResultChan {
_ = result
cnt++
}
}()
for {
if len(pool.TaskChan) == 0 && cnt == 5 {
break
}
}
require.Equal(t, 5, SyncMapLen(pool.Workers))
time.Sleep(25 * time.Millisecond)
err := pool.ClosePool()
require.NoError(t, err)
})
t.Run("receive result", func(t *testing.T) {
pool := InitPool(2, 2, 0)
pool.TaskChan <- &Task{
ID: "0",
Args: []int{1, 2},
Fn: func(args any) (any, error) {
arr := args.([]int)
return arr[0] + arr[1], nil
},
}
pool.TaskChan <- &Task{
ID: "1",
Args: nil,
Fn: func(args any) (any, error) {
panic("boom!")
},
}
m := make(map[string]*TaskResult, 2)
cnt := 0
go func() {
for result := range pool.TaskResultChan {
m[result.ID] = result
cnt++
}
}()
for {
if cnt == 2 {
break
}
}
require.Equal(t, 3, m["0"].Result)
require.Equal(t, errors.New("boom!"), m["1"].Error)
})
}
func TestPool_ClosePool(t *testing.T) {
t.Run("workers are busy", func(t *testing.T) {
pool := InitPool(2, 2, 0)
pool.TaskChan <- &Task{
ID: "0",
Args: nil,
Fn: func(args any) (any, error) {
time.Sleep(10 * time.Minute)
return nil, nil
},
}
time.Sleep(25 * time.Millisecond)
for {
if len(pool.TaskChan) == 0 {
break
}
}
err := pool.ClosePool()
require.Equal(t, errors.New("[pool] workers are busy, waiting for them to finish"), err)
})
t.Run("workers are free", func(t *testing.T) {
pool := InitPool(2, 2, 0)
err := pool.ClosePool()
require.NoError(t, err)
})
}