amqplib 0.4.2 → 0.5.3
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 +88 -0
- package/Makefile +2 -1
- package/README.md +4 -4
- package/callback_api.js +1 -0
- package/channel_api.js +7 -6
- package/examples/headers.js +2 -2
- package/examples/receive_generator.js +39 -0
- package/examples/send_generators.js +42 -0
- package/examples/ssl.js +1 -1
- package/examples/tutorials/README.md +1 -1
- package/examples/tutorials/callback_api/emit_log.js +1 -1
- package/examples/tutorials/callback_api/emit_log_direct.js +1 -1
- package/examples/tutorials/callback_api/emit_log_topic.js +1 -1
- package/examples/tutorials/callback_api/new_task.js +1 -1
- package/examples/tutorials/callback_api/rpc_client.js +1 -1
- package/examples/tutorials/callback_api/rpc_server.js +1 -1
- package/examples/tutorials/callback_api/send.js +1 -1
- package/examples/tutorials/emit_log.js +4 -5
- package/examples/tutorials/emit_log_direct.js +4 -5
- package/examples/tutorials/emit_log_topic.js +4 -5
- package/examples/tutorials/new_task.js +5 -6
- 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 +2 -2
- package/examples/tutorials/send.js +4 -5
- package/examples/tutorials/worker.js +1 -1
- package/examples/waitForConfirms.js +2 -2
- package/lib/api_args.js +2 -0
- package/lib/callback_model.js +11 -10
- package/lib/channel.js +24 -12
- package/lib/channel_model.js +57 -65
- package/lib/codec.js +64 -38
- package/lib/connect.js +26 -15
- package/lib/connection.js +6 -4
- package/lib/credentials.js +20 -3
- package/lib/defs.js +203 -75
- package/lib/frame.js +2 -1
- package/lib/heartbeat.js +1 -1
- package/package.json +10 -8
- package/test/callback_api.js +6 -5
- package/test/channel.js +125 -107
- package/test/channel_api.js +125 -136
- package/test/codec.js +48 -9
- package/test/connect.js +88 -3
- package/test/connection.js +57 -56
- package/test/data.js +32 -9
- package/test/frame.js +4 -4
- package/test/mux.js +10 -6
- package/test/util.js +35 -21
- package/.npmignore +0 -9
package/test/channel_api.js
CHANGED
|
@@ -6,8 +6,8 @@ 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
|
|
9
|
+
var Promise = require('bluebird');
|
|
10
|
+
var Buffer = require('safe-buffer').Buffer;
|
|
11
11
|
|
|
12
12
|
var URL = process.env.URL || 'amqp://localhost';
|
|
13
13
|
|
|
@@ -17,27 +17,14 @@ function connect() {
|
|
|
17
17
|
|
|
18
18
|
// Expect this promise to fail, and flip the results accordingly.
|
|
19
19
|
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
|
-
});
|
|
20
|
+
return new Promise(function(resolve, reject) {
|
|
21
|
+
return promise.then(reject).catch(resolve);
|
|
22
|
+
});
|
|
36
23
|
}
|
|
37
24
|
|
|
38
25
|
// I'll rely on operations being rejected, rather than the channel
|
|
39
26
|
// close error, to detect failure.
|
|
40
|
-
function ignore() {}
|
|
27
|
+
function ignore () {}
|
|
41
28
|
function ignoreErrors(c) {
|
|
42
29
|
c.on('error', ignore); return c;
|
|
43
30
|
}
|
|
@@ -54,7 +41,7 @@ function channel_test(chmethod, name, chfun) {
|
|
|
54
41
|
c[chmethod]().then(ignoreErrors).then(chfun)
|
|
55
42
|
.then(succeed(done), fail(done))
|
|
56
43
|
// close the connection regardless of what happens with the test
|
|
57
|
-
.
|
|
44
|
+
.finally(function() {c.close();});
|
|
58
45
|
});
|
|
59
46
|
});
|
|
60
47
|
}
|
|
@@ -124,15 +111,15 @@ chtest("fail on reasserting exchange with different type",
|
|
|
124
111
|
});
|
|
125
112
|
|
|
126
113
|
chtest("channel break on publishing to non-exchange", function(ch) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
114
|
+
return new Promise(function(resolve) {
|
|
115
|
+
ch.on('error', resolve);
|
|
116
|
+
ch.publish(randomString(), '', Buffer.from('foobar'));
|
|
117
|
+
});
|
|
131
118
|
});
|
|
132
119
|
|
|
133
120
|
chtest("delete queue", function(ch) {
|
|
134
121
|
var q = 'test.delete-queue';
|
|
135
|
-
return
|
|
122
|
+
return Promise.join(
|
|
136
123
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
137
124
|
ch.checkQueue(q))
|
|
138
125
|
.then(function() {
|
|
@@ -143,7 +130,7 @@ chtest("delete queue", function(ch) {
|
|
|
143
130
|
|
|
144
131
|
chtest("delete exchange", function(ch) {
|
|
145
132
|
var ex = 'test.delete-exchange';
|
|
146
|
-
return
|
|
133
|
+
return Promise.join(
|
|
147
134
|
ch.assertExchange(ex, 'fanout', EX_OPTS),
|
|
148
135
|
ch.checkExchange(ex))
|
|
149
136
|
.then(function() {
|
|
@@ -157,23 +144,23 @@ chtest("delete exchange", function(ch) {
|
|
|
157
144
|
// Wait for the queue to meet the condition; useful for waiting for
|
|
158
145
|
// messages to arrive, for example.
|
|
159
146
|
function waitForQueue(q, condition) {
|
|
160
|
-
|
|
161
|
-
connect(URL).then(function(c) {
|
|
147
|
+
return connect(URL).then(function(c) {
|
|
162
148
|
return c.createChannel()
|
|
163
149
|
.then(function(ch) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
150
|
+
return ch.checkQueue(q).then(function(qok) {
|
|
151
|
+
function check() {
|
|
152
|
+
return ch.checkQueue(q).then(function(qok) {
|
|
153
|
+
if (condition(qok)) {
|
|
154
|
+
c.close();
|
|
155
|
+
return qok;
|
|
156
|
+
}
|
|
157
|
+
else schedule(check);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return check();
|
|
161
|
+
});
|
|
174
162
|
});
|
|
175
163
|
});
|
|
176
|
-
return ready.promise;
|
|
177
164
|
}
|
|
178
165
|
|
|
179
166
|
// Return a promise that resolves when the queue has at least `num`
|
|
@@ -191,14 +178,14 @@ suite("sendMessage", function() {
|
|
|
191
178
|
chtest("send to queue and get from queue", function(ch) {
|
|
192
179
|
var q = 'test.send-to-q';
|
|
193
180
|
var msg = randomString();
|
|
194
|
-
return
|
|
195
|
-
ch.assertQueue(q, QUEUE_OPTS),
|
|
196
|
-
ch.purgeQueue(q))
|
|
181
|
+
return Promise.join(ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q))
|
|
197
182
|
.then(function() {
|
|
198
|
-
ch.sendToQueue(q,
|
|
199
|
-
return waitForMessages(q);
|
|
183
|
+
ch.sendToQueue(q, Buffer.from(msg));
|
|
184
|
+
return waitForMessages(q);
|
|
185
|
+
})
|
|
200
186
|
.then(function() {
|
|
201
|
-
return ch.get(q, {noAck: true});
|
|
187
|
+
return ch.get(q, {noAck: true});
|
|
188
|
+
})
|
|
202
189
|
.then(function(m) {
|
|
203
190
|
assert(m);
|
|
204
191
|
assert.equal(msg, m.content.toString());
|
|
@@ -207,8 +194,8 @@ chtest("send to queue and get from queue", function(ch) {
|
|
|
207
194
|
|
|
208
195
|
chtest("send (and get) zero content to queue", function(ch) {
|
|
209
196
|
var q = 'test.send-to-q';
|
|
210
|
-
var msg =
|
|
211
|
-
return
|
|
197
|
+
var msg = Buffer.alloc(0);
|
|
198
|
+
return Promise.join(
|
|
212
199
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
213
200
|
ch.purgeQueue(q))
|
|
214
201
|
.then(function() {
|
|
@@ -232,13 +219,13 @@ chtest("route message", function(ch) {
|
|
|
232
219
|
var q = 'test.route-message-q';
|
|
233
220
|
var msg = randomString();
|
|
234
221
|
|
|
235
|
-
return
|
|
222
|
+
return Promise.join(
|
|
236
223
|
ch.assertExchange(ex, 'fanout', EX_OPTS),
|
|
237
224
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
238
225
|
ch.purgeQueue(q),
|
|
239
226
|
ch.bindQueue(q, ex, '', {}))
|
|
240
227
|
.then(function() {
|
|
241
|
-
ch.publish(ex, '',
|
|
228
|
+
ch.publish(ex, '', Buffer.from(msg));
|
|
242
229
|
return waitForMessages(q);})
|
|
243
230
|
.then(function() {
|
|
244
231
|
return ch.get(q, {noAck: true});})
|
|
@@ -253,7 +240,7 @@ chtest("purge queue", function(ch) {
|
|
|
253
240
|
var q = 'test.purge-queue';
|
|
254
241
|
return ch.assertQueue(q, {durable: false})
|
|
255
242
|
.then(function() {
|
|
256
|
-
ch.sendToQueue(q,
|
|
243
|
+
ch.sendToQueue(q, Buffer.from('foobar'));
|
|
257
244
|
return waitForMessages(q);})
|
|
258
245
|
.then(function() {
|
|
259
246
|
ch.purgeQueue(q);
|
|
@@ -270,13 +257,13 @@ chtest("unbind queue", function(ch) {
|
|
|
270
257
|
var viabinding = randomString();
|
|
271
258
|
var direct = randomString();
|
|
272
259
|
|
|
273
|
-
return
|
|
260
|
+
return Promise.join(
|
|
274
261
|
ch.assertExchange(ex, 'fanout', EX_OPTS),
|
|
275
262
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
276
263
|
ch.purgeQueue(q),
|
|
277
264
|
ch.bindQueue(q, ex, '', {}))
|
|
278
265
|
.then(function() {
|
|
279
|
-
ch.publish(ex, '',
|
|
266
|
+
ch.publish(ex, '', Buffer.from('foobar'));
|
|
280
267
|
return waitForMessages(q);})
|
|
281
268
|
.then(function() { // message got through!
|
|
282
269
|
return ch.get(q, {noAck:true})
|
|
@@ -285,9 +272,9 @@ chtest("unbind queue", function(ch) {
|
|
|
285
272
|
return ch.unbindQueue(q, ex, '', {});})
|
|
286
273
|
.then(function() {
|
|
287
274
|
// via the no-longer-existing binding
|
|
288
|
-
ch.publish(ex, '',
|
|
275
|
+
ch.publish(ex, '', Buffer.from(viabinding));
|
|
289
276
|
// direct to the queue
|
|
290
|
-
ch.sendToQueue(q,
|
|
277
|
+
ch.sendToQueue(q, Buffer.from(direct));
|
|
291
278
|
return waitForMessages(q);})
|
|
292
279
|
.then(function() {return ch.get(q)})
|
|
293
280
|
.then(function(m) {
|
|
@@ -303,7 +290,7 @@ chtest("consume via exchange-exchange binding", function(ch) {
|
|
|
303
290
|
var ex1 = 'test.ex-ex-binding1', ex2 = 'test.ex-ex-binding2';
|
|
304
291
|
var q = 'test.ex-ex-binding-q';
|
|
305
292
|
var rk = 'test.routing.key', msg = randomString();
|
|
306
|
-
return
|
|
293
|
+
return Promise.join(
|
|
307
294
|
ch.assertExchange(ex1, 'direct', EX_OPTS),
|
|
308
295
|
ch.assertExchange(ex2, 'fanout',
|
|
309
296
|
{durable: false, internal: true}),
|
|
@@ -312,16 +299,16 @@ chtest("consume via exchange-exchange binding", function(ch) {
|
|
|
312
299
|
ch.bindExchange(ex2, ex1, rk, {}),
|
|
313
300
|
ch.bindQueue(q, ex2, '', {}))
|
|
314
301
|
.then(function() {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
302
|
+
return new Promise(function(resolve, reject) {
|
|
303
|
+
function delivery(m) {
|
|
304
|
+
if (m.content.toString() === msg) resolve();
|
|
305
|
+
else reject(new Error("Wrong message"));
|
|
306
|
+
}
|
|
307
|
+
ch.consume(q, delivery, {noAck: true})
|
|
308
|
+
.then(function() {
|
|
309
|
+
ch.publish(ex1, rk, Buffer.from(msg));
|
|
310
|
+
});
|
|
311
|
+
});
|
|
325
312
|
});
|
|
326
313
|
});
|
|
327
314
|
|
|
@@ -333,7 +320,7 @@ chtest("unbind exchange", function(ch) {
|
|
|
333
320
|
var viabinding = randomString();
|
|
334
321
|
var direct = randomString();
|
|
335
322
|
|
|
336
|
-
return
|
|
323
|
+
return Promise.join(
|
|
337
324
|
ch.assertExchange(source, 'fanout', EX_OPTS),
|
|
338
325
|
ch.assertExchange(dest, 'fanout', EX_OPTS),
|
|
339
326
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
@@ -341,7 +328,7 @@ chtest("unbind exchange", function(ch) {
|
|
|
341
328
|
ch.bindExchange(dest, source, '', {}),
|
|
342
329
|
ch.bindQueue(q, dest, '', {}))
|
|
343
330
|
.then(function() {
|
|
344
|
-
ch.publish(source, '',
|
|
331
|
+
ch.publish(source, '', Buffer.from('foobar'));
|
|
345
332
|
return waitForMessages(q);})
|
|
346
333
|
.then(function() { // message got through!
|
|
347
334
|
return ch.get(q, {noAck:true})
|
|
@@ -350,9 +337,9 @@ chtest("unbind exchange", function(ch) {
|
|
|
350
337
|
return ch.unbindExchange(dest, source, '', {});})
|
|
351
338
|
.then(function() {
|
|
352
339
|
// via the no-longer-existing binding
|
|
353
|
-
ch.publish(source, '',
|
|
340
|
+
ch.publish(source, '', Buffer.from(viabinding));
|
|
354
341
|
// direct to the queue
|
|
355
|
-
ch.sendToQueue(q,
|
|
342
|
+
ch.sendToQueue(q, Buffer.from(direct));
|
|
356
343
|
return waitForMessages(q);})
|
|
357
344
|
.then(function() {return ch.get(q)})
|
|
358
345
|
.then(function(m) {
|
|
@@ -365,58 +352,56 @@ chtest("unbind exchange", function(ch) {
|
|
|
365
352
|
// This is a bit convoluted. Sorry.
|
|
366
353
|
chtest("cancel consumer", function(ch) {
|
|
367
354
|
var q = 'test.consumer-cancel';
|
|
368
|
-
var recv1 = defer();
|
|
369
355
|
var ctag;
|
|
370
|
-
|
|
371
|
-
|
|
356
|
+
var recv1 = new Promise(function (resolve, reject) {
|
|
357
|
+
Promise.join(
|
|
372
358
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
373
359
|
ch.purgeQueue(q),
|
|
374
360
|
// My callback is 'resolve the promise in `arrived`'
|
|
375
|
-
ch.consume(q,
|
|
361
|
+
ch.consume(q, resolve, {noAck:true})
|
|
376
362
|
.then(function(ok) {
|
|
377
363
|
ctag = ok.consumerTag;
|
|
378
|
-
ch.sendToQueue(q,
|
|
364
|
+
ch.sendToQueue(q, Buffer.from('foo'));
|
|
379
365
|
}));
|
|
366
|
+
});
|
|
367
|
+
|
|
380
368
|
// A message should arrive because of the consume
|
|
381
|
-
return recv1.
|
|
382
|
-
|
|
383
|
-
recv1 = defer();
|
|
384
|
-
|
|
385
|
-
return doAll(
|
|
369
|
+
return recv1.then(function() {
|
|
370
|
+
var recv2 = Promise.join(
|
|
386
371
|
ch.cancel(ctag).then(function() {
|
|
387
|
-
ch.sendToQueue(q,
|
|
372
|
+
return ch.sendToQueue(q, Buffer.from('bar'));
|
|
388
373
|
}),
|
|
389
374
|
// but check a message did arrive in the queue
|
|
390
375
|
waitForMessages(q))
|
|
391
376
|
.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
|
|
377
|
+
return ch.get(q, {noAck:true});
|
|
378
|
+
})
|
|
379
|
+
.then(function(m) {
|
|
380
|
+
// I'm going to reject it, because I flip succeed/fail
|
|
381
|
+
// just below
|
|
382
|
+
if (m.content.toString() === 'bar') {
|
|
383
|
+
throw new Error();
|
|
384
|
+
}
|
|
401
385
|
});
|
|
386
|
+
|
|
387
|
+
return expectFail(recv2);
|
|
402
388
|
});
|
|
403
389
|
});
|
|
404
390
|
|
|
405
391
|
chtest("cancelled consumer", function(ch) {
|
|
406
392
|
var q = 'test.cancelled-consumer';
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
return nullRecv.promise;
|
|
393
|
+
return new Promise(function(resolve, reject) {
|
|
394
|
+
return Promise.join(
|
|
395
|
+
ch.assertQueue(q),
|
|
396
|
+
ch.purgeQueue(q),
|
|
397
|
+
ch.consume(q, function(msg) {
|
|
398
|
+
if (msg === null) resolve();
|
|
399
|
+
else reject(new Error('Message not expected'));
|
|
400
|
+
}))
|
|
401
|
+
.then(function() {
|
|
402
|
+
return ch.deleteQueue(q);
|
|
403
|
+
});
|
|
404
|
+
});
|
|
420
405
|
});
|
|
421
406
|
|
|
422
407
|
// ack, by default, removes a single message from the queue
|
|
@@ -424,12 +409,12 @@ chtest("ack", function(ch) {
|
|
|
424
409
|
var q = 'test.ack';
|
|
425
410
|
var msg1 = randomString(), msg2 = randomString();
|
|
426
411
|
|
|
427
|
-
return
|
|
412
|
+
return Promise.join(
|
|
428
413
|
ch.assertQueue(q, QUEUE_OPTS),
|
|
429
414
|
ch.purgeQueue(q))
|
|
430
415
|
.then(function() {
|
|
431
|
-
ch.sendToQueue(q,
|
|
432
|
-
ch.sendToQueue(q,
|
|
416
|
+
ch.sendToQueue(q, Buffer.from(msg1));
|
|
417
|
+
ch.sendToQueue(q, Buffer.from(msg2));
|
|
433
418
|
return waitForMessages(q, 2);
|
|
434
419
|
})
|
|
435
420
|
.then(function() {
|
|
@@ -454,10 +439,10 @@ chtest("nack", function(ch) {
|
|
|
454
439
|
var q = 'test.nack';
|
|
455
440
|
var msg1 = randomString();
|
|
456
441
|
|
|
457
|
-
return
|
|
442
|
+
return Promise.join(
|
|
458
443
|
ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q))
|
|
459
444
|
.then(function() {
|
|
460
|
-
ch.sendToQueue(q,
|
|
445
|
+
ch.sendToQueue(q, Buffer.from(msg1));
|
|
461
446
|
return waitForMessages(q);})
|
|
462
447
|
.then(function() {
|
|
463
448
|
return ch.get(q, {noAck: false})})
|
|
@@ -479,10 +464,10 @@ chtest("reject", function(ch) {
|
|
|
479
464
|
var q = 'test.reject';
|
|
480
465
|
var msg1 = randomString();
|
|
481
466
|
|
|
482
|
-
return
|
|
467
|
+
return Promise.join(
|
|
483
468
|
ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q))
|
|
484
469
|
.then(function() {
|
|
485
|
-
ch.sendToQueue(q,
|
|
470
|
+
ch.sendToQueue(q, Buffer.from(msg1));
|
|
486
471
|
return waitForMessages(q);})
|
|
487
472
|
.then(function() {
|
|
488
473
|
return ch.get(q, {noAck: false})})
|
|
@@ -500,30 +485,37 @@ chtest("reject", function(ch) {
|
|
|
500
485
|
|
|
501
486
|
chtest("prefetch", function(ch) {
|
|
502
487
|
var q = 'test.prefetch';
|
|
503
|
-
return
|
|
488
|
+
return Promise.join(
|
|
504
489
|
ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q),
|
|
505
490
|
ch.prefetch(1))
|
|
506
491
|
.then(function() {
|
|
507
|
-
ch.sendToQueue(q,
|
|
508
|
-
ch.sendToQueue(q,
|
|
492
|
+
ch.sendToQueue(q, Buffer.from('foobar'));
|
|
493
|
+
ch.sendToQueue(q, Buffer.from('foobar'));
|
|
509
494
|
return waitForMessages(q, 2);
|
|
510
495
|
})
|
|
511
496
|
.then(function() {
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
497
|
+
return new Promise(function(resolve) {
|
|
498
|
+
var messageCount = 0;
|
|
499
|
+
function receive(msg) {
|
|
500
|
+
ch.ack(msg);
|
|
501
|
+
if (++messageCount > 1) {
|
|
502
|
+
resolve(messageCount);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return ch.consume(q, receive, {noAck: false})
|
|
506
|
+
});
|
|
507
|
+
})
|
|
508
|
+
.then(function(c) {
|
|
509
|
+
return assert.equal(2, c);
|
|
524
510
|
});
|
|
525
511
|
});
|
|
526
512
|
|
|
513
|
+
chtest('close', function(ch) {
|
|
514
|
+
// Resolving promise guarantees
|
|
515
|
+
// channel is closed
|
|
516
|
+
return ch.close();
|
|
517
|
+
});
|
|
518
|
+
|
|
527
519
|
});
|
|
528
520
|
|
|
529
521
|
var confirmtest = channel_test.bind(null, 'createConfirmChannel');
|
|
@@ -532,10 +524,10 @@ suite("confirms", function() {
|
|
|
532
524
|
|
|
533
525
|
confirmtest('message is confirmed', function(ch) {
|
|
534
526
|
var q = 'test.confirm-message';
|
|
535
|
-
return
|
|
527
|
+
return Promise.join(
|
|
536
528
|
ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q))
|
|
537
529
|
.then(function() {
|
|
538
|
-
return ch.sendToQueue(q,
|
|
530
|
+
return ch.sendToQueue(q, Buffer.from('bleep'));
|
|
539
531
|
});
|
|
540
532
|
});
|
|
541
533
|
|
|
@@ -546,7 +538,7 @@ confirmtest('message is confirmed', function(ch) {
|
|
|
546
538
|
// multi-ack.
|
|
547
539
|
confirmtest('multiple confirms', function(ch) {
|
|
548
540
|
var q = 'test.multiple-confirms';
|
|
549
|
-
return
|
|
541
|
+
return Promise.join(
|
|
550
542
|
ch.assertQueue(q, QUEUE_OPTS), ch.purgeQueue(q))
|
|
551
543
|
.then(function() {
|
|
552
544
|
var multipleRainbows = false;
|
|
@@ -558,18 +550,15 @@ confirmtest('multiple confirms', function(ch) {
|
|
|
558
550
|
var cs = [];
|
|
559
551
|
|
|
560
552
|
function sendAndPushPromise() {
|
|
561
|
-
var conf =
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
else conf.resolve();
|
|
566
|
-
});
|
|
567
|
-
cs.push(conf.promise);
|
|
553
|
+
var conf = Promise.fromCallback(function(cb) {
|
|
554
|
+
return ch.sendToQueue(q, Buffer.from('bleep'), {}, cb);
|
|
555
|
+
});
|
|
556
|
+
cs.push(conf);
|
|
568
557
|
}
|
|
569
558
|
|
|
570
559
|
for (var i=0; i < num; i++) sendAndPushPromise();
|
|
571
560
|
|
|
572
|
-
return
|
|
561
|
+
return Promise.all(cs).then(function() {
|
|
573
562
|
if (multipleRainbows) return true;
|
|
574
563
|
else if (num > 500) throw new Error(
|
|
575
564
|
"Couldn't provoke the server" +
|
|
@@ -587,7 +576,7 @@ confirmtest('multiple confirms', function(ch) {
|
|
|
587
576
|
|
|
588
577
|
confirmtest('wait for confirms', function(ch) {
|
|
589
578
|
for (var i=0; i < 1000; i++) {
|
|
590
|
-
ch.publish('', '',
|
|
579
|
+
ch.publish('', '', Buffer.from('foobar'), {});
|
|
591
580
|
}
|
|
592
581
|
return ch.waitForConfirms();
|
|
593
582
|
})
|
package/test/codec.js
CHANGED
|
@@ -4,7 +4,7 @@ var codec = require('../lib/codec');
|
|
|
4
4
|
var defs = require('../lib/defs');
|
|
5
5
|
var assert = require('assert');
|
|
6
6
|
var ints = require('buffer-more-ints');
|
|
7
|
-
|
|
7
|
+
var Buffer = require('safe-buffer').Buffer
|
|
8
8
|
var C = require('claire');
|
|
9
9
|
var forAll = C.forAll;
|
|
10
10
|
|
|
@@ -31,12 +31,12 @@ var testCases = [
|
|
|
31
31
|
['float value', {double: 0.5}, [6,100,111,117,98,108,101,100,63,224,0,0,0,0,0,0]],
|
|
32
32
|
['negative float value', {double: -0.5}, [6,100,111,117,98,108,101,100,191,224,0,0,0,0,0,0]],
|
|
33
33
|
// %% test some boundaries of precision?
|
|
34
|
-
|
|
34
|
+
|
|
35
35
|
// string
|
|
36
36
|
['string', {string: "boop"}, [6,115,116,114,105,110,103,83,0,0,0,4,98,111,111,112]],
|
|
37
37
|
|
|
38
38
|
// buffer -> byte array
|
|
39
|
-
['byte array from buffer', {bytes:
|
|
39
|
+
['byte array from buffer', {bytes: Buffer.from([1,2,3,4])},
|
|
40
40
|
[5,98,121,116,101,115,120,0,0,0,4,1,2,3,4]],
|
|
41
41
|
|
|
42
42
|
// boolean, void
|
|
@@ -64,12 +64,12 @@ function bufferToArray(b) {
|
|
|
64
64
|
return Array.prototype.slice.call(b);
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
suite("
|
|
67
|
+
suite("Implicit encodings", function() {
|
|
68
68
|
|
|
69
69
|
testCases.forEach(function(tc) {
|
|
70
70
|
var name = tc[0], val = tc[1], expect = tc[2];
|
|
71
71
|
test(name, function() {
|
|
72
|
-
var buffer =
|
|
72
|
+
var buffer = Buffer.alloc(1000);
|
|
73
73
|
var size = codec.encodeTable(buffer, val, 0);
|
|
74
74
|
var result = buffer.slice(4, size);
|
|
75
75
|
assert.deepEqual(expect, bufferToArray(result));
|
|
@@ -82,11 +82,11 @@ suite("Explicit encodings", function() {
|
|
|
82
82
|
var amqp = require('./data');
|
|
83
83
|
|
|
84
84
|
function roundtrip_table(t) {
|
|
85
|
-
var buf =
|
|
85
|
+
var buf = Buffer.alloc(4096);
|
|
86
86
|
var size = codec.encodeTable(buf, t, 0);
|
|
87
87
|
var decoded = codec.decodeFields(buf.slice(4, size)); // ignore the length-prefix
|
|
88
88
|
try {
|
|
89
|
-
assert.deepEqual(t, decoded);
|
|
89
|
+
assert.deepEqual(removeExplicitTypes(t), decoded);
|
|
90
90
|
}
|
|
91
91
|
catch (e) { return false; }
|
|
92
92
|
return true;
|
|
@@ -119,6 +119,45 @@ suite("Roundtrip values", function() {
|
|
|
119
119
|
});
|
|
120
120
|
});
|
|
121
121
|
|
|
122
|
+
// When encoding, you can supply explicitly-typed fields like `{'!':
|
|
123
|
+
// int32, 50}`. Most of these do not appear in the decoded values, so
|
|
124
|
+
// to compare like-to-like we have to remove them from the input.
|
|
125
|
+
function removeExplicitTypes(input) {
|
|
126
|
+
switch (typeof input) {
|
|
127
|
+
case 'object':
|
|
128
|
+
if (input == null) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
if (Array.isArray(input)) {
|
|
132
|
+
var newArr = [];
|
|
133
|
+
for (var i = 0; i < input.length; i++) {
|
|
134
|
+
newArr[i] = removeExplicitTypes(input[i]);
|
|
135
|
+
}
|
|
136
|
+
return newArr;
|
|
137
|
+
}
|
|
138
|
+
if (Buffer.isBuffer(input)) {
|
|
139
|
+
return input;
|
|
140
|
+
}
|
|
141
|
+
switch (input['!']) {
|
|
142
|
+
case 'timestamp':
|
|
143
|
+
case 'decimal':
|
|
144
|
+
case 'float':
|
|
145
|
+
return input;
|
|
146
|
+
case undefined:
|
|
147
|
+
var newObj = {}
|
|
148
|
+
for (var k in input) {
|
|
149
|
+
newObj[k] = removeExplicitTypes(input[k]);
|
|
150
|
+
}
|
|
151
|
+
return newObj;
|
|
152
|
+
default:
|
|
153
|
+
return input.value;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
default:
|
|
157
|
+
return input;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
122
161
|
// Asserts that the decoded fields are equal to the original fields,
|
|
123
162
|
// or equal to a default where absent in the original. The defaults
|
|
124
163
|
// depend on the type of method or properties.
|
|
@@ -141,11 +180,11 @@ function assertEqualModuloDefaults(original, decodedFields) {
|
|
|
141
180
|
// given as strings rather than buffers, but the decoded values
|
|
142
181
|
// will be buffers.
|
|
143
182
|
assert.deepEqual((arg.type === 'longstr') ?
|
|
144
|
-
|
|
183
|
+
Buffer.from(arg.default) : arg.default,
|
|
145
184
|
decodedValue);
|
|
146
185
|
}
|
|
147
186
|
else {
|
|
148
|
-
assert.deepEqual(originalValue, decodedValue);
|
|
187
|
+
assert.deepEqual(removeExplicitTypes(originalValue), decodedValue);
|
|
149
188
|
}
|
|
150
189
|
}
|
|
151
190
|
catch (assertionErr) {
|