-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpqueue_test.go
162 lines (149 loc) · 3 KB
/
pqueue_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
// This package provides a priority queue implementation and
// scaffold interfaces.
//
// Copyright (C) 2011 by Krzysztof Kowalik <[email protected]>
package pqueue
import (
"math/rand"
"testing"
"time"
)
type DummyTask struct {
priority int
}
func NewDummyTask(p int) *DummyTask {
return &DummyTask{priority: p}
}
func (dt *DummyTask) Less(other interface{}) bool {
return dt.priority < other.(*DummyTask).priority
}
func TestNewQueue(t *testing.T) {
q := New(100)
if q.Limit != 100 {
t.Errorf("Expected to set queue limit on create")
}
}
func TestEnqueueAndDequeue(t *testing.T) {
q := New(0)
for _, x := range []int{1, 3, 4, 2, 7, 3} {
q.Enqueue(NewDummyTask(x))
}
if q.Len() != 6 {
t.Errorf("Expected to enqueue all the items")
}
for _, x := range []int{1, 2, 3, 3, 4, 7} {
task := q.Dequeue().(*DummyTask)
if task.priority != x {
t.Errorf("Expected priority to be %d, given %d", x, task.priority)
}
}
if q.Len() != 0 {
t.Errorf("Expected to dequeue all the items")
}
}
func TestWaitForDequeue(t *testing.T) {
q := New(0)
dequeued := false
go func() {
if q.Dequeue() != nil {
dequeued = true
}
}()
<-time.After(1e9)
q.Enqueue(NewDummyTask(1))
<-time.After(1e2)
if !dequeued {
t.Errorf("Expected to wait for dequeue")
}
}
func TestIsEmpty(t *testing.T) {
q := New(0)
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty")
}
for _, x := range []int{1, 2, 3, 4} {
q.Enqueue(NewDummyTask(x))
}
if q.IsEmpty() {
t.Errorf("Expected queue to not be empty")
}
}
func TestLimit(t *testing.T) {
q := New(10)
var err error
for i := 0; i < 20; i += 1 {
err = q.Enqueue(NewDummyTask(i))
}
if err == nil || err.Error() != "Queue limit reached" {
t.Errorf("Expected to reach queue limit")
}
if q.Len() != 10 {
t.Errorf("Expected to enqueue only 10 items, %d enqueued", q.Len())
}
}
func BenchmarkEnqueue(b *testing.B) {
b.StopTimer()
q := New(0)
b.StartTimer()
for i := 0; i < 200000; i += 1 {
q.Enqueue(NewDummyTask(rand.Intn(10)))
}
}
func BenchmarkMultiEnqueue(b *testing.B) {
b.StopTimer()
q := New(0)
done := make(chan bool)
b.StartTimer()
for i := 0; i < 4; i += 1 {
go func() {
for j := 0; j < 50000; j += 1 {
q.Enqueue(NewDummyTask(rand.Intn(10)))
}
done <- true
}()
}
for i := 0; i < 4; i += 1 {
<-done
}
}
func BenchmarkDequeue(b *testing.B) {
b.StopTimer()
q := New(0)
b.StartTimer()
go func() {
for i := 0; i < 200000; i += 1 {
q.Enqueue(NewDummyTask(rand.Intn(10)))
}
q.Enqueue(NewDummyTask(1000000))
}()
for {
task := q.Dequeue().(*DummyTask)
if task.priority == 1000000 {
break
}
}
}
func BenchmarkMultiDequeue(b *testing.B) {
b.StopTimer()
q := New(0)
done := make(chan bool)
b.StartTimer()
go func() {
for i := 0; i < 200000; i += 1 {
q.Enqueue(NewDummyTask(rand.Intn(10)))
}
q.Enqueue(NewDummyTask(1000000))
}()
for i := 0; i < 4; i += 1 {
go func() {
for {
task := q.Dequeue().(*DummyTask)
if task.priority == 1000000 {
done <- true
break
}
}
}()
}
<-done
}