channel.js
16.5 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// Test the channel machinery
'use strict';
var assert = require('assert');
var defer = require('when').defer;
var Channel = require('../lib/channel').Channel;
var Connection = require('../lib/connection').Connection;
var util = require('./util');
var succeed = util.succeed, fail = util.fail, latch = util.latch;
var completes = util.completes;
var defs = require('../lib/defs');
var conn_handshake = require('./connection').connection_handshake;
var OPEN_OPTS = require('./connection').OPEN_OPTS;
var LOG_ERRORS = process.env.LOG_ERRORS;
function baseChannelTest(client, server) {
return function(done) {
var bothDone = latch(2, done);
var pair = util.socketPair();
var c = new Connection(pair.client);
if (LOG_ERRORS) c.on('error', console.warn);
c.open(OPEN_OPTS, function(err, ok) {
if (err === null) client(c, bothDone);
else fail(bothDone);
});
pair.server.read(8); // discard the protocol header
var s = util.runServer(pair.server, function(send, await) {
conn_handshake(send, await)
.then(function() {
server(send, await, bothDone);
}, fail(bothDone));
});
};
}
function channelTest(client, server) {
return baseChannelTest(
function(conn, done) {
var ch = new Channel(conn);
if (LOG_ERRORS) ch.on('error', console.warn);
client(ch, done, conn);
},
function(send, await, done) {
channel_handshake(send, await)
.then(function(ch) {
return server(send, await, done, ch);
}).then(null, fail(done)); // so you can return a promise to let
// errors bubble out
}
);
};
function channel_handshake(send, await) {
return await(defs.ChannelOpen)()
.then(function(open) {
assert.notEqual(0, open.channel);
send(defs.ChannelOpenOk, {channelId: new Buffer('')}, open.channel);
return open.channel;
});
}
// fields for deliver and publish and get-ok
var DELIVER_FIELDS = {
consumerTag: 'fake',
deliveryTag: 1,
redelivered: false,
exchange: 'foo',
routingKey: 'bar',
replyCode: defs.constants.NO_ROUTE,
replyText: 'derp',
};
function open(ch) {
var opened = defer();
ch.allocate();
ch._rpc(defs.ChannelOpen, {outOfBand: ''}, defs.ChannelOpenOk,
function(err, _) {
if (err === null) opened.resolve();
else opened.reject(err);
});
return opened.promise;
}
suite("channel open and close", function() {
test("open", channelTest(
function(ch, done) {
open(ch).then(succeed(done), fail(done));
},
function(send, await, done) {
done();
}));
test("bad server", baseChannelTest(
function(c, done) {
var ch = new Channel(c);
open(ch).then(fail(done), succeed(done));
},
function(send, await, done) {
return await(defs.ChannelOpen)()
.then(function(open) {
send(defs.ChannelCloseOk, {}, open.channel);
}).then(succeed(done), fail(done));
}));
test("open, close", channelTest(
function(ch, done) {
open(ch)
.then(function() {
var closed = defer();
ch.closeBecause("Bye", defs.constants.REPLY_SUCCESS,
closed.resolve);
return closed.promise;
})
.then(succeed(done), fail(done));
},
function(send, await, done, ch) {
return await(defs.ChannelClose)()
.then(function(close) {
send(defs.ChannelCloseOk, {}, ch);
}).then(succeed(done), fail(done));
}));
test("server close", channelTest(
function(ch, done) {
ch.on('error', function(error) {
assert.strictEqual(504, error.code);
succeed(done)();
});
open(ch);
},
function(send, await, done, ch) {
send(defs.ChannelClose, {
replyText: 'Forced close',
replyCode: defs.constants.CHANNEL_ERROR,
classId: 0, methodId: 0
}, ch);
await(defs.ChannelCloseOk)()
.then(succeed(done), fail(done));
}));
test("overlapping channel/server close", channelTest(
function(ch, done, conn) {
var both = latch(2, done);
conn.on('error', succeed(both));
ch.on('close', succeed(both));
open(ch).then(function() {
ch.closeBecause("Bye", defs.constants.REPLY_SUCCESS);
}, fail(both));
},
function(send, await, done, ch) {
await(defs.ChannelClose)()
.then(function() {
send(defs.ConnectionClose, {
replyText: 'Got there first',
replyCode: defs.constants.INTERNAL_ERROR,
classId: 0, methodId: 0
}, 0);
})
.then(await(defs.ConnectionCloseOk))
.then(succeed(done), fail(done));
}));
test("double close", channelTest(
function(ch, done) {
open(ch).then(function() {
ch.closeBecause("First close", defs.constants.REPLY_SUCCESS);
// NB no synchronisation, we do this straight away
assert.throws(function() {
ch.closeBecause("Second close", defs.constants.REPLY_SUCCESS);
});
}).then(succeed(done), fail(done));
},
function(send, await, done, ch) {
await(defs.ChannelClose)()
.then(function() {
send(defs.ChannelCloseOk, {
}, ch);
})
.then(succeed(done), fail(done));
}));
}); //suite
suite("channel machinery", function() {
test("RPC", channelTest(
function(ch, done) {
var rpcLatch = latch(3, done);
open(ch).then(function() {
function wheeboom(err, f) {
if (err !== null) rpcLatch(err);
else rpcLatch();
}
var fields = {
prefetchCount: 10,
prefetchSize: 0,
global: false
};
ch._rpc(defs.BasicQos, fields, defs.BasicQosOk, wheeboom);
ch._rpc(defs.BasicQos, fields, defs.BasicQosOk, wheeboom);
ch._rpc(defs.BasicQos, fields, defs.BasicQosOk, wheeboom);
}).then(null, fail(rpcLatch));
},
function(send, await, done, ch) {
function sendOk(f) {
send(defs.BasicQosOk, {}, ch);
}
return await(defs.BasicQos)()
.then(sendOk)
.then(await(defs.BasicQos))
.then(sendOk)
.then(await(defs.BasicQos))
.then(sendOk)
.then(succeed(done), fail(done));
}));
test("Bad RPC", channelTest(
function(ch, done) {
// We want to see the RPC rejected and the channel closed (with an
// error)
var errLatch = latch(2, done);
ch.on('error', function(error) {
assert.strictEqual(505, error.code);
succeed(errLatch)();
});
open(ch)
.then(function() {
ch._rpc(defs.BasicRecover, {requeue: true}, defs.BasicRecoverOk,
function(err) {
if (err !== null) errLatch();
else errLatch(new Error('Expected RPC failure'));
});
}, fail(errLatch));
},
function(send, await, done, ch) {
return await()()
.then(function() {
send(defs.BasicGetEmpty, {clusterId: ''}, ch);
}) // oh wait! that was wrong! expect a channel close
.then(await(defs.ChannelClose))
.then(function() {
send(defs.ChannelCloseOk, {}, ch);
}).then(succeed(done), fail(done));
}));
test("RPC on closed channel", channelTest(
function(ch, done) {
open(ch);
var close = defer(), fail1 = defer(), fail2 = defer();
ch.on('error', function(error) {
assert.strictEqual(504, error.code);
close.resolve();
});
function failureCb(d) {
return function(err) {
if (err !== null) d.resolve();
else d.reject();
}
}
ch._rpc(defs.BasicRecover, {requeue:true}, defs.BasicRecoverOk,
failureCb(fail1));
ch._rpc(defs.BasicRecover, {requeue:true}, defs.BasicRecoverOk,
failureCb(fail2));
close.promise
.then(function() { return fail1.promise; })
.then(function() { return fail2.promise; })
.then(succeed(done), fail(done));
},
function(send, await, done, ch) {
await(defs.BasicRecover)()
.then(function() {
send(defs.ChannelClose, {
replyText: 'Nuh-uh!',
replyCode: defs.constants.CHANNEL_ERROR,
methodId: 0, classId: 0
}, ch);
return await(defs.ChannelCloseOk);
})
.then(succeed(done), fail(done));
}));
test("publish all < single chunk threshold", channelTest(
function(ch, done) {
open(ch)
.then(function() {
ch.sendMessage({
exchange: 'foo', routingKey: 'bar',
mandatory: false, immediate: false, ticket: 0
}, {}, new Buffer('foobar'));
})
.then(succeed(done), fail(done));
},
function(send, await, done, ch) {
await(defs.BasicPublish)()
.then(await(defs.BasicProperties))
.then(await(undefined)) // content frame
.then(function(f) {
assert.equal('foobar', f.content.toString());
}).then(succeed(done), fail(done));
}));
test("publish content > single chunk threshold", channelTest(
function(ch, done) {
open(ch);
completes(function() {
ch.sendMessage({
exchange: 'foo', routingKey: 'bar',
mandatory: false, immediate: false, ticket: 0
}, {}, new Buffer(3000));
}, done);
},
function(send, await, done, ch) {
await(defs.BasicPublish)()
.then(await(defs.BasicProperties))
.then(await(undefined)) // content frame
.then(function(f) {
assert.equal(3000, f.content.length);
}).then(succeed(done), fail(done));
}));
test("publish method & headers > threshold", channelTest(
function(ch, done) {
open(ch);
completes(function() {
ch.sendMessage({
exchange: 'foo', routingKey: 'bar',
mandatory: false, immediate: false, ticket: 0
}, {
headers: {foo: new Buffer(3000)}
}, new Buffer('foobar'));
}, done);
},
function(send, await, done, ch) {
await(defs.BasicPublish)()
.then(await(defs.BasicProperties))
.then(await(undefined)) // content frame
.then(function(f) {
assert.equal('foobar', f.content.toString());
}).then(succeed(done), fail(done));
}));
test("publish zero-length message", channelTest(
function(ch, done) {
open(ch);
completes(function() {
ch.sendMessage({
exchange: 'foo', routingKey: 'bar',
mandatory: false, immediate: false, ticket: 0
}, {}, new Buffer(0));
ch.sendMessage({
exchange: 'foo', routingKey: 'bar',
mandatory: false, immediate: false, ticket: 0
}, {}, new Buffer(0));
}, done);
},
function(send, await, done, ch) {
await(defs.BasicPublish)()
.then(await(defs.BasicProperties))
// no content frame for a zero-length message
.then(await(defs.BasicPublish))
.then(succeed(done), fail(done));
}));
test("delivery", channelTest(
function(ch, done) {
open(ch);
ch.on('delivery', function(m) {
completes(function() {
assert.equal('barfoo', m.content.toString());
}, done);
});
},
function(send, await, done, ch) {
completes(function() {
send(defs.BasicDeliver, DELIVER_FIELDS, ch, new Buffer('barfoo'));
}, done);
}));
test("zero byte msg", channelTest(
function(ch, done) {
open(ch);
ch.on('delivery', function(m) {
completes(function() {
assert.deepEqual(new Buffer(0), m.content);
}, done);
});
},
function(send, await, done, ch) {
completes(function() {
send(defs.BasicDeliver, DELIVER_FIELDS, ch, new Buffer(''));
}, done);
}));
test("bad delivery", channelTest(
function(ch, done) {
var errorAndClose = latch(2, done);
ch.on('error', function(error) {
assert.strictEqual(505, error.code);
succeed(errorAndClose)();
});
ch.on('close', succeed(errorAndClose));
open(ch);
},
function(send, await, done, ch) {
send(defs.BasicDeliver, DELIVER_FIELDS, ch);
// now send another deliver without having sent the content
send(defs.BasicDeliver, DELIVER_FIELDS, ch);
return await(defs.ChannelClose)()
.then(function() {
send(defs.ChannelCloseOk, {}, ch);
}).then(succeed(done), fail(done));
}));
test("bad content send", channelTest(
function(ch, done) {
completes(function() {
open(ch);
assert.throws(function() {
ch.sendMessage({routingKey: 'foo',
exchange: 'amq.direct'},
{}, null);
});
}, done);
},
function(send, await, done, ch) {
done();
}));
test("bad properties send", channelTest(
function(ch, done) {
completes(function() {
open(ch);
assert.throws(function() {
ch.sendMessage({routingKey: 'foo',
exchange: 'amq.direct'},
{contentEncoding: 7},
new Buffer('foobar'));
});
}, done);
},
function(send, await, done, ch) {
done();
}));
test("bad consumer", channelTest(
function(ch, done) {
var errorAndClose = latch(2, done);
ch.on('delivery', function() {
throw new Error("I am a bad consumer");
});
ch.on('error', function(error) {
assert.strictEqual(541, error.code);
succeed(errorAndClose)();
});
ch.on('close', succeed(errorAndClose));
open(ch);
},
function(send, await, done, ch) {
send(defs.BasicDeliver, DELIVER_FIELDS, ch, new Buffer('barfoo'));
return await(defs.ChannelClose)()
.then(function() {
send(defs.ChannelCloseOk, {}, ch);
}).then(succeed(done), fail(done));
}));
test("bad send in consumer", channelTest(
function(ch, done) {
var errorAndClose = latch(2, done);
ch.on('close', succeed(errorAndClose));
ch.on('error', function(error) {
assert.strictEqual(541, error.code);
succeed(errorAndClose)();
});
ch.on('delivery', function() {
ch.sendMessage({routingKey: 'foo',
exchange: 'amq.direct'},
{}, null); // can't send null
});
open(ch);
},
function(send, await, done, ch) {
completes(function() {
send(defs.BasicDeliver, DELIVER_FIELDS, ch,
new Buffer('barfoo'));
}, done);
return await(defs.ChannelClose)()
.then(function() {
send(defs.ChannelCloseOk, {}, ch);
}).then(succeed(done), fail(done));
}));
test("return", channelTest(
function(ch, done) {
ch.on('return', function(m) {
completes(function() {
assert.equal('barfoo', m.content.toString());
}, done);
});
open(ch);
},
function(send, await, done, ch) {
completes(function() {
send(defs.BasicReturn, DELIVER_FIELDS, ch, new Buffer('barfoo'));
}, done);
}));
test("cancel", channelTest(
function(ch, done) {
ch.on('cancel', function(f) {
completes(function() {
assert.equal('product of society', f.consumerTag);
}, done);
});
open(ch);
},
function(send, await, done, ch) {
completes(function() {
send(defs.BasicCancel, {
consumerTag: 'product of society',
nowait: false
}, ch);
}, done);
}));
function confirmTest(variety, Method) {
return test('confirm ' + variety, channelTest(
function(ch, done) {
ch.on(variety, function(f) {
completes(function() {
assert.equal(1, f.deliveryTag);
}, done);
});
open(ch);
},
function(send, await, done, ch) {
completes(function() {
send(Method, {
deliveryTag: 1,
multiple: false
}, ch);
}, done);
}));
}
confirmTest("ack", defs.BasicAck);
confirmTest("nack", defs.BasicNack);
test("out-of-order acks", channelTest(
function(ch, done) {
var allConfirms = latch(3, function() {
completes(function() {
assert.equal(0, ch.unconfirmed.length);
assert.equal(4, ch.lwm);
}, done);
});
ch.pushConfirmCallback(allConfirms);
ch.pushConfirmCallback(allConfirms);
ch.pushConfirmCallback(allConfirms);
open(ch);
},
function(send, await, done, ch) {
completes(function() {
send(defs.BasicAck, {deliveryTag: 2, multiple: false}, ch);
send(defs.BasicAck, {deliveryTag: 3, multiple: false}, ch);
send(defs.BasicAck, {deliveryTag: 1, multiple: false}, ch);
}, done);
}));
test("not all out-of-order acks", channelTest(
function(ch, done) {
var allConfirms = latch(2, function() {
completes(function() {
assert.equal(1, ch.unconfirmed.length);
assert.equal(3, ch.lwm);
}, done);
});
ch.pushConfirmCallback(allConfirms); // tag = 1
ch.pushConfirmCallback(allConfirms); // tag = 2
ch.pushConfirmCallback(function() {
done(new Error('Confirm callback should not be called'));
});
open(ch);
},
function(send, await, done, ch) {
completes(function() {
send(defs.BasicAck, {deliveryTag: 2, multiple: false}, ch);
send(defs.BasicAck, {deliveryTag: 1, multiple: false}, ch);
}, done);
}));
});