-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqueue_test.go
237 lines (219 loc) · 4.81 KB
/
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
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
package queue
import (
"fmt"
"testing"
"time"
"github.com/garyburd/redigo/redis"
)
var testRedis = "redis://localhost:6379"
func TestQueue_Urls(t *testing.T) {
var q Queue
q.Urls([]string{testRedis})
_, err := q.pool[0].Do("PING")
if err != nil {
t.Error("SetRedisPool items are not set correctly")
}
}
func TestBadURL(t *testing.T) {
paniced := false
defer func() {
e := recover()
if e != nil {
paniced = true
}
}()
var q Queue
q.Urls([]string{"redis://127.0.0.1:0"})
if !paniced {
t.Error("Accepted a bad URL without panic")
}
}
func TestQueue_AddTask(t *testing.T) {
var q Queue
q.Urls([]string{testRedis})
redisdb := q.pool[0]
redisdb.Do("FLUSHALL")
q.AddTask(4, "test")
r, e := redisdb.Do("RPOP", "QUEUE::0")
s, e := redis.String(r, e)
if s != "4;test" {
t.Error("Task is stored incorrectly: ", s)
}
}
func TestQueue_QueueName(t *testing.T) {
var q Queue
if q.queueName(5) != "QUEUE::0" {
t.Error("Queue name is wrong for id 5 and queues default: ", q.queueName(5))
}
q.Queues = 1
if q.queueName(5) != "QUEUE::0" {
t.Error("Queue name is wrong for id 5 and queues 1: ", q.queueName(5))
}
q.Queues = 2
if q.queueName(5) != "QUEUE::1" {
t.Error("Queue name is wrong for id 5 and queues 1: ", q.queueName(5))
}
}
func TestQueue_AnalysePool(t *testing.T) {
var q Queue
q.Urls([]string{testRedis})
redisdb := q.pool[0]
redisdb.Do("FLUSHALL")
q.QueueName = "CUSTOM"
q.AddTask(1, "start")
q.AddTask(2, "start")
q.AddTask(1, "stop")
q.AddTask(2, "stop")
analyzer := func(id int, msg_channel chan string, success chan bool) {
for {
select {
case msg := <-msg_channel:
if msg == "stop" {
success <- true
return
}
}
}
}
exitOnEmpty := func() bool {
return true
}
q.AnalysePool(1, exitOnEmpty, analyzer)
r, e := redisdb.Do("LLEN", "QUEUE::0")
s, e := redis.Int64(r, e)
if s != 0 {
t.Error("Queue is not empty after processing tasks: ", s)
}
}
func TestAnalysePoolFailurePending(t *testing.T) {
var q Queue
q.Urls([]string{testRedis})
redisdb := q.pool[0]
redisdb.Do("FLUSHALL")
q.AddTask(1, "start")
q.AddTask(2, "start")
q.AddTask(1, "stop")
analyzer := func(id int, msg_channel chan string, success chan bool) {
for {
select {
case msg := <-msg_channel:
if msg == "stop" {
success <- true
return
}
case <-time.After(1 * time.Second):
fmt.Println("no new event for 2 seconds for ID", id)
success <- false
return
}
}
}
exitOnEmpty := func() bool {
return true
}
q.AnalysePool(1, exitOnEmpty, analyzer)
r, e := redisdb.Do("GET", "QUEUE::0::PENDING::2")
s, e := redis.Int(r, e)
if s != 1 {
t.Error("Task id 2 is not pending: ", s)
}
}
func TestAnalysePoolCheckingWaiting(t *testing.T) {
var q Queue
q.AnalyzerBuff = 2
q.Urls([]string{testRedis})
redisdb := q.pool[0]
redisdb.Do("FLUSHALL")
q.AddTask(1, "start")
q.AddTask(2, "start")
q.AddTask(1, "stop")
analyzer := func(id int, msg_channel chan string, success chan bool) {
for {
select {
case msg := <-msg_channel:
if msg == "stop" {
success <- true
return
}
}
}
}
exit := false
exitOnEmpty := func() bool {
return exit
}
go q.AnalysePool(1, exitOnEmpty, analyzer)
time.Sleep(100 * time.Millisecond)
r, e := redisdb.Do("GET", "QUEUE::0::PENDING::2")
s, e := redis.Int(r, e)
if s != 1 {
t.Error("Task id 2 is not pending after queue is empty: ", s)
}
q.AddTask(2, "stop")
time.Sleep(200 * time.Millisecond)
r, e = redisdb.Do("GET", "QUEUE::PENDING::2")
s, e = redis.Int(r, e)
if s != 0 {
t.Error("Task 2 did not clear: ", s)
}
exit = true
time.Sleep(200 * time.Millisecond)
}
func BenchmarkQueue_AddTask(b *testing.B) {
var q Queue
q.Urls([]string{testRedis})
q.pool[0].Do("FLUSHALL")
b.ResetTimer()
for i := 0; i < b.N; i++ {
q.AddTask(i, "stop")
}
}
func BenchmarkRemoveTask(b *testing.B) {
var q Queue
q.Urls([]string{testRedis})
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, s := q.removeTask(q.pool[0], q.queueName(1))
if s == "" {
panic("Reached an empty queue. Benchmark is not valid:")
}
}
}
// This will act both as test and example in documentation
func ExampleQueue_AnalysePool() {
var q Queue
q.Urls([]string{testRedis})
q.pool[0].Do("FLUSHALL")
q.AddTask(1, "start")
q.AddTask(2, "start")
q.AddTask(1, "stop")
q.AddTask(2, "stop")
analyzer := func(id int, msg_channel chan string, success chan bool) {
for {
select {
case msg := <-msg_channel:
if id == 2 {
time.Sleep(20 * time.Millisecond)
}
fmt.Println(id, msg)
if msg == "stop" {
success <- true
return
}
case <-time.After(2 * time.Second):
fmt.Println("no new event for 2 seconds for ID", id)
success <- false
return
}
}
}
exitOnEmpty := func() bool {
return true
}
q.AnalysePool(1, exitOnEmpty, analyzer)
// Output:
// 1 start
// 1 stop
// 2 start
// 2 stop
}