-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpriority_queue_test.go
188 lines (175 loc) · 4.6 KB
/
priority_queue_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
package golongpoll
import (
"container/heap"
"container/list"
"testing"
"time"
)
func Test_priorityQueue_Len(t *testing.T) {
pq := make(priorityQueue, 0)
if pq.Len() != 0 {
t.Errorf("priorityQueue had unexpected Len(). was: %d, expected: %d",
pq.Len(), 0)
}
// add an item
now_ms := timeToEpochMilliseconds(time.Now())
buf := &eventBuffer{
list.New(),
100,
now_ms,
}
expiringBuf := &expiringBuffer{
eventBuffer_ptr: buf,
category: "some random category",
priority: now_ms,
}
heap.Push(&pq, expiringBuf)
if pq.Len() != 1 {
t.Errorf("priorityQueue had unexpected Len(). was: %d, expected: %d",
pq.Len(), 1)
}
// add another
buf2 := &eventBuffer{
list.New(),
100,
now_ms,
}
expiringBuf2 := &expiringBuffer{
eventBuffer_ptr: buf2,
category: "some different category",
priority: now_ms,
}
heap.Push(&pq, expiringBuf2)
if pq.Len() != 2 {
t.Errorf("priorityQueue had unexpected Len(). was: %d, expected: %d",
pq.Len(), 2)
}
// Remove an item
pq.Pop()
if pq.Len() != 1 {
t.Errorf("priorityQueue had unexpected Len(). was: %d, expected: %d",
pq.Len(), 1)
}
pq.Pop()
if pq.Len() != 0 {
t.Errorf("priorityQueue had unexpected Len(). was: %d, expected: %d",
pq.Len(), 0)
}
}
func Test_priorityQueue(t *testing.T) {
now_ms := timeToEpochMilliseconds(time.Now())
pq := make(priorityQueue, 0)
heap.Init(&pq)
if _, e := pq.peakTopPriority(); e == nil {
t.Errorf("No error returned when calling peakTopPriority on an empty priorityQueue")
}
buf1 := &eventBuffer{
list.New(),
100,
now_ms,
}
expiringBuf1 := &expiringBuffer{
eventBuffer_ptr: buf1,
category: "some random category",
priority: 10003, // lower number is a higher ( priority 1 > priority 2)
}
heap.Push(&pq, expiringBuf1)
if p, e := pq.peakTopPriority(); e != nil {
t.Errorf("Error returned when calling peakTopPriority: %v", e)
} else {
if p != 10003 {
t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.",
10003, p)
}
}
buf2 := &eventBuffer{
list.New(),
100,
now_ms,
}
expiringBuf2 := &expiringBuffer{
eventBuffer_ptr: buf2,
category: "some random category",
priority: 10001,
}
heap.Push(&pq, expiringBuf2)
if p, e := pq.peakTopPriority(); e != nil {
t.Errorf("Error returned when calling peakTopPriority: %v", e)
} else {
if p != 10001 {
t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.",
10001, p)
}
}
buf3 := &eventBuffer{
list.New(),
100,
now_ms,
}
expiringBuf3 := &expiringBuffer{
eventBuffer_ptr: buf3,
category: "some random category",
priority: 10051,
}
heap.Push(&pq, expiringBuf3)
if p, e := pq.peakTopPriority(); e != nil {
t.Errorf("Error returned when calling peakTopPriority: %v", e)
} else {
if p != 10001 {
t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.",
10001, p)
}
}
buf4 := &eventBuffer{
list.New(),
100,
now_ms,
}
expiringBuf4 := &expiringBuffer{
eventBuffer_ptr: buf4,
category: "some random category",
priority: 10011,
}
heap.Push(&pq, expiringBuf4)
if p, e := pq.peakTopPriority(); e != nil {
t.Errorf("Error returned when calling peakTopPriority: %v", e)
} else {
if p != 10001 {
t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.",
10001, p)
}
}
if item := heap.Pop(&pq).(*expiringBuffer); item != expiringBuf2 {
t.Errorf("Expected popped item != expiringBuf2")
}
if p, _ := pq.peakTopPriority(); p != 10003 {
t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.",
10003, p)
}
if item := heap.Pop(&pq).(*expiringBuffer); item != expiringBuf1 {
t.Errorf("Expected popped item != expiringBuf1")
}
if p, _ := pq.peakTopPriority(); p != 10011 {
t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.",
10011, p)
}
// Now stir the pot by updating expiringBuf3 to higher priority than expiringBuf4
pq.updatePriority(expiringBuf3, 10008)
if p, _ := pq.peakTopPriority(); p != 10008 {
t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.",
10008, p)
}
if item := heap.Pop(&pq).(*expiringBuffer); item != expiringBuf3 {
t.Errorf("Expected popped item != expiringBuf3")
}
if p, _ := pq.peakTopPriority(); p != 10011 {
t.Errorf("Unexpected peakTopPriority result. was: %d, expected: %d.",
10011, p)
}
if item := heap.Pop(&pq).(*expiringBuffer); item != expiringBuf4 {
t.Errorf("Expected popped item != expiringBuf4")
}
if _, e := pq.peakTopPriority(); e == nil {
t.Errorf("No error returned when calling peakTopPriority on an empty priorityQueue")
}
}