forked from gocelery/gocelery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocelery_test.go
215 lines (179 loc) · 4.84 KB
/
gocelery_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
213
214
215
package gocelery
import (
"fmt"
"log"
"math/rand"
"reflect"
"testing"
"time"
)
func multiply(a int, b int) int {
return a * b
}
type multiplyKwargs struct {
a int
b int
}
func (m *multiplyKwargs) ParseKwargs(kwargs map[string]interface{}) error {
kwargA, ok := kwargs["a"]
if !ok {
return fmt.Errorf("undefined kwarg a")
}
kwargAFloat, ok := kwargA.(float64)
if !ok {
return fmt.Errorf("malformed kwarg a")
}
m.a = int(kwargAFloat)
kwargB, ok := kwargs["b"]
if !ok {
return fmt.Errorf("undefined kwarg b")
}
kwargBFloat, ok := kwargB.(float64)
if !ok {
return fmt.Errorf("malformed kwarg b")
}
m.b = int(kwargBFloat)
return nil
}
func (m *multiplyKwargs) RunTask() (interface{}, error) {
result := m.a * m.b
return result, nil
}
func getAMQPClient() (*CeleryClient, error) {
amqpBroker := NewAMQPCeleryBroker("amqp://")
amqpBackend := NewAMQPCeleryBackend("amqp://")
return NewCeleryClient(amqpBroker, amqpBackend, 4)
}
func getRedisClient() (*CeleryClient, error) {
redisBroker := NewRedisCeleryBroker("redis://localhost:6379")
redisBackend := NewRedisCeleryBackend("redis://localhost:6379")
return NewCeleryClient(redisBroker, redisBackend, 1)
}
func getClients() ([]*CeleryClient, error) {
redisClient, err := getRedisClient()
if err != nil {
return nil, err
}
amqpClient, err := getAMQPClient()
if err != nil {
return nil, err
}
return []*CeleryClient{
redisClient,
amqpClient,
}, nil
}
func debugLog(client *CeleryClient, format string, args ...interface{}) {
pre := fmt.Sprintf("client[%p] - ", client)
log.Printf(pre+format, args...)
}
func TestWorkerClient(t *testing.T) {
// prepare clients
celeryClients, err := getClients()
if err != nil {
t.Errorf("failed to create clients")
return
}
for i := 0; i < 2; i++ {
kwargTaskName := generateUUID()
kwargTask := &multiplyKwargs{}
argTaskName := generateUUID()
argTask := multiply
for j := 0; j < 2; j++ {
for _, celeryClient := range celeryClients {
debugLog(celeryClient, "registering kwarg task %s %p", kwargTaskName, kwargTask)
celeryClient.Register(kwargTaskName, kwargTask)
debugLog(celeryClient, "registering arg task %s %p", argTaskName, argTask)
celeryClient.Register(argTaskName, argTask)
debugLog(celeryClient, "starting worker")
celeryClient.StartWorker()
arg1 := rand.Intn(100)
arg2 := rand.Intn(100)
expected := arg1 * arg2
debugLog(celeryClient, "submitting tasks")
kwargAsyncResult, err := celeryClient.DelayKwargs(kwargTaskName, map[string]interface{}{
"a": arg1,
"b": arg2,
})
if err != nil {
t.Errorf("failed to submit kwarg task %s: %v", kwargTaskName, err)
return
}
argAsyncResult, err := celeryClient.Delay(argTaskName, arg1, arg2)
if err != nil {
t.Errorf("failed to submit arg task %s: %v", argTaskName, err)
return
}
debugLog(celeryClient, "waiting for result")
kwargVal, err := kwargAsyncResult.Get(10 * time.Second)
if err != nil {
t.Errorf("failed to get result: %v", err)
return
}
debugLog(celeryClient, "validating result")
actual := int(kwargVal.(float64))
if actual != expected {
t.Errorf("returned result %v is different from expected value %v", actual, expected)
return
}
debugLog(celeryClient, "waiting for result")
argVal, err := argAsyncResult.Get(10 * time.Second)
if err != nil {
t.Errorf("failed to get result: %v", err)
return
}
debugLog(celeryClient, "validating result")
actual = int(argVal.(float64))
if actual != expected {
t.Errorf("returned result %v is different from expected value %v", actual, expected)
return
}
debugLog(celeryClient, "stopping worker")
celeryClient.StopWorker()
}
}
}
}
func TestRegister(t *testing.T) {
celeryClients, err := getClients()
if err != nil {
t.Errorf("failed to create CeleryClients: %v", err)
return
}
taskName := generateUUID()
for _, celeryClient := range celeryClients {
celeryClient.Register(taskName, multiply)
task := celeryClient.worker.GetTask(taskName)
if !reflect.DeepEqual(reflect.ValueOf(multiply), reflect.ValueOf(task)) {
t.Errorf("registered task %v is different from received task %v", reflect.ValueOf(multiply), reflect.ValueOf(task))
return
}
}
}
/*
func TestBlockingGet(t *testing.T) {
celeryClients, err := getClients()
if err != nil {
t.Errorf("failed to create CeleryClients: %v", err)
return
}
for _, celeryClient := range celeryClients {
// send task
asyncResult, err := celeryClient.Delay("dummy", 3, 5)
if err != nil {
t.Errorf("failed to get async result")
return
}
duration := 1 * time.Second
var asyncError error
go func() {
_, asyncError = asyncResult.Get(duration)
}()
time.Sleep(duration + time.Millisecond)
if asyncError == nil {
t.Errorf("failed to timeout in time")
return
}
}
}
*/