forked from jhunters/bigqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilequeue_test.go
More file actions
333 lines (249 loc) · 6.68 KB
/
filequeue_test.go
File metadata and controls
333 lines (249 loc) · 6.68 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package bigqueue
import (
"fmt"
"strings"
"testing"
"time"
)
func TestFileQueue_OpenError(t *testing.T) {
path := Tempfile()
defer clearFiles(path, "testqueue")
var queue = &FileQueue{
HeadIndex: 0,
TailIndex: 0,
}
//var queue = new(FileQueue)
err := queue.Open("", "", nil)
if err == nil {
t.Error("Error parameter 'path' should return non-nil err")
}
defer queue.Close()
err = queue.Open(path, "", nil)
if err == nil {
t.Error("Error parameter 'queueName' should return non-nil err")
}
defer queue.Close()
}
func TestFileQueue_Open(t *testing.T) {
path := Tempfile()
defer clearFiles(path, "testqueue")
var queue = new(FileQueue)
err := queue.Open(path, "testqueue", nil)
if err != nil {
fmt.Println(err)
}
defer queue.Close()
sz := queue.Size()
if sz != 0 {
t.Error("Init queue size must be zero, but now is", sz)
}
empty := queue.IsEmpty()
if !empty {
t.Error("Init queue must be empty, but now is not empty")
}
}
func TestFileQueue_Enqueue(t *testing.T) {
path := Tempfile()
defer clearFiles(path, "testqueue")
var queue = new(FileQueue)
err := queue.Open(path, "testqueue", nil)
if err != nil {
fmt.Println(err)
}
defer queue.Close()
enqueue(queue, []byte("hello xiemalin"), 10, t)
}
func TestFileQueue_DequeueEmpty(t *testing.T) {
path := Tempfile()
defer clearFiles(path, "testqueue")
var queue = new(FileQueue)
err := queue.Open(path, "testqueue", nil)
if err != nil {
fmt.Println(err)
}
defer queue.Close()
dequeueEmpty(queue, t)
}
func TestFileQueue_EnqueueDequeue(t *testing.T) {
path := Tempfile()
defer clearFiles(path, "testqueue")
var queue = new(FileQueue)
err := queue.Open(path, "testqueue", nil)
if err != nil {
fmt.Println(err)
}
defer queue.Close()
enqueue(queue, []byte("hello xiemalin"), 10, t)
dequeue(queue, []byte("hello xiemalin"), 10, t)
// to check there are no message avaiable
dequeueEmpty(queue, t)
}
func TestFileQueue_Skip(t *testing.T) {
path := Tempfile()
defer clearFiles(path, "testqueue")
var queue = new(FileQueue)
err := queue.Open(path, "testqueue", nil)
if err != nil {
fmt.Println(err)
}
defer queue.Close()
enqueue(queue, []byte("hello xiemalin"), 10, t)
queue.Skip(5)
dequeue(queue, []byte("hello xiemalin"), 5, t)
// to check there are no message avaiable
dequeueEmpty(queue, t)
}
func TestFileQueue_Peek(t *testing.T) {
path := Tempfile()
defer clearFiles(path, "testqueue")
var queue = new(FileQueue)
err := queue.Open(path, "testqueue", nil)
if err != nil {
fmt.Println(err)
}
defer queue.Close()
// peek to an empty queue
index, bb, err := queue.Peek()
if err != nil {
t.Error("Error peek to an empty queue should return nil err, but actually is ", err)
}
if index != -1 {
t.Error("Error peek to an empty queue should return index of '-1', but actually is ", index)
}
if bb != nil {
t.Error("Error peek to an empty queue should return nil, but actually is ", bb)
}
enqueue(queue, []byte("hello xiemalin"), 10, t)
index, bb, err = queue.Peek()
index2, bb2, err2 := queue.Peek()
if index != index2 || strings.Compare(string(bb), string(bb2)) != 0 || err != err2 {
t.Error("Error peek twice but return different result ")
}
}
func TestFileQueue_Gc(t *testing.T) {
path := Tempfile()
defer clearFiles(path, "testqueue")
var queue = new(FileQueue)
// use custom options
var options = &Options{
DataPageSize: 128,
GcLock: false,
IndexItemsPerPage: 17,
}
err := queue.Open(path, "testqueue", options)
if err != nil {
fmt.Println(err)
}
defer queue.Close()
enqueue(queue, []byte("hello xiemalin"), 500, t)
dequeue(queue, []byte("hello xiemalin"), 500, t)
queue.Gc()
}
func TestFileQueue_Subscribe(t *testing.T) {
path := Tempfile()
defer clearFiles(path, "testqueue")
i := 0
var queue = new(FileQueue)
err := queue.Subscribe(func(index int64, bb []byte, err error) {
i++
})
// here should err
if err != SubscribeFailedNoOpenErr {
t.Error("Subscribe shoule return err before queue opened.")
}
err = queue.Open(path, "testqueue", nil)
queue.Subscribe(func(index int64, bb []byte, err error) {
i++
})
if err != nil {
fmt.Println(err)
}
defer queue.Close()
sz := 10
doEnqueue(queue, []byte("hello xiemalin"), sz, t)
time.Sleep(time.Duration(2) * time.Second)
if i != sz {
t.Error("subscribe count should be", sz, " but actually is ", i)
}
queue.FreeSubscribe()
}
func TestFileQueue_FreeSubscribe(t *testing.T) {
path := Tempfile()
defer clearFiles(path, "testqueue")
i := 0
var queue = new(FileQueue)
err := queue.Open(path, "testqueue", nil)
queue.Subscribe(func(index int64, bb []byte, err error) {
i++
})
if err != nil {
fmt.Println(err)
}
defer queue.Close()
queue.FreeSubscribe()
sz := 10
// no longer receive subscrbie callback
enqueue(queue, []byte("hello xiemalin"), sz, t)
if i != 0 {
t.Error("subscribe count should be 0, but actually is ", i)
}
}
// tempfile returns a temporary file path.
func Tempfile() string {
return "./bin/temp"
}
func enqueue(queue Queue, content []byte, size int, t *testing.T) {
doEnqueue(queue, content, size, t)
sz := queue.Size()
if sz != int64(size) {
t.Error("Error enqueue count expect size is ", size, ", but acutal is ", sz)
}
}
func doEnqueue(queue Queue, content []byte, size int, t *testing.T) {
for i := 0; i < size; i++ {
idx, err := queue.Enqueue(content)
if err != nil {
t.Error("Enqueue failed with err:", err)
}
if idx != int64(i) {
t.Error("Error enqueue index, current is", idx, " expected is", i)
}
}
}
func dequeue(queue Queue, expectContent []byte, expectSize int, t *testing.T) {
count := 0
// enqueue 10 items
for i := 0; i < expectSize; i++ {
idx, bb, err := queue.Dequeue()
if err != nil {
t.Error("Dequeue failed with err:", err)
continue
}
if idx == -1 {
t.Error("Error enqueue index, current is -1")
continue
}
count++
if strings.Compare(string(expectContent), string(bb)) != 0 {
t.Error("Dequeue error with unexcept message from queue, expect is", string(expectContent), "but actually is", string(bb))
}
}
if count != expectSize {
t.Error("Error dequeue count expect size is ", expectSize, ", but acutal is ", count)
}
}
func dequeueEmpty(queue Queue, t *testing.T) {
idx, _, err := queue.Dequeue()
if err != nil {
t.Error("Met errors on dequeue action from an empty file queue =>", err)
}
if idx != -1 {
t.Error("Empty queue dequeue index must return -1, but actually is ", idx)
}
}
func clearFiles(path string, queueName string) {
RemoveFiles(path + "/" + queueName + "/" + DataFileName)
RemoveFiles(path + "/" + queueName + "/" + FrontFileName)
RemoveFiles(path + "/" + queueName + "/" + IndexFileName)
RemoveFiles(path + "/" + queueName + "/" + MetaFileName)
}