This repository has been archived by the owner on May 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
worker_test.go
242 lines (182 loc) · 4.92 KB
/
worker_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package dispatcher
import (
"encoding/json"
"github.com/gofort/dispatcher/log"
"github.com/satori/go.uuid"
"github.com/streadway/amqp"
"os"
"sync"
"testing"
"time"
)
const workerTestExchange = "dispatcher_test"
const workerTestQueue = "test_queue"
const workerTestRoutingKey = "test_rk_1"
const workerTestName = "test_worker_1"
const workerTestLimit = 3
func createWorkerTestEnv(test1, test2, test3 chan struct{}) (*amqp.Connection, *Worker, error) {
w := &Worker{
log: log.InitLogger(true),
name: workerTestName,
tasks: newWorkerTestTaskConfigs(test1, test2, test3),
limit: workerTestLimit,
queue: workerTestQueue,
tasksInProgress: new(sync.WaitGroup),
}
con, err := amqp.Dial(os.Getenv("DISPATCHER_AMQP_CON"))
if err != nil {
return nil, nil, err
}
ch, err := con.Channel()
if err != nil {
return nil, nil, err
}
defer ch.Close()
if err := declareExchange(ch, workerTestExchange); err != nil {
return nil, nil, err
}
if err := declareQueue(ch, workerTestQueue); err != nil {
return nil, nil, err
}
if err := queueBind(ch, workerTestExchange, workerTestQueue, workerTestRoutingKey); err != nil {
return nil, nil, err
}
return con, w, nil
}
func destroyWorkerTestEnv(con *amqp.Connection) {
ch, _ := con.Channel()
ch.ExchangeDelete(publisherTestExchange, false, false)
ch.QueueDelete(publisherTestExchange, false, false, false)
ch.Close()
con.Close()
}
func newWorkerTestTaskConfigs(test1, test2, test3 chan struct{}) map[string]TaskConfig {
t := make(map[string]TaskConfig)
t["test_task_1"] = TaskConfig{
TimeoutSeconds: 30,
Function: func(taskuuid string, str string, someint int) {
test1 <- struct{}{}
},
TaskUUIDAsFirstArg: true,
}
t["test_task_2"] = TaskConfig{
TimeoutSeconds: 0,
Function: func(
tbool bool,
tstring string,
tint int, tint8 int8, tint16 int16, tint32 int32, tint64 int64,
tuint uint, tuint8 uint8, tuint16 uint16, tuint32 uint32, tuint64 uint64,
tfloat32 float32, tfloat64 float64) {
test2 <- struct{}{}
},
}
t["test_task_3"] = TaskConfig{
TimeoutSeconds: 3,
Function: func() (string, error) {
time.Sleep(time.Second * 9)
test3 <- struct{}{}
return "", nil
},
}
return t
}
func newWorkerTest1Task() (*Task, []byte) {
t := Task{
UUID: uuid.NewV4().String(),
Name: "test_task_1",
Args: []TaskArgument{{"string", "test string"}, {"int", 1}},
RoutingKey: workerTestRoutingKey,
}
msg, _ := json.Marshal(t)
return &t, msg
}
func newWorkerTest2Task() (*Task, []byte) {
t := Task{
UUID: uuid.NewV4().String(),
Name: "test_task_2",
Args: []TaskArgument{
{"bool", true},
{"string", "test string"},
{"int", 1}, {"int8", 8}, {"int16", 16}, {"int32", 32}, {"int64", 64},
{"uint", 1}, {"uint8", 8}, {"uint16", 16}, {"uint32", 32}, {"uint64", 64},
{"float32", 0.32}, {"float64", 0.64}},
RoutingKey: workerTestRoutingKey,
}
msg, _ := json.Marshal(t)
return &t, msg
}
func newWorkerTest3Task() (*Task, []byte) {
t := Task{
UUID: uuid.NewV4().String(),
Name: "test_task_3",
RoutingKey: workerTestRoutingKey,
}
msg, _ := json.Marshal(t)
return &t, msg
}
func newWorkerTest4Task() (*Task, []byte) {
t := Task{
UUID: uuid.NewV4().String(),
Name: "test_task_4",
RoutingKey: workerTestRoutingKey,
}
msg, _ := json.Marshal(t)
return &t, msg
}
func TestWorker(t *testing.T) {
test1 := make(chan struct{})
test2 := make(chan struct{})
test3 := make(chan struct{})
con, w, err := createWorkerTestEnv(test1, test2, test3)
if err != nil {
t.Error(err)
return
}
defer destroyWorkerTestEnv(con)
ch, err := con.Channel()
if err != nil {
t.Error(err)
return
}
if err = w.init(con); err != nil {
t.Error(err)
return
}
task1, msg1 := newWorkerTest1Task()
if err = publishMessage(ch, publisherTestExchange, task1.RoutingKey, task1.Headers, msg1); err != nil {
t.Error(err)
return
}
<-test1
t.Log("Task 1 with TaskUUIDAsFirstArg parameter passed!")
task2, msg2 := newWorkerTest2Task()
if err = publishMessage(ch, publisherTestExchange, task2.RoutingKey, task2.Headers, msg2); err != nil {
t.Error(err)
return
}
<-test2
t.Log("Task 2 with all types of arguments and zero timeout passed!")
task3, msg3 := newWorkerTest3Task()
if err = publishMessage(ch, publisherTestExchange, task3.RoutingKey, task3.Headers, msg3); err != nil {
t.Error(err)
return
}
timer := time.NewTimer(time.Second * 6)
select {
case <-test2:
t.Error("Timeout of task 3 hadn't worked")
case <-timer.C:
t.Log("Task 3 without arguments and with timeout passed!")
}
if err = publishMessage(ch, publisherTestExchange, task3.RoutingKey, task3.Headers, []byte("msg")); err != nil {
t.Error(err)
return
}
task4, msg4 := newWorkerTest4Task()
if err = publishMessage(ch, publisherTestExchange, task4.RoutingKey, task4.Headers, msg4); err != nil {
t.Error(err)
return
}
w.Close()
w.Close()
}