forked from gocelery/gocelery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker_test.go
170 lines (164 loc) · 5.08 KB
/
broker_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
// Copyright (c) 2019 Sick Yoon
// This file is part of gocelery which is released under MIT license.
// See file LICENSE for full license details.
package gocelery
import (
"context"
"encoding/json"
"math/rand"
"reflect"
"testing"
"time"
)
func makeCeleryMessage() (*CeleryMessage, error) {
taskMessage := GetTaskMessage("add")
taskMessage.Args = []interface{}{rand.Intn(10), rand.Intn(10)}
defer releaseTaskMessage(taskMessage)
encodedTaskMessage, err := taskMessage.Encode()
if err != nil {
return nil, err
}
return getCeleryMessage(encodedTaskMessage), nil
}
// TestBrokerRedisSend is Redis specific test that sets CeleryMessage to queue
func TestBrokerRedisSend(t *testing.T) {
testCases := []struct {
name string
broker *RedisCeleryBroker
}{
{
name: "send task to redis broker",
broker: redisBroker,
},
{
name: "send task to redis broker with connection",
broker: redisBrokerWithConn,
},
}
for _, tc := range testCases {
celeryMessage, err := makeCeleryMessage()
if err != nil || celeryMessage == nil {
t.Errorf("test '%s': failed to construct celery message: %v", tc.name, err)
continue
}
err = tc.broker.SendCeleryMessage(context.Background(), celeryMessage)
if err != nil {
t.Errorf("test '%s': failed to send celery message to broker: %v", tc.name, err)
releaseCeleryMessage(celeryMessage)
continue
}
resp := tc.broker.BRPop(context.Background(), time.Second*1, tc.broker.QueueName)
if resp.Err() != nil || resp.Val() == nil {
t.Errorf("test '%s': failed to get celery message from broker: %v", tc.name, resp.Err())
releaseCeleryMessage(celeryMessage)
continue
}
messageList := resp.Val()
if messageList[0] != "celery" {
t.Errorf("test '%s': non celery message received", tc.name)
releaseCeleryMessage(celeryMessage)
continue
}
var message CeleryMessage
if err := json.Unmarshal([]byte(messageList[1]), &message); err != nil {
t.Errorf("test '%s': failed to unmarshal received message: %v", tc.name, err)
releaseCeleryMessage(celeryMessage)
continue
}
if !reflect.DeepEqual(celeryMessage, &message) {
t.Errorf("test '%s': received message %v different from original message %v", tc.name, &message, celeryMessage)
}
releaseCeleryMessage(celeryMessage)
}
}
// TestBrokerRedisGet is Redis specific test that gets CeleryMessage from queue
func TestBrokerRedisGet(t *testing.T) {
testCases := []struct {
name string
broker *RedisCeleryBroker
}{
{
name: "get task from redis broker",
broker: redisBroker,
},
{
name: "get task from redis broker with connection",
broker: redisBrokerWithConn,
},
}
for _, tc := range testCases {
celeryMessage, err := makeCeleryMessage()
if err != nil || celeryMessage == nil {
t.Errorf("test '%s': failed to construct celery message: %v", tc.name, err)
continue
}
jsonBytes, err := json.Marshal(celeryMessage)
if err != nil {
t.Errorf("test '%s': failed to marshal celery message: %v", tc.name, err)
releaseCeleryMessage(celeryMessage)
continue
}
resp := tc.broker.LPush(context.Background(), tc.broker.QueueName, jsonBytes)
if resp.Err() != nil {
t.Errorf("test '%s': failed to push celery message to redis: %v", tc.name, resp.Err())
releaseCeleryMessage(celeryMessage)
continue
}
message, err := tc.broker.GetCeleryMessage(context.Background())
if err != nil {
t.Errorf("test '%s': failed to get celery message from broker: %v", tc.name, err)
releaseCeleryMessage(celeryMessage)
continue
}
if !reflect.DeepEqual(message, celeryMessage) {
t.Errorf("test '%s': received message %v different from original message %v", tc.name, message, celeryMessage)
}
releaseCeleryMessage(celeryMessage)
}
}
// TestBrokerSendGet tests set/get features for all brokers
func TestBrokerSendGet(t *testing.T) {
testCases := []struct {
name string
broker CeleryBroker
}{
{
name: "send/get task for redis broker",
broker: redisBroker,
},
{
name: "send/get task for redis broker with connection",
broker: redisBrokerWithConn,
},
{
name: "send/get task for amqp broker",
broker: amqpBroker,
},
}
for _, tc := range testCases {
celeryMessage, err := makeCeleryMessage()
if err != nil || celeryMessage == nil {
t.Errorf("test '%s': failed to construct celery message: %v", tc.name, err)
continue
}
err = tc.broker.SendCeleryMessage(context.Background(), celeryMessage)
if err != nil {
t.Errorf("test '%s': failed to send celery message to broker: %v", tc.name, err)
releaseCeleryMessage(celeryMessage)
continue
}
// wait arbitrary time for message to propagate
time.Sleep(1 * time.Second)
message, err := tc.broker.GetTaskMessage(context.Background())
if err != nil {
t.Errorf("test '%s': failed to get celery message from broker: %v", tc.name, err)
releaseCeleryMessage(celeryMessage)
continue
}
originalMessage := celeryMessage.GetTaskMessage()
if !reflect.DeepEqual(message, originalMessage) {
t.Errorf("test '%s': received message %v different from original message %v", tc.name, message, originalMessage)
}
releaseCeleryMessage(celeryMessage)
}
}