-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprocessor_test.go
More file actions
232 lines (202 loc) · 5.04 KB
/
processor_test.go
File metadata and controls
232 lines (202 loc) · 5.04 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package qsim
import (
"testing"
)
// A simple ProcTimeGenerator function that returns a constant
func simplePtg(j *Job) int {
return 293
}
// Tests the starting of a job
func TestProcessorStart(t *testing.T) {
t.Parallel()
var proc *Processor
var j0, j1 *Job
var procTime int
var err error
proc = NewProcessor(simplePtg)
j0 = NewJob(0)
procTime, err = proc.Start(j0)
if procTime != 293 {
t.Log("Expected processing time of 293 but got", procTime)
t.Fail()
}
if err != nil {
t.Log("Got unexpected error from proc.Start:", err)
t.Fail()
}
// Make sure we get an error if we try to start a job while the
// processor is busy.
j1 = NewJob(0)
procTime, err = proc.Start(j1)
if err == nil {
t.Log("Expected 'job already in progress error', got no error")
t.Fail()
}
// We should still be able to start that new job as long as we
// finish the first job:
proc.Finish()
procTime, err = proc.Start(j1)
if procTime != 293 {
t.Log("Expected processing time of 293 but got", procTime)
t.Fail()
}
if err != nil {
t.Log("Got unexpected error from proc.Start:", err)
t.Fail()
}
}
// Tests the finishing of a job
func TestProcessorFinish(t *testing.T) {
t.Parallel()
var proc *Processor
var j *Job
proc = NewProcessor(simplePtg)
j = NewJob(0)
proc.Start(j)
if j != proc.Finish() {
t.Log("Expected to get back from proc.Finish the job that was processing")
t.Fail()
}
if nil != proc.Finish() {
t.Log("proc.Finish on an idle job didn't return nil as expected")
t.Fail()
}
}
// Tests the BeforeStart callback
func TestBeforeStart(t *testing.T) {
t.Parallel()
var proc, receivedProc *Processor
var j0, j1, receivedJob *Job
proc = NewProcessor(simplePtg)
j0 = NewJob(0)
cbBeforeStart := func(cbProc *Processor, cbJob *Job) {
receivedProc = cbProc
receivedJob = cbJob
}
proc.BeforeStart(cbBeforeStart)
proc.Start(j0)
if receivedProc != proc {
t.Log("BeforeStart callback called with wrong Processor")
t.Fail()
}
if receivedJob != j0 {
t.Log("BeforeStart callback called with wrong Job")
t.Fail()
}
// Make sure that, if Start is called on a busy Processor, the callback
// still runs.
j1 = NewJob(0)
proc.Start(j1)
if receivedProc != proc {
t.Log("BeforeStart callback called with wrong Processor")
t.Fail()
}
if receivedJob != j1 {
t.Log("BeforeStart callback called with wrong Job")
t.Fail()
}
}
// Tests the AfterStart callback
func TestAfterStart(t *testing.T) {
t.Parallel()
var proc, receivedProc *Processor
var j0, j1, receivedJob *Job
var receivedProcTime int
proc = NewProcessor(simplePtg)
j0 = NewJob(0)
cbAfterStart := func(cbProc *Processor, cbJob *Job, cbProcTime int) {
receivedProc = cbProc
receivedJob = cbJob
receivedProcTime = cbProcTime
}
proc.AfterStart(cbAfterStart)
proc.Start(j0)
if receivedProc != proc {
t.Log("AfterStart callback called with wrong Processor")
t.Fail()
}
if receivedJob != j0 {
t.Log("AfterStart callback called with wrong Job")
t.Fail()
}
// Make sure that, if Start is called on a busy Processor, the callback
// still runs but returns nil.
j1 = NewJob(0)
proc.Start(j1)
if receivedProc != proc {
t.Log("AfterStart callback called with wrong Processor")
t.Fail()
}
if receivedJob != nil {
t.Log("AfterStart callback on a busy Processor called with job != nil")
t.Fail()
}
}
// Tests the BeforeFinish callback
func TestBeforeFinish(t *testing.T) {
t.Parallel()
var proc, receivedProc *Processor
var j, receivedJob *Job
proc = NewProcessor(simplePtg)
j = NewJob(0)
proc.Start(j)
cbBeforeFinish := func(cbProc *Processor, cbJob *Job) {
receivedProc = cbProc
receivedJob = cbJob
}
proc.BeforeFinish(cbBeforeFinish)
proc.Finish()
if receivedProc != proc {
t.Log("BeforeFinish callback called with wrong Processor")
t.Fail()
}
if receivedJob != j {
t.Log("BeforeFinish callback called with wrong Job")
t.Fail()
}
// Make sure that, if Finish is called on an idle Processor, the callback
// still runs but j=nil.
proc.Finish()
if receivedProc != proc {
t.Log("BeforeFinish on an idle Processor was called with wrong Processor")
t.Fail()
}
if receivedJob != nil {
t.Log("BeforeFinish on an idle Processor was called with job != nil")
t.Fail()
}
}
// Tests the AfterFinish callback
func TestAfterFinish(t *testing.T) {
t.Parallel()
var proc, receivedProc *Processor
var j, receivedJob *Job
proc = NewProcessor(simplePtg)
j = NewJob(0)
proc.Start(j)
cbAfterFinish := func(cbProc *Processor, cbJob *Job) {
receivedProc = cbProc
receivedJob = cbJob
}
proc.AfterFinish(cbAfterFinish)
proc.Finish()
if receivedProc != proc {
t.Log("AfterFinish callback called with wrong Processor")
t.Fail()
}
if receivedJob != j {
t.Log("AfterFinish callback called with wrong Job")
t.Fail()
}
// Make sure that, if Finish is called on an idle Processor, the callback
// still runs but j=nil.
proc.Finish()
if receivedProc != proc {
t.Log("AfterFinish on idle Processor called with wrong Processor")
t.Fail()
}
if receivedJob != nil {
t.Log("AfterFinish on idle Processor called with job != nil")
t.Fail()
}
}