Skip to content

Commit 8d55e10

Browse files
committed
Made discipline test more robust against weirdness.
Also split out Job Attrs by type.
1 parent 24d0ce3 commit 8d55e10

File tree

4 files changed

+67
-56
lines changed

4 files changed

+67
-56
lines changed

discipline.go

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type OneToOneFIFODiscipline struct {
1515
Processors []*Processor
1616
}
1717

18+
// AssignQueueToProcessor assigns a given Queue to a given Processor. That
19+
// Processor pull Jobs from that Queue, and only from that Queue.
1820
func (d *OneToOneFIFODiscipline) AssignQueueToProcessor(q *Queue, p *Processor) {
1921
cbAfterFinish := func(cbProc *Processor, cbJob *Job) {
2022
var j *Job

discipline_test.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,31 @@ func TestOneToOneFIFODiscipline(t *testing.T) {
1414

1515
for i = 0; i < 3; i++ {
1616
queues = append(queues, NewQueue())
17+
queues[i].QueueId = i
1718
procs = append(procs, NewProcessor(simplePtg))
19+
procs[i].ProcessorId = i
20+
21+
// Make sure that Processors only pull work from their own queues.
22+
procs[i].BeforeStart(func(p *Processor, j *Job) {
23+
var queueId int
24+
queueId = j.IntAttrs["queue_id"]
25+
if queueId != p.ProcessorId {
26+
t.Log("Processor", p.ProcessorId, "pulled a Job from Queue", queueId)
27+
t.Fail()
28+
}
29+
})
1830
}
1931
NewOneToOneFIFODiscipline(queues, procs)
2032

2133
for i = 0; i < 3; i++ {
22-
procs[i].Start(NewJob(0))
34+
j = NewJob(0)
35+
j.IntAttrs["queue_id"] = i
36+
procs[i].Start(j)
2337
}
2438
for i = 0; i < 6; i++ {
25-
queues[i%3].Append(NewJob(0))
39+
j = NewJob(0)
40+
j.IntAttrs["queue_id"] = i % 3
41+
queues[i%3].Append(j)
2642
}
2743

2844
j = queues[2].Jobs[0]
@@ -35,16 +51,4 @@ func TestOneToOneFIFODiscipline(t *testing.T) {
3551
t.Log("A Job should've been shifted out of the Queue, but Queue is still the same length as before")
3652
t.Fail()
3753
}
38-
39-
// Make sure that Processors only pull work from their own Queue
40-
procs[2].Finish()
41-
procs[2].Finish()
42-
if !procs[2].IsIdle() {
43-
t.Log("Processor is not idle, but it shouldn't have had any more work in its queue")
44-
t.Fail()
45-
}
46-
if queues[2].Length() != 0 {
47-
t.Log("Processor finished all work in its Queue, but the Queue isn't empty")
48-
t.Fail()
49-
}
5054
}

job.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ type Job struct {
1111
JobId int64
1212
// The time the Job arrived in the system.
1313
ArrTime int
14-
// Attrs contains user-defined, string-valued job attributes. One would use
15-
// this if the behavior of jobs in the system isn't uniform.
16-
Attrs map[string]string
14+
// IntAttrs contains user-defined, int-valued job attributes. You can use
15+
// this feature for testing, or for debugging, or for changing the behavior
16+
// of the system for particular types of jobs.
17+
IntAttrs map[string]int
18+
// StrAttrs contains user-defined, str-valued job attributes. You can use
19+
// this feature for testing, or for debugging, or for changing the behavior
20+
// of the system for particular types of jobs.
21+
StrAttrs map[string]string
1722
}
1823

1924
// NewJob creates a new... wait for it... Job.
@@ -24,7 +29,8 @@ type Job struct {
2429
// is expected to seed the PRNG if necessary..
2530
func NewJob(arrTime int) (j *Job) {
2631
j = new(Job)
27-
j.Attrs = make(map[string]string)
32+
j.IntAttrs = make(map[string]int)
33+
j.StrAttrs = make(map[string]string)
2834
j.JobId = rand.Int63()
2935
j.ArrTime = arrTime
3036
return j

queue_test.go

+37-38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package qsim
22

33
import (
4-
"fmt"
54
"testing"
65
)
76

@@ -14,15 +13,15 @@ func TestQueueAppend(t *testing.T) {
1413

1514
for i := 0; i < 20; i++ {
1615
j = NewJob(0)
17-
j.Attrs["i"] = fmt.Sprintf("%d", i)
16+
j.IntAttrs["i"] = i
1817
q.Append(j)
1918
}
2019

21-
if q.Jobs[0].Attrs["i"] != "0" {
20+
if q.Jobs[0].IntAttrs["i"] != 0 {
2221
t.Log("Zeroeth element not found at index 0")
2322
t.Fail()
2423
}
25-
if q.Jobs[19].Attrs["i"] != "19" {
24+
if q.Jobs[19].IntAttrs["i"] != 19 {
2625
t.Log("Nineteenth element not found at index 19")
2726
t.Fail()
2827
}
@@ -40,15 +39,15 @@ func TestQueueShift(t *testing.T) {
4039
q = NewQueue()
4140

4241
j0 = NewJob(0)
43-
j0.Attrs["i"] = "0"
42+
j0.IntAttrs["i"] = 0
4443
q.Append(j0)
4544

4645
j1 = NewJob(0)
47-
j1.Attrs["i"] = "1"
46+
j1.IntAttrs["i"] = 1
4847
q.Append(j1)
4948

5049
j, nrem = q.Shift()
51-
if j.Attrs["i"] != "0" {
50+
if j.IntAttrs["i"] != 0 {
5251
t.Log("Zeroeth element was not the first to be shifted")
5352
t.Fail()
5453
}
@@ -58,7 +57,7 @@ func TestQueueShift(t *testing.T) {
5857
}
5958

6059
j, nrem = q.Shift()
61-
if j.Attrs["i"] != "1" {
60+
if j.IntAttrs["i"] != 1 {
6261
t.Log("Last element was not the last to be shifted")
6362
t.Fail()
6463
}
@@ -81,18 +80,18 @@ func TestQueueBeforeAppend(t *testing.T) {
8180
var j *Job
8281
q = NewQueue()
8382

84-
var counter string
83+
var counter int
8584
var qLen int
8685
cb := func(q *Queue, j *Job) {
87-
counter = j.Attrs["i"]
86+
counter = j.IntAttrs["i"]
8887
qLen = len(q.Jobs)
8988
}
9089
q.BeforeAppend(cb)
9190

9291
j = NewJob(0)
93-
j.Attrs["i"] = "0"
92+
j.IntAttrs["i"] = 0
9493
q.Append(j)
95-
if counter != "0" {
94+
if counter != 0 {
9695
t.Log("Expected BeforeAppend callback to set counter=0")
9796
t.Fail()
9897
}
@@ -102,9 +101,9 @@ func TestQueueBeforeAppend(t *testing.T) {
102101
}
103102

104103
j = NewJob(0)
105-
j.Attrs["i"] = "1"
104+
j.IntAttrs["i"] = 1
106105
q.Append(j)
107-
if counter != "1" {
106+
if counter != 1 {
108107
t.Log("Expected BeforeAppend callback to set counter=1")
109108
t.Fail()
110109
}
@@ -121,18 +120,18 @@ func TestQueueAfterAppend(t *testing.T) {
121120
var j *Job
122121
q = NewQueue()
123122

124-
var counter string
123+
var counter int
125124
var qLen int
126125
cb := func(q *Queue, j *Job) {
127-
counter = j.Attrs["i"]
126+
counter = j.IntAttrs["i"]
128127
qLen = len(q.Jobs)
129128
}
130129
q.AfterAppend(cb)
131130

132131
j = NewJob(0)
133-
j.Attrs["i"] = "0"
132+
j.IntAttrs["i"] = 0
134133
q.Append(j)
135-
if counter != "0" {
134+
if counter != 0 {
136135
t.Log("Expected AfterAppend callback to set counter=0")
137136
t.Fail()
138137
}
@@ -142,9 +141,9 @@ func TestQueueAfterAppend(t *testing.T) {
142141
}
143142

144143
j = NewJob(0)
145-
j.Attrs["i"] = "1"
144+
j.IntAttrs["i"] = 1
146145
q.Append(j)
147-
if counter != "1" {
146+
if counter != 1 {
148147
t.Log("Expected AfterAppend callback to set counter=1")
149148
t.Fail()
150149
}
@@ -161,27 +160,27 @@ func TestQueueBeforeShift(t *testing.T) {
161160
var j *Job
162161
q = NewQueue()
163162

164-
var counter string
163+
var counter int
165164
var qLen int
166165
cb := func(q *Queue, j *Job) {
167166
if j != nil {
168-
counter = j.Attrs["i"]
167+
counter = j.IntAttrs["i"]
169168
} else {
170-
counter = "queue_was_empty"
169+
counter = -1
171170
}
172171
qLen = len(q.Jobs)
173172
}
174173
q.BeforeShift(cb)
175174

176175
j = NewJob(0)
177-
j.Attrs["i"] = "0"
176+
j.IntAttrs["i"] = 0
178177
q.Append(j)
179178
j = NewJob(0)
180-
j.Attrs["i"] = "1"
179+
j.IntAttrs["i"] = 1
181180
q.Append(j)
182181

183182
q.Shift()
184-
if counter != "0" {
183+
if counter != 0 {
185184
t.Log("Expected BeforeShift callback to set counter=0")
186185
t.Fail()
187186
}
@@ -191,7 +190,7 @@ func TestQueueBeforeShift(t *testing.T) {
191190
}
192191

193192
q.Shift()
194-
if counter != "1" {
193+
if counter != 1 {
195194
t.Log("Expected BeforeShift callback to set counter=1")
196195
t.Fail()
197196
}
@@ -201,8 +200,8 @@ func TestQueueBeforeShift(t *testing.T) {
201200
}
202201

203202
q.Shift()
204-
if counter != "queue_was_empty" {
205-
t.Log("Expected BeforeShift callback to set counter=queue_was_empty")
203+
if counter != -1 {
204+
t.Log("Expected BeforeShift callback to set counter=-1")
206205
t.Fail()
207206
}
208207
if qLen != 0 {
@@ -218,27 +217,27 @@ func TestQueueAfterShift(t *testing.T) {
218217
var j *Job
219218
q = NewQueue()
220219

221-
var counter string
220+
var counter int
222221
var qLen int
223222
cb := func(q *Queue, j *Job) {
224223
if j != nil {
225-
counter = j.Attrs["i"]
224+
counter = j.IntAttrs["i"]
226225
} else {
227-
counter = "queue_was_empty"
226+
counter = -1
228227
}
229228
qLen = len(q.Jobs)
230229
}
231230
q.AfterShift(cb)
232231

233232
j = NewJob(0)
234-
j.Attrs["i"] = "0"
233+
j.IntAttrs["i"] = 0
235234
q.Append(j)
236235
j = NewJob(0)
237-
j.Attrs["i"] = "1"
236+
j.IntAttrs["i"] = 1
238237
q.Append(j)
239238

240239
q.Shift()
241-
if counter != "0" {
240+
if counter != 0 {
242241
t.Log("Expected AfterShift callback to set counter=0")
243242
t.Fail()
244243
}
@@ -248,7 +247,7 @@ func TestQueueAfterShift(t *testing.T) {
248247
}
249248

250249
q.Shift()
251-
if counter != "1" {
250+
if counter != 1 {
252251
t.Log("Expected AfterShift callback to set counter=1")
253252
t.Fail()
254253
}
@@ -258,8 +257,8 @@ func TestQueueAfterShift(t *testing.T) {
258257
}
259258

260259
q.Shift()
261-
if counter != "queue_was_empty" {
262-
t.Log("Expected AfterShift callback to set counter=queue_was_empty")
260+
if counter != -1 {
261+
t.Log("Expected AfterShift callback to set counter=-1")
263262
t.Fail()
264263
}
265264
if qLen != 0 {

0 commit comments

Comments
 (0)