-
Notifications
You must be signed in to change notification settings - Fork 2
/
job_test.go
166 lines (137 loc) · 3.67 KB
/
job_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
package qless
import (
"testing"
"fmt"
"github.com/stretchr/testify/assert"
)
var _ = fmt.Println
func TestJob_HeartbeatForInvalidReturnsError(t *testing.T) {
c := newClientFlush()
c.SetConfig("heartbeat", -10)
c.SetConfig("grace-period", 0)
q := c.Queue("test_queue")
q.Put("class", "data", WithJID("jobTestDEF"))
workerNameStr = "worker-1"
j1, err := q.PopOne()
assert.NoError(t, err)
workerNameStr = "worker-2"
q.PopOne()
_, err = j1.Heartbeat()
assert.Error(t, err)
assert.True(t, IsJobLost(err))
}
func TestJob_TTL(t *testing.T) {
c := newClientFlush()
q := c.Queue("test_queue")
q.Put("class", "data", WithJID("jobTestDEF"))
workerNameStr = "worker-1"
j1, err := q.PopOne()
assert.NoError(t, err)
assert.Condition(t, func() bool {
return j1.TTL() > 55
})
}
func TestJob_Complete(t *testing.T) {
c := newClientFlush()
q := c.Queue("test_queue")
q.Put("class", "data", WithJID("jobTestDEF"))
workerNameStr = "worker-1"
j1, err := q.PopOne()
assert.NoError(t, err)
res, err := j1.Complete()
assert.NoError(t, err)
assert.Equal(t, "complete", res)
}
func TestJob_Fail(t *testing.T) {
c := newClientFlush()
q := c.Queue("test_queue")
q.Put("class", "data", WithJID("jobTestDEF"))
workerNameStr = "worker-1"
j1, err := q.PopOne()
assert.NoError(t, err)
res, err := j1.Fail("type", "message")
assert.NoError(t, err)
assert.Equal(t, true, res)
j1, err = c.GetJob("jobTestDEF")
assert.NoError(t, err)
assert.NotNil(t, j1)
failure := j1.Failure()
failure.When = 0
assert.Equal(t, &Failure{Group: "type", Message: "message", Worker: workerNameStr}, failure)
}
func TestJob_Retry(t *testing.T) {
c := newClientFlush()
q := c.Queue("test_queue")
q.Put("class", "data", WithJID("jobTestDEF"))
workerNameStr = "worker-1"
j1, err := q.PopOne()
assert.NoError(t, err)
remain, err := j1.Retry(0)
assert.NoError(t, err)
assert.Equal(t, 4, remain)
}
func TestJob_RetryRespectsPutArgument(t *testing.T) {
c := newClientFlush()
q := c.Queue("test_queue")
q.Put("class", "data", WithJID("jobTestDEF"), WithRetries(1))
workerNameStr = "worker-1"
j1, err := q.PopOne()
assert.NoError(t, err)
remain, err := j1.Retry(0)
assert.NoError(t, err)
assert.Equal(t, 0, remain)
}
func TestJob_RetryReturnsNegativeOneWhenNoMoreAvailable(t *testing.T) {
c := newClientFlush()
q := c.Queue("test_queue")
q.Put("class", "data", WithJID("jobTestDEF"), WithRetries(0))
workerNameStr = "worker-1"
j1, err := q.PopOne()
assert.NoError(t, err)
remain, err := j1.Retry(0)
assert.NoError(t, err)
assert.Equal(t, -1, remain)
}
func TestJob_Cancel(t *testing.T) {
c := newClientFlush()
q := c.Queue("test_queue")
q.Put("class", "data", WithJID("jobTestDEF"))
workerNameStr = "worker-1"
j1, err := q.PopOne()
assert.NoError(t, err)
j1.Cancel()
j1, err = q.PopOne()
assert.NoError(t, err)
assert.Nil(t, j1)
}
func TestJob_UnmarshalDataOfStruct(t *testing.T) {
type testData struct {
String string
Int int
StringArray []string
}
inData := testData{String: "a string", Int: 0xbadf00d, StringArray: []string{"the", "fox"}}
c := newClientFlush()
q := c.Queue("test_queue")
q.Put("class", inData, WithJID("jobTestDEF"))
workerNameStr = "worker-1"
j1, err := q.PopOne()
assert.NoError(t, err)
var outData testData
err = j1.UnmarshalData(&outData)
assert.NoError(t, err)
assert.Equal(t, inData, outData)
}
func TestJob_UnmarshalDataOfString(t *testing.T) {
inData := "foo bar"
c := newClientFlush()
q := c.Queue("test_queue")
q.Put("class", inData, WithJID("jobTestDEF"))
workerNameStr = "worker-1"
j1, err := q.PopOne()
assert.NoError(t, err)
var outData string
err = j1.UnmarshalData(&outData)
assert.NoError(t, err)
assert.Equal(t, inData, outData)
}