-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworker_test.go
94 lines (87 loc) · 2.01 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
package goworker
import (
"flag"
"reflect"
"testing"
)
var workerMarshalJSONTests = []struct {
w worker
expected []byte
}{
{
worker{},
[]byte(`":0-:"`),
},
{
worker{
process: process{
Hostname: "hostname",
Pid: 12345,
ID: "123",
Queues: []string{"high", "low"},
},
},
[]byte(`"hostname:12345-123:high,low"`),
},
}
func TestWorkerMarshalJSON(t *testing.T) {
for _, tt := range workerMarshalJSONTests {
actual, err := tt.w.MarshalJSON()
if err != nil {
t.Errorf("Worker(%#v): error %s", tt.w, err)
} else {
if string(actual) != string(tt.expected) {
t.Errorf("Worker(%#v): expected %s, actual %s", tt.w, tt.expected, actual)
}
}
}
}
func TestEnqueue(t *testing.T) {
expectedArgs := []interface{}{"a", "lot", "of", "params"}
jobName := "SomethingCool"
queueName := "testQueue"
expectedJob := &Job{
Queue: queueName,
Payload: Payload{
Class: jobName,
Args: expectedArgs,
},
}
var err error
err = flag.Set("exit-on-complete", "true")
if err != nil {
t.Errorf("Error setting flag exit-on-complete: %v", err)
}
err = flag.Set("queues", queueName)
if err != nil {
t.Errorf("Error setting flag queues: %v", err)
}
err = flag.Set("concurrency", "1")
if err != nil {
t.Errorf("Error setting flag concurrency: %v", err)
}
err = flag.Set("use-number", "true")
if err != nil {
t.Errorf("Error setting flag use-number: %v", err)
}
err = Enqueue(expectedJob)
if err != nil {
t.Errorf("Error while enqueue %s", err)
}
var actualArgs []interface{}
actualQueueName := ""
Register(jobName, func(queue string, args ...interface{}) error {
actualArgs = args
actualQueueName = queue
return nil
})
if err := Work(); err != nil {
t.Errorf("(Enqueue) Failed on work %s", err)
}
if !reflect.DeepEqual(actualArgs, expectedArgs) {
t.Errorf("(Enqueue) Expected %v, actual %v", expectedArgs, actualArgs)
}
if !reflect.DeepEqual(actualQueueName, queueName) {
t.Errorf("(Enqueue) Expected %v, actual %v", queueName, actualQueueName)
}
}