forked from gocelery/gocelery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_test.go
164 lines (154 loc) · 4.69 KB
/
backend_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
// 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"
"fmt"
"math/rand"
"reflect"
"testing"
"time"
uuid "github.com/satori/go.uuid"
)
// TestBackendRedisGetResult is Redis specific test to get result from backend
func TestBackendRedisGetResult(t *testing.T) {
testCases := []struct {
name string
backend *RedisCeleryBackend
}{
{
name: "get result from redis backend",
backend: redisBackend,
},
{
name: "get result from redis backend with connection",
backend: redisBackendWithConn,
},
}
for _, tc := range testCases {
taskID := uuid.Must(uuid.NewV4()).String()
// value must be float64 for testing due to json limitation
value := reflect.ValueOf(rand.Float64())
resultMessage := getReflectionResultMessage(&value)
messageBytes, err := json.Marshal(resultMessage)
if err != nil {
t.Errorf("test '%s': error marshalling result message: %v", tc.name, err)
releaseResultMessage(resultMessage)
continue
}
resp := tc.backend.SetEx(context.Background(), fmt.Sprintf("celery-task-meta-%s", taskID), messageBytes, time.Second*86400)
if resp.Err() != nil {
t.Errorf("test '%s': error setting result message to celery: %v", tc.name, resp.Err())
releaseResultMessage(resultMessage)
continue
}
res, err := tc.backend.GetResult(context.Background(), taskID)
if err != nil {
t.Errorf("test '%s': error getting result from backend: %v", tc.name, err)
releaseResultMessage(resultMessage)
continue
}
if !reflect.DeepEqual(res, resultMessage) {
t.Errorf("test '%s': result message received %v is different from original %v", tc.name, res, resultMessage)
}
releaseResultMessage(resultMessage)
}
}
// TestBackendRedisSetResult is Redis specific test to set result to backend
func TestBackendRedisSetResult(t *testing.T) {
testCases := []struct {
name string
backend *RedisCeleryBackend
}{
{
name: "set result to redis backend",
backend: redisBackend,
},
{
name: "set result to redis backend with connection",
backend: redisBackendWithConn,
},
}
for _, tc := range testCases {
taskID := uuid.Must(uuid.NewV4()).String()
value := reflect.ValueOf(rand.Float64())
resultMessage := getReflectionResultMessage(&value)
err := tc.backend.SetResult(context.Background(), taskID, resultMessage)
if err != nil {
t.Errorf("test '%s': error setting result to backend: %v", tc.name, err)
releaseResultMessage(resultMessage)
continue
}
resp := tc.backend.Get(context.Background(), fmt.Sprintf("celery-task-meta-%s", taskID))
if resp.Err() != nil {
t.Errorf("test '%s': error getting data from redis: %v", tc.name, resp.Err())
releaseResultMessage(resultMessage)
continue
}
val, err := resp.Bytes()
if err != nil {
t.Errorf("test '%s': error getting data from redis: %v", tc.name, err)
releaseResultMessage(resultMessage)
continue
}
if val == nil {
t.Errorf("test '%s': result not available from redis", tc.name)
releaseResultMessage(resultMessage)
continue
}
var res ResultMessage
err = json.Unmarshal(val, &res)
if err != nil {
t.Errorf("test '%s': error parsing json result", tc.name)
releaseResultMessage(resultMessage)
continue
}
if !reflect.DeepEqual(&res, resultMessage) {
t.Errorf("test '%s': result message received %v is different from original %v", tc.name, &res, resultMessage)
}
releaseResultMessage(resultMessage)
}
}
// TestBackendSetGetResult tests set/get result feature for all backends
func TestBackendSetGetResult(t *testing.T) {
testCases := []struct {
name string
backend CeleryBackend
}{
{
name: "set/get result to redis backend",
backend: redisBackend,
},
{
name: "set/get result to redis backend with connection",
backend: redisBackendWithConn,
},
{
name: "set/get result to amqp backend",
backend: amqpBackend,
},
}
for _, tc := range testCases {
taskID := uuid.Must(uuid.NewV4()).String()
value := reflect.ValueOf(rand.Float64())
resultMessage := getReflectionResultMessage(&value)
err := tc.backend.SetResult(context.Background(), taskID, resultMessage)
if err != nil {
t.Errorf("error setting result to backend: %v", err)
releaseResultMessage(resultMessage)
continue
}
res, err := tc.backend.GetResult(context.Background(), taskID)
if err != nil {
t.Errorf("error getting result from backend: %v", err)
releaseResultMessage(resultMessage)
continue
}
if !reflect.DeepEqual(res, resultMessage) {
t.Errorf("result message received %v is different from original %v", res, resultMessage)
}
releaseResultMessage(resultMessage)
}
}