-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkerpool_test.go
More file actions
129 lines (106 loc) · 2.7 KB
/
workerpool_test.go
File metadata and controls
129 lines (106 loc) · 2.7 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
package workerpool
import (
"runtime"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type testCase struct {
Description string
WorkersCount int
ActionsCount int
Sleep time.Duration
Expected time.Duration
}
func getSleepAction(wg *sync.WaitGroup, d time.Duration) Action {
return func() {
time.Sleep(d)
wg.Done()
}
}
const delta = time.Millisecond * 200
func runSleepTestCase(t *testing.T, wp *WorkerPool, testCase testCase) {
wg := new(sync.WaitGroup)
wg.Add(testCase.ActionsCount)
startAt := time.Now()
for range testCase.ActionsCount {
go func(sleep time.Duration) {
err := wp.Push(getSleepAction(wg, sleep))
assert.NoError(t, err)
}(testCase.Sleep)
}
wg.Wait()
actualTime := time.Since(startAt)
assert.InDelta(t, testCase.Expected, actualTime, float64(delta))
err := wp.Close()
assert.NoError(t, err)
}
func TestWorkerPoolWithSleepAction(t *testing.T) {
cpuCount := runtime.NumCPU()
testCases := []testCase{
{
Description: "Count of workers equal count of actions",
WorkersCount: cpuCount,
ActionsCount: cpuCount,
Sleep: time.Second,
Expected: time.Second,
},
{
Description: "One worker and many actions",
WorkersCount: 1,
ActionsCount: cpuCount,
Sleep: time.Second,
Expected: time.Duration(cpuCount * int(time.Second)),
},
{
Description: "Workers count 2 times less then actions ",
WorkersCount: cpuCount,
ActionsCount: cpuCount * 2,
Sleep: time.Second,
Expected: time.Second * 2,
},
{
Description: "Workers count should be number of CPU when input count is zero",
WorkersCount: 0,
ActionsCount: cpuCount,
Sleep: time.Second,
Expected: time.Second,
},
{
Description: "Workers count should be number of CPU when input count is negative number",
WorkersCount: -10,
ActionsCount: cpuCount,
Sleep: time.Second,
Expected: time.Second,
},
}
for _, testCase := range testCases {
wp := NewWorkerPool(testCase.WorkersCount)
runSleepTestCase(t, wp, testCase)
}
}
func TestWorkerPoolPushAfterCloseShouldBeWithError(t *testing.T) {
testCase := testCase{
WorkersCount: 1,
ActionsCount: 1,
Sleep: time.Second,
Expected: time.Second,
}
wp := NewWorkerPool(testCase.WorkersCount)
runSleepTestCase(t, wp, testCase)
err := wp.Push(func() {})
assert.ErrorIs(t, err, errPoolClosed)
}
func TestWorkerPoolAfterFirstCloseShouldBeWithError(t *testing.T) {
testCase := testCase{
WorkersCount: 1,
ActionsCount: 1,
Sleep: time.Second,
Expected: time.Second,
}
wp := NewWorkerPool(testCase.WorkersCount)
runSleepTestCase(t, wp, testCase)
err := wp.Close()
assert.ErrorIs(t, err, errPoolClosed)
}