-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactionqueue_test.go
More file actions
139 lines (102 loc) · 2.41 KB
/
actionqueue_test.go
File metadata and controls
139 lines (102 loc) · 2.41 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
package actionqueue
import (
"fmt"
"math/rand"
"os"
"testing"
"time"
)
var filename string = "/tmp/action-queue-test"
func cleanup(queue *ActionQueue) {
queue.Close()
os.Remove(filename)
}
func addRandomActions(queue *ActionQueue, delay time.Duration) int {
rand.Seed(time.Now().UnixNano())
total := rand.Intn(100)
for i := 0; i < total; i++ {
queue.AddAction(fmt.Sprintf("Action %d", i))
time.Sleep(delay)
}
return total
}
func testEntry(entry *ActionEntry, tested int, err error, t *testing.T) {
if entry.pos != tested {
t.Error(fmt.Sprintf("Position %d != %d", entry.pos, tested))
}
if entry.def != fmt.Sprintf("Action %d", tested) {
t.Error(fmt.Sprintf("Action %d != %s", tested, entry.def))
}
if err != nil {
t.Error(err)
}
}
func TestAddAction(t *testing.T) {
queue, err := NewActionQueue(filename)
defer cleanup(queue)
if err != nil {
t.Fatal(err)
}
pos, err := queue.AddAction("{\"test\":\"value\"}")
if err != nil {
t.Fatal(err)
}
if pos <= 0 {
t.Fatal("Position was", pos)
}
}
func TestReadAllHistory(t *testing.T) {
queue, _ := NewActionQueue(filename)
tested := 0
defer cleanup(queue)
total := addRandomActions(queue, 0)
callback := func(entry *ActionEntry, err error) {
testEntry(entry, tested, err, t)
tested++
}
count, err := queue.ReadHistory(callback, 0, -1)
if err != nil {
t.Fatal(err)
}
if count != total || tested != total {
t.Fatal(fmt.Sprintf("Count %d != %d != %d", total, count, tested))
}
}
func TestReadHistoryParts(t *testing.T) {
queue, _ := NewActionQueue(filename)
tested := 0
defer cleanup(queue)
total := 67
begin := 23
stop := 60
for i := 0; i < total; i++ {
queue.AddAction(fmt.Sprintf("Action %d", i))
}
callback := func(entry *ActionEntry, err error) {
testEntry(entry, tested+begin, err, t)
tested++
}
count, err := queue.ReadHistory(callback, begin, stop)
if err != nil {
t.Fatal(err)
}
if count != tested {
t.Fatal(fmt.Sprintf("Count %d > %d != %d", total, count, tested))
}
}
func TestTailHistory(t *testing.T) {
queue, _ := NewActionQueue(filename)
tested := 0
defer cleanup(queue)
done := make(chan bool)
callback := func(entry *ActionEntry, err error) {
testEntry(entry, tested, err, t)
tested++
}
go queue.TailHistory(callback, 0, done)
total := addRandomActions(queue, 10*time.Millisecond)
done <- true
if tested != total {
t.Fatal(fmt.Sprintf("Tested %d != %d", total, tested))
}
}