forked from Azure/azure-event-hubs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender_test.go
396 lines (336 loc) · 9.88 KB
/
sender_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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package eventhub
import (
"context"
"errors"
"net"
"sync"
"testing"
"github.com/Azure/go-amqp"
"github.com/Azure/go-autorest/autorest/to"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// conforms to amqpSender
type testAmqpSender struct {
sendErrors []error
sendCount int
}
type recoveryCall struct {
linkID string
err error
recover bool
}
func (s *testAmqpSender) LinkName() string {
return "sender-id"
}
func (s *testAmqpSender) Send(ctx context.Context, msg *amqp.Message) error {
var err error
if len(s.sendErrors) > s.sendCount {
err = s.sendErrors[s.sendCount]
}
s.sendCount++
return err
}
func (s *testAmqpSender) Close(ctx context.Context) error {
return nil
}
func TestSenderRetries(t *testing.T) {
var recoverCalls []recoveryCall
var sender *testAmqpSender
getAmqpSender := func() amqpSender {
return sender
}
recover := func(linkID string, err error, recover bool) {
recoverCalls = append(recoverCalls, recoveryCall{linkID, err, recover})
}
t.Run("SendSucceedsOnFirstTry", func(t *testing.T) {
recoverCalls = nil
sender = &testAmqpSender{}
err := sendMessage(context.TODO(), getAmqpSender, 3, nil, recover)
assert.NoError(t, err)
assert.EqualValues(t, 1, sender.sendCount)
assert.Empty(t, recoverCalls)
})
t.Run("SendExceedingRetries", func(*testing.T) {
recoverCalls = nil
sender = &testAmqpSender{
sendErrors: []error{
amqp.ErrLinkDetached,
amqp.ErrSessionClosed,
errors.New("We'll never attempt to use this one since we ran out of retries")},
}
actualErr := sendMessage(context.TODO(), getAmqpSender,
1, // note we're only allowing 1 retry attempt - so we get the first send() and then 1 additional.
nil, recover)
assert.EqualValues(t, amqp.ErrSessionClosed, actualErr)
assert.EqualValues(t, 2, sender.sendCount)
assert.EqualValues(t, []recoveryCall{
{
linkID: "sender-id",
err: amqp.ErrLinkDetached,
recover: true,
},
{
linkID: "sender-id",
err: amqp.ErrSessionClosed,
recover: true,
},
}, recoverCalls)
})
t.Run("SendWithUnrecoverableAndNonRetryableError", func(*testing.T) {
recoverCalls = nil
sender = &testAmqpSender{
sendErrors: []error{
errors.New("Anything not explicitly retryable kills all retries"),
amqp.ErrConnClosed, // we'll never get here.
},
}
actualErr := sendMessage(context.TODO(), getAmqpSender, 5, nil, recover)
assert.EqualValues(t, errors.New("Anything not explicitly retryable kills all retries"), actualErr)
assert.EqualValues(t, 1, sender.sendCount)
assert.Empty(t, recoverCalls, "No recovery attempts should happen for non-recoverable errors")
})
t.Run("SendIsNotRecoverableIfLinkIsClosed", func(*testing.T) {
recoverCalls = nil
sender = &testAmqpSender{
sendErrors: []error{
amqp.ErrLinkClosed, // this is no longer considered a retryable error (ErrLinkDetached is, however)
},
}
actualErr := sendMessage(context.TODO(), getAmqpSender, 5, nil, recover)
assert.EqualValues(t, amqp.ErrLinkClosed, actualErr)
assert.EqualValues(t, 1, sender.sendCount)
assert.Empty(t, recoverCalls, "No recovery attempts should happen for non-recoverable errors")
})
t.Run("SendWithAmqpErrors", func(*testing.T) {
recoverCalls = nil
sender = &testAmqpSender{
sendErrors: []error{&amqp.Error{
// retry but doesn't attempt to recover the connection
Condition: errorServerBusy,
}, &amqp.Error{
// retry but doesn't attempt to recover the connection
Condition: errorTimeout,
},
&amqp.Error{
// retry and will attempt to recover the connection
Condition: amqp.ErrorNotImplemented,
}},
}
err := sendMessage(context.TODO(), getAmqpSender, 6, nil, recover)
assert.NoError(t, err)
assert.EqualValues(t, 4, sender.sendCount)
assert.EqualValues(t, []recoveryCall{
{
linkID: "sender-id",
err: &amqp.Error{
Condition: errorServerBusy,
},
recover: false,
},
{
linkID: "sender-id",
err: &amqp.Error{
Condition: errorTimeout,
},
recover: false,
},
{
linkID: "sender-id",
err: &amqp.Error{
Condition: amqp.ErrorNotImplemented,
},
recover: true,
},
}, recoverCalls)
})
t.Run("SendWithDetachOrNetErrors", func(*testing.T) {
recoverCalls = nil
sender = &testAmqpSender{
sendErrors: []error{
&amqp.DetachError{},
&net.DNSError{},
},
}
err := sendMessage(context.TODO(), getAmqpSender, 6, nil, recover)
assert.NoError(t, err)
assert.EqualValues(t, 3, sender.sendCount)
assert.EqualValues(t, []recoveryCall{
{
linkID: "sender-id",
err: &amqp.DetachError{},
recover: true,
},
{
linkID: "sender-id",
err: &net.DNSError{},
recover: true,
},
}, recoverCalls)
})
t.Run("SendWithRecoverableCloseError", func(*testing.T) {
recoverCalls = nil
sender = &testAmqpSender{
sendErrors: []error{
amqp.ErrConnClosed,
amqp.ErrLinkDetached,
amqp.ErrSessionClosed,
},
}
err := sendMessage(context.TODO(), getAmqpSender, 6, nil, recover)
assert.NoError(t, err)
assert.EqualValues(t, 4, sender.sendCount)
assert.EqualValues(t, []recoveryCall{
{
linkID: "sender-id",
err: amqp.ErrConnClosed,
recover: true,
},
{
linkID: "sender-id",
err: amqp.ErrLinkDetached,
recover: true,
},
{
linkID: "sender-id",
err: amqp.ErrSessionClosed,
recover: true,
},
}, recoverCalls)
})
t.Run("SendWithInfiniteRetries", func(*testing.T) {
maxRetries := -1
recoverCalls = nil
sender = &testAmqpSender{
sendErrors: []error{
// kind of silly but let's just make sure we would continue to retry.
amqp.ErrConnClosed,
amqp.ErrConnClosed,
amqp.ErrConnClosed,
},
}
err := sendMessage(context.TODO(), getAmqpSender, maxRetries, nil, recover)
assert.NoError(t, err, "Last call succeeds")
assert.EqualValues(t, 3+1, sender.sendCount)
assert.EqualValues(t, recoverCalls, []recoveryCall{
{linkID: "sender-id", err: amqp.ErrConnClosed, recover: true},
{linkID: "sender-id", err: amqp.ErrConnClosed, recover: true},
{linkID: "sender-id", err: amqp.ErrConnClosed, recover: true},
})
})
t.Run("SendWithNoRetries", func(*testing.T) {
maxRetries := 0
recoverCalls = nil
sender = &testAmqpSender{
sendErrors: []error{
amqp.ErrConnClosed, // this is normally a retryable error _but_ we disabled retries.
},
}
err := sendMessage(context.TODO(), getAmqpSender, maxRetries, nil, recover)
assert.EqualValues(t, amqp.ErrConnClosed, err)
assert.EqualValues(t, maxRetries+1, sender.sendCount)
assert.EqualValues(t, recoverCalls, []recoveryCall{
{linkID: "sender-id", err: amqp.ErrConnClosed, recover: true},
})
})
t.Run("SendRespectsContextStatus", func(*testing.T) {
maxRetries := 0
recoverCalls = nil
sender = &testAmqpSender{
sendErrors: []error{
amqp.ErrConnClosed, // this is normally a retryable error _but_ we disabled retries.
},
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := sendMessage(ctx, getAmqpSender, maxRetries, nil, recover)
assert.EqualValues(t, context.Canceled, err)
assert.EqualValues(t, 0, sender.sendCount)
assert.Empty(t, recoverCalls)
})
}
type FakeLocker struct {
afterBlock1 func()
mu *sync.Mutex
}
func (l FakeLocker) Lock() {
l.mu.Lock()
}
func (l FakeLocker) Unlock() {
l.afterBlock1()
l.mu.Unlock()
}
// TestRecoveryBlock1 tests recoverWithExpectedLinkID function's first "block" of code that
// decides if we are going to recover the link, ignore it, or wait for an in-progress recovery to
// complete.
func TestRecoveryBlock1(t *testing.T) {
t.Run("Empty link ID skips link ID checking and just does recovery", func(t *testing.T) {
cleanup, sender := createRecoveryBlock1Sender(t, func(s *sender) {
require.True(t, s.recovering)
})
defer cleanup()
sender.recoverWithExpectedLinkID(context.TODO(), "")
})
t.Run("Matching link ID does recovery", func(t *testing.T) {
cleanup, sender := createRecoveryBlock1Sender(t, func(s *sender) {
require.True(t, s.recovering, "s.recovering should be true since the lock is available and we have our expected link ID matches")
})
defer cleanup()
sender.recoverWithExpectedLinkID(context.TODO(), "the-actual-link-id")
})
t.Run("Non-matching link ID skips recovery", func(t *testing.T) {
cleanup, sender := createRecoveryBlock1Sender(t, func(s *sender) {
require.False(t, s.recovering, "s.recovering should be false - the link ID isn't current, so nothing needs to be closed/recovered")
})
defer cleanup()
sender.recoverWithExpectedLinkID(context.TODO(), "non-matching-link-id")
})
// TODO: can't quite test this one
// t.Run("Already recovering, should wait for condition variable", func(t *testing.T) {
// cleanup, sender := createRecoveryBlock1Sender(t, func(s *sender) {
// })
// defer cleanup()
// sender.recovering = true // oops, someone else is already recovering
// sender.recoverWithExpectedLinkID(context.TODO(), "the-actual-link-id")
// })
}
func TestAMQPSenderIsCompatibleWithInterface(t *testing.T) {
var validateCompile amqpSender = &amqp.Sender{}
require.NotNil(t, validateCompile)
}
type fakeSender struct {
id string
closed bool
}
func (s *fakeSender) ID() string {
return s.id
}
func (s *fakeSender) Send(ctx context.Context, msg *amqp.Message) error {
return nil
}
func (s *fakeSender) Close(ctx context.Context) error {
s.closed = true
return nil
}
func createRecoveryBlock1Sender(t *testing.T, afterBlock1 func(s *sender)) (func(), *sender) {
s := &sender{
partitionID: to.StringPtr("0"),
hub: &Hub{
namespace: &namespace{},
},
}
s.sender.Store(&fakeSender{
id: "the-actual-link-id",
})
s.cond = &sync.Cond{
L: FakeLocker{
mu: &sync.Mutex{},
afterBlock1: func() {
afterBlock1(s)
panic("Panicking to exit before block 2")
},
}}
return func() {
require.EqualValues(t, recover(), "Panicking to exit before block 2")
}, s
}