-
Notifications
You must be signed in to change notification settings - Fork 13
/
gobeanstalk_test.go
210 lines (186 loc) · 4.43 KB
/
gobeanstalk_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
package gobeanstalk
import (
"bytes"
"testing"
"time"
)
const (
testtube = "testtube"
testjob = "testjob"
)
func dial(t *testing.T) *Conn {
conn, err := Dial("localhost:11300")
if err != nil {
t.Fatal("Dial failed.err = :", err.Error())
}
return conn
}
func TestDial(t *testing.T) {
if _, err := Dial("localhost:11300"); err != nil {
t.Fatal("Dial failed.err = :", err.Error())
}
}
func TestUse(t *testing.T) {
conn := dial(t)
defer conn.Quit()
err := conn.Use(testtube)
if err != nil {
t.Fatal("use failed.Err = ", err.Error())
}
}
func put(t *testing.T, tubename string, jobBody string) {
conn := dial(t)
defer conn.Quit()
err := conn.Use(tubename)
if err != nil {
t.Fatal("use failed.Err = ", err.Error())
}
_, err = conn.Put([]byte(jobBody), 0, 0*time.Second, 30*time.Second)
if err != nil {
t.Fatal("Put failed. Err = ", err.Error())
}
}
func TestPut(t *testing.T) {
put(t, testtube, testjob)
}
func TestPutBigJob(t *testing.T) {
var buf bytes.Buffer
for i := 0; i < 500; i++ {
buf.WriteString("1234567890")
}
put(t, "TestPutBigJob", buf.String())
}
func watch(t *testing.T, tubename string) *Conn {
conn := dial(t)
_, err := conn.Watch(tubename)
if err != nil {
t.Fatal(err)
}
return conn
}
func TestWatch(t *testing.T) {
watch(t, testtube)
}
func reserve(t *testing.T, tubename string, timeout ...time.Duration) (*Conn, *Job) {
conn := watch(t, tubename)
var j *Job
var err error
if len(timeout) > 0 {
j, err = conn.Reserve(timeout[0])
} else {
j, err = conn.Reserve()
}
if err == ErrTimedOut {
return conn, nil
}
if err != nil {
t.Fatal(err)
}
if string(j.Body) != testjob {
t.Fatalf("job body check failed. content = %v", string(j.Body))
}
return conn, j
}
func TestReserve(t *testing.T) {
conn, j := reserve(t, testtube)
_, j2 := reserve(t, testtube, 2*time.Second) // this should make the test take ~2 seconds, but not 30!
if j2 != nil {
t.Error("reserving with timeout when there is nothing to reserve did not return nothing")
}
conn.Release(j.ID, 0, 0*time.Second)
}
func TestStatsJob(t *testing.T) {
conn, j := reserve(t, testtube)
yaml, err := conn.StatsJob(j.ID)
if err != nil {
t.Fatal("StatsJob failed. Err = ", err.Error())
}
t.Log(string(yaml))
conn.Release(j.ID, 0, 0*time.Second)
// test that it works without reserving first, now that we have a valid
// job id
yaml, err = conn.StatsJob(j.ID)
if err != nil {
t.Fatal("StatsJob failed without reserving first. Err = ", err.Error())
}
}
func TestStatsTube(t *testing.T) {
conn := dial(t)
yaml, err := conn.StatsTube(testtube)
if err != nil {
t.Fatal("StatsTube failed. Err = ", err.Error())
}
t.Log(string(yaml))
}
func TestStats(t *testing.T) {
conn := dial(t)
yaml, err := conn.Stats()
if err != nil {
t.Fatal("Stats failed. Err = ", err.Error())
}
t.Log(string(yaml))
}
func TestListTubes(t *testing.T) {
conn := dial(t)
yaml, err := conn.ListTubes()
if err != nil {
t.Fatal("ListTubes failed. Err = ", err.Error())
}
t.Log(string(yaml))
}
func TestDelete(t *testing.T) {
conn, j := reserve(t, testtube)
err := conn.Delete(j.ID)
if err != nil {
t.Error("delete failed. Err = ", err.Error())
}
}
func TestBury(t *testing.T) {
put(t, testtube, testjob)
conn, j := reserve(t, testtube)
err := conn.Bury(j.ID, 0)
if err != nil {
t.Error("bury failed. Err = ", err.Error())
}
conn, j = reserve(t, testtube, 0*time.Second)
if j != nil {
t.Error("bury did not make the job unreservable")
}
}
func TestKick(t *testing.T) {
conn := watch(t, testtube)
conn.Use(testtube)
num, err := conn.Kick(5)
if err != nil {
t.Error("kick failed. Err = ", err.Error())
}
if num != 1 {
t.Error("kick did not return the expected number of jobs kicked")
}
conn, j := reserve(t, testtube, 0*time.Second)
if j == nil {
t.Fatal("kick did not make the job reservable")
}
// since we know the job ID here, we'll test KickJob as well
jobID := j.ID
conn.Bury(jobID, 0)
err = conn.KickJob(jobID)
if err != nil {
t.Error("kick-job failed. Err = ", err.Error())
}
conn, j = reserve(t, testtube, 0*time.Second)
if j == nil {
t.Fatal("kick-job did not make the job reservable")
}
conn.Delete(jobID)
}
func TestParseError(t *testing.T) {
err := parseError("OUT_OF_MEMORY\r\n")
if err != ErrOutOfMemory {
t.Fatalf("expected ErrOutOfMemory got :%v", err)
}
err = parseError("test error")
if err.Error() != "unknown error: test error" {
t.Fatalf("expected `unknown error: test error` got `%v`", err)
}
}