forked from Azure/go-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_test.go
416 lines (344 loc) · 12 KB
/
link_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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package amqp
import (
"context"
"encoding/binary"
"fmt"
"sync"
"testing"
"time"
"github.com/Azure/go-amqp/internal/encoding"
"github.com/Azure/go-amqp/internal/frames"
"github.com/Azure/go-amqp/internal/mocks"
"github.com/stretchr/testify/require"
)
func TestLinkFlowForSender(t *testing.T) {
// senders don't actually send flow frames but they do enable require tranfers to be
// assigned. We should refactor, this is just fallout from my "lift and shift" of the
// flow logic in `mux`
l := newTestLink(t)
l.receiver = nil
err := l.DrainCredit(context.Background())
require.Error(t, err, "drain can only be used with receiver links using manual credit management")
err = l.IssueCredit(1)
require.Error(t, err, "issueCredit can only be used with receiver links using manual credit management")
// and flow goes through the non-manual credit path
require.EqualValues(t, 0, l.linkCredit, "No link credits have been added")
require.EqualValues(t, 0, l.Paused, "Link not paused")
// if we have link credit we can enable outgoing transfers
l.linkCredit = 1
ok, enableOutgoingTransfers := l.doFlow()
require.True(t, ok, "no errors, should continue to process")
require.True(t, enableOutgoingTransfers, "outgoing transfers needed for senders")
require.EqualValues(t, 0, l.Paused, "Link not paused")
}
func TestLinkFlowThatNeedsToReplenishCredits(t *testing.T) {
l := newTestLink(t)
err := l.DrainCredit(context.Background())
require.Error(t, err, "drain can only be used with receiver links using manual credit management")
err = l.IssueCredit(1)
require.Error(t, err, "issueCredit can only be used with receiver links using manual credit management")
// and flow goes through the non-manual credit path
require.EqualValues(t, 0, l.linkCredit, "No link credits have been added")
require.EqualValues(t, 0, l.Paused, "Link not paused")
// we've consumed half of the maximum credit we're allowed to have - reflow!
l.receiver.maxCredit = 2
l.linkCredit = 1
l.unsettledMessages = map[string]struct{}{}
ok, enableOutgoingTransfers := l.doFlow()
require.True(t, ok, "no errors, should continue to process")
require.False(t, enableOutgoingTransfers, "outgoing transfers only needed for senders")
require.EqualValues(t, 0, l.Paused, "Link not paused")
// flow happens immmediately in 'mux'
txFrame := <-l.Session.tx
switch frame := txFrame.(type) {
case *frames.PerformFlow:
require.False(t, frame.Drain)
// replenished credits: l.receiver.maxCredit-uint32(l.countUnsettled())
require.EqualValues(t, 2, *frame.LinkCredit)
default:
require.Fail(t, fmt.Sprintf("Unexpected frame was transferred: %+v", txFrame))
}
}
func TestLinkFlowWithZeroCredits(t *testing.T) {
l := newTestLink(t)
err := l.DrainCredit(context.Background())
require.Error(t, err, "drain can only be used with receiver links using manual credit management")
err = l.IssueCredit(1)
require.Error(t, err, "issueCredit can only be used with receiver links using manual credit management")
// and flow goes through the non-manual credit path
require.EqualValues(t, 0, l.linkCredit, "No link credits have been added")
require.EqualValues(t, 0, l.Paused, "Link not paused...yet")
l.receiver.maxCredit = 2
l.linkCredit = 0
l.unsettledMessages = map[string]struct{}{
"hello": {},
"hello2": {},
}
ok, enableOutgoingTransfers := l.doFlow()
require.True(t, ok)
require.False(t, enableOutgoingTransfers)
require.EqualValues(t, uint32(1), l.Paused, "Link is paused because credits are zero")
}
func TestLinkFlowDrain(t *testing.T) {
l := newTestLink(t)
// now initialize it as a manual credit link
require.NoError(t, LinkWithManualCredits()(l))
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
<-l.ReceiverReady
l.receiver.manualCreditor.EndDrain()
}()
require.NoError(t, l.DrainCredit(context.Background()))
}
func TestLinkFlowWithManualCreditor(t *testing.T) {
l := newTestLink(t)
require.NoError(t, LinkWithManualCredits()(l))
l.linkCredit = 1
require.NoError(t, l.IssueCredit(100))
ok, enableOutgoingTransfers := l.doFlow()
require.True(t, ok)
require.False(t, enableOutgoingTransfers, "sender related state is not enabled")
// flow happens immmediately in 'mux'
txFrame := <-l.Session.tx
switch frame := txFrame.(type) {
case *frames.PerformFlow:
require.False(t, frame.Drain)
require.EqualValues(t, 100+1, *frame.LinkCredit)
default:
require.Fail(t, fmt.Sprintf("Unexpected frame was transferred: %+v", txFrame))
}
}
func TestLinkFlowWithDrain(t *testing.T) {
l := newTestLink(t)
require.NoError(t, LinkWithManualCredits()(l))
go func() {
<-l.ReceiverReady
ok, enableOutgoingTransfers := l.doFlow()
require.True(t, ok)
require.False(t, enableOutgoingTransfers, "sender related state is not enabled")
// flow happens immmediately in 'mux'
txFrame := <-l.Session.tx
switch frame := txFrame.(type) {
case *frames.PerformFlow:
require.True(t, frame.Drain)
// When we're draining we just automatically set the flow link credit to 0.
// This should allow any outstanding messages to get flushed.
require.EqualValues(t, 0, *frame.LinkCredit)
default:
require.Fail(t, fmt.Sprintf("Unexpected frame was transferred: %+v", txFrame))
}
// simulate the return of the flow from the service
err := l.muxHandleFrame(&frames.PerformFlow{
Drain: true,
})
require.NoError(t, err)
}()
l.linkCredit = 1
require.NoError(t, l.DrainCredit(context.Background()))
}
func TestLinkFlowWithManualCreditorAndNoFlowNeeded(t *testing.T) {
l := newTestLink(t)
require.NoError(t, LinkWithManualCredits()(l))
l.linkCredit = 1
ok, enableOutgoingTransfers := l.doFlow()
require.True(t, ok)
require.False(t, enableOutgoingTransfers, "sender related state is not enabled")
// flow happens immmediately in 'mux'
select {
case fr := <-l.Session.tx: // there won't be a flow this time.
require.Failf(t, "No flow frame would be needed since no link credits were added and drain was not requested", "Frame was %+v", fr)
case <-time.After(time.Second * 2):
// this is the expected case since no frame will be sent.
}
}
func TestMuxFlowHandlesDrainProperly(t *testing.T) {
l := newTestLink(t)
require.NoError(t, LinkWithManualCredits()(l))
l.linkCredit = 101
// simulate what our 'drain' call to muxFlow would look like
// when draining
require.NoError(t, l.muxFlow(0, true))
require.EqualValues(t, 101, l.linkCredit, "credits are untouched when draining")
// when doing a non-drain flow we update the linkCredit to our new link credit total.
require.NoError(t, l.muxFlow(501, false))
require.EqualValues(t, 501, l.linkCredit, "credits are untouched when draining")
}
func newTestLink(t *testing.T) *link {
l := &link{
Source: &frames.Source{},
receiver: &Receiver{
// adding just enough so the debug() print will still work...
// debug(1, "FLOW Link Mux half: source: %s, inflight: %d, credit: %d, deliveryCount: %d, messages: %d, unsettled: %d, maxCredit : %d, settleMode: %s", l.source.Address, l.receiver.inFlight.len(), l.linkCredit, l.deliveryCount, len(l.messages), l.countUnsettled(), l.receiver.maxCredit, l.receiverSettleMode.String())
inFlight: inFlight{},
},
Session: &Session{
tx: make(chan frames.FrameBody, 100),
done: make(chan struct{}),
},
RX: make(chan frames.FrameBody, 100),
ReceiverReady: make(chan struct{}, 1),
}
return l
}
func TestLinkOptions(t *testing.T) {
tests := []struct {
label string
opts []LinkOption
wantSource *frames.Source
wantProperties map[encoding.Symbol]interface{}
}{
{
label: "no options",
},
{
label: "link-filters",
opts: []LinkOption{
LinkSelectorFilter("amqp.annotation.x-opt-offset > '100'"),
LinkProperty("x-opt-test1", "test1"),
LinkProperty("x-opt-test2", "test2"),
LinkProperty("x-opt-test1", "test3"),
LinkPropertyInt64("x-opt-test4", 1),
LinkPropertyInt32("x-opt-test5", 2),
LinkSourceFilter("com.microsoft:session-filter", 0x00000137000000C, "123"),
},
wantSource: &frames.Source{
Filter: map[encoding.Symbol]*encoding.DescribedType{
"apache.org:selector-filter:string": {
Descriptor: binary.BigEndian.Uint64([]byte{0x00, 0x00, 0x46, 0x8C, 0x00, 0x00, 0x00, 0x04}),
Value: "amqp.annotation.x-opt-offset > '100'",
},
"com.microsoft:session-filter": {
Descriptor: binary.BigEndian.Uint64([]byte{0x00, 0x00, 0x00, 0x13, 0x70, 0x00, 0x00, 0x0C}),
Value: "123",
},
},
},
wantProperties: map[encoding.Symbol]interface{}{
"x-opt-test1": "test3",
"x-opt-test2": "test2",
"x-opt-test4": int64(1),
"x-opt-test5": int32(2),
},
},
{
label: "more-link-filters",
opts: []LinkOption{
LinkSourceFilter("com.microsoft:session-filter", 0x00000137000000C, nil),
},
wantSource: &frames.Source{
Filter: map[encoding.Symbol]*encoding.DescribedType{
"com.microsoft:session-filter": {
Descriptor: binary.BigEndian.Uint64([]byte{0x00, 0x00, 0x00, 0x13, 0x70, 0x00, 0x00, 0x0C}),
Value: nil,
},
},
},
},
{
label: "link-source-capabilities",
opts: []LinkOption{
LinkSourceCapabilities("cap1", "cap2", "cap3"),
},
wantSource: &frames.Source{
Capabilities: []encoding.Symbol{"cap1", "cap2", "cap3"},
},
},
}
for _, tt := range tests {
t.Run(tt.label, func(t *testing.T) {
got, err := newLink(nil, nil, tt.opts)
if err != nil {
t.Fatal(err)
}
if !testEqual(got.Source, tt.wantSource) {
t.Errorf("Source properties don't match expected:\n %s", testDiff(got.Source, tt.wantSource))
}
if !testEqual(got.properties, tt.wantProperties) {
t.Errorf("Link properties don't match expected:\n %s", testDiff(got.properties, tt.wantProperties))
}
})
}
}
func TestSourceName(t *testing.T) {
expectedSourceName := "source-name"
opts := []LinkOption{
LinkName(expectedSourceName),
}
got, err := newLink(nil, nil, opts)
if err != nil {
t.Fatal(err)
}
if got.Key.name != expectedSourceName {
t.Errorf("Link Source Name does not match expected: %v got: %v", expectedSourceName, got.Key.name)
}
}
func TestSessionFlowDisablesTransfer(t *testing.T) {
t.Skip("TODO: finish for link testing")
nextIncomingID := uint32(0)
responder := func(req frames.FrameBody) ([]byte, error) {
switch req.(type) {
case *mocks.AMQPProto:
return []byte{'A', 'M', 'Q', 'P', 0, 1, 0, 0}, nil
case *frames.PerformOpen:
return mocks.PerformOpen("container")
case *frames.PerformBegin:
return mocks.PerformBegin(0)
case *frames.PerformFlow:
return nil, nil
case *frames.PerformEnd:
return mocks.PerformEnd(0, nil)
default:
return nil, fmt.Errorf("unhandled frame %T", req)
}
}
netConn := mocks.NewNetConn(responder)
client, err := New(netConn)
require.NoError(t, err)
session, err := client.NewSession()
require.NoError(t, err)
time.Sleep(100 * time.Millisecond)
b, err := mocks.EncodeFrame(mocks.FrameAMQP, 0, &frames.PerformFlow{
NextIncomingID: &nextIncomingID,
IncomingWindow: 0,
OutgoingWindow: 100,
NextOutgoingID: 1,
})
require.NoError(t, err)
netConn.SendFrame(b)
time.Sleep(100 * time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
err = session.Close(ctx)
cancel()
require.NoError(t, err)
time.Sleep(100 * time.Millisecond)
require.NoError(t, client.Close())
}
func TestExactlyOnceDoesntWork(t *testing.T) {
responder := func(req frames.FrameBody) ([]byte, error) {
switch req.(type) {
case *mocks.AMQPProto:
return []byte{'A', 'M', 'Q', 'P', 0, 1, 0, 0}, nil
case *frames.PerformOpen:
return mocks.PerformOpen("container")
case *frames.PerformBegin:
return mocks.PerformBegin(0)
case *frames.PerformEnd:
return mocks.PerformEnd(0, nil)
default:
return nil, fmt.Errorf("unhandled frame %T", req)
}
}
netConn := mocks.NewNetConn(responder)
client, err := New(netConn)
require.NoError(t, err)
session, err := client.NewSession()
require.NoError(t, err)
snd, err := session.NewSender(LinkSenderSettle(ModeMixed),
LinkReceiverSettle(ModeSecond),
LinkTargetAddress("doesntwork"))
require.Error(t, err)
require.Nil(t, snd)
time.Sleep(100 * time.Millisecond)
require.NoError(t, client.Close())
}