-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobqueue_test.go
203 lines (186 loc) · 4.58 KB
/
jobqueue_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
package main
import (
"encoding/json"
"fmt"
"github.com/nutrun/lentil"
"testing"
"os"
)
func resetConfig() {
Config = NewConfig("", "", "")
Config.deps = make(map[string][]string)
}
func TestPriority(t *testing.T) {
q := connect(t)
resetConfig()
Config.deps["tube1"] = []string{"tube2"}
put(t, "job1", "tube1", 0, q)
put(t, "job2", "tube2", 0, q)
jobs := NewJobQueue(q, false, make([]string, 0))
assertNextJob(t, jobs, "job2")
assertNextJob(t, jobs, "job1")
}
func TestIncludeExclude(t *testing.T) {
q := connect(t)
resetConfig()
all := NewJobQueue(q, false, make([]string, 0))
if !all.Include("tube") {
t.Errorf("Should include tube")
}
if !all.Include("another") {
t.Errorf("Should include another")
}
none := NewJobQueue(q, true, make([]string, 0))
if none.Include("none") {
t.Errorf("Should not include tube none")
}
include := NewJobQueue(q, true, []string{"in"})
if !include.Include("in") {
t.Errorf("Should include tube in")
}
if include.Include("out") {
t.Errorf("Should not include tube out")
}
exclude := NewJobQueue(q, false, []string{"out"})
if !exclude.Include("in") {
t.Errorf("Should not include tube in")
}
if exclude.Include("out") {
t.Errorf("Should not include tube out")
}
}
func TestMoarPriorities(t *testing.T) {
q := connect(t)
resetConfig()
Config.deps["tube3"] = []string{"tube2"}
Config.deps["tube1"] = []string{"tube3"}
put(t, "job11", "tube1", 0, q)
put(t, "job21", "tube2", 0, q)
put(t, "job31", "tube3", 0, q)
put(t, "job22", "tube2", 0, q)
put(t, "job32", "tube3", 0, q)
put(t, "job12", "tube1", 0, q)
jobs := NewJobQueue(q, false, make([]string, 0))
assertNextJob(t, jobs, "job21")
assertNextJob(t, jobs, "job22")
assertNextJob(t, jobs, "job31")
assertNextJob(t, jobs, "job32")
assertNextJob(t, jobs, "job11")
assertNextJob(t, jobs, "job12")
}
func TestSleepWhenNoJobs(t *testing.T) {
q := connect(t)
resetConfig()
jobs := NewJobQueue(q, false, make([]string, 0))
no_job, err := reserveNextJob(t, jobs, "job11")
if no_job != nil {
t.Error(fmt.Sprintf("Reserved %v when should not have", no_job))
}
if err == nil {
t.Error(fmt.Sprintf("Should have thrown a TIME_OUT, threw %v instead", err))
}
}
func TestBlockOnReserved(t *testing.T) {
q := connect(t)
resetConfig()
Config.deps["tube1"] = []string{"tube2"}
put(t, "job1", "tube1", 0, q)
put(t, "job2", "tube2", 0, q)
jobs := NewJobQueue(q, false, make([]string, 0))
job, err := reserveNextJob(t, jobs, "job2")
if err != nil {
t.Error(fmt.Sprintf("Could not reserve job %s", job))
}
no_job, err := reserveNextJob(t, jobs, "job1")
if no_job != nil {
t.Error(fmt.Sprintf("Reserved %v when should not have", no_job))
}
if err == nil {
t.Error(fmt.Sprintf("Should have thrown a TIME_OUT, threw %v instead", err))
}
}
func TestBlockOnIgnored(t *testing.T) {
q := connect(t)
resetConfig()
Config.deps["another"] = []string{"block_on"}
put(t, "job", "block_on", 0, q)
put(t, "another", "another", 0, q)
jobs := NewJobQueue(q, false, []string{"block_on"})
no_job, err := reserveNextJob(t, jobs, "job")
if no_job != nil {
t.Error(fmt.Sprintf("Reserved %v when should not have", no_job))
}
if err == nil {
t.Error(fmt.Sprintf("Should have thrown a TIME_OUT, threw %v instead", err))
}
}
func assertNextJob(t *testing.T, jobqueue *JobQueue, expected string) {
jobinfo := make(map[string]string)
job, e := jobqueue.Next()
if e != nil {
t.Error(fmt.Sprintf("%v on [%v]", e, expected))
return
}
json.Unmarshal(job.Body, &jobinfo)
if jobinfo["name"] != expected {
t.Errorf("%s != %s\n", expected, jobinfo["name"])
}
jobqueue.Delete(job.Id)
}
func reserveNextJob(t *testing.T, jobqueue *JobQueue, expected string) (*lentil.Job, error) {
job, e := jobqueue.Next()
if e != nil {
return nil, e
}
return job, e
}
func put(t *testing.T, jobName, tube string, delay int, q *lentil.Beanstalkd) {
job := make(map[string]string)
job["tube"] = tube
job["name"] = jobName
jobjson, _ := json.Marshal(job)
e := q.Use(tube)
if e != nil {
t.Fatal(e)
}
_, e = q.Put(0, delay, 60, jobjson)
if e != nil {
t.Error(e)
}
}
func connect(t *testing.T) *lentil.Beanstalkd {
gq := os.Getenv("GLOW_QUEUE")
if gq == "" {
gq = "localhost:11300"
}
q, e := lentil.Dial(gq)
if e != nil {
t.Fatal(e)
}
// Clear beanstalkd
tubes, e := q.ListTubes()
if e != nil {
t.Fatal(e)
}
for _, tube := range tubes {
if tube == "default" {
continue
}
_, e = q.Watch(tube)
if e != nil {
t.Fatal(e)
}
for {
job, e := q.ReserveWithTimeout(0)
if e != nil {
break
}
q.Delete(job.Id)
}
_, e := q.Ignore(tube)
if e != nil {
t.Fatal(e)
}
}
return q
}