-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgotasks_test.go
193 lines (165 loc) · 5.48 KB
/
gotasks_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
package gotasks
import (
"context"
"errors"
"log"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const (
testJobName = "test_job"
testPanicJobName = "test_panic_job"
testArgsPassJobName = "test_args_pass_job"
testReentrantJobName = "test_reentrant_job"
testHandlerNotFoundJobName = "test_handler_not_found"
testQueueName = "test_queue"
testRedisURL = "redis://127.0.0.1:6379/0"
)
func TestAckWhen(t *testing.T) {
AckWhen(AckWhenAcquired)
AckWhen(AckWhenSucceed)
}
func TestGenFunctions(t *testing.T) {
assert.Equal(t, "gt:task:abcd", genTaskName("abcd"))
assert.Equal(t, "gt:queue:abcd", genQueueName("abcd"))
}
func TestRedisBroker(t *testing.T) {
// register tasks
handler1 := func(args ArgsMap) (ArgsMap, error) {
time.Sleep(time.Duration(1) * time.Microsecond)
return args, nil
}
handler2 := func(args ArgsMap) (ArgsMap, error) {
time.Sleep(time.Duration(1) * time.Microsecond)
return args, nil
}
Register(testJobName, handler1, handler2)
// set broker
UseRedisBroker(testRedisURL, WithRedisTaskTTL(100))
// enqueue
log.Printf("current jobMap: %+v", jobMap)
queue := NewQueue(testQueueName, WithMaxLimit(20), WithMonitorInterval(5))
taskID := queue.Enqueue(testJobName, MapToArgsMap(map[string]interface{}{}))
defer rc.Del(genTaskName(taskID))
ctx, cancel := context.WithCancel(context.Background())
go Run(ctx, testQueueName) // it will blocking until the first job is executed
time.Sleep(time.Second * time.Duration(1))
cancel()
log.Printf("Run function returned, ctx: %+v", ctx)
}
func TestPanicHandler(t *testing.T) {
// register tasks
handler1 := func(args ArgsMap) (ArgsMap, error) {
time.Sleep(time.Duration(1) * time.Microsecond)
return args, nil
}
handler2 := func(args ArgsMap) (ArgsMap, error) {
time.Sleep(time.Duration(1) * time.Microsecond)
panic("whoops")
//return args, nil
}
handler3 := func(args ArgsMap) (ArgsMap, error) {
time.Sleep(time.Duration(1) * time.Microsecond)
return args, nil
}
Register(testPanicJobName, handler1, handler2, handler3)
// set broker
UseRedisBroker(testRedisURL)
// enqueue
log.Printf("current jobMap: %+v", jobMap)
queue := NewQueue(testQueueName)
taskID := queue.Enqueue(testPanicJobName, MapToArgsMap(map[string]interface{}{}))
defer rc.Del(genTaskName(taskID))
ctx, cancel := context.WithCancel(context.Background())
go Run(ctx, testQueueName) // it will blocking until the first job is executed
time.Sleep(time.Second * time.Duration(1))
cancel()
log.Printf("Run function returned, ctx: %+v", ctx)
// check result
taskBytes := []byte{}
if err := rc.Get(genTaskName(taskID)).Scan(&taskBytes); err != nil {
t.Logf("failed to get task %s: %s", taskID, err)
t.FailNow()
}
task := Task{}
if err := json.Unmarshal(taskBytes, &task); err != nil {
t.Logf("failed to get task %s: %s", taskID, err)
t.FailNow()
}
assert.Equal(t, 1, task.CurrentHandlerIndex)
// check result ttl
duration, err := rc.TTL(genTaskName(taskID)).Result()
if err != nil {
t.Logf("task %s should have ttl with err %s", taskID, err)
t.FailNow()
}
if duration.Seconds() == 0 {
t.Logf("task %s should have ttl but not", taskID)
t.FailNow()
}
}
func TestArgsPass(t *testing.T) {
// register tasks
handler1 := func(args ArgsMap) (ArgsMap, error) {
time.Sleep(time.Duration(1) * time.Microsecond)
args["hello"] = "world"
return args, nil
}
handler2 := func(args ArgsMap) (ArgsMap, error) {
time.Sleep(time.Duration(1) * time.Microsecond)
assert.Equal(t, "world", args["hello"])
return args, nil
}
Register(testArgsPassJobName, handler1, handler2)
// set broker
UseRedisBroker(testRedisURL)
// enqueue
log.Printf("current jobMap: %+v", jobMap)
queue := NewQueue(testQueueName)
taskID := queue.Enqueue(testArgsPassJobName, MapToArgsMap(map[string]interface{}{}))
defer rc.Del(genTaskName(taskID))
ctx, cancel := context.WithCancel(context.Background())
go Run(ctx, testQueueName) // it will blocking until the first job is executed
time.Sleep(time.Second * time.Duration(1))
cancel()
log.Printf("Run function returned, ctx: %+v", ctx)
}
func TestReentrant(t *testing.T) {
// register tasks
handler1 := func(args ArgsMap) (ArgsMap, error) {
time.Sleep(time.Duration(1) * time.Microsecond)
args["hello"] = "world"
return args, nil
}
handler2 := func(args ArgsMap) (ArgsMap, error) {
return args, errors.New("hello world error")
}
Register(testReentrantJobName, handler1, Reentrant(handler2, WithMaxTimes(3), WithSleepyMS(10)))
// set broker
UseRedisBroker(testRedisURL)
// enqueue
log.Printf("current jobMap: %+v", jobMap)
queue := NewQueue(testQueueName)
taskID := queue.Enqueue(testReentrantJobName, MapToArgsMap(map[string]interface{}{}))
defer rc.Del(genTaskName(taskID))
ctx, cancel := context.WithCancel(context.Background())
go Run(ctx, testQueueName) // it will blocking until the first job is executed
time.Sleep(time.Second * time.Duration(1))
cancel()
log.Printf("Run function returned, ctx: %+v", ctx)
}
func TestJobHandlerNotFound(t *testing.T) {
// set broker
UseRedisBroker(testRedisURL)
// enqueue
log.Printf("current jobMap: %+v", jobMap)
queue := NewQueue(testQueueName)
taskID := queue.Enqueue(testHandlerNotFoundJobName, MapToArgsMap(map[string]interface{}{}))
defer rc.Del(genTaskName(taskID))
ctx, cancel := context.WithCancel(context.Background())
go Run(ctx, testQueueName) // it will blocking until the first job is executed
time.Sleep(time.Second * time.Duration(1))
cancel()
log.Printf("Run function returned, ctx: %+v", ctx)
}