-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue_test.go
154 lines (136 loc) · 3.41 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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func createTempQueue(t *testing.T) *IndexQueue {
tmpDir, err := ioutil.TempDir("", "apec-")
if err != nil {
t.Fatalf("could not create temporary directory: %s", err)
}
path := filepath.Join(tmpDir, "queue")
queue, err := OpenIndexQueue(path)
if err != nil {
t.Fatalf("could not open queue: %s", err)
}
return queue
}
func deleteTempQueue(t *testing.T, queue *IndexQueue) {
queue.Close()
os.RemoveAll(queue.Path())
}
func TestEmptyQueue(t *testing.T) {
queue := createTempQueue(t)
defer deleteTempQueue(t, queue)
// Empty queue should be empty
size := queue.Size()
if size != 0 {
t.Fatalf("empty queue is not empty: %d", size)
}
// Cannot fetch anything from empty queue
queued, err := queue.FetchMany(10)
if err != nil {
t.Fatalf("failed to fetch from empty queue: %s", err)
}
if len(queued) != 0 {
t.Fatalf("fetch items from empty queue: %v", queue)
}
// Can delete anything from empty queue
err = queue.DeleteMany(3)
if err != nil {
t.Fatalf("cannot delete from empty queue: %s", err)
}
}
func checkFetched(t *testing.T, queue *IndexQueue, wanted []Queued) {
for i := 0; i < len(wanted)+1; i++ {
queued, err := queue.FetchMany(i)
if err != nil {
t.Fatalf("could not fetch %d items: %s", i, err)
}
n := i
if n > len(wanted) {
n = len(wanted)
}
if len(queued) != n {
t.Fatalf("fetched %d, got %d", i, len(queued))
}
for j := 0; j < n; j++ {
q := queued[j]
w := wanted[j]
w.Seq = q.Seq
if w != q {
t.Fatalf("items %d differ: %+v != %+v", j, w, q)
}
}
}
}
func checkQueue(t *testing.T, queue *IndexQueue, entries []Queued, wanted []Queued) {
size := queue.Size()
err := queue.QueueMany(entries)
if err != nil {
t.Fatalf("cannot queue %d items: %s", len(entries), err)
}
if queue.Size() != (size + len(entries)) {
t.Fatalf("queue size should be %d: %d", size+len(entries), queue.Size())
}
checkFetched(t, queue, wanted)
}
func checkDelete(t *testing.T, queue *IndexQueue, count int, wanted []Queued) {
size := queue.Size()
err := queue.DeleteMany(count)
if err != nil {
t.Fatalf("cannot delete %d items: %s", count, err)
}
size -= count
if size < 0 {
size = 0
}
if size != queue.Size() {
t.Fatalf("queue size should be %d: %d", size, queue.Size())
}
checkFetched(t, queue, wanted)
}
func TestQueueOperations(t *testing.T) {
queue := createTempQueue(t)
defer deleteTempQueue(t, queue)
err := queue.QueueMany(nil)
if err != nil {
t.Fatalf("cannot queue zero item: %s", err)
}
// Add 3 entries
entries := []Queued{
{Id: "0", Op: AddOp},
{Id: "1", Op: RemoveOp},
{Id: "2", Op: AddOp},
}
checkQueue(t, queue, entries, entries)
// Remove less than available
checkDelete(t, queue, 0, entries)
entries = entries[2:]
checkDelete(t, queue, 2, entries)
// Add more entries and remove the exact amount
added := []Queued{
Queued{Id: "3", Op: RemoveOp},
}
entries = append(entries, added...)
checkQueue(t, queue, added, entries)
// Remove exact amount
entries = entries[2:]
checkDelete(t, queue, 2, entries)
// Add again and remove more than added
added = []Queued{
Queued{Id: "4", Op: AddOp},
Queued{Id: "5", Op: RemoveOp},
Queued{Id: "6", Op: AddOp},
}
entries = append(entries, added...)
checkQueue(t, queue, added, entries)
entries = nil
checkDelete(t, queue, 25, entries)
err = queue.Close()
if err != nil {
t.Fatalf("could not close queue: %s", err)
}
}