-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_test.go
172 lines (144 loc) · 3.66 KB
/
worker_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
package go_workerpool
import (
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewWorker(t *testing.T) {
maxWorkers := 2
workerPool := make(chan chan Job, maxWorkers)
worker := NewWorker(1, workerPool, true)
expectedWorker := &Worker{
id: 1,
jobQueue: make(chan Job),
workerPool: workerPool,
quitChan: make(chan bool),
started: false,
}
assert.Equal(t, expectedWorker.id, worker.id)
assert.Equal(t, expectedWorker.workerPool, worker.workerPool)
assert.Equal(t, expectedWorker.started, worker.started)
}
func TestStart(t *testing.T) {
// Create worker
maxWorkers := 2
workerPool := make(chan chan Job, maxWorkers)
worker := NewWorker(1, workerPool, true)
worker.Start()
// Create job and add to worker pool
noJobs := 3
job := &JobTest{noCalls: 0, response: nil}
for i := 0; i < noJobs; i++ {
workerJobQueue := <-workerPool
workerJobQueue <- job
}
// Retry until worker jobs are done
noRetries := 0
maxRetries := 10
testTicker := time.NewTicker(time.Millisecond * 10)
for _ = range testTicker.C {
noRetries++
if job.noCalls == noJobs {
break
} else if noRetries == maxRetries {
break
}
}
testTicker.Stop()
worker.Stop()
assert.Equal(t, noJobs, job.noCalls)
}
func TestStartError(t *testing.T) {
// Create worker
maxWorkers := 2
workerPool := make(chan chan Job, maxWorkers)
worker := NewWorker(1, workerPool, true)
errMsgs := []error{}
worker.verbose = func(debug bool, msg string, args ...interface{}) {
errMsgs = append(errMsgs, fmt.Errorf(msg, args...))
}
worker.Start()
// Create job and add to worker pool
noJobsSuccess := 3
noJobsError := 2
jobSuccess := &JobTest{noCalls: 0, response: nil}
for i := 0; i < noJobsSuccess; i++ {
workerJobQueue := <-workerPool
workerJobQueue <- jobSuccess
}
jobError := &JobTest{noCalls: 0, response: errors.New("worker error")}
for i := 0; i < noJobsError; i++ {
workerJobQueue := <-workerPool
workerJobQueue <- jobError
}
// Retry until worker jobs are done
noRetries := 0
maxRetries := 10
testTicker := time.NewTicker(time.Millisecond * 10)
for _ = range testTicker.C {
noRetries++
if jobSuccess.noCalls == noJobsSuccess && jobError.noCalls == noJobsError {
break
} else if noRetries == maxRetries {
break
}
}
testTicker.Stop()
worker.Stop()
assert.Equal(t, noJobsSuccess, jobSuccess.noCalls)
assert.Equal(t, noJobsError, jobError.noCalls)
assert.Equal(t, noJobsError, len(errMsgs))
}
func TestStopAndStarted(t *testing.T) {
// Create worker
maxWorkers := 2
workerPool := make(chan chan Job, maxWorkers)
worker := NewWorker(1, workerPool, true)
var infoMsg string
worker.verbose = func(debug bool, msg string, args ...interface{}) {
infoMsg = fmt.Sprintf(msg, args...)
}
worker.Start()
job := &JobTest{noCalls: 0, response: nil}
workerJobQueue := <-workerPool
workerJobQueue <- job
worker.Stop()
// Retry until worker was stoped
noRetries := 0
maxRetries := 10
testTicker := time.NewTicker(time.Millisecond * 10)
for _ = range testTicker.C {
noRetries++
if worker.Started() == false {
break
} else if noRetries == maxRetries {
break
}
}
assert.Equal(t, worker.Started(), false)
assert.Equal(t, "worker 1 stopping\n", infoMsg)
}
func TestID(t *testing.T) {
// Create worker
maxWorkers := 2
workerPool := make(chan chan Job, maxWorkers)
worker := NewWorker(1, workerPool, true)
worker.Start()
assert.Equal(t, 1, worker.ID())
worker.Stop()
}
// JobTest structure which simulate a job
type JobTest struct {
noCalls int
workerID int
response error
}
func (j *JobTest) Work() error {
j.noCalls++
return j.response
}
func (j *JobTest) SetWorkerID(ID int) {
j.workerID = ID
}