-
Notifications
You must be signed in to change notification settings - Fork 2
/
disque_test.go
212 lines (164 loc) · 4.54 KB
/
disque_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
package disque
import (
"bytes"
"os"
"os/exec"
"reflect"
"strings"
"testing"
"time"
)
var pool *Pool
func TestMain(m *testing.M) {
cmd := exec.Command("docker", "run", "-dp", "7711:7711", "efrecon/disque:1.0-rc1")
var id bytes.Buffer
cmd.Stderr = os.Stderr
cmd.Stdout = &id
if err := cmd.Run(); err != nil {
panic(err)
}
pool = New("127.0.0.1:7711", 240*time.Second)
status := m.Run()
cmd = exec.Command("docker", "kill", strings.TrimSpace(id.String()))
cmd.Stderr = os.Stderr
cmd.Run()
os.Exit(status)
}
func TestAck(t *testing.T) {
if err := pool.Ack(Job{ID: "D-49a4ad3b-fCAnZ8eFxSxKyl5fX58KCrGM-05a1"}); err != nil {
t.Fatal(err)
}
}
func TestAdd(t *testing.T) {
pool.flush()
if _, err := pool.Add("foo", "bar", time.Second, nil); err != nil {
t.Fatal(err)
}
if _, err := pool.Add(
"foo",
"bar",
time.Second,
&AddOptions{true, time.Second, 9, 1, time.Minute, time.Hour},
); err != nil {
t.Fatal(err)
}
}
func TestAddDelayGreaterThanTTL(t *testing.T) {
pool.flush()
exp := "ERR The specified DELAY is greater than TTL. Job refused since would never be delivered"
if _, err := pool.Add(
"foo",
"bar",
time.Second,
&AddOptions{Delay: time.Second, TTL: time.Second},
); err == nil || err.Error() != exp {
t.Errorf("Error was incorrect, got: %s, want: %s.", err, exp)
}
}
func TestAddNotEnoughReachableNodes(t *testing.T) {
pool.flush()
exp := "NOREPL Not enough reachable nodes for the requested replication level"
if _, err := pool.Add(
"foo", "bar", time.Second, &AddOptions{Replicate: 2},
); err == nil || err.Error() != exp {
t.Errorf("Error was incorrect, got: %s, want: %s.", err, exp)
}
}
func TestFastAck(t *testing.T) {
if err := pool.FastAck(Job{ID: "D-49a4ad3b-fCAnZ8eFxSxKyl5fX58KCrGM-05a1"}); err != nil {
t.Fatal(err)
}
}
func TestGet(t *testing.T) {
pool.flush()
exp := []Job{{Queue: "foo", Body: "bar"}}
exp[0].ID, _ = pool.Add("foo", "bar", time.Second, nil)
if jobs, err := pool.Get(&GetOptions{NoHang: true}, "foo"); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(jobs, exp) {
t.Errorf("Get was incorrect, got: %#v, want: %#v.", jobs, exp)
}
}
func TestGetMultiWithCounters(t *testing.T) {
pool.flush()
exp := []Job{{Queue: "foo", Body: "bar"}, {Queue: "foo", Body: "baz"}}
exp[0].ID, _ = pool.Add("foo", "bar", time.Second, nil)
exp[1].ID, _ = pool.Add("foo", "baz", time.Second, nil)
if jobs, err := pool.Get(&GetOptions{2, true, time.Second, true}, "foo"); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(jobs, exp) {
t.Errorf("Get was incorrect, got: %#v, want: %#v.", jobs, exp)
}
}
func TestGetNoQueue(t *testing.T) {
exp := "ERR syntax error"
if _, err := pool.Get(nil); err == nil || err.Error() != exp {
t.Errorf("Error was incorrect, got: %s, want: %s.", err, exp)
}
}
func TestGetEmpty(t *testing.T) {
pool.flush()
if _, err := pool.Get(&GetOptions{NoHang: true}, "foo"); err != nil {
t.Fatal(err)
}
}
func TestLen(t *testing.T) {
pool.flush()
pool.Add("foo", "bar", time.Second, nil)
if got, err := pool.Len("foo"); err != nil {
t.Fatal(err)
} else if got != 1 {
t.Errorf("Len was incorrect, got: %d, want: %d.", got, 1)
}
}
func TestNack(t *testing.T) {
if err := pool.Nack(Job{ID: "D-49a4ad3b-fCAnZ8eFxSxKyl5fX58KCrGM-05a1"}); err != nil {
t.Fatal(err)
}
}
func TestPing(t *testing.T) {
if got, err := pool.Ping(); err != nil {
t.Fatal(err)
} else if got != "PONG" {
t.Errorf("Ping was incorrect, got: %s, want: %s.", got, "PONG")
}
}
func TestStat(t *testing.T) {
pool.flush()
pool.Add("foo", "bar", time.Second, nil)
exp := map[string]interface{}{
"age": int64(0),
"blocked": int64(0),
"idle": int64(0),
"import-from": []string{},
"import-rate": int64(0),
"jobs-in": int64(1),
"jobs-out": int64(0),
"len": int64(1),
"name": "foo",
"pause": "none",
}
if stat, err := pool.Stat("foo"); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(stat, exp) {
t.Errorf("Stat was incorrect, got: %#v, want: %#v.", stat, exp)
}
}
func TestStatNonExistentQueue(t *testing.T) {
if stat, err := pool.Stat("non-existent-queue"); err != nil {
t.Fatal(err)
} else if stat != nil {
t.Errorf("Stat was incorrect, got: %#v, want: %#v.", stat, nil)
}
}
func TestWorking(t *testing.T) {
pool.flush()
exp := time.Minute
job := Job{}
job.ID, _ = pool.Add("foo", "bar", time.Second, &AddOptions{Retry: exp})
if got, err := pool.Working(job); err != nil {
t.Fatal(err)
} else if got != exp {
t.Errorf("Working was incorrect, got: %q, want: %q.", got, exp)
}
}