/
manager_test.go
244 lines (203 loc) · 6.38 KB
/
manager_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
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
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"io/ioutil"
"testing"
"time"
)
func TestAssignJobToWorkerWhenNonEmpty(t *testing.T) {
manager := Manager{}
expectedJob := TestJob{Id: 1, CostPredictionSeconds: 4,
SentAtSecondsSinceEpoch: 100, CreatedAt: time.Now()}
manager.jobs = []TestJob{
expectedJob,
TestJob{Id: 2, CostPredictionSeconds: 1,
SentAtSecondsSinceEpoch: 100, CreatedAt: time.Now()},
TestJob{Id: 3, CostPredictionSeconds: 2,
SentAtSecondsSinceEpoch: 100, CreatedAt: time.Now()},
TestJob{Id: 4, CostPredictionSeconds: 3,
SentAtSecondsSinceEpoch: 100, CreatedAt: time.Now()},
}
oldJobs := len(manager.jobs)
assigned := manager.AssignJobToWorker()
if !assigned {
t.Error("It should return true")
}
if len(manager.jobs) != oldJobs-1 {
t.Error("Should pop a job from the queue")
}
if manager.workerCurrentJobCostPredictionSeconds !=
expectedJob.CostPredictionSeconds {
t.Error("Should pop the first job in queue")
}
}
func TestAssignJobToWorkerWhenEmpty(t *testing.T) {
manager := Manager{}
manager.jobs = []TestJob{}
oldJobs := len(manager.jobs)
assigned := manager.AssignJobToWorker()
if assigned {
t.Error("Should return false")
}
if oldJobs != 0 || len(manager.jobs) != 0 {
t.Error("jobs queue should be empty")
}
}
func TestTotalWorkloadInQueueSeconds(t *testing.T) {
manager := Manager{
workerCurrentJobCostPredictionSeconds: 1,
jobs: []TestJob{
TestJob{Id: 2, CostPredictionSeconds: 2,
SentAtSecondsSinceEpoch: 100, CreatedAt: time.Now()},
TestJob{Id: 3, CostPredictionSeconds: 10,
SentAtSecondsSinceEpoch: 100, CreatedAt: time.Now()},
TestJob{Id: 4, CostPredictionSeconds: 100,
SentAtSecondsSinceEpoch: 100, CreatedAt: time.Now()},
},
}
// seconds left on worker is 0 since the time passed since the default
// workerCurrentJobStartedAt is a really big number.
if workload := manager.TotalWorkloadInQueueSeconds(); workload != 112 {
t.Error("Expected 112, got: ", workload)
}
}
func TestLowWorkload(t *testing.T) {
manager := Manager{
workerCurrentJobCostPredictionSeconds: 1,
jobs: []TestJob{
TestJob{Id: 2, CostPredictionSeconds: 1,
SentAtSecondsSinceEpoch: 2, CreatedAt: time.Now()},
TestJob{Id: 3, CostPredictionSeconds: 2,
SentAtSecondsSinceEpoch: 10, CreatedAt: time.Now()},
TestJob{Id: 4, CostPredictionSeconds: 3,
SentAtSecondsSinceEpoch: 100, CreatedAt: time.Now()},
},
}
if !manager.LowWorkload() {
t.Error("LowWorkload should return true when workload is 6")
}
}
func TestParseChannelsWhenNoJobExists(t *testing.T) {
newJobsChannel := make(chan []TestJob)
manager := Manager{jobs: []TestJob{}, newJobsChannel: newJobsChannel}
go func() {
manager.newJobsChannel <- []TestJob{
TestJob{},
}
}()
if len(manager.jobs) != 0 {
t.Error("There should be no jobs in queue")
}
manager.ParseChannels()
if len(manager.jobs) != 1 {
t.Error("Expected to find 1 job but found: ", manager.jobs)
}
}
func TestParseChannelsWhenJobsExists(t *testing.T) {
jobsChannel := make(chan *TestJob)
newJobsChannel := make(chan []TestJob)
var newJob *TestJob
manager := Manager{
jobs: []TestJob{
// Keep the cost predicton big enough to avoid a call to FetchJobs
TestJob{Id: 2, CostPredictionSeconds: 100,
SentAtSecondsSinceEpoch: 2, CreatedAt: time.Now()},
TestJob{Id: 3, CostPredictionSeconds: 200,
SentAtSecondsSinceEpoch: 10, CreatedAt: time.Now()},
},
jobsChannel: jobsChannel,
newJobsChannel: newJobsChannel,
}
go func() {
newJob = <-manager.jobsChannel
}()
if newJob != nil {
t.Error("No job should be read yet")
}
oldJobs := len(manager.jobs)
manager.ParseChannels()
if newJob == nil {
t.Error("A Job should be read from jobsChannel")
}
if len(manager.jobs) != oldJobs-1 {
t.Error("A Job should be removed from jobs list")
}
go func() {
manager.newJobsChannel <- []TestJob{
TestJob{},
}
}()
oldJobs = len(manager.jobs)
manager.ParseChannels()
if len(manager.jobs) != oldJobs+1 {
t.Error("Expected jobs to be ", oldJobs+1, "but found: ", len(manager.jobs))
}
}
func TestParseChannelsWhenWorkerIsIdlingAndThereAreNoJobs(t *testing.T) {
workerIdlingChannel := make(chan bool)
manager := Manager{
jobs: []TestJob{},
workerIdlingChannel: workerIdlingChannel,
workerCurrentJobCostPredictionSeconds: 1000,
}
go func() {
manager.workerIdlingChannel <- true
}()
manager.ParseChannels()
if manager.workerCurrentJobCostPredictionSeconds != 0 {
t.Error("It should reset workerCurrentJobCostPredictionSeconds to 0 but got: ",
manager.workerCurrentJobCostPredictionSeconds)
}
}
func TestParseChannelsWhenWorkerIsIdlingAndThereAreJobs(t *testing.T) {
workerIdlingChannel := make(chan bool)
manager := Manager{
jobs: []TestJob{
TestJob{},
},
workerIdlingChannel: workerIdlingChannel,
workerCurrentJobCostPredictionSeconds: 1000,
}
go func() {
manager.workerIdlingChannel <- true
}()
manager.ParseChannels()
if manager.workerCurrentJobCostPredictionSeconds != 0 {
t.Error("It should reset workerCurrentJobCostPredictionSeconds to 0 but got: ",
manager.workerCurrentJobCostPredictionSeconds)
}
}
func TestCancelTestRuns(t *testing.T) {
cancelledTestRunIdsChan := make(chan []int)
job1 := TestJob{Id: 1, TestRunId: 1234}
job2 := TestJob{Id: 2, TestRunId: 2345}
job3 := TestJob{Id: 3, TestRunId: 2345}
job4 := TestJob{Id: 4, TestRunId: 1234}
manager := Manager{
jobs: []TestJob{job1, job2, job3, job4},
cancelledTestRunIdsChan: cancelledTestRunIdsChan,
logger: Logger{"Manager", ioutil.Discard},
}
manager.CancelTestRuns([]int{1234})
if len(manager.jobs) != 2 || manager.jobs[0] != job2 || manager.jobs[1] != job3 {
t.Error("It should remove cancelled builds from the jobs slice")
}
}
func TestCancelTestRunsThroughParseChannels(t *testing.T) {
cancelledTestRunIdsChan := make(chan []int)
job1 := TestJob{Id: 1, TestRunId: 1234}
job2 := TestJob{Id: 2, TestRunId: 2345}
job3 := TestJob{Id: 3, TestRunId: 2345}
job4 := TestJob{Id: 4, TestRunId: 1234}
manager := Manager{
jobs: []TestJob{job1, job2, job3, job4},
cancelledTestRunIdsChan: cancelledTestRunIdsChan,
logger: Logger{"Manager", ioutil.Discard},
}
go func() {
cancelledTestRunIdsChan <- []int{1234}
}()
manager.ParseChannels()
if len(manager.jobs) != 2 || manager.jobs[0] != job2 || manager.jobs[1] != job3 {
t.Error("It should remove cancelled builds from the jobs slice")
}
}