forked from Azure/azure-amqp-common-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_test.go
329 lines (274 loc) · 8.43 KB
/
rpc_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
package rpc
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/Azure/azure-amqp-common-go/v3/uuid"
"github.com/Azure/go-amqp"
"github.com/stretchr/testify/require"
)
func TestResponseRouterBasic(t *testing.T) {
receiver := &fakeReceiver{
Responses: []rpcResponse{
{amqpMessageWithCorrelationId("my message id"), nil},
{nil, amqp.ErrLinkClosed},
},
}
link := &Link{
responseMap: map[string]chan rpcResponse{
"my message id": make(chan rpcResponse, 1),
},
receiver: receiver,
}
ch := link.responseMap["my message id"]
link.startResponseRouter()
result := <-ch
require.EqualValues(t, result.message.Data[0], []byte("ID was my message id"))
require.Empty(t, receiver.Responses)
require.Nil(t, link.responseMap, "Response map is nil'd out after we get a closed error")
}
func TestResponseRouterMissingMessageID(t *testing.T) {
receiver := &fakeReceiver{
Responses: []rpcResponse{
{amqpMessageWithCorrelationId("my message id"), nil},
{nil, amqp.ErrLinkClosed},
},
}
link := &Link{
responseMap: map[string]chan rpcResponse{},
receiver: receiver,
}
link.startResponseRouter()
require.Empty(t, receiver.Responses)
}
func TestResponseRouterBadCorrelationID(t *testing.T) {
messageWithBadCorrelationID := &amqp.Message{
Properties: &amqp.MessageProperties{
CorrelationID: uint64(1),
},
}
receiver := &fakeReceiver{
Responses: []rpcResponse{
{messageWithBadCorrelationID, nil},
{nil, amqp.ErrLinkClosed},
},
}
link := &Link{
responseMap: map[string]chan rpcResponse{},
receiver: receiver,
}
link.startResponseRouter()
require.Empty(t, receiver.Responses)
}
func TestResponseRouterFatalErrors(t *testing.T) {
fatalErrors := []error{
amqp.ErrLinkClosed,
amqp.ErrLinkDetached,
amqp.ErrConnClosed,
amqp.ErrSessionClosed,
}
for _, fatalError := range fatalErrors {
t.Run(fatalError.Error(), func(t *testing.T) {
receiver := &fakeReceiver{
Responses: []rpcResponse{
{nil, fatalError},
},
}
sentinelCh := make(chan rpcResponse, 1)
link := &Link{
responseMap: map[string]chan rpcResponse{
"sentinel": sentinelCh,
},
receiver: receiver,
}
link.startResponseRouter()
require.Empty(t, receiver.Responses)
// also, we should have broadcasted that the link is closed to anyone else
// that had not yet received a response but was still waiting.
select {
case rpcResponse := <-sentinelCh:
require.Error(t, rpcResponse.err, fatalError.Error())
require.Nil(t, rpcResponse.message)
case <-time.After(time.Second * 5):
require.Fail(t, "sentinel channel should have received a message")
}
})
}
}
func TestResponseRouterNoResponse(t *testing.T) {
receiver := &fakeReceiver{
Responses: []rpcResponse{
{nil, errors.New("Some other error that will get ignored since we can't route it to anyone (ie: no message ID)")},
{nil, amqp.ErrConnClosed},
},
}
link := &Link{
responseMap: map[string]chan rpcResponse{},
receiver: receiver,
}
link.startResponseRouter()
require.Empty(t, receiver.Responses)
}
func TestAddMessageID(t *testing.T) {
message, id, err := addMessageID(&amqp.Message{}, uuid.NewV4)
require.NoError(t, err)
require.NotEmpty(t, id)
require.EqualValues(t, message.Properties.MessageID, id)
m := &amqp.Message{
Data: [][]byte{[]byte("hello world")},
Properties: &amqp.MessageProperties{
UserID: []byte("my user ID"),
MessageID: "is that will not be copied"},
}
message, id, err = addMessageID(m, uuid.NewV4)
require.NoError(t, err)
require.NotEmpty(t, id)
require.EqualValues(t, message.Properties.MessageID, id)
require.EqualValues(t, message.Properties.UserID, []byte("my user ID"))
require.EqualValues(t, message.Data[0], []byte("hello world"))
}
func TestRPCBasic(t *testing.T) {
fakeUUID := uuid.UUID([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
replyMessage := amqpMessageWithCorrelationId(fakeUUID.String())
replyMessage.ApplicationProperties = map[string]interface{}{
"status-code": int32(200),
}
ch := make(chan struct{})
sender := &fakeSender{ch: ch}
receiver := &fakeReceiver{
Responses: []rpcResponse{
{replyMessage, nil},
{nil, amqp.ErrConnClosed},
},
ch: ch,
}
l := &Link{
receiver: receiver,
sender: sender,
startResponseRouterOnce: &sync.Once{},
responseMap: map[string]chan rpcResponse{},
uuidNewV4: func() (uuid.UUID, error) {
return fakeUUID, nil
},
messageAccept: func(ctx context.Context, message *amqp.Message) error {
return nil
},
}
messageToSend := &amqp.Message{}
resp, err := l.RPC(context.Background(), messageToSend)
require.NoError(t, err)
require.EqualValues(t, fakeUUID.String(), sender.Sent[0].Properties.MessageID, "Sent message contains a uniquely generated ID")
require.EqualValues(t, fakeUUID.String(), resp.Message.Properties.CorrelationID, "Correlation ID matches our originally sent message")
require.Nil(t, replyMessage.Properties.MessageID, "Original message not modified")
}
func TestRPCFailedSend(t *testing.T) {
// important bit is that we clean up the channel we stored in the map
// since we're no longer waiting for the response.
fakeUUID := uuid.UUID([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
replyMessage := amqpMessageWithCorrelationId(fakeUUID.String())
replyMessage.ApplicationProperties = map[string]interface{}{
"status-code": int32(200),
}
ch := make(chan struct{})
sender := &fakeSender{
ch: ch,
}
receiver := &fakeReceiver{
ch: ch,
Responses: []rpcResponse{
{nil, amqp.ErrConnClosed},
},
}
l := &Link{
receiver: receiver,
sender: sender,
startResponseRouterOnce: &sync.Once{},
responseMap: map[string]chan rpcResponse{},
uuidNewV4: func() (uuid.UUID, error) {
return fakeUUID, nil
},
messageAccept: func(ctx context.Context, message *amqp.Message) error {
panic("Should not be called")
},
}
messageToSend := &amqp.Message{}
cancelledContext, cancel := context.WithCancel(context.Background())
cancel()
resp, err := l.RPC(cancelledContext, messageToSend)
require.Nil(t, resp)
require.EqualError(t, err, context.Canceled.Error())
require.EqualValues(t, fakeUUID.String(), sender.Sent[0].Properties.MessageID, "Sent message contains a uniquely generated ID")
}
func TestRPCNilMessageMap(t *testing.T) {
fakeSender := &fakeSender{}
fakeReceiver := &fakeReceiver{
Responses: []rpcResponse{
// this should let us see what deleteChannelFromMap does
{amqpMessageWithCorrelationId("hello"), nil},
{nil, amqp.ErrLinkClosed},
},
}
link := &Link{
sender: fakeSender,
receiver: fakeReceiver,
// responseMap is nil if the broadcastError() function is called. Since this can be
// at any time our individual map functions need to handle the map not being
// there.
responseMap: nil,
startResponseRouterOnce: &sync.Once{},
uuidNewV4: uuid.NewV4,
}
// sanity check - all the map/channel functions are returning nil
require.Nil(t, link.addChannelToMap("hello"))
require.Nil(t, link.deleteChannelFromMap("hello"))
link.startResponseRouter()
require.Empty(t, fakeReceiver.Responses, "All responses are used")
// we're not testing the responseRouter for this second part, so just short-circuit
// the running.
link.startResponseRouterOnce.Do(func() {})
// now check that sending can handle it.
resp, err := link.RPC(context.Background(), &amqp.Message{})
require.Error(t, err, amqp.ErrLinkClosed.Error())
require.Nil(t, resp)
}
func amqpMessageWithCorrelationId(id string) *amqp.Message {
return &amqp.Message{
Data: [][]byte{[]byte(fmt.Sprintf("ID was %s", id))},
Properties: &amqp.MessageProperties{
CorrelationID: id,
},
}
}
type fakeReceiver struct {
Responses []rpcResponse
ch <-chan struct{}
}
func (fr *fakeReceiver) Receive(ctx context.Context) (*amqp.Message, error) {
// wait until the actual send if we're simulating request/response
if fr.ch != nil {
<-fr.ch
}
resp := fr.Responses[0]
fr.Responses = fr.Responses[1:]
return resp.message, resp.err
}
func (fr *fakeReceiver) Close(ctx context.Context) error {
panic("Not used for this test")
}
type fakeSender struct {
Sent []*amqp.Message
ch chan<- struct{}
}
func (s *fakeSender) Send(ctx context.Context, msg *amqp.Message) error {
s.Sent = append(s.Sent, msg)
if s.ch != nil {
close(s.ch)
}
return nil
}
func (fs *fakeSender) Close(ctx context.Context) error {
panic("Not used for this test")
}