amqplib 0.10.8 → 1.0.0

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.
Files changed (85) hide show
  1. package/.claude/commands/lint-disable.md +9 -0
  2. package/.claude/commands/lint-enable.md +12 -0
  3. package/.claude/settings.local.json +16 -0
  4. package/.github/workflows/publish.yml +1 -1
  5. package/.github/workflows/test.yml +1 -1
  6. package/CHANGELOG.md +34 -0
  7. package/CLAUDE.md +12 -0
  8. package/Makefile +5 -12
  9. package/bin/generate-defs.js +382 -369
  10. package/biome.json +51 -0
  11. package/callback_api.js +12 -8
  12. package/channel_api.js +5 -10
  13. package/examples/direct_reply_to_client.js +9 -5
  14. package/examples/direct_reply_to_server.js +8 -6
  15. package/examples/headers.js +13 -10
  16. package/examples/receive_generator.js +13 -20
  17. package/examples/send_generators.js +2 -9
  18. package/examples/ssl.js +2 -3
  19. package/examples/stream_queues/receive_stream.js +39 -36
  20. package/examples/stream_queues/send_stream.js +26 -29
  21. package/examples/tutorials/callback_api/emit_log.js +4 -3
  22. package/examples/tutorials/callback_api/emit_log_direct.js +5 -4
  23. package/examples/tutorials/callback_api/emit_log_topic.js +5 -4
  24. package/examples/tutorials/callback_api/new_task.js +5 -4
  25. package/examples/tutorials/callback_api/receive.js +16 -10
  26. package/examples/tutorials/callback_api/receive_logs.js +17 -11
  27. package/examples/tutorials/callback_api/receive_logs_direct.js +19 -13
  28. package/examples/tutorials/callback_api/receive_logs_topic.js +19 -14
  29. package/examples/tutorials/callback_api/rpc_client.js +20 -16
  30. package/examples/tutorials/callback_api/rpc_server.js +27 -21
  31. package/examples/tutorials/callback_api/send.js +4 -3
  32. package/examples/tutorials/callback_api/worker.js +18 -13
  33. package/examples/tutorials/emit_log.js +4 -6
  34. package/examples/tutorials/emit_log_direct.js +5 -7
  35. package/examples/tutorials/emit_log_topic.js +5 -7
  36. package/examples/tutorials/new_task.js +4 -6
  37. package/examples/tutorials/receive.js +8 -4
  38. package/examples/tutorials/receive_logs.js +10 -6
  39. package/examples/tutorials/receive_logs_direct.js +16 -10
  40. package/examples/tutorials/receive_logs_topic.js +16 -11
  41. package/examples/tutorials/rpc_client.js +17 -15
  42. package/examples/tutorials/rpc_server.js +9 -8
  43. package/examples/tutorials/send.js +4 -6
  44. package/examples/tutorials/worker.js +17 -14
  45. package/examples/waitForConfirms.js +2 -2
  46. package/lefthook.yml +14 -0
  47. package/lib/api_args.js +124 -149
  48. package/lib/bitset.js +28 -18
  49. package/lib/callback_model.js +134 -190
  50. package/lib/channel.js +175 -213
  51. package/lib/channel_model.js +41 -85
  52. package/lib/codec.js +257 -228
  53. package/lib/connect.js +74 -82
  54. package/lib/connection.js +204 -240
  55. package/lib/credentials.js +21 -29
  56. package/lib/defs.js +2559 -1927
  57. package/lib/error.js +5 -6
  58. package/lib/format.js +15 -33
  59. package/lib/frame.js +66 -66
  60. package/lib/heartbeat.js +17 -24
  61. package/lib/mux.js +32 -41
  62. package/package.json +8 -5
  63. package/test/bitset.test.js +65 -0
  64. package/test/callback_api.test.js +438 -0
  65. package/test/channel.test.js +592 -0
  66. package/test/channel_api.test.js +537 -0
  67. package/test/codec.test.js +172 -0
  68. package/test/connect.test.js +196 -0
  69. package/test/connection.test.js +330 -0
  70. package/test/data.test.js +33 -0
  71. package/test/frame.test.js +180 -0
  72. package/test/lib/data.js +249 -0
  73. package/test/lib/util.js +223 -0
  74. package/test/mux.test.js +215 -0
  75. package/test/bitset.js +0 -69
  76. package/test/callback_api.js +0 -379
  77. package/test/channel.js +0 -621
  78. package/test/channel_api.js +0 -606
  79. package/test/codec.js +0 -237
  80. package/test/connect.js +0 -197
  81. package/test/connection.js +0 -390
  82. package/test/data.js +0 -269
  83. package/test/frame.js +0 -191
  84. package/test/mux.js +0 -203
  85. package/test/util.js +0 -217
@@ -0,0 +1,537 @@
1
+ const { describe, it } = require('node:test');
2
+ const assert = require('node:assert');
3
+ const api = require('../channel_api');
4
+ const util = require('./lib/util');
5
+ const schedule = util.schedule;
6
+ const randomString = util.randomString;
7
+ const promisify = require('node:util').promisify;
8
+
9
+ const URL = process.env.URL || 'amqp://localhost';
10
+ const QUEUE_OPTS = { durable: false };
11
+ const EX_OPTS = { durable: false };
12
+
13
+ function ignore() {}
14
+
15
+ describe('connect', () => {
16
+ it('at all', () => connect().then((c) => c.close()));
17
+
18
+ it('create channel', () => withChannel(ignore));
19
+ });
20
+
21
+ describe('updateSecret', () => {
22
+ it('updateSecret', () => {
23
+ return connect().then((c) =>
24
+ c.updateSecret(Buffer.from('new secret'), 'no reason')
25
+ .finally(() => c.close())
26
+ );
27
+ });
28
+ });
29
+
30
+ describe('assert, check, delete', () => {
31
+ it('assert and check queue', () => {
32
+ return withChannel((ch) => {
33
+ return ch.assertQueue('test.check-queue', QUEUE_OPTS)
34
+ .then((qok) => assert.strictEqual(qok.queue, 'test.check-queue'))
35
+ .then(() => ch.checkQueue('test.check-queue'))
36
+ .then((qok) => assert.strictEqual(qok.queue, 'test.check-queue'))
37
+ });
38
+ });
39
+
40
+ it('assert and check exchange', () => {
41
+ return withChannel((ch) => {
42
+ return ch.assertExchange('test.check-exchange', 'direct', EX_OPTS)
43
+ .then((eok) => assert.strictEqual(eok.exchange, 'test.check-exchange'))
44
+ .then(() => ch.checkExchange('test.check-exchange'))
45
+ .then((eok) => assert.ok(eok));
46
+ });
47
+ });
48
+
49
+ it('fail on reasserting queue with different options', () => {
50
+ return withChannel((ch) => {
51
+ ch.once('error', ignore);
52
+ return ch.assertQueue('test.reassert-queue', { durable: false, autoDelete: true })
53
+ .then(() => assert.rejects(() => ch.assertQueue('test.reassert-queue', { durable: false, autoDelete: false }), (err) => {
54
+ assert.match(err.message, /PRECONDITION_FAILED/);
55
+ assert.strictEqual(406, err.code);
56
+ assert.strictEqual(50, err.classId);
57
+ assert.strictEqual(10, err.methodId);
58
+ return true;
59
+ }));
60
+ });
61
+ });
62
+
63
+ it("fail on checking a queue that's not there", () => {
64
+ return withChannel((ch) => {
65
+ ch.once('error', ignore);
66
+ return assert.rejects(() => ch.checkQueue(`test.random-${randomString()}`), (err) => {
67
+ assert.match(err.message, /NOT_FOUND/);
68
+ assert.strictEqual(404, err.code);
69
+ assert.strictEqual(50, err.classId);
70
+ assert.strictEqual(10, err.methodId);
71
+ return true;
72
+ });
73
+ });
74
+ });
75
+
76
+ it("fail on checking an exchange that's not there", () => {
77
+ return withChannel((ch) => {
78
+ ch.once('error', ignore);
79
+ return assert.rejects(() => ch.checkExchange(`test.random-${randomString()}`), (err) => {
80
+ assert.match(err.message, /NOT_FOUND/);
81
+ assert.strictEqual(404, err.code);
82
+ return true;
83
+ });
84
+ });
85
+ });
86
+
87
+ it('fail on reasserting exchange with different type', () => {
88
+ return withChannel((ch) => {
89
+ ch.once('error', ignore);
90
+ const ex = 'test.reassert-ex';
91
+ return ch.assertExchange(ex, 'fanout', EX_OPTS)
92
+ .then(() => assert.rejects(() => ch.assertExchange(ex, 'direct', EX_OPTS), (err) => {
93
+ assert.match(err.message, /PRECONDITION_FAILED/);
94
+ assert.strictEqual(406, err.code);
95
+ return true;
96
+ }));
97
+ });
98
+ });
99
+
100
+ it('channel break on publishing to non-exchange', () => {
101
+ return withChannel((ch) =>
102
+ new Promise((resolve) => {
103
+ ch.once('error', (err) => {
104
+ assert.match(err.message, /NOT_FOUND/);
105
+ assert.strictEqual(404, err.code);
106
+ resolve();
107
+ });
108
+ ch.publish(randomString(), '', Buffer.from('foobar'));
109
+ })
110
+ );
111
+ });
112
+
113
+ it('delete queue', () => {
114
+ return withChannel((ch) => {
115
+ return ch.assertQueue('test.delete-queue', QUEUE_OPTS)
116
+ .then(() => ch.deleteQueue('test.delete-queue'))
117
+ .then(() => {
118
+ ch.once('error', ignore);
119
+ return assert.rejects(() => ch.checkQueue('test.delete-queue'), (err) => {
120
+ assert.match(err.message, /NOT_FOUND/);
121
+ assert.strictEqual(404, err.code);
122
+ return true;
123
+ });
124
+ });
125
+ });
126
+ });
127
+
128
+ it('delete exchange', () => {
129
+ return withChannel((ch) => {
130
+ return ch.assertExchange('test.delete-exchange', 'fanout', EX_OPTS)
131
+ .then(() => ch.deleteExchange('test.delete-exchange'))
132
+ .then(() => {
133
+ ch.once('error', ignore);
134
+ return assert.rejects(() => ch.checkExchange('test.delete-exchange'), (err) => {
135
+ assert.match(err.message, /NOT_FOUND/);
136
+ assert.strictEqual(404, err.code);
137
+ return true;
138
+ });
139
+ });
140
+ });
141
+ });
142
+ });
143
+
144
+ describe('sendMessage', () => {
145
+ it('send to queue and get from queue', () => {
146
+ const msg = randomString();
147
+ return withChannel((ch) =>
148
+ ch.assertQueue('test.send-to-q', QUEUE_OPTS)
149
+ .then(() => ch.purgeQueue('test.send-to-q'))
150
+ .then(() => {
151
+ ch.sendToQueue('test.send-to-q', Buffer.from(msg));
152
+ return waitForMessages('test.send-to-q');
153
+ })
154
+ .then(() => ch.get('test.send-to-q', { noAck: true }))
155
+ .then((m) => {
156
+ assert(m);
157
+ assert.equal(msg, m.content.toString());
158
+ })
159
+ );
160
+ });
161
+
162
+ it('send (and get) zero content to queue', () => {
163
+ return withChannel((ch) =>
164
+ ch.assertQueue('test.send-to-q', QUEUE_OPTS)
165
+ .then(() => ch.purgeQueue('test.send-to-q'))
166
+ .then(() => {
167
+ ch.sendToQueue('test.send-to-q', Buffer.alloc(0));
168
+ return waitForMessages('test.send-to-q');
169
+ })
170
+ .then(() => ch.get('test.send-to-q', { noAck: true }))
171
+ .then((m) => {
172
+ assert(m);
173
+ assert.deepEqual(Buffer.alloc(0), m.content);
174
+ })
175
+ );
176
+ });
177
+ });
178
+
179
+ describe('binding, consuming', () => {
180
+ // bind, publish, get
181
+ it('route message', () => {
182
+ const msg = randomString();
183
+ return withChannel((ch) =>
184
+ ch.assertExchange('test.route-message', 'fanout', EX_OPTS)
185
+ .then(() => ch.assertQueue('test.route-message-q', QUEUE_OPTS))
186
+ .then(() => ch.purgeQueue('test.route-message-q'))
187
+ .then(() => ch.bindQueue('test.route-message-q', 'test.route-message', '', {}))
188
+ .then(() => {
189
+ ch.publish('test.route-message', '', Buffer.from(msg));
190
+ return waitForMessages('test.route-message-q');
191
+ })
192
+ .then(() => ch.get('test.route-message-q', { noAck: true }))
193
+ .then((m) => {
194
+ assert(m);
195
+ assert.equal(msg, m.content.toString());
196
+ })
197
+ );
198
+ });
199
+
200
+ // send to queue, purge, get-empty
201
+ it('purge queue', () => {
202
+ return withChannel((ch) =>
203
+ ch.assertQueue('test.purge-queue', QUEUE_OPTS)
204
+ .then(() => {
205
+ ch.sendToQueue('test.purge-queue', Buffer.from('foobar'));
206
+ return waitForMessages('test.purge-queue');
207
+ })
208
+ .then(() => ch.purgeQueue('test.purge-queue'))
209
+ .then(() => ch.get('test.purge-queue', { noAck: true }))
210
+ .then((m) => {
211
+ assert(!m); // get-empty
212
+ })
213
+ );
214
+ });
215
+
216
+ // bind again, unbind, publish, get-empty
217
+ it('unbind queue', () => {
218
+ const viabinding = randomString();
219
+ const direct = randomString();
220
+ return withChannel((ch) =>
221
+ ch.assertExchange('test.unbind-queue-ex', 'fanout', EX_OPTS)
222
+ .then(() => ch.assertQueue('test.unbind-queue', QUEUE_OPTS))
223
+ .then(() => ch.purgeQueue('test.unbind-queue'))
224
+ .then(() => ch.bindQueue('test.unbind-queue', 'test.unbind-queue-ex', '', {}))
225
+ .then(() => {
226
+ ch.publish('test.unbind-queue-ex', '', Buffer.from('foobar'));
227
+ return waitForMessages('test.unbind-queue');
228
+ })
229
+ .then(() => ch.get('test.unbind-queue', { noAck: true }))
230
+ .then((m) => assert(m)) // message got through!
231
+ .then(() => ch.unbindQueue('test.unbind-queue', 'test.unbind-queue-ex', '', {}))
232
+ .then(() => {
233
+ // via the no-longer-existing binding
234
+ ch.publish('test.unbind-queue-ex', '', Buffer.from(viabinding));
235
+ // direct to the queue
236
+ ch.sendToQueue('test.unbind-queue', Buffer.from(direct));
237
+ return waitForMessages('test.unbind-queue');
238
+ })
239
+ .then(() => ch.get('test.unbind-queue'))
240
+ .then((m) => {
241
+ // the direct to queue message got through, the via-binding message (sent first) did not
242
+ assert.equal(direct, m.content.toString());
243
+ })
244
+ );
245
+ });
246
+
247
+ // To some extent this is now just testing semantics of the server,
248
+ // but we can at least try out a few settings, and consume.
249
+ it('consume via exchange-exchange binding', () => {
250
+ const msg = randomString();
251
+ return withChannel((ch) =>
252
+ ch.assertExchange('test.ex-ex-binding1', 'direct', EX_OPTS)
253
+ .then(() => ch.assertExchange('test.ex-ex-binding2', 'fanout', { durable: false, internal: true }))
254
+ .then(() => ch.assertQueue('test.ex-ex-binding-q', QUEUE_OPTS))
255
+ .then(() => ch.purgeQueue('test.ex-ex-binding-q'))
256
+ .then(() => ch.bindExchange('test.ex-ex-binding2', 'test.ex-ex-binding1', 'test.routing.key', {}))
257
+ .then(() => ch.bindQueue('test.ex-ex-binding-q', 'test.ex-ex-binding2', '', {}))
258
+ .then(() => new Promise((resolve, reject) => {
259
+ ch.consume('test.ex-ex-binding-q', (m) => {
260
+ if (m.content.toString() === msg) resolve();
261
+ else reject(new Error('Wrong message'));
262
+ }, { noAck: true }).then(() => {
263
+ ch.publish('test.ex-ex-binding1', 'test.routing.key', Buffer.from(msg));
264
+ });
265
+ }))
266
+ );
267
+ });
268
+
269
+ // bind again, unbind, publish, get-empty
270
+ it('unbind exchange', () => {
271
+ const viabinding = randomString();
272
+ const direct = randomString();
273
+ return withChannel((ch) =>
274
+ ch.assertExchange('test.unbind-ex-source', 'fanout', EX_OPTS)
275
+ .then(() => ch.assertExchange('test.unbind-ex-dest', 'fanout', EX_OPTS))
276
+ .then(() => ch.assertQueue('test.unbind-ex-queue', QUEUE_OPTS))
277
+ .then(() => ch.purgeQueue('test.unbind-ex-queue'))
278
+ .then(() => ch.bindExchange('test.unbind-ex-dest', 'test.unbind-ex-source', '', {}))
279
+ .then(() => ch.bindQueue('test.unbind-ex-queue', 'test.unbind-ex-dest', '', {}))
280
+ .then(() => {
281
+ ch.publish('test.unbind-ex-source', '', Buffer.from('foobar'));
282
+ return waitForMessages('test.unbind-ex-queue');
283
+ })
284
+ .then(() => ch.get('test.unbind-ex-queue', { noAck: true }))
285
+ .then((m) => assert(m)) // message got through!
286
+ .then(() => ch.unbindExchange('test.unbind-ex-dest', 'test.unbind-ex-source', '', {}))
287
+ .then(() => {
288
+ // via the no-longer-existing binding
289
+ ch.publish('test.unbind-ex-source', '', Buffer.from(viabinding));
290
+ // direct to the queue
291
+ ch.sendToQueue('test.unbind-ex-queue', Buffer.from(direct));
292
+ return waitForMessages('test.unbind-ex-queue');
293
+ })
294
+ .then(() => ch.get('test.unbind-ex-queue'))
295
+ .then((m) => {
296
+ // the direct to queue message got through, the via-binding message (sent first) did not
297
+ assert.equal(direct, m.content.toString());
298
+ })
299
+ );
300
+ });
301
+
302
+ it('cancel consumer', () => {
303
+ let ctag;
304
+ return withChannel((ch) =>
305
+ ch.assertQueue('test.consumer-cancel', QUEUE_OPTS)
306
+ .then(() => ch.purgeQueue('test.consumer-cancel'))
307
+ .then(() => new Promise((resolve) => {
308
+ ch.consume('test.consumer-cancel', resolve, { noAck: true })
309
+ .then((ok) => {
310
+ ctag = ok.consumerTag;
311
+ ch.sendToQueue('test.consumer-cancel', Buffer.from('foo'));
312
+ });
313
+ }))
314
+ // first message arrived via consumer
315
+ .then(() => ch.cancel(ctag))
316
+ .then(() => {
317
+ ch.sendToQueue('test.consumer-cancel', Buffer.from('bar'));
318
+ return waitForMessages('test.consumer-cancel');
319
+ })
320
+ .then(() => ch.get('test.consumer-cancel', { noAck: true }))
321
+ .then((m) => {
322
+ // message arrived in queue but NOT via the cancelled consumer
323
+ assert.equal('bar', m.content.toString());
324
+ })
325
+ );
326
+ });
327
+
328
+ it('cancelled consumer', () => {
329
+ return withChannel((ch) =>
330
+ ch.assertQueue('test.cancelled-consumer')
331
+ .then(() => ch.purgeQueue('test.cancelled-consumer'))
332
+ .then(() => new Promise((resolve, reject) => {
333
+ ch.consume('test.cancelled-consumer', (msg) => {
334
+ if (msg === null) resolve();
335
+ else reject(new Error('Message not expected'));
336
+ }).then(() => ch.deleteQueue('test.cancelled-consumer'));
337
+ }))
338
+ );
339
+ });
340
+
341
+ // ack, by default, removes a single message from the queue
342
+ it('ack', () => {
343
+ const msg1 = randomString();
344
+ const msg2 = randomString();
345
+ return withChannel((ch) =>
346
+ ch.assertQueue('test.ack', QUEUE_OPTS)
347
+ .then(() => ch.purgeQueue('test.ack'))
348
+ .then(() => {
349
+ ch.sendToQueue('test.ack', Buffer.from(msg1));
350
+ ch.sendToQueue('test.ack', Buffer.from(msg2));
351
+ return waitForMessages('test.ack', 2);
352
+ })
353
+ .then(() => ch.get('test.ack', { noAck: false }))
354
+ .then((m) => {
355
+ assert.equal(msg1, m.content.toString());
356
+ ch.ack(m);
357
+ // %%% is there a race here? may depend on rabbitmq-specific semantics
358
+ return ch.get('test.ack');
359
+ })
360
+ .then((m) => {
361
+ assert(m);
362
+ assert.equal(msg2, m.content.toString());
363
+ })
364
+ );
365
+ });
366
+
367
+ // Nack, by default, puts a message back on the queue (where in the queue is up to the server)
368
+ it('nack', () => {
369
+ const msg1 = randomString();
370
+ return withChannel((ch) =>
371
+ ch.assertQueue('test.nack', QUEUE_OPTS)
372
+ .then(() => ch.purgeQueue('test.nack'))
373
+ .then(() => {
374
+ ch.sendToQueue('test.nack', Buffer.from(msg1));
375
+ return waitForMessages('test.nack');
376
+ })
377
+ .then(() => ch.get('test.nack', { noAck: false }))
378
+ .then((m) => {
379
+ assert.equal(msg1, m.content.toString());
380
+ ch.nack(m);
381
+ return waitForMessages('test.nack');
382
+ })
383
+ .then(() => ch.get('test.nack'))
384
+ .then((m) => {
385
+ assert(m);
386
+ assert.equal(msg1, m.content.toString());
387
+ })
388
+ );
389
+ });
390
+
391
+ // reject is a near-synonym for nack, the latter of which is not available in earlier RabbitMQ (or in AMQP proper).
392
+ it('reject', () => {
393
+ const msg1 = randomString();
394
+ return withChannel((ch) =>
395
+ ch.assertQueue('test.reject', QUEUE_OPTS)
396
+ .then(() => ch.purgeQueue('test.reject'))
397
+ .then(() => {
398
+ ch.sendToQueue('test.reject', Buffer.from(msg1));
399
+ return waitForMessages('test.reject');
400
+ })
401
+ .then(() => ch.get('test.reject', { noAck: false }))
402
+ .then((m) => {
403
+ assert.equal(msg1, m.content.toString());
404
+ ch.reject(m);
405
+ return waitForMessages('test.reject');
406
+ })
407
+ .then(() => ch.get('test.reject'))
408
+ .then((m) => {
409
+ assert(m);
410
+ assert.equal(msg1, m.content.toString());
411
+ })
412
+ );
413
+ });
414
+
415
+ it('prefetch', () => {
416
+ return withChannel((ch) =>
417
+ ch.assertQueue('test.prefetch', QUEUE_OPTS)
418
+ .then(() => ch.purgeQueue('test.prefetch'))
419
+ .then(() => ch.prefetch(1))
420
+ .then(() => {
421
+ ch.sendToQueue('test.prefetch', Buffer.from('foobar'));
422
+ ch.sendToQueue('test.prefetch', Buffer.from('foobar'));
423
+ return waitForMessages('test.prefetch', 2);
424
+ })
425
+ .then(() => new Promise((resolve) => {
426
+ let messageCount = 0;
427
+ ch.consume('test.prefetch', (msg) => {
428
+ ch.ack(msg);
429
+ if (++messageCount > 1) resolve(messageCount);
430
+ }, { noAck: false });
431
+ }))
432
+ .then((c) => assert.equal(2, c))
433
+ );
434
+ });
435
+
436
+ it('close', () => {
437
+ return withChannel((ch) => ch.close());
438
+ });
439
+ });
440
+
441
+ describe('confirms', () => {
442
+ it('message is confirmed', () => {
443
+ return withConfirmChannel((ch) =>
444
+ ch.assertQueue('test.confirm-message', QUEUE_OPTS)
445
+ .then(() => ch.purgeQueue('test.confirm-message'))
446
+ .then(() => ch.sendToQueue('test.confirm-message', Buffer.from('bleep')))
447
+ );
448
+ });
449
+
450
+ // Usually one can provoke the server into confirming more than one
451
+ // message in an ack by simply sending a few messages in quick
452
+ // succession; a bit unscientific I know. Luckily we can eavesdrop on
453
+ // the acknowledgements coming through to see if we really did get a
454
+ // multi-ack.
455
+ it('multiple confirms', () => {
456
+ return withConfirmChannel((ch) =>
457
+ ch.assertQueue('test.multiple-confirms', QUEUE_OPTS)
458
+ .then(() => ch.purgeQueue('test.multiple-confirms'))
459
+ .then(() => {
460
+ let multipleRainbows = false;
461
+ ch.on('ack', (a) => {
462
+ if (a.multiple) multipleRainbows = true;
463
+ });
464
+
465
+ function prod(num) {
466
+ const cs = [];
467
+ for (let i = 0; i < num; i++) {
468
+ cs.push(promisify((cb) => ch.sendToQueue('test.multiple-confirms', Buffer.from('bleep'), {}, cb))());
469
+ }
470
+ return Promise.all(cs).then(() => {
471
+ if (multipleRainbows) return true;
472
+ else if (num > 500) throw new Error(`Couldn't provoke the server into multi-acking with ${num} messages; giving up`);
473
+ else return prod(num * 2);
474
+ });
475
+ }
476
+ return prod(5);
477
+ })
478
+ );
479
+ });
480
+
481
+ it('wait for confirms', () => {
482
+ return withConfirmChannel((ch) => {
483
+ for (let i = 0; i < 1000; i++) {
484
+ ch.publish('', '', Buffer.from('foobar'), {});
485
+ }
486
+ return ch.waitForConfirms();
487
+ });
488
+ });
489
+
490
+ it('works when channel is closed', () => {
491
+ return withConfirmChannel((ch) => {
492
+ for (let i = 0; i < 1000; i++) {
493
+ ch.publish('', '', Buffer.from('foobar'), {});
494
+ }
495
+ return ch.close()
496
+ .then(() => assert.rejects(() => ch.waitForConfirms(), (e) => {
497
+ assert.strictEqual(e.message, 'channel closed');
498
+ return true;
499
+ }));
500
+ });
501
+ });
502
+ });
503
+
504
+ function connect() {
505
+ return api.connect(URL);
506
+ }
507
+
508
+ function withChannel(cb) {
509
+ return connect().then((c) => c.createChannel().then(cb).finally(() => c.close()));
510
+ }
511
+
512
+ function withConfirmChannel(cb) {
513
+ return connect().then((c) => c.createConfirmChannel().then(cb).finally(() => c.close()));
514
+ }
515
+
516
+ function waitForQueue(q, condition) {
517
+ return connect(URL).then((c) =>
518
+ c.createChannel().then((ch) =>
519
+ ch.checkQueue(q).then((_qok) => {
520
+ function check() {
521
+ return ch.checkQueue(q).then((qok) => {
522
+ if (condition(qok)) {
523
+ c.close();
524
+ return qok;
525
+ } else schedule(check);
526
+ });
527
+ }
528
+ return check();
529
+ }),
530
+ ),
531
+ );
532
+ }
533
+
534
+ function waitForMessages(q, num) {
535
+ const min = num === undefined ? 1 : num;
536
+ return waitForQueue(q, (qok) => qok.messageCount >= min);
537
+ }