-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathproducer_test.go
277 lines (233 loc) · 7.3 KB
/
producer_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package beanstalk
import (
"context"
"errors"
"fmt"
"math/rand"
"testing"
"time"
)
func TestProducer(t *testing.T) {
server := NewServer()
defer server.Close()
var producer *Producer
defer func() {
if producer != nil {
producer.Stop()
}
}()
// Create a new producer.
t.Run("NewProducer", func(t *testing.T) {
var err error
producer, err = NewProducer([]string{server.Socket()}, Config{ReconnectTimeout: 1 * time.Microsecond})
if err != nil {
t.Fatalf("Unable to create a new producer: %s", err)
}
time.Sleep(10 * time.Millisecond)
if !producer.IsConnected() {
t.Fatalf("Producer was unable to connect")
}
// Create a new producer that connects to an invalid socket.
t.Run("WithInvalidSocket", func(t *testing.T) {
p, err := NewProducer([]string{"127.0.0.2:10311"}, Config{})
if err != nil {
t.Fatalf("Unable to create a new producer: %s", err)
}
defer p.Stop()
time.Sleep(10 * time.Millisecond)
if p.IsConnected() {
t.Fatal("Producer to unknown socket was connected")
}
})
// Test if a producer works when it has both a connected and disconnected
// connection.
t.Run("WithDegradedConnections", func(t *testing.T) {
p, err := NewProducer([]string{server.Socket(), "127.0.0.2:10311"}, Config{})
if err != nil {
t.Fatalf("Unable to create a new producer: %s", err)
}
defer p.Stop()
time.Sleep(10 * time.Millisecond)
if !producer.IsConnected() {
t.Fatalf("Producer was unable to connect")
}
})
// Test if a producer works when IgnoreURIValidation is set true and URIs are invalid
// connection.
t.Run("IgnoreURIValidation", func(t *testing.T) {
p, err := NewProducer([]string{"test-uri-1", "test-uri-2"}, Config{IgnoreURIValidation: true})
if err != nil {
t.Fatalf("Unable to create a new producer: %s", err)
}
defer p.Stop()
})
})
ctx := context.Background()
// Put a new job into a tube.
t.Run("Put", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "use foobar"):
return "USING foobar"
case line.At(2, "put 1024 10 60 11"):
case line.At(3, "Hello World"):
return "INSERTED 5"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
params := PutParams{Priority: 1024, Delay: 10 * time.Second, TTR: 60 * time.Second}
id, err := producer.Put(ctx, "foobar", []byte("Hello World"), params)
switch {
case err != nil:
t.Fatalf("Error inserting a new job: %s", err)
case id != 5:
t.Fatalf("Expected job ID 5, but got %d", id)
}
// Check if ErrTooBig is returned when a job is too big.
t.Run("ErrTooBig", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "put 1024 10 60 11"):
case line.At(2, "Hello World"):
return "JOB_TOO_BIG"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
_, err = producer.Put(ctx, "foobar", []byte("Hello World"), params)
if !errors.Is(err, ErrTooBig) {
t.Fatalf("Expected JOB_TOO_BIG error, but got %s", err)
}
})
// Check if ErrDisconnected is returned when an error occurs while putting
// a job into a tube.
t.Run("WithError", func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "put 1024 10 60 11"):
case line.At(2, "Hello World"):
return "FOOBAR"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
_, err = producer.Put(ctx, "foobar", []byte("Hello World"), params)
if !errors.Is(err, ErrDisconnected) {
t.Fatalf("Expected %v error, but got %s", ErrDisconnected, err)
}
})
})
// Check if Stop properly stops the producer connection.
t.Run("Stop", func(t *testing.T) {
time.Sleep(10 * time.Millisecond)
if !producer.IsConnected() {
t.Fatal("Expected producer to be connected")
}
producer.Stop()
if producer.IsConnected() {
t.Fatalf("Expected producer to be disconnected")
}
})
}
// TestProducerPool tests the Producer with more than 1 connection.
func TestProducerPool(t *testing.T) {
var servers [3]*Server
servers[0] = NewServer()
defer servers[0].Close()
servers[1] = NewServer()
defer servers[1].Close()
servers[2] = NewServer()
defer servers[2].Close()
// Create the producer.
producer, err := NewProducer([]string{servers[0].Socket(), servers[1].Socket(), servers[2].Socket()}, Config{})
if err != nil {
t.Fatalf("Unable to create a new producer: %s", err)
}
defer producer.Stop()
// Test that at least 1 connection is connected.
time.Sleep(10 * time.Millisecond)
if !producer.IsConnected() {
t.Fatal("Expected producer to be connected")
}
params := PutParams{Priority: 1024, Delay: 10 * time.Second, TTR: 60 * time.Second}
// Set the seeder up in such a way that 3 calls to Put will select the 1st,
// 2nd and 3rd server in that order.
rand.Seed(76)
// Test if 3 calls to Put will result in the 3 servers being called.
for i, server := range servers {
t.Run(fmt.Sprintf("PutOnServer%d", i+1), func(t *testing.T) {
server.HandleFunc(func(line Line) string {
switch {
case line.At(1, "use foobar"):
return "USING foobar"
case line.At(2, "put 1024 10 60 11"):
case line.At(3, "Hello World"):
return "INSERTED 5"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
id, err := producer.Put(context.Background(), "foobar", []byte("Hello World"), params)
switch {
case err != nil:
t.Fatalf("Error inserting a new job: %s", err)
case id != 5:
t.Fatalf("Expected job ID 5, but got %d", id)
}
})
}
// Test if 1 or 2 connections out of 3 connections in the pool makes Put
// still work.
for i, server := range servers[:2] {
// Set the seeder up in such a way that a Put call with try to select the
// 1st, 2nd and 3rd server in that order.
rand.Seed(1)
t.Run(fmt.Sprintf("PutWith%dServersDisconnected", i+1), func(t *testing.T) {
server.Close()
// Since server is closed, server+1 should get the Put request.
servers[i+1].HandleFunc(func(line Line) string {
switch {
case line.At(1, "put 1024 10 60 11"):
case line.At(2, "Hello World"):
return "INSERTED 5"
default:
t.Fatalf("Unexpected client request at line %d: %s", line.lineno, line.line)
}
return ""
})
id, err := producer.Put(context.Background(), "foobar", []byte("Hello World"), params)
switch {
case err != nil:
t.Fatalf("Error inserting a new job: %s", err)
case id != 5:
t.Fatalf("Expected job ID 5, but got %d", id)
}
// Test if the Producer is still marked as connected.
t.Run("IsConnected", func(t *testing.T) {
if !producer.IsConnected() {
t.Fatal("Expected producer to be connected")
}
})
})
}
t.Run("PutWithAllServersDisconnected", func(t *testing.T) {
servers[2].Close()
_, err = producer.Put(context.Background(), "foobar", []byte("Hello World"), params)
switch {
case errors.Is(err, ErrDisconnected):
case err != nil:
t.Fatalf("Error inserting a new job: %s", err)
}
// Test if the Producer is still marked as connected.
t.Run("IsConnected", func(t *testing.T) {
if producer.IsConnected() {
t.Fatal("Expected producer to be disconnected")
}
})
})
}