-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpool_test.go
206 lines (184 loc) · 5.73 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
198
199
200
201
202
203
204
205
206
/**
* Copyright © 2019 Hamed Yousefi <[email protected]>.
*/
package gowl
import (
"context"
"errors"
"fmt"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/hamed-yousefi/gowl/status/pool"
"github.com/hamed-yousefi/gowl/status/process"
)
type (
pTestFunc func(ctx context.Context, pid PID, duration time.Duration) error
mockProcess struct {
name string
pid PID
sleepTime time.Duration
pFunc pTestFunc
}
)
func (t mockProcess) Start(ctx context.Context) error {
return t.pFunc(ctx, t.pid, t.sleepTime)
}
func (t mockProcess) Name() string {
return t.name
}
func (t mockProcess) PID() PID {
return t.pid
}
func newTestProcess(name string, id int, duration time.Duration, f pTestFunc) Process {
return mockProcess{
name: name,
pid: PID("p-" + strconv.Itoa(id)),
sleepTime: duration,
pFunc: f,
}
}
var errCancelled = errors.New("task was cancelled")
// Close pool before adding all processes to the queue
func TestNewPool(t *testing.T) {
a := assert.New(t)
wp := NewPool(2)
a.Equal(pool.Created, wp.Monitor().PoolStatus())
wp.Register(createProcess(10, 1, 300*time.Millisecond, processFunc)...)
err := wp.Start()
a.NoError(err)
a.Equal(pool.Running, wp.Monitor().PoolStatus())
time.Sleep(500 * time.Millisecond)
err = wp.Close()
a.NoError(err)
a.Equal(pool.Closed, wp.Monitor().PoolStatus())
}
// Four different goroutine will publish processes to the queue
func TestNewPoolMultiPublisher(t *testing.T) {
a := assert.New(t)
wp := NewPool(2)
a.Equal(pool.Created, wp.Monitor().PoolStatus())
err := wp.Start()
a.NoError(err)
a.Equal(pool.Running, wp.Monitor().PoolStatus())
wp.Register(createProcess(10, 1, 300*time.Millisecond, processFunc)...)
wp.Register(createProcess(10, 2, 200*time.Millisecond, processFunc)...)
wp.Register(createProcess(10, 3, 100*time.Millisecond, processFunc)...)
wp.Register(createProcess(10, 4, 500*time.Millisecond, processFunc)...)
time.Sleep(10 * time.Second)
err = wp.Close()
a.NoError(err)
a.Equal(pool.Closed, wp.Monitor().PoolStatus())
}
// Kill a processFunc before it starts
func TestWorkerPool_Kill(t *testing.T) {
a := assert.New(t)
wp := NewPool(5)
a.Equal(pool.Created, wp.Monitor().PoolStatus())
err := wp.Start()
a.NoError(err)
a.Equal(pool.Running, wp.Monitor().PoolStatus())
wp.Register(createProcess(10, 1, 3*time.Second, processFunc)...)
wp.Kill("p-18")
time.Sleep(7 * time.Second)
err = wp.Close()
a.NoError(err)
a.Equal(pool.Closed, wp.Monitor().PoolStatus())
a.Equal(process.Killed, wp.Monitor().ProcessStats("p-18").Status)
}
// Kill a processFunc after it started
func TestWorkerPoolStarted_Kill(t *testing.T) {
a := assert.New(t)
wp := NewPool(3)
a.Equal(pool.Created, wp.Monitor().PoolStatus())
err := wp.Start()
a.NoError(err)
a.Equal(pool.Running, wp.Monitor().PoolStatus())
wp.Register(createProcess(3, 1, 3*time.Second, processFunc)...)
time.Sleep(2 * time.Second)
wp.Kill("p-12")
err = wp.Close()
a.NoError(err)
a.Equal(pool.Closed, wp.Monitor().PoolStatus())
a.Equal(process.Killed, wp.Monitor().ProcessStats("p-12").Status)
a.Error(wp.Monitor().Error("p-12"))
a.Equal("task was cancelled", wp.Monitor().Error("p-12").Error())
}
// Process returns error and monitor should cache it
func TestMonitor_Error(t *testing.T) {
a := assert.New(t)
wp := NewPool(5)
a.Equal(pool.Created, wp.Monitor().PoolStatus())
err := wp.Start()
a.NoError(err)
a.Equal(pool.Running, wp.Monitor().PoolStatus())
wp.Register(createProcess(1, 1, 1*time.Second, processFuncWithError)...)
time.Sleep(2 * time.Second)
err = wp.Close()
a.NoError(err)
a.Equal(pool.Closed, wp.Monitor().PoolStatus())
a.Equal(process.Failed, wp.Monitor().ProcessStats("p-11").Status)
a.Error(wp.Monitor().Error("p-11"))
a.Equal("unable to start processFunc with id: p-11", wp.Monitor().Error("p-11").Error())
}
// Close a created pool should return error
func TestWorkerPool_Close(t *testing.T) {
a := assert.New(t)
wp := NewPool(3)
a.Equal(pool.Created, wp.Monitor().PoolStatus())
err := wp.Close()
a.Error(err)
a.Equal("pool is not running, status "+wp.Monitor().PoolStatus().String(), err.Error())
err = wp.Start()
a.NoError(err)
a.Equal(pool.Running, wp.Monitor().PoolStatus())
wp.Register(createProcess(1, 1, 100*time.Millisecond, processFunc)...)
time.Sleep(1 * time.Second)
err = wp.Close()
a.NoError(err)
a.Equal(pool.Closed, wp.Monitor().PoolStatus())
}
// Get worker list and check their status
func TestWorkerPool_WorkerList(t *testing.T) {
a := assert.New(t)
wp := NewPool(3)
a.Equal(pool.Created, wp.Monitor().PoolStatus())
err := wp.Close()
a.Error(err)
a.Equal("pool is not running, status "+wp.Monitor().PoolStatus().String(), err.Error())
err = wp.Start()
a.NoError(err)
a.Equal(pool.Running, wp.Monitor().PoolStatus())
wp.Register(createProcess(5, 1, 700*time.Millisecond, processFunc)...)
time.Sleep(1 * time.Second)
err = wp.Start()
a.Error(err)
a.Equal("unable to start the pool, status: "+pool.Running.String(), err.Error())
wList := wp.Monitor().WorkerList()
for _, wn := range wList {
fmt.Println(wp.Monitor().WorkerStatus(wn))
}
err = wp.Close()
a.NoError(err)
a.Equal(pool.Closed, wp.Monitor().PoolStatus())
}
func createProcess(n int, g int, d time.Duration, f pTestFunc) []Process {
pList := make([]Process, 0)
for i := 1; i <= n; i++ {
pList = append(pList, newTestProcess("p-"+strconv.Itoa(i), (g*10)+i, d, f))
}
return pList
}
func processFunc(ctx context.Context, pid PID, d time.Duration) error {
fmt.Printf("process with id %v has been started.\n", pid)
select {
case <-time.After(d):
case <-ctx.Done():
return errCancelled
}
return nil
}
func processFuncWithError(ctx context.Context, pid PID, d time.Duration) error {
return errors.New("unable to start processFunc with id: " + pid.String())
}