amqplib 0.4.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/Makefile +1 -1
- package/README.md +27 -17
- package/channel_api.js +6 -6
- package/examples/headers.js +42 -0
- package/examples/send_generators.js +42 -0
- package/examples/tutorials/README.md +1 -1
- package/examples/tutorials/emit_log.js +3 -4
- package/examples/tutorials/emit_log_direct.js +3 -4
- package/examples/tutorials/emit_log_topic.js +3 -4
- package/examples/tutorials/new_task.js +4 -5
- package/examples/tutorials/package.json +2 -3
- package/examples/tutorials/receive.js +4 -4
- package/examples/tutorials/receive_logs.js +1 -1
- package/examples/tutorials/receive_logs_direct.js +2 -2
- package/examples/tutorials/receive_logs_topic.js +6 -6
- package/examples/tutorials/rpc_client.js +25 -26
- package/examples/tutorials/rpc_server.js +1 -1
- package/examples/tutorials/send.js +9 -5
- package/examples/tutorials/worker.js +1 -1
- package/examples/waitForConfirms.js +2 -2
- package/lib/callback_model.js +13 -12
- package/lib/channel_model.js +58 -66
- package/lib/connection.js +19 -16
- package/lib/heartbeat.js +1 -1
- package/package.json +3 -3
- package/test/channel.js +37 -32
- package/test/channel_api.js +104 -116
- package/test/util.js +19 -19
package/test/channel_api.js
CHANGED
|
@@ -6,8 +6,7 @@ var util = require('./util');
|
|
|
6
6
|
var succeed = util.succeed, fail = util.fail;
|
|
7
7
|
var schedule = util.schedule;
|
|
8
8
|
var randomString = util.randomString;
|
|
9
|
-
var
|
|
10
|
-
var defer = when.defer;
|
|
9
|
+
var Promise = require('bluebird');
|
|
11
10
|
|
|
12
11
|
var URL = process.env.URL || 'amqp://localhost';
|
|
13
12
|
|
|
@@ -17,22 +16,9 @@ function connect() {
|
|
|
17
16
|
|
|
18
17
|
// Expect this promise to fail, and flip the results accordingly.
|
|
19
18
|
function expectFail(promise) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Often, even interdependent operations don't need to be explicitly
|
|
26
|
-
// chained together with `.then`, since the channel implicitly
|
|
27
|
-
// serialises RPCs. Synchronising on the last operation is sufficient,
|
|
28
|
-
// provided all the operations are successful. This procedure removes
|
|
29
|
-
// some of the `then` noise, while still failing if any of its
|
|
30
|
-
// arguments fail.
|
|
31
|
-
function doAll(/* promise... */) {
|
|
32
|
-
return when.all(arguments)
|
|
33
|
-
.then(function(results) {
|
|
34
|
-
return results[results.length - 1];
|
|
35
|
-
});
|
|
19
|
+
return new Promise(function(resolve, reject) {
|
|
20
|
+
return promise.then(reject).catch(resolve);
|
|
21
|
+
});
|
|
36
22
|
}
|
|
37
23
|
|
|
38
24
|
// I'll rely on operations being rejected, rather than the channel
|
|
@@ -54,7 +40,7 @@ function channel_test(chmethod, name, chfun) {
|
|
|
54
40
|
c[chmethod]().then(ignoreErrors).then(chfun)
|
|
55
41
|
.then(succeed(done), fail(done))
|
|
56
42
|
// close the connection regardless of what happens with the test
|
|
57
|
-
.
|
|
43
|
+
.finally(function() {c.close();});
|
|
58
44
|
});
|
|
59
45
|
});
|
|
60
46
|
}
|
|
@@ -124,15 +110,15 @@ chtest("fail on reasserting exchange with different type",
|
|
|
124
110
|
});
|
|
125
111
|
|
|
126
112
|
chtest("channel break on publishing to non-exchange", function(ch) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
113
|
+
return new Promise(function(resolve) {
|
|
114
|
+
ch.on('error', resolve);
|
|
115
|
+
ch.publish(randomString(), '', new Buffer('foobar'));
|
|
116
|
+
});
|
|
131
117
|
});
|
|
132
118
|
|
|
133
119
|
chtest("delete queue", function(ch) {
|
|
134
120
|
var q = 'test.delete-queue';
|
|
135
|
-
return
|
|
121
|
+
return Promise.join(
|
|
136
122
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
137
123
|
ch.checkQueue(q))
|
|
138
124
|
.then(function() {
|
|
@@ -143,7 +129,7 @@ chtest("delete queue", function(ch) {
|
|
|
143
129
|
|
|
144
130
|
chtest("delete exchange", function(ch) {
|
|
145
131
|
var ex = 'test.delete-exchange';
|
|
146
|
-
return
|
|
132
|
+
return Promise.join(
|
|
147
133
|
ch.assertExchange(ex, 'fanout', EX_OPTS),
|
|
148
134
|
ch.checkExchange(ex))
|
|
149
135
|
.then(function() {
|
|
@@ -157,23 +143,23 @@ chtest("delete exchange", function(ch) {
|
|
|
157
143
|
// Wait for the queue to meet the condition; useful for waiting for
|
|
158
144
|
// messages to arrive, for example.
|
|
159
145
|
function waitForQueue(q, condition) {
|
|
160
|
-
|
|
161
|
-
connect(URL).then(function(c) {
|
|
146
|
+
return connect(URL).then(function(c) {
|
|
162
147
|
return c.createChannel()
|
|
163
148
|
.then(function(ch) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
149
|
+
return ch.checkQueue(q).then(function(qok) {
|
|
150
|
+
function check() {
|
|
151
|
+
return ch.checkQueue(q).then(function(qok) {
|
|
152
|
+
if (condition(qok)) {
|
|
153
|
+
c.close();
|
|
154
|
+
return qok;
|
|
155
|
+
}
|
|
156
|
+
else schedule(check);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
return check();
|
|
160
|
+
});
|
|
174
161
|
});
|
|
175
162
|
});
|
|
176
|
-
return ready.promise;
|
|
177
163
|
}
|
|
178
164
|
|
|
179
165
|
// Return a promise that resolves when the queue has at least `num`
|
|
@@ -191,14 +177,14 @@ suite("sendMessage", function() {
|
|
|
191
177
|
chtest("send to queue and get from queue", function(ch) {
|
|
192
178
|
var q = 'test.send-to-q';
|
|
193
179
|
var msg = randomString();
|
|
194
|
-
return
|
|
195
|
-
ch.assertQueue(q, QUEUE_OPTS),
|
|
196
|
-
ch.purgeQueue(q))
|
|
180
|
+
return Promise.join(ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q))
|
|
197
181
|
.then(function() {
|
|
198
182
|
ch.sendToQueue(q, new Buffer(msg));
|
|
199
|
-
return waitForMessages(q);
|
|
183
|
+
return waitForMessages(q);
|
|
184
|
+
})
|
|
200
185
|
.then(function() {
|
|
201
|
-
return ch.get(q, {noAck: true});
|
|
186
|
+
return ch.get(q, {noAck: true});
|
|
187
|
+
})
|
|
202
188
|
.then(function(m) {
|
|
203
189
|
assert(m);
|
|
204
190
|
assert.equal(msg, m.content.toString());
|
|
@@ -208,7 +194,7 @@ chtest("send to queue and get from queue", function(ch) {
|
|
|
208
194
|
chtest("send (and get) zero content to queue", function(ch) {
|
|
209
195
|
var q = 'test.send-to-q';
|
|
210
196
|
var msg = new Buffer(0);
|
|
211
|
-
return
|
|
197
|
+
return Promise.join(
|
|
212
198
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
213
199
|
ch.purgeQueue(q))
|
|
214
200
|
.then(function() {
|
|
@@ -232,7 +218,7 @@ chtest("route message", function(ch) {
|
|
|
232
218
|
var q = 'test.route-message-q';
|
|
233
219
|
var msg = randomString();
|
|
234
220
|
|
|
235
|
-
return
|
|
221
|
+
return Promise.join(
|
|
236
222
|
ch.assertExchange(ex, 'fanout', EX_OPTS),
|
|
237
223
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
238
224
|
ch.purgeQueue(q),
|
|
@@ -270,7 +256,7 @@ chtest("unbind queue", function(ch) {
|
|
|
270
256
|
var viabinding = randomString();
|
|
271
257
|
var direct = randomString();
|
|
272
258
|
|
|
273
|
-
return
|
|
259
|
+
return Promise.join(
|
|
274
260
|
ch.assertExchange(ex, 'fanout', EX_OPTS),
|
|
275
261
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
276
262
|
ch.purgeQueue(q),
|
|
@@ -303,7 +289,7 @@ chtest("consume via exchange-exchange binding", function(ch) {
|
|
|
303
289
|
var ex1 = 'test.ex-ex-binding1', ex2 = 'test.ex-ex-binding2';
|
|
304
290
|
var q = 'test.ex-ex-binding-q';
|
|
305
291
|
var rk = 'test.routing.key', msg = randomString();
|
|
306
|
-
return
|
|
292
|
+
return Promise.join(
|
|
307
293
|
ch.assertExchange(ex1, 'direct', EX_OPTS),
|
|
308
294
|
ch.assertExchange(ex2, 'fanout',
|
|
309
295
|
{durable: false, internal: true}),
|
|
@@ -312,16 +298,16 @@ chtest("consume via exchange-exchange binding", function(ch) {
|
|
|
312
298
|
ch.bindExchange(ex2, ex1, rk, {}),
|
|
313
299
|
ch.bindQueue(q, ex2, '', {}))
|
|
314
300
|
.then(function() {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
301
|
+
return new Promise(function(resolve, reject) {
|
|
302
|
+
function delivery(m) {
|
|
303
|
+
if (m.content.toString() === msg) resolve();
|
|
304
|
+
else reject(new Error("Wrong message"));
|
|
305
|
+
}
|
|
306
|
+
ch.consume(q, delivery, {noAck: true})
|
|
307
|
+
.then(function() {
|
|
308
|
+
ch.publish(ex1, rk, new Buffer(msg));
|
|
309
|
+
});
|
|
310
|
+
});
|
|
325
311
|
});
|
|
326
312
|
});
|
|
327
313
|
|
|
@@ -333,7 +319,7 @@ chtest("unbind exchange", function(ch) {
|
|
|
333
319
|
var viabinding = randomString();
|
|
334
320
|
var direct = randomString();
|
|
335
321
|
|
|
336
|
-
return
|
|
322
|
+
return Promise.join(
|
|
337
323
|
ch.assertExchange(source, 'fanout', EX_OPTS),
|
|
338
324
|
ch.assertExchange(dest, 'fanout', EX_OPTS),
|
|
339
325
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
@@ -365,58 +351,56 @@ chtest("unbind exchange", function(ch) {
|
|
|
365
351
|
// This is a bit convoluted. Sorry.
|
|
366
352
|
chtest("cancel consumer", function(ch) {
|
|
367
353
|
var q = 'test.consumer-cancel';
|
|
368
|
-
var recv1 = defer();
|
|
369
354
|
var ctag;
|
|
370
|
-
|
|
371
|
-
|
|
355
|
+
var recv1 = new Promise(function (resolve, reject) {
|
|
356
|
+
Promise.join(
|
|
372
357
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
373
358
|
ch.purgeQueue(q),
|
|
374
359
|
// My callback is 'resolve the promise in `arrived`'
|
|
375
|
-
ch.consume(q,
|
|
360
|
+
ch.consume(q, resolve, {noAck:true})
|
|
376
361
|
.then(function(ok) {
|
|
377
362
|
ctag = ok.consumerTag;
|
|
378
363
|
ch.sendToQueue(q, new Buffer('foo'));
|
|
379
364
|
}));
|
|
365
|
+
});
|
|
366
|
+
|
|
380
367
|
// A message should arrive because of the consume
|
|
381
|
-
return recv1.
|
|
382
|
-
|
|
383
|
-
recv1 = defer();
|
|
384
|
-
|
|
385
|
-
return doAll(
|
|
368
|
+
return recv1.then(function() {
|
|
369
|
+
var recv2 = Promise.join(
|
|
386
370
|
ch.cancel(ctag).then(function() {
|
|
387
|
-
ch.sendToQueue(q, new Buffer('bar'));
|
|
371
|
+
return ch.sendToQueue(q, new Buffer('bar'));
|
|
388
372
|
}),
|
|
389
373
|
// but check a message did arrive in the queue
|
|
390
374
|
waitForMessages(q))
|
|
391
375
|
.then(function() {
|
|
392
|
-
ch.get(q, {noAck:true})
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
// i.e., fail on delivery, succeed on get-ok
|
|
376
|
+
return ch.get(q, {noAck:true});
|
|
377
|
+
})
|
|
378
|
+
.then(function(m) {
|
|
379
|
+
// I'm going to reject it, because I flip succeed/fail
|
|
380
|
+
// just below
|
|
381
|
+
if (m.content.toString() === 'bar') {
|
|
382
|
+
throw new Error();
|
|
383
|
+
}
|
|
401
384
|
});
|
|
385
|
+
|
|
386
|
+
return expectFail(recv2);
|
|
402
387
|
});
|
|
403
388
|
});
|
|
404
389
|
|
|
405
390
|
chtest("cancelled consumer", function(ch) {
|
|
406
391
|
var q = 'test.cancelled-consumer';
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
return nullRecv.promise;
|
|
392
|
+
return new Promise(function(resolve, reject) {
|
|
393
|
+
return Promise.join(
|
|
394
|
+
ch.assertQueue(q),
|
|
395
|
+
ch.purgeQueue(q),
|
|
396
|
+
ch.consume(q, function(msg) {
|
|
397
|
+
if (msg === null) resolve();
|
|
398
|
+
else reject(new Error('Message not expected'));
|
|
399
|
+
}))
|
|
400
|
+
.then(function() {
|
|
401
|
+
return ch.deleteQueue(q);
|
|
402
|
+
});
|
|
403
|
+
});
|
|
420
404
|
});
|
|
421
405
|
|
|
422
406
|
// ack, by default, removes a single message from the queue
|
|
@@ -424,7 +408,7 @@ chtest("ack", function(ch) {
|
|
|
424
408
|
var q = 'test.ack';
|
|
425
409
|
var msg1 = randomString(), msg2 = randomString();
|
|
426
410
|
|
|
427
|
-
return
|
|
411
|
+
return Promise.join(
|
|
428
412
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
429
413
|
ch.purgeQueue(q))
|
|
430
414
|
.then(function() {
|
|
@@ -454,7 +438,7 @@ chtest("nack", function(ch) {
|
|
|
454
438
|
var q = 'test.nack';
|
|
455
439
|
var msg1 = randomString();
|
|
456
440
|
|
|
457
|
-
return
|
|
441
|
+
return Promise.join(
|
|
458
442
|
ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q))
|
|
459
443
|
.then(function() {
|
|
460
444
|
ch.sendToQueue(q, new Buffer(msg1));
|
|
@@ -479,7 +463,7 @@ chtest("reject", function(ch) {
|
|
|
479
463
|
var q = 'test.reject';
|
|
480
464
|
var msg1 = randomString();
|
|
481
465
|
|
|
482
|
-
return
|
|
466
|
+
return Promise.join(
|
|
483
467
|
ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q))
|
|
484
468
|
.then(function() {
|
|
485
469
|
ch.sendToQueue(q, new Buffer(msg1));
|
|
@@ -500,7 +484,7 @@ chtest("reject", function(ch) {
|
|
|
500
484
|
|
|
501
485
|
chtest("prefetch", function(ch) {
|
|
502
486
|
var q = 'test.prefetch';
|
|
503
|
-
return
|
|
487
|
+
return Promise.join(
|
|
504
488
|
ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q),
|
|
505
489
|
ch.prefetch(1))
|
|
506
490
|
.then(function() {
|
|
@@ -509,21 +493,28 @@ chtest("prefetch", function(ch) {
|
|
|
509
493
|
return waitForMessages(q, 2);
|
|
510
494
|
})
|
|
511
495
|
.then(function() {
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
496
|
+
return new Promise(function(resolve) {
|
|
497
|
+
var messageCount = 0;
|
|
498
|
+
function receive(msg) {
|
|
499
|
+
ch.ack(msg);
|
|
500
|
+
if (++messageCount > 1) {
|
|
501
|
+
resolve(messageCount);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return ch.consume(q, receive, {noAck: false})
|
|
505
|
+
});
|
|
506
|
+
})
|
|
507
|
+
.then(function(c) {
|
|
508
|
+
return assert.equal(2, c);
|
|
524
509
|
});
|
|
525
510
|
});
|
|
526
511
|
|
|
512
|
+
chtest('close', function(ch) {
|
|
513
|
+
// Resolving promise guarantees
|
|
514
|
+
// channel is closed
|
|
515
|
+
return ch.close();
|
|
516
|
+
});
|
|
517
|
+
|
|
527
518
|
});
|
|
528
519
|
|
|
529
520
|
var confirmtest = channel_test.bind(null, 'createConfirmChannel');
|
|
@@ -532,7 +523,7 @@ suite("confirms", function() {
|
|
|
532
523
|
|
|
533
524
|
confirmtest('message is confirmed', function(ch) {
|
|
534
525
|
var q = 'test.confirm-message';
|
|
535
|
-
return
|
|
526
|
+
return Promise.join(
|
|
536
527
|
ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q))
|
|
537
528
|
.then(function() {
|
|
538
529
|
return ch.sendToQueue(q, new Buffer('bleep'));
|
|
@@ -546,7 +537,7 @@ confirmtest('message is confirmed', function(ch) {
|
|
|
546
537
|
// multi-ack.
|
|
547
538
|
confirmtest('multiple confirms', function(ch) {
|
|
548
539
|
var q = 'test.multiple-confirms';
|
|
549
|
-
return
|
|
540
|
+
return Promise.join(
|
|
550
541
|
ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q))
|
|
551
542
|
.then(function() {
|
|
552
543
|
var multipleRainbows = false;
|
|
@@ -558,18 +549,15 @@ confirmtest('multiple confirms', function(ch) {
|
|
|
558
549
|
var cs = [];
|
|
559
550
|
|
|
560
551
|
function sendAndPushPromise() {
|
|
561
|
-
var conf =
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
else conf.resolve();
|
|
566
|
-
});
|
|
567
|
-
cs.push(conf.promise);
|
|
552
|
+
var conf = Promise.fromCallback(function(cb) {
|
|
553
|
+
return ch.sendToQueue(q, new Buffer('bleep'), {}, cb);
|
|
554
|
+
});
|
|
555
|
+
cs.push(conf);
|
|
568
556
|
}
|
|
569
557
|
|
|
570
558
|
for (var i=0; i < num; i++) sendAndPushPromise();
|
|
571
559
|
|
|
572
|
-
return
|
|
560
|
+
return Promise.all(cs).then(function() {
|
|
573
561
|
if (multipleRainbows) return true;
|
|
574
562
|
else if (num > 500) throw new Error(
|
|
575
563
|
"Couldn't provoke the server" +
|
package/test/util.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var Promise = require('bluebird');
|
|
3
4
|
var crypto = require('crypto');
|
|
4
5
|
var Connection = require('../lib/connection').Connection;
|
|
5
6
|
var PassThrough =
|
|
6
7
|
require('stream').PassThrough ||
|
|
7
8
|
require('readable-stream/passthrough');
|
|
8
|
-
var defer = require('when').defer;
|
|
9
9
|
var defs = require('../lib/defs');
|
|
10
10
|
var assert = require('assert');
|
|
11
11
|
|
|
@@ -78,24 +78,24 @@ function runServer(socket, run) {
|
|
|
78
78
|
|
|
79
79
|
function await(method) {
|
|
80
80
|
return function() {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
81
|
+
return new Promise(function(resolve, reject) {
|
|
82
|
+
if (method) {
|
|
83
|
+
frames.step(function(e, f) {
|
|
84
|
+
if (e !== null) return reject(e);
|
|
85
|
+
if (f.id === method)
|
|
86
|
+
resolve(f);
|
|
87
|
+
else
|
|
88
|
+
reject(new Error("Expected method: " + method +
|
|
89
|
+
", got " + f.id));
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
frames.step(function(e, f) {
|
|
94
|
+
if (e !== null) return reject(e);
|
|
95
|
+
else resolve(f);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
99
|
};
|
|
100
100
|
}
|
|
101
101
|
run(send, await);
|