-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker_test.go
132 lines (110 loc) · 3.06 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
package redisq
import (
"encoding/json"
"fmt"
"github.com/rafaeljusto/redigomock"
"reflect"
"testing"
)
const (
WORKER_REDIS_PREFIX = "foo"
WORKER_TASK_TYPE = "dummy"
WORKER_TASK_UUID = "dummy_task_uuid_id"
)
func getWorkerTaskDetails() *TaskDetails {
return &TaskDetails{
Arguments: []string{"foo", "bar", "next"},
CreatedAt: "Friday, 22-Nov-63 12:30:00 CST",
Attempts: 0,
Type: CLIENT_TASK_TYPE,
LastAttempt: "",
}
}
func getRedisConnMock(t *testing.T) *redigomock.Conn {
conn := redigomock.NewConn()
// RemoveOneFromList
conn.Command(
"LREM",
fmt.Sprintf("%s:%s:%s", WORKER_REDIS_PREFIX, LIST_PROCESSING, WORKER_TASK_TYPE),
1,
WORKER_TASK_UUID,
)
// GetTaskDetails
originalTaskDetails := getWorkerTaskDetails()
jsonTaskDetails, err := json.Marshal(originalTaskDetails)
if err != nil {
t.Fatal(err)
}
conn.Command(
"GET",
fmt.Sprintf("%s:%s:%s:%s", WORKER_REDIS_PREFIX, QUEUE_TASK, WORKER_TASK_TYPE, WORKER_TASK_UUID),
).Expect([]byte(jsonTaskDetails))
// SaveTaskDetails
modifiedTaskDetails := originalTaskDetails
modifiedTaskDetails.NewAttempt()
jsonModifiedTaskDetails, err := json.Marshal(modifiedTaskDetails)
if err != nil {
t.Fatal(err)
}
conn.Command(
"SET",
fmt.Sprintf("%s:%s:%s:%s", WORKER_REDIS_PREFIX, QUEUE_TASK, WORKER_TASK_TYPE, WORKER_TASK_UUID),
jsonModifiedTaskDetails,
)
// PushTaskToList
conn.Command(
"LPUSH",
fmt.Sprintf("%s:%s:%s", WORKER_REDIS_PREFIX, LIST_FAILURE_FINAL, WORKER_TASK_TYPE),
WORKER_TASK_UUID,
)
// PushTaskToList
conn.Command(
"LPUSH",
fmt.Sprintf("%s:%s:%s", WORKER_REDIS_PREFIX, LIST_FAILURE_FINAL, WORKER_TASK_TYPE),
WORKER_TASK_UUID,
)
// PushTaskToList
conn.Command(
"DEL",
fmt.Sprintf("%s:%s:%s:%s", WORKER_REDIS_PREFIX, QUEUE_TASK, WORKER_TASK_TYPE, WORKER_TASK_UUID),
)
return conn
}
func TestWorker_processTask(t *testing.T) {
failure := make(chan error, 0)
conn := getRedisConnMock(t)
handler := WorkerHandler(func(logger Logger, args []string) error {
expectedArgs := []string{"foo", "bar", "next"}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("Task details do not match, expected %+v, got %+v", expectedArgs, args)
t.FailNow()
}
return nil
})
w := NewWorker(1, conn, WORKER_REDIS_PREFIX, WORKER_TASK_TYPE, handler, failure)
w.processTask(WORKER_TASK_UUID)
if len(conn.Errors) > 0 {
t.Fatal(conn.Errors)
}
}
func TestWorker_GetInstanceId(t *testing.T) {
failure := make(chan error, 0)
conn := getFailureRedisConnMock(t)
expected := 1
w := NewWorker(expected, conn, WORKER_REDIS_PREFIX, WORKER_TASK_TYPE, nil, failure)
got := w.GetInstanceId()
if got != expected {
t.Errorf("Unexpected instance id value, expected %+v, got %+v", expected, got)
t.FailNow()
}
}
func TestWorker_GetTaskType(t *testing.T) {
failure := make(chan error, 0)
conn := getFailureRedisConnMock(t)
w := NewWorker(1, conn, WORKER_REDIS_PREFIX, WORKER_TASK_TYPE, nil, failure)
got := w.GetTaskType()
if got != WORKER_TASK_TYPE {
t.Errorf("Unexpected instance id value, expected %+v, got %+v", WORKER_TASK_TYPE, got)
t.FailNow()
}
}