forked from benmanns/goworker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
failover_test.go
192 lines (166 loc) · 4.5 KB
/
failover_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
package goworker
import (
"encoding/json"
"fmt"
"github.com/garyburd/redigo/redis"
"reflect"
"strings"
"testing"
"time"
)
const (
MasterSetName = "resque"
SentinelPort = 26379
FailoverTimeout = 1000 * time.Millisecond
Password = "foobared"
)
func TestMasterFailover(t *testing.T) {
setup, className, testKey, expectedPayloads := setupFailoverTest(t)
defer func() {
teardownFailoverTest(setup, t)
}()
registerWork(className, testKey, t, func() {
pauseContainer(setup["master"][0].container, FailoverTimeout*3, t)
})
response := readTestKey(testKey, setup, t)
compareExpectedJobsToActual(response, expectedPayloads, t)
}
func TestSentinelFailover(t *testing.T) {
setup, className, testKey, expectedPayloads := setupFailoverTest(t)
defer func() {
teardownFailoverTest(setup, t)
}()
registerWork(className, testKey, t, func() {
pauseContainer(setup["master"][0].container, time.Duration(0), t)
pauseContainer(setup["sentinel"][0].container, FailoverTimeout*3, t)
})
response := readTestKey(testKey, setup, t)
compareExpectedJobsToActual(response, expectedPayloads, t)
}
func setupFailoverTest(t *testing.T) (containerSetup, string, string, []string) {
setup := setupFailoverContainers(t)
queueName, className, testKey := setupWorkerSettings(setup)
expectedPayloads := scheduleJobs(queueName, className, t)
return setup, className, testKey, expectedPayloads
}
func setupFailoverContainers(t *testing.T) containerSetup {
dockerComposeUp(t)
dockerComposeScaleSentinels(t)
setup := scanSetup(t)
verifySetupSize(setup, t)
dockerComposeUnpause(setup, t)
ensureMaster(setup, t)
dockerClearDb(setup, t)
return setup
}
func setupWorkerSettings(setup containerSetup) (string, string, string) {
// config
queueName := "failover_sentinel"
className := "SomeRubyClass"
testKey := fmt.Sprintf("test:%v", queueName)
// setup worker
workerSettings.URI = fmt.Sprintf(
"redis+sentinel://%s@%s:26379/resque",
Password,
setup["sentinel"][0].address,
)
workerSettings.Queues = []string{queueName}
workerSettings.UseNumber = true
workerSettings.ExitOnComplete = true
workerSettings.Concurrency = 1
workerSettings.Connections = 1
workerSettings.Timeout = FailoverTimeout / 2
return queueName, className, testKey
}
func scheduleJobs(queueName, className string, t *testing.T) []string {
// schedule a few jobs and store their payload
expectedPayloads := []string{}
for i := 1; i <= 5; i++ {
err := Enqueue(&Job{
Queue: queueName,
Payload: Payload{
Class: className,
Args: []interface{}{i},
},
})
if err != nil {
t.Fatal(err)
}
expectedPayloads = append(expectedPayloads, fmt.Sprint(i))
}
return expectedPayloads
}
func registerWork(className, testKey string, t *testing.T, middleWorker func()) {
// register worker
Register(className, func(queue string, args ...interface{}) error {
// parse int payload
val := args[0].(json.Number)
int64Val, _ := val.Int64()
intVal := int(int64Val)
// in a separate connection store the payload in Redis under the test key
func() {
conn, err := GetConn()
if err != nil {
t.Fatal(err)
}
err = conn.Send("RPUSH", testKey, intVal)
if err != nil {
t.Fatal(err)
}
if err := conn.Flush(); err != nil {
t.Fatal(err)
}
PutConn(conn)
}()
// kill master to force new connections from the pool and
// kill primary sentinel to test auto discovery
if intVal == 3 {
middleWorker()
}
return nil
})
if err := Work(); err != nil {
t.Fatal(err)
}
}
func readTestKey(testKey string, setup containerSetup, t *testing.T) []string {
prepareSentinels(setup)
if err := Init(); err != nil {
t.Fatal(err)
}
conn, err := GetConn()
if err != nil {
t.Fatal(err)
}
defer func() {
PutConn(conn)
Close()
}()
response, err := redis.Strings(conn.Do("LRANGE", testKey, 0, -1))
if err != nil {
t.Fatal(err)
}
return response
}
func prepareSentinels(setup containerSetup) {
sentinels := []string{}
for _, sentinel := range setup["sentinel"] {
sentinels = append(sentinels, fmt.Sprintf("%s:26379", sentinel.address))
}
workerSettings.URI = fmt.Sprintf(
"redis+sentinel://%s@%s/resque",
Password,
strings.Join(sentinels, ","),
)
}
func compareExpectedJobsToActual(response, expected []string, t *testing.T) {
if !reflect.DeepEqual(response, expected) {
t.Errorf("Expected jobs %v, got %v", expected, response)
}
}
func teardownFailoverTest(setup containerSetup, t *testing.T) {
dockerComposeUnpause(setup, t)
time.Sleep(time.Second * 3)
ensureMaster(setup, t)
dockerComposeStop(t)
}