From 6547b00ed9e37fbf6166c5be3f9ebf6714a9fc7d Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Sat, 6 Sep 2025 22:37:00 +0100 Subject: [PATCH 1/8] Stop auto formatting tests --- biome.json | 9 ++++----- lefthook.yml | 6 +++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/biome.json b/biome.json index 56d8b492..e0bbd0d1 100644 --- a/biome.json +++ b/biome.json @@ -6,7 +6,8 @@ "indentWidth": 2, "lineWidth": 140, "lineEnding": "lf", - "attributePosition": "auto" + "attributePosition": "auto", + "includes": ["**/*.js", "!test"] }, "linter": { "enabled": true, @@ -30,7 +31,8 @@ "style": { "useSingleVarDeclarator": "error" } - } + }, + "includes": ["**/*.js", "!lib/defs.js"] }, "javascript": { "formatter": { @@ -43,8 +45,5 @@ "semicolons": "always", "trailingCommas": "all" } - }, - "files": { - "includes": ["**/*.js", "!lib/defs.js"] } } diff --git a/lefthook.yml b/lefthook.yml index 84d7fd29..efc48a50 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -3,4 +3,8 @@ pre-commit: format: glob: "**/*.js" run: npx biome format {staged_files} --write - stage_fixed: true \ No newline at end of file + stage_fixed: true + lint: + glob: "**/*.js" + run: npx biome lint {staged_files} --write + stage_fixed: true From 95e7eaeb5c6c116b9886c87ac924cf263b4806b7 Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Sat, 6 Sep 2025 22:52:32 +0100 Subject: [PATCH 2/8] Format test/bitset.js --- lefthook.yml | 2 + test/bitset.js | 129 ++++++++++++++++++------------------------------- 2 files changed, 50 insertions(+), 81 deletions(-) diff --git a/lefthook.yml b/lefthook.yml index efc48a50..6b854253 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -2,6 +2,8 @@ pre-commit: commands: format: glob: "**/*.js" + exclude: + - "test/*.js" run: npx biome format {staged_files} --write stage_fixed: true lint: diff --git a/test/bitset.js b/test/bitset.js index 7e1b360c..aa811b93 100644 --- a/test/bitset.js +++ b/test/bitset.js @@ -4,94 +4,61 @@ const { BitSet } = require('../lib/bitset'); const { forAll, data: arb, label, choice, transform } = claire; const PosInt = transform(Math.floor, arb.Positive); - -const EmptyBitSet = label( - 'bitset', - transform( - (size) => { - return new BitSet(size); - }, - choice(arb.Nothing, PosInt), - ), -); +const EmptyBitSet = label('bitset', transform((size) => new BitSet(size), choice(arb.Nothing, PosInt))); suite('BitSet', () => { - test( - 'get bit', - forAll(EmptyBitSet, PosInt) - .satisfy((b, bit) => { - b.set(bit); - return b.get(bit); - }) - .asTest(), - ); + test('get bit', forAll(EmptyBitSet, PosInt) + .satisfy((b, bit) => { + b.set(bit); + return b.get(bit); + }) + .asTest()); - test( - 'clear bit', - forAll(EmptyBitSet, PosInt) - .satisfy((b, bit) => { - b.set(bit); - b.clear(bit); - return !b.get(bit); - }) - .asTest(), - ); + test('clear bit', forAll(EmptyBitSet, PosInt) + .satisfy((b, bit) => { + b.set(bit); + b.clear(bit); + return !b.get(bit); + }) + .asTest()); - test( - 'next set of empty', - forAll(EmptyBitSet) - .satisfy((b) => { - return b.nextSetBit(0) === -1; - }) - .asTest(), - ); + test('next set of empty', forAll(EmptyBitSet) + .satisfy((b) => { + return b.nextSetBit(0) === -1; + }) + .asTest()); - test( - 'next set of one bit', - forAll(EmptyBitSet, PosInt) - .satisfy((b, bit) => { - b.set(bit); - return b.nextSetBit(0) === bit; - }) - .asTest(), - ); + test('next set of one bit', forAll(EmptyBitSet, PosInt) + .satisfy((b, bit) => { + b.set(bit); + return b.nextSetBit(0) === bit; + }) + .asTest()); - test( - 'next set same bit', - forAll(EmptyBitSet, PosInt) - .satisfy((b, bit) => { - b.set(bit); - return b.nextSetBit(bit) === bit; - }) - .asTest(), - ); + test('next set same bit', forAll(EmptyBitSet, PosInt) + .satisfy((b, bit) => { + b.set(bit); + return b.nextSetBit(bit) === bit; + }) + .asTest()); - test( - 'next set following bit', - forAll(EmptyBitSet, PosInt) - .satisfy((b, bit) => { - b.set(bit); - return b.nextSetBit(bit + 1) === -1; - }) - .asTest(), - ); + test('next set following bit', forAll(EmptyBitSet, PosInt) + .satisfy((b, bit) => { + b.set(bit); + return b.nextSetBit(bit + 1) === -1; + }) + .asTest()); - test( - 'next clear of empty', - forAll(EmptyBitSet, PosInt) - .satisfy((b, bit) => { - return b.nextClearBit(bit) === bit; - }) - .asTest(), - ); + test('next clear of empty', forAll(EmptyBitSet, PosInt) + .satisfy((b, bit) => { + return b.nextClearBit(bit) === bit; + }) + .asTest()); - test( - 'next clear of one set', - forAll(EmptyBitSet, PosInt) - .satisfy((b, bit) => { - b.set(bit); - return b.nextClearBit(bit) === bit + 1; - }) - .asTest(), - ); + test('next clear of one set', forAll(EmptyBitSet, PosInt) + .satisfy((b, bit) => { + b.set(bit); + return b.nextClearBit(bit) === bit + 1; + }) + .asTest()); }); From cad3dfbf5c240a2865d0381c966cc9be8a75af9b Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Sat, 6 Sep 2025 23:00:39 +0100 Subject: [PATCH 3/8] Reformat test/callback_api.js --- test/callback_api.js | 151 ++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 101 deletions(-) diff --git a/test/callback_api.js b/test/callback_api.js index c53d5028..6266b6a2 100644 --- a/test/callback_api.js +++ b/test/callback_api.js @@ -71,11 +71,9 @@ suite('connect', () => { suite('updateSecret', () => { test('updateSecret', (done) => { - connect( - kCallback((c) => { - c.updateSecret(Buffer.from('new secret'), 'no reason', doneCallback(done)); - }), - ); + connect(kCallback((c) => { + c.updateSecret(Buffer.from('new secret'), 'no reason', doneCallback(done)); + })); }); }); @@ -86,16 +84,11 @@ const channel_test_fn = (method) => { options = {}; } test(name, (done) => { - connect( - kCallback((c) => { - c[method]( - options, - kCallback((ch) => { - chfun(ch, done); - }, done), - ); - }, done), - ); + connect(kCallback((c) => { + c[method](options, kCallback((ch) => { + chfun(ch, done); + }, done)); + }, done)); }); }; }; @@ -118,30 +111,19 @@ suite('assert, check, delete', () => { 'test.cb.queue', {}, kCallback((_q) => { - ch.checkQueue( - 'test.cb.queue', - kCallback((_ok) => { - ch.deleteQueue('test.cb.queue', {}, doneCallback(done)); - }, done), - ); + ch.checkQueue('test.cb.queue', kCallback((_ok) => { + ch.deleteQueue('test.cb.queue', {}, doneCallback(done)); + }, done)); }, done), ); }); channel_test('assert, check, delete exchange', (ch, done) => { - ch.assertExchange( - 'test.cb.exchange', - 'topic', - {}, - kCallback((_ex) => { - ch.checkExchange( - 'test.cb.exchange', - kCallback((_ok) => { - ch.deleteExchange('test.cb.exchange', {}, doneCallback(done)); - }, done), - ); - }, done), - ); + ch.assertExchange('test.cb.exchange', 'topic', {}, kCallback((_ex) => { + ch.checkExchange('test.cb.exchange', kCallback((_ok) => { + ch.deleteExchange('test.cb.exchange', {}, doneCallback(done)); + }, done)); + }, done)); }); channel_test('fail on check non-queue', (ch, done) => { @@ -159,38 +141,19 @@ suite('assert, check, delete', () => { suite('bindings', () => { channel_test('bind queue', (ch, done) => { - ch.assertQueue( - 'test.cb.bindq', - {}, - kCallback((q) => { - ch.assertExchange( - 'test.cb.bindex', - 'fanout', - {}, - kCallback((ex) => { - ch.bindQueue(q.queue, ex.exchange, '', {}, doneCallback(done)); - }, done), - ); - }, done), - ); + ch.assertQueue('test.cb.bindq', {}, kCallback((q) => { + ch.assertExchange('test.cb.bindex', 'fanout', {}, kCallback((ex) => { + ch.bindQueue(q.queue, ex.exchange, '', {}, doneCallback(done)); + }, done)); + }, done)); }); channel_test('bind exchange', (ch, done) => { - ch.assertExchange( - 'test.cb.bindex1', - 'fanout', - {}, - kCallback((ex1) => { - ch.assertExchange( - 'test.cb.bindex2', - 'fanout', - {}, - kCallback((ex2) => { - ch.bindExchange(ex1.exchange, ex2.exchange, '', {}, doneCallback(done)); - }, done), - ); - }, done), - ); + ch.assertExchange('test.cb.bindex1', 'fanout', {}, kCallback((ex1) => { + ch.assertExchange('test.cb.bindex2', 'fanout', {}, kCallback((ex2) => { + ch.bindExchange(ex1.exchange, ex2.exchange, '', {}, doneCallback(done)); + }, done)); + }, done)); }); }); @@ -199,14 +162,10 @@ suite('sending messages', () => { const msg = randomString(); ch.assertQueue('', { exclusive: true }, (e, q) => { if (e !== null) return done(e); - ch.consume( - q.queue, - (m) => { - if (m.content.toString() === msg) done(); - else done(new Error(`message content doesn't match:${msg} =/= ${m.content.toString()}`)); - }, - { noAck: true, exclusive: true }, - ); + ch.consume(q.queue, (m) => { + if (m.content.toString() === msg) done(); + else done(new Error(`message content doesn't match:${msg} =/= ${m.content.toString()}`)); + }, { noAck: true, exclusive: true }); ch.sendToQueue(q.queue, Buffer.from(msg)); }); }); @@ -215,16 +174,12 @@ suite('sending messages', () => { const msg = randomString(); ch.assertQueue('', { exclusive: true }, (e, q) => { if (e !== null) return done(e); - ch.consume( - q.queue, - (m) => { - if (m.content.toString() === msg) { - ch.ack(m); - done(); - } else done(new Error(`message content doesn't match:${msg} =/= ${m.content.toString()}`)); - }, - { noAck: false, exclusive: true }, - ); + ch.consume(q.queue, (m) => { + if (m.content.toString() === msg) { + ch.ack(m); + done(); + } else done(new Error(`message content doesn't match:${msg} =/= ${m.content.toString()}`)); + }, { noAck: false, exclusive: true }); ch.sendToQueue(q.queue, Buffer.from(msg)); }); }); @@ -337,11 +292,9 @@ suite('Error handling', () => { test('Throw error in connection open callback', (done) => { const dom = domain.createDomain(); dom.on('error', failCallback(done)); - connect( - dom.bind((_err, _conn) => { - throw new Error('Spurious connection open callback error'); - }), - ); + connect(dom.bind((_err, _conn) => { + throw new Error('Spurious connection open callback error'); + })); }); } @@ -350,21 +303,17 @@ suite('Error handling', () => { test(name, (done) => { const dom = domain.createDomain(); dom.run(() => { - connect( - kCallback((c) => { - // Seems like there were some unironed wrinkles in 0.8's - // implementation of domains; explicitly adding the connection - // to the domain makes sure any exception thrown in the course - // of processing frames is handled by the domain. For other - // versions of Node.JS, this ends up being belt-and-braces. - dom.add(c); - c.createChannel( - kCallback((ch) => { - fun(ch, done, dom); - }, done), - ); - }, done), - ); + connect(kCallback((c) => { + // Seems like there were some unironed wrinkles in 0.8's + // implementation of domains; explicitly adding the connection + // to the domain makes sure any exception thrown in the course + // of processing frames is handled by the domain. For other + // versions of Node.JS, this ends up being belt-and-braces. + dom.add(c); + c.createChannel(kCallback((ch) => { + fun(ch, done, dom); + }, done)); + }, done)); }); }); } From e36cb2c67c6518c5909eb7c4f561d7d87eb1835b Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Sat, 6 Sep 2025 23:33:37 +0100 Subject: [PATCH 4/8] Reformat test/channel.js --- test/channel.js | 1032 ++++++++++++++++++++--------------------------- 1 file changed, 437 insertions(+), 595 deletions(-) diff --git a/test/channel.js b/test/channel.js index 8790e6aa..ee353e8b 100644 --- a/test/channel.js +++ b/test/channel.js @@ -78,299 +78,234 @@ function open(ch) { } suite('channel open and close', () => { - test( - 'open', - channelTest( - (ch, done) => { - open(ch).then(succeed(done), fail(done)); - }, - (_send, _wait, done) => { - done(); - }, - ), - ); - - test( - 'bad server', - baseChannelTest( - (c, done) => { - const ch = new Channel(c); - open(ch).then(fail(done), succeed(done)); - }, - (send, wait, done) => - wait(defs.ChannelOpen)() - .then((open) => { - send(defs.ChannelCloseOk, {}, open.channel); - }) - .then(succeed(done), fail(done)), - ), + test('open', channelTest( + (ch, done) => { + open(ch).then(succeed(done), fail(done)); + }, + (_send, _wait, done) => { + done(); + } + )); + + test('bad server', baseChannelTest((c, done) => { + const ch = new Channel(c); + open(ch).then(fail(done), succeed(done)); + }, (send, wait, done) => + wait(defs.ChannelOpen)() + .then((open) => { + send(defs.ChannelCloseOk, {}, open.channel); + }) + .then(succeed(done), fail(done))) ); - test( - 'open, close', - channelTest( - (ch, done) => { - open(ch) - .then( - () => - new Promise((resolve) => { - ch.closeBecause('Bye', defs.constants.REPLY_SUCCESS, resolve); - }), - ) - .then(succeed(done), fail(done)); - }, - (send, wait, done, ch) => - wait(defs.ChannelClose)() - .then((_close) => { - send(defs.ChannelCloseOk, {}, ch); - }) - .then(succeed(done), fail(done)), - ), + test('open, close', channelTest((ch, done) => { + open(ch) + .then(() => new Promise((resolve) => { + ch.closeBecause('Bye', defs.constants.REPLY_SUCCESS, resolve); + })) + .then(succeed(done), fail(done)); + }, (send, wait, done, ch) => + wait(defs.ChannelClose)() + .then((_close) => { + send(defs.ChannelCloseOk, {}, ch); + }) + .then(succeed(done), fail(done))) ); - test( - 'server close', - channelTest( - (ch, done) => { - ch.on('error', (error) => { - assert.strictEqual(504, error.code); - assert.strictEqual(0, error.classId); - assert.strictEqual(0, error.methodId); - succeed(done)(); - }); - open(ch); - }, - (send, wait, done, ch) => { - send( - defs.ChannelClose, - { - replyText: 'Forced close', - replyCode: defs.constants.CHANNEL_ERROR, + test('server close', channelTest((ch, done) => { + ch.on('error', (error) => { + assert.strictEqual(504, error.code); + assert.strictEqual(0, error.classId); + assert.strictEqual(0, error.methodId); + succeed(done)(); + }); + open(ch); + }, (send, wait, done, ch) => { + send(defs.ChannelClose, { + replyText: 'Forced close', + replyCode: defs.constants.CHANNEL_ERROR, + classId: 0, + methodId: 0, + }, ch); + wait(defs.ChannelCloseOk)().then(succeed(done), fail(done)); + })); + + test('overlapping channel/server close', channelTest( + (ch, done, conn) => { + const both = latch(2, done); + conn.on('error', succeed(both)); + ch.on('close', succeed(both)); + open(ch).then(() => { + ch.closeBecause('Bye', defs.constants.REPLY_SUCCESS); + }, fail(both)); + }, (send, wait, done, _ch) => { + wait(defs.ChannelClose)() + .then(() => { + send(defs.ConnectionClose, { + replyText: 'Got there first', + replyCode: defs.constants.INTERNAL_ERROR, classId: 0, methodId: 0, - }, - ch, - ); - wait(defs.ChannelCloseOk)().then(succeed(done), fail(done)); - }, - ), - ); - - test( - 'overlapping channel/server close', - channelTest( - (ch, done, conn) => { - const both = latch(2, done); - conn.on('error', succeed(both)); - ch.on('close', succeed(both)); - open(ch).then(() => { - ch.closeBecause('Bye', defs.constants.REPLY_SUCCESS); - }, fail(both)); - }, - (send, wait, done, _ch) => { - wait(defs.ChannelClose)() - .then(() => { - send( - defs.ConnectionClose, - { - replyText: 'Got there first', - replyCode: defs.constants.INTERNAL_ERROR, - classId: 0, - methodId: 0, - }, - 0, - ); - }) - .then(wait(defs.ConnectionCloseOk)) - .then(succeed(done), fail(done)); - }, - ), - ); - - test( - 'double close', - channelTest( - (ch, done) => { - open(ch) - .then(() => { - ch.closeBecause('First close', defs.constants.REPLY_SUCCESS); - // NB no synchronisation, we do this straight away - assert.throws(() => { - ch.closeBecause('Second close', defs.constants.REPLY_SUCCESS); - }); - }) - .then(succeed(done), fail(done)); - }, - (send, wait, done, ch) => { - wait(defs.ChannelClose)() - .then(() => { - send(defs.ChannelCloseOk, {}, ch); - }) - .then(succeed(done), fail(done)); - }, - ), - ); -}); //suite + }, 0); + }) + .then(wait(defs.ConnectionCloseOk)) + .then(succeed(done), fail(done)); + })); + + test('double close', channelTest((ch, done) => { + open(ch) + .then(() => { + ch.closeBecause('First close', defs.constants.REPLY_SUCCESS); + // NB no synchronisation, we do this straight away + assert.throws(() => { + ch.closeBecause('Second close', defs.constants.REPLY_SUCCESS); + }); + }) + .then(succeed(done), fail(done)); + }, (send, wait, done, ch) => { + wait(defs.ChannelClose)() + .then(() => { + send(defs.ChannelCloseOk, {}, ch); + }).then(succeed(done), fail(done)); + })); +}); suite('channel machinery', () => { - test( - 'RPC', - channelTest( - (ch, done) => { - const rpcLatch = latch(3, done); - open(ch) - .then(() => { - function wheeboom(err, _f) { - if (err !== null) rpcLatch(err); - else rpcLatch(); - } - - const 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)); - }, - (send, wait, done, ch) => { - function sendOk(_f) { - send(defs.BasicQosOk, {}, ch); + test('RPC', channelTest((ch, done) => { + const rpcLatch = latch(3, done); + open(ch) + .then(() => { + function wheeboom(err, _f) { + if (err !== null) rpcLatch(err); + else rpcLatch(); } - return wait(defs.BasicQos)() - .then(sendOk) - .then(wait(defs.BasicQos)) - .then(sendOk) - .then(wait(defs.BasicQos)) - .then(sendOk) - .then(succeed(done), fail(done)); - }, - ), - ); - - test( - 'Bad RPC', - channelTest( - (ch, done) => { - // We want to see the RPC rejected and the channel closed (with an - // error) - const errLatch = latch(2, done); - ch.on('error', (error) => { - assert.strictEqual(505, error.code); - assert.strictEqual(60, error.classId); - assert.strictEqual(72, error.methodId); - succeed(errLatch)(); + const 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)); + }, (send, wait, done, ch) => { + function sendOk(_f) { + send(defs.BasicQosOk, {}, ch); + } + + return wait(defs.BasicQos)() + .then(sendOk) + .then(wait(defs.BasicQos)) + .then(sendOk) + .then(wait(defs.BasicQos)) + .then(sendOk) + .then(succeed(done), fail(done)); + })); + + test('Bad RPC', channelTest( + (ch, done) => { + // We want to see the RPC rejected and the channel closed (with an + // error) + const errLatch = latch(2, done); + ch.on('error', (error) => { + assert.strictEqual(505, error.code); + assert.strictEqual(60, error.classId); + assert.strictEqual(72, error.methodId); + succeed(errLatch)(); + }); + + open(ch).then(() => { + ch._rpc(defs.BasicRecover, { requeue: true }, defs.BasicRecoverOk, (err) => { + if (err !== null) errLatch(); + else errLatch(new Error('Expected RPC failure')); }); - - open(ch).then(() => { - ch._rpc(defs.BasicRecover, { requeue: true }, defs.BasicRecoverOk, (err) => { - if (err !== null) errLatch(); - else errLatch(new Error('Expected RPC failure')); - }); - }, fail(errLatch)); - }, - (send, wait, done, ch) => - wait()() - .then(() => { - send(defs.BasicGetEmpty, { clusterId: '' }, ch); - }) // oh wait! that was wrong! expect a channel close - .then(wait(defs.ChannelClose)) - .then(() => { - send(defs.ChannelCloseOk, {}, ch); - }) - .then(succeed(done), fail(done)), - ), - ); - - test( - 'RPC on closed channel', - channelTest( - (ch, done) => { - open(ch); - - const close = new Promise((resolve) => { - ch.on('error', (error) => { - assert.strictEqual(504, error.code); - assert.strictEqual(0, error.classId); - assert.strictEqual(0, error.methodId); - resolve(); - }); + }, fail(errLatch)); + }, + (send, wait, done, ch) => + wait()() + .then(() => { + send(defs.BasicGetEmpty, { clusterId: '' }, ch); + }) // oh wait! that was wrong! expect a channel close + .then(wait(defs.ChannelClose)) + .then(() => { + send(defs.ChannelCloseOk, {}, ch); + }) + .then(succeed(done), fail(done)), + )); + + test('RPC on closed channel', channelTest( + (ch, done) => { + open(ch); + + const close = new Promise((resolve) => { + ch.on('error', (error) => { + assert.strictEqual(504, error.code); + assert.strictEqual(0, error.classId); + assert.strictEqual(0, error.methodId); + resolve(); }); + }); - function failureCb(resolve, reject) { - return (err) => { - if (err !== null) resolve(); - else reject(); - }; - } + function failureCb(resolve, reject) { + return (err) => { + if (err !== null) resolve(); + else reject(); + }; + } - const fail1 = new Promise((resolve, reject) => - ch._rpc(defs.BasicRecover, { requeue: true }, defs.BasicRecoverOk, failureCb(resolve, reject)), - ); + const fail1 = new Promise((resolve, reject) => + ch._rpc(defs.BasicRecover, { requeue: true }, defs.BasicRecoverOk, failureCb(resolve, reject)), + ); - const fail2 = new Promise((resolve, reject) => - ch._rpc(defs.BasicRecover, { requeue: true }, defs.BasicRecoverOk, failureCb(resolve, reject)), - ); + const fail2 = new Promise((resolve, reject) => + ch._rpc(defs.BasicRecover, { requeue: true }, defs.BasicRecoverOk, failureCb(resolve, reject)), + ); - Promise.all([close, fail1, fail2]).then(succeed(done)).catch(fail(done)); - }, - (send, wait, done, ch) => { - wait(defs.BasicRecover)() - .then(() => { - send( - defs.ChannelClose, - { - replyText: 'Nuh-uh!', - replyCode: defs.constants.CHANNEL_ERROR, - methodId: 0, - classId: 0, - }, - ch, - ); - return wait(defs.ChannelCloseOk); - }) - .then(succeed(done)) - .catch(fail(done)); - }, - ), - ); - - test( - 'publish all < single chunk threshold', - channelTest( - (ch, done) => { - open(ch) - .then(() => { - ch.sendMessage( - { - exchange: 'foo', - routingKey: 'bar', - mandatory: false, - immediate: false, - ticket: 0, - }, - {}, - Buffer.from('foobar'), - ); - }) - .then(succeed(done), fail(done)); - }, - (_send, wait, done, _ch) => { - wait(defs.BasicPublish)() - .then(wait(defs.BasicProperties)) - .then(wait(undefined)) // content frame - .then((f) => { - assert.equal('foobar', f.content.toString()); - }) - .then(succeed(done), fail(done)); - }, - ), - ); + Promise.all([close, fail1, fail2]).then(succeed(done)).catch(fail(done)); + }, + (send, wait, done, ch) => { + wait(defs.BasicRecover)() + .then(() => { + send(defs.ChannelClose, { + replyText: 'Nuh-uh!', + replyCode: defs.constants.CHANNEL_ERROR, + methodId: 0, + classId: 0, + }, ch); + return wait(defs.ChannelCloseOk); + }) + .then(succeed(done)) + .catch(fail(done)); + }, + )); + + test('publish all < single chunk threshold', channelTest( + (ch, done) => { + open(ch) + .then(() => { + ch.sendMessage({ + exchange: 'foo', + routingKey: 'bar', + mandatory: false, + immediate: false, + ticket: 0, + }, {}, Buffer.from('foobar')); + }) + .then(succeed(done), fail(done)); + }, + (_send, wait, done, _ch) => { + wait(defs.BasicPublish)() + .then(wait(defs.BasicProperties)) + .then(wait(undefined)) // content frame + .then((f) => { + assert.equal('foobar', f.content.toString()); + }) + .then(succeed(done), fail(done)); + }, + )); test( 'publish content > single chunk threshold', @@ -390,8 +325,7 @@ suite('channel machinery', () => { Buffer.alloc(3000), ); }, done); - }, - (_send, wait, done, _ch) => { + }, (_send, wait, done, _ch) => { wait(defs.BasicPublish)() .then(wait(defs.BasicProperties)) .then(wait(undefined)) // content frame @@ -399,101 +333,73 @@ suite('channel machinery', () => { assert.equal(3000, f.content.length); }) .then(succeed(done), fail(done)); - }, - ), - ); - - test( - 'publish method & headers > threshold', - channelTest( - (ch, done) => { - open(ch); - completes(() => { - ch.sendMessage( - { - exchange: 'foo', - routingKey: 'bar', - mandatory: false, - immediate: false, - ticket: 0, - }, - { - headers: { foo: Buffer.alloc(3000) }, - }, - Buffer.from('foobar'), - ); - }, done); - }, - (_send, wait, done, _ch) => { - wait(defs.BasicPublish)() - .then(wait(defs.BasicProperties)) - .then(wait(undefined)) // content frame - .then((f) => { - assert.equal('foobar', f.content.toString()); - }) - .then(succeed(done), fail(done)); - }, - ), + } + ) ); - test( - 'publish zero-length message', - channelTest( - (ch, done) => { - open(ch); - completes(() => { - ch.sendMessage( - { - exchange: 'foo', - routingKey: 'bar', - mandatory: false, - immediate: false, - ticket: 0, - }, - {}, - Buffer.alloc(0), - ); - ch.sendMessage( - { - exchange: 'foo', - routingKey: 'bar', - mandatory: false, - immediate: false, - ticket: 0, - }, - {}, - Buffer.alloc(0), - ); - }, done); - }, - (_send, wait, done, _ch) => { - wait(defs.BasicPublish)() - .then(wait(defs.BasicProperties)) - // no content frame for a zero-length message - .then(wait(defs.BasicPublish)) - .then(succeed(done), fail(done)); - }, - ), - ); - - test( - 'delivery', - channelTest( - (ch, done) => { - open(ch); - ch.on('delivery', (m) => { - completes(() => { - assert.equal('barfoo', m.content.toString()); - }, done); - }); - }, - (send, _wait, done, ch) => { - completes(() => { - send(defs.BasicDeliver, DELIVER_FIELDS, ch, Buffer.from('barfoo')); - }, done); - }, - ), - ); + test('publish method & headers > threshold', channelTest( + (ch, done) => { + open(ch); + completes(() => { + ch.sendMessage({ + exchange: 'foo', + routingKey: 'bar', + mandatory: false, + immediate: false, + ticket: 0, + }, { + headers: { foo: Buffer.alloc(3000) }, + }, Buffer.from('foobar')); + }, done); + }, (_send, wait, done, _ch) => { + wait(defs.BasicPublish)() + .then(wait(defs.BasicProperties)) + .then(wait(undefined)) // content frame + .then((f) => { + assert.equal('foobar', f.content.toString()); + }) + .then(succeed(done), fail(done)); + } + )); + + test('publish zero-length message', channelTest((ch, done) => { + open(ch); + completes(() => { + ch.sendMessage({ + exchange: 'foo', + routingKey: 'bar', + mandatory: false, + immediate: false, + ticket: 0, + }, {}, Buffer.alloc(0)); + ch.sendMessage({ + exchange: 'foo', + routingKey: 'bar', + mandatory: false, + immediate: false, + ticket: 0, + }, {}, Buffer.alloc(0)); + }, done); + }, (_send, wait, done, _ch) => { + wait(defs.BasicPublish)() + .then(wait(defs.BasicProperties)) + // no content frame for a zero-length message + .then(wait(defs.BasicPublish)) + .then(succeed(done), fail(done)); + })); + + test('delivery', channelTest((ch, done) => { + open(ch); + ch.on('delivery', (m) => { + completes(() => { + assert.equal('barfoo', m.content.toString()); + }, done); + }); + }, (send, _wait, done, ch) => { + completes(() => { + send(defs.BasicDeliver, DELIVER_FIELDS, ch, Buffer.from('barfoo')); + }, done); + })); test( 'zero byte msg', @@ -514,53 +420,39 @@ suite('channel machinery', () => { ), ); - test( - 'bad delivery', - channelTest( - (ch, done) => { - const errorAndClose = latch(2, done); - ch.on('error', (error) => { - assert.strictEqual(505, error.code); - assert.strictEqual(60, error.classId); - assert.strictEqual(60, error.methodId); - succeed(errorAndClose)(); - }); - ch.on('close', succeed(errorAndClose)); - open(ch); - }, - (send, wait, done, ch) => { - send(defs.BasicDeliver, DELIVER_FIELDS, ch); - // now send another deliver without having sent the content - send(defs.BasicDeliver, DELIVER_FIELDS, ch); - return wait(defs.ChannelClose)() - .then(() => { - send(defs.ChannelCloseOk, {}, ch); - }) - .then(succeed(done), fail(done)); - }, - ), - ); - - test( - 'bad content send', - channelTest( - (ch, done) => { - completes(() => { - open(ch); - assert.throws(() => { - ch.sendMessage({ routingKey: 'foo', exchange: 'amq.direct' }, {}, null); - }); - }, done); - }, - (_send, _wait, done, _ch) => { - done(); - }, - ), - ); - - test( - 'bad properties send', - channelTest( + test('bad delivery', channelTest((ch, done) => { + const errorAndClose = latch(2, done); + ch.on('error', (error) => { + assert.strictEqual(505, error.code); + assert.strictEqual(60, error.classId); + assert.strictEqual(60, error.methodId); + succeed(errorAndClose)(); + }); + ch.on('close', succeed(errorAndClose)); + open(ch); + }, (send, wait, done, ch) => { + send(defs.BasicDeliver, DELIVER_FIELDS, ch); + // now send another deliver without having sent the content + send(defs.BasicDeliver, DELIVER_FIELDS, ch); + return wait(defs.ChannelClose)() + .then(() => { + send(defs.ChannelCloseOk, {}, ch); + }) + .then(succeed(done), fail(done)); + })); + + test('bad content send', channelTest((ch, done) => { + completes(() => { + open(ch); + assert.throws(() => { + ch.sendMessage({ routingKey: 'foo', exchange: 'amq.direct' }, {}, null); + }); + }, done); + }, (_send, _wait, done, _ch) => { + done(); + })); + + test('bad properties send', channelTest( (ch, done) => { completes(() => { open(ch); @@ -575,190 +467,140 @@ suite('channel machinery', () => { ), ); - test( - 'bad consumer', - channelTest( - (ch, done) => { - const errorAndClose = latch(2, done); - ch.on('delivery', () => { - throw new Error('I am a bad consumer'); - }); - ch.on('error', (error) => { - assert.strictEqual(541, error.code); - assert.strictEqual(undefined, error.classId); - assert.strictEqual(undefined, error.methodId); - succeed(errorAndClose)(); - }); - ch.on('close', succeed(errorAndClose)); - open(ch); - }, - (send, wait, done, ch) => { - send(defs.BasicDeliver, DELIVER_FIELDS, ch, Buffer.from('barfoo')); - return wait(defs.ChannelClose)() - .then(() => { - send(defs.ChannelCloseOk, {}, ch); - }) - .then(succeed(done), fail(done)); - }, - ), - ); - - test( - 'bad send in consumer', - channelTest( - (ch, done) => { - const errorAndClose = latch(2, done); - ch.on('close', succeed(errorAndClose)); - ch.on('error', (error) => { - assert.strictEqual(541, error.code); - assert.strictEqual(undefined, error.classId); - assert.strictEqual(undefined, error.methodId); - succeed(errorAndClose)(); - }); - - ch.on('delivery', () => { - ch.sendMessage({ routingKey: 'foo', exchange: 'amq.direct' }, {}, null); // can't send null - }); + test('bad consumer', channelTest((ch, done) => { + const errorAndClose = latch(2, done); + ch.on('delivery', () => { + throw new Error('I am a bad consumer'); + }); + ch.on('error', (error) => { + assert.strictEqual(541, error.code); + assert.strictEqual(undefined, error.classId); + assert.strictEqual(undefined, error.methodId); + succeed(errorAndClose)(); + }); + ch.on('close', succeed(errorAndClose)); + open(ch); + }, (send, wait, done, ch) => { + send(defs.BasicDeliver, DELIVER_FIELDS, ch, Buffer.from('barfoo')); + return wait(defs.ChannelClose)() + .then(() => { + send(defs.ChannelCloseOk, {}, ch); + }) + .then(succeed(done), fail(done)); + })); + + test('bad send in consumer', channelTest((ch, done) => { + const errorAndClose = latch(2, done); + ch.on('close', succeed(errorAndClose)); + ch.on('error', (error) => { + assert.strictEqual(541, error.code); + assert.strictEqual(undefined, error.classId); + assert.strictEqual(undefined, error.methodId); + succeed(errorAndClose)(); + }); - open(ch); - }, - (send, wait, done, ch) => { - completes(() => { - send(defs.BasicDeliver, DELIVER_FIELDS, ch, Buffer.from('barfoo')); - }, done); - return wait(defs.ChannelClose)() - .then(() => { - send(defs.ChannelCloseOk, {}, ch); - }) - .then(succeed(done), fail(done)); - }, - ), - ); + ch.on('delivery', () => { + ch.sendMessage({ routingKey: 'foo', exchange: 'amq.direct' }, {}, null); // can't send null + }); - test( - 'return', - channelTest( - (ch, done) => { - ch.on('return', (m) => { - completes(() => { - assert.equal('barfoo', m.content.toString()); - }, done); - }); - open(ch); - }, - (send, _wait, done, ch) => { - completes(() => { - send(defs.BasicReturn, DELIVER_FIELDS, ch, Buffer.from('barfoo')); - }, done); - }, - ), - ); + open(ch); + }, (send, wait, done, ch) => { + completes(() => { + send(defs.BasicDeliver, DELIVER_FIELDS, ch, Buffer.from('barfoo')); + }, done); + return wait(defs.ChannelClose)() + .then(() => { + send(defs.ChannelCloseOk, {}, ch); + }) + .then(succeed(done), fail(done)); + })); + + test('return', channelTest((ch, done) => { + ch.on('return', (m) => { + completes(() => { + assert.equal('barfoo', m.content.toString()); + }, done); + }); + open(ch); + }, (send, _wait, done, ch) => { + completes(() => { + send(defs.BasicReturn, DELIVER_FIELDS, ch, Buffer.from('barfoo')); + }, done); + })); + + test('cancel', channelTest((ch, done) => { + ch.on('cancel', (f) => { + completes(() => { + assert.equal('product of society', f.consumerTag); + }, done); + }); + open(ch); + }, (send, _wait, done, ch) => { + completes(() => { + send(defs.BasicCancel, { + consumerTag: 'product of society', + nowait: false, + }, ch); + }, done); + })); - test( - 'cancel', - channelTest( - (ch, done) => { - ch.on('cancel', (f) => { - completes(() => { - assert.equal('product of society', f.consumerTag); - }, done); - }); - open(ch); - }, - (send, _wait, done, ch) => { + function confirmTest(variety, Method) { + return test(`confirm ${variety}`, channelTest((ch, done) => { + ch.on(variety, (f) => { completes(() => { - send( - defs.BasicCancel, - { - consumerTag: 'product of society', - nowait: false, - }, - ch, - ); + assert.equal(1, f.deliveryTag); }, done); - }, - ), - ); - - function confirmTest(variety, Method) { - return test( - `confirm ${variety}`, - channelTest( - (ch, done) => { - ch.on(variety, (f) => { - completes(() => { - assert.equal(1, f.deliveryTag); - }, done); - }); - open(ch); - }, - (send, _wait, done, ch) => { - completes(() => { - send( - Method, - { - deliveryTag: 1, - multiple: false, - }, - ch, - ); - }, done); - }, - ), - ); + }); + open(ch); + }, (send, _wait, done, ch) => { + completes(() => { + send(Method, { + deliveryTag: 1, + multiple: false, + }, ch); + }, done); + })); } confirmTest('ack', defs.BasicAck); confirmTest('nack', defs.BasicNack); - test( - 'out-of-order acks', - channelTest( - (ch, done) => { - const allConfirms = latch(3, () => { - completes(() => { - assert.equal(0, ch.unconfirmed.length); - assert.equal(4, ch.lwm); - }, done); - }); - ch.pushConfirmCallback(allConfirms); - ch.pushConfirmCallback(allConfirms); - ch.pushConfirmCallback(allConfirms); - open(ch); - }, - (send, _wait, done, ch) => { - completes(() => { - 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( - (ch, done) => { - const allConfirms = latch(2, () => { - completes(() => { - assert.equal(1, ch.unconfirmed.length); - assert.equal(3, ch.lwm); - }, done); - }); - ch.pushConfirmCallback(allConfirms); // tag = 1 - ch.pushConfirmCallback(allConfirms); // tag = 2 - ch.pushConfirmCallback(() => { - done(new Error('Confirm callback should not be called')); - }); - open(ch); - }, - (send, _wait, done, ch) => { - completes(() => { - send(defs.BasicAck, { deliveryTag: 2, multiple: false }, ch); - send(defs.BasicAck, { deliveryTag: 1, multiple: false }, ch); - }, done); - }, - ), - ); + test('out-of-order acks', channelTest((ch, done) => { + const allConfirms = latch(3, () => { + completes(() => { + assert.equal(0, ch.unconfirmed.length); + assert.equal(4, ch.lwm); + }, done); + }); + ch.pushConfirmCallback(allConfirms); + ch.pushConfirmCallback(allConfirms); + ch.pushConfirmCallback(allConfirms); + open(ch); + }, (send, _wait, done, ch) => { + completes(() => { + 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((ch, done) => { + const allConfirms = latch(2, () => { + completes(() => { + assert.equal(1, ch.unconfirmed.length); + assert.equal(3, ch.lwm); + }, done); + }); + ch.pushConfirmCallback(allConfirms); // tag = 1 + ch.pushConfirmCallback(allConfirms); // tag = 2 + ch.pushConfirmCallback(() => { + done(new Error('Confirm callback should not be called')); + }); + open(ch); + }, (send, _wait, done, ch) => { + completes(() => { + send(defs.BasicAck, { deliveryTag: 2, multiple: false }, ch); + send(defs.BasicAck, { deliveryTag: 1, multiple: false }, ch); + }, done); + })); }); From 13b7b5d516a238583b26f8dea462d7b45bc413fa Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Sat, 6 Sep 2025 23:41:04 +0100 Subject: [PATCH 5/8] Reformat test/channel_api.js --- test/channel_api.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/channel_api.js b/test/channel_api.js index 9a9aabe1..2746fb70 100644 --- a/test/channel_api.js +++ b/test/channel_api.js @@ -105,13 +105,11 @@ suite('assert, check, delete', () => { return ch.assertExchange(ex, 'fanout', EX_OPTS).then(() => expectFail(ch.assertExchange(ex, 'direct', EX_OPTS))); }); - chtest( - 'channel break on publishing to non-exchange', - (ch) => - new Promise((resolve) => { - ch.on('error', resolve); - ch.publish(randomString(), '', Buffer.from('foobar')); - }), + chtest('channel break on publishing to non-exchange', (ch) => + new Promise((resolve) => { + ch.on('error', resolve); + ch.publish(randomString(), '', Buffer.from('foobar')); + }) ); chtest('delete queue', (ch) => { From dea4e9ac2f4eb83ea29c7daee41b75311e8f5380 Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Sat, 6 Sep 2025 23:54:18 +0100 Subject: [PATCH 6/8] Reformat test/connect.js --- test/connect.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/connect.js b/test/connect.js index 49527808..0a5ac677 100644 --- a/test/connect.js +++ b/test/connect.js @@ -31,21 +31,25 @@ suite('Credentials', () => { const creds = credentialsFromUrl(parts); checkCreds(creds, 'guest', 'guest', done); }); + test('usual user:pass', (done) => { const parts = urlparse('amqp://user:pass@localhost'); const creds = credentialsFromUrl(parts); checkCreds(creds, 'user', 'pass', done); }); + test('missing user', (done) => { const parts = urlparse('amqps://:password@localhost'); const creds = credentialsFromUrl(parts); checkCreds(creds, '', 'password', done); }); + test('missing password', (done) => { const parts = urlparse('amqps://username:@localhost'); const creds = credentialsFromUrl(parts); checkCreds(creds, 'username', '', done); }); + test('escaped colons', (done) => { const parts = urlparse('amqp://user%3Aname:pass%3Aword@localhost'); const creds = credentialsFromUrl(parts); From 5fc78fb02151c03eb86cd2c4f1d1bd48a196a089 Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Sat, 6 Sep 2025 23:54:29 +0100 Subject: [PATCH 7/8] Reformat test/connection.js --- test/connection.js | 545 ++++++++++++++++++--------------------------- 1 file changed, 220 insertions(+), 325 deletions(-) diff --git a/test/connection.js b/test/connection.js index d953669a..a36e0489 100644 --- a/test/connection.js +++ b/test/connection.js @@ -94,302 +94,209 @@ suite('Connection errors', () => { }); suite('Connection open', () => { - test( - 'happy', - connectionTest( - (c, done) => { - c.open(OPEN_OPTS, kCallback(succeed(done), fail(done))); - }, - (send, wait, done) => { - happy_open(send, wait).then(succeed(done), fail(done)); - }, - ), - ); + test('happy', connectionTest((c, done) => { + c.open(OPEN_OPTS, kCallback(succeed(done), fail(done))); + }, (send, wait, done) => { + happy_open(send, wait).then(succeed(done), fail(done)); + })); - test( - 'wrong first frame', - connectionTest( - (c, done) => { - c.open(OPEN_OPTS, kCallback(fail(done), succeed(done))); - }, - (send, _wait, done) => { - // bad server! bad! whatever were you thinking? - completes(() => { - send(defs.ConnectionTune, { channelMax: 0, heartbeat: 0, frameMax: 0 }); - }, done); - }, - ), - ); + test('wrong first frame', connectionTest((c, done) => { + c.open(OPEN_OPTS, kCallback(fail(done), succeed(done))); + }, (send, _wait, done) => { + // bad server! bad! whatever were you thinking? + completes(() => { + send(defs.ConnectionTune, { channelMax: 0, heartbeat: 0, frameMax: 0 }); + }, done); + })); - test( - 'unexpected socket close', - connectionTest( - (c, done) => { - c.open(OPEN_OPTS, kCallback(fail(done), succeed(done))); - }, - (send, wait, done, socket) => { - send(defs.ConnectionStart, { - versionMajor: 0, - versionMinor: 9, - serverProperties: {}, - mechanisms: Buffer.from('PLAIN'), - locales: Buffer.from('en_US'), - }); - return wait(defs.ConnectionStartOk)() - .then(() => { - socket.end(); - }) - .then(succeed(done), fail(done)); - }, - ), - ); + test('unexpected socket close', connectionTest((c, done) => { + c.open(OPEN_OPTS, kCallback(fail(done), succeed(done))); + }, (send, wait, done, socket) => { + send(defs.ConnectionStart, { + versionMajor: 0, + versionMinor: 9, + serverProperties: {}, + mechanisms: Buffer.from('PLAIN'), + locales: Buffer.from('en_US'), + }); + return wait(defs.ConnectionStartOk)() + .then(() => { + socket.end(); + }) + .then(succeed(done), fail(done)); + })); }); suite('Connection running', () => { - test( - 'wrong frame on channel 0', - connectionTest( - (c, done) => { - c.on('error', succeed(done)); - c.open(OPEN_OPTS); - }, - (send, wait, done) => { - happy_open(send, wait) - .then(() => { - // there's actually nothing that would plausibly be sent to a - // just opened connection, so this is violating more than one - // rule. Nonetheless. - send(defs.ChannelOpenOk, { channelId: Buffer.from('') }, 0); - }) - .then(wait(defs.ConnectionClose)) - .then((_close) => { - send(defs.ConnectionCloseOk, {}, 0); - }) - .then(succeed(done), fail(done)); - }, - ), - ); + test('wrong frame on channel 0', connectionTest((c, done) => { + c.on('error', succeed(done)); + c.open(OPEN_OPTS); + }, (send, wait, done) => { + happy_open(send, wait) + .then(() => { + // there's actually nothing that would plausibly be sent to a + // just opened connection, so this is violating more than one + // rule. Nonetheless. + send(defs.ChannelOpenOk, { channelId: Buffer.from('') }, 0); + }) + .then(wait(defs.ConnectionClose)) + .then((_close) => { + send(defs.ConnectionCloseOk, {}, 0); + }) + .then(succeed(done), fail(done)); + })); - test( - 'unopened channel', - connectionTest( - (c, done) => { - c.on('error', succeed(done)); - c.open(OPEN_OPTS); - }, - (send, wait, done) => { - happy_open(send, wait) - .then(() => { - // there's actually nothing that would plausibly be sent to a - // just opened connection, so this is violating more than one - // rule. Nonetheless. - send(defs.ChannelOpenOk, { channelId: Buffer.from('') }, 3); - }) - .then(wait(defs.ConnectionClose)) - .then((_close) => { - send(defs.ConnectionCloseOk, {}, 0); - }) - .then(succeed(done), fail(done)); - }, - ), - ); + test('unopened channel', connectionTest((c, done) => { + c.on('error', succeed(done)); + c.open(OPEN_OPTS); + }, (send, wait, done) => { + happy_open(send, wait) + .then(() => { + // there's actually nothing that would plausibly be sent to a + // just opened connection, so this is violating more than one + // rule. Nonetheless. + send(defs.ChannelOpenOk, { channelId: Buffer.from('') }, 3); + }) + .then(wait(defs.ConnectionClose)) + .then((_close) => { + send(defs.ConnectionCloseOk, {}, 0); + }) + .then(succeed(done), fail(done)); + })); - test( - 'unexpected socket close', - connectionTest( - (c, done) => { - const errorAndClosed = latch(2, done); - c.on('error', succeed(errorAndClosed)); - c.on('close', succeed(errorAndClosed)); - c.open( - OPEN_OPTS, - kCallback(() => { - c.sendHeartbeat(); - }, fail(errorAndClosed)), - ); - }, - (send, wait, done, socket) => { - happy_open(send, wait) - .then(wait()) - .then(() => { - socket.end(); - }) - .then(succeed(done)); - }, - ), - ); + test('unexpected socket close', connectionTest((c, done) => { + const errorAndClosed = latch(2, done); + c.on('error', succeed(errorAndClosed)); + c.on('close', succeed(errorAndClosed)); + c.open(OPEN_OPTS, kCallback(() => { + c.sendHeartbeat(); + }, fail(errorAndClosed))); + }, (send, wait, done, socket) => { + happy_open(send, wait) + .then(wait()) + .then(() => { + socket.end(); + }) + .then(succeed(done)); + })); - test( - 'connection.blocked', - connectionTest( - (c, done) => { - c.on('blocked', succeed(done)); - c.open(OPEN_OPTS); - }, - (send, wait, done, _socket) => { - happy_open(send, wait) - .then(() => { - send(defs.ConnectionBlocked, { reason: 'felt like it' }, 0); - }) - .then(succeed(done)); - }, - ), - ); + test('connection.blocked', connectionTest((c, done) => { + c.on('blocked', succeed(done)); + c.open(OPEN_OPTS); + }, (send, wait, done, _socket) => { + happy_open(send, wait) + .then(() => { + send(defs.ConnectionBlocked, { reason: 'felt like it' }, 0); + }) + .then(succeed(done)); + })); - test( - 'connection.unblocked', - connectionTest( - (c, done) => { - c.on('unblocked', succeed(done)); - c.open(OPEN_OPTS); - }, - (send, wait, done, _socket) => { - happy_open(send, wait) - .then(() => { - send(defs.ConnectionUnblocked, {}, 0); - }) - .then(succeed(done)); - }, - ), - ); + test('connection.unblocked', connectionTest((c, done) => { + c.on('unblocked', succeed(done)); + c.open(OPEN_OPTS); + }, (send, wait, done, _socket) => { + happy_open(send, wait) + .then(() => { + send(defs.ConnectionUnblocked, {}, 0); + }) + .then(succeed(done)); + })); }); suite('Connection close', () => { - test( - 'happy', - connectionTest( - (c, done0) => { - const done = latch(2, done0); - c.on('close', done); - c.open( - OPEN_OPTS, - kCallback( - (_ok) => { - c.close(kCallback(succeed(done), fail(done))); - }, - () => {}, - ), - ); - }, - (send, wait, done) => { - happy_open(send, wait) - .then(wait(defs.ConnectionClose)) - .then((_close) => { - send(defs.ConnectionCloseOk, {}); - }) - .then(succeed(done), fail(done)); - }, - ), - ); + test('happy', connectionTest((c, done0) => { + const done = latch(2, done0); + c.on('close', done); + c.open(OPEN_OPTS, kCallback((_ok) => { + c.close(kCallback(succeed(done), fail(done))); + }, () => {})); + }, (send, wait, done) => { + happy_open(send, wait) + .then(wait(defs.ConnectionClose)) + .then((_close) => { + send(defs.ConnectionCloseOk, {}); + }) + .then(succeed(done), fail(done)); + })); - test( - 'interleaved close frames', - connectionTest( - (c, done0) => { - const done = latch(2, done0); - c.on('close', done); - c.open( - OPEN_OPTS, - kCallback((_ok) => { - c.close(kCallback(succeed(done), fail(done))); - }, done), - ); - }, - (send, wait, done) => { - happy_open(send, wait) - .then(wait(defs.ConnectionClose)) - .then((_f) => { - send(defs.ConnectionClose, { - replyText: 'Ha!', - replyCode: defs.constants.REPLY_SUCCESS, - methodId: 0, - classId: 0, - }); - }) - .then(wait(defs.ConnectionCloseOk)) - .then((_f) => { - send(defs.ConnectionCloseOk, {}); - }) - .then(succeed(done), fail(done)); - }, - ), - ); + test('interleaved close frames', connectionTest((c, done0) => { + const done = latch(2, done0); + c.on('close', done); + c.open(OPEN_OPTS, kCallback((_ok) => { + c.close(kCallback(succeed(done), fail(done))); + }, done)); + }, (send, wait, done) => { + happy_open(send, wait) + .then(wait(defs.ConnectionClose)) + .then((_f) => { + send(defs.ConnectionClose, { + replyText: 'Ha!', + replyCode: defs.constants.REPLY_SUCCESS, + methodId: 0, + classId: 0, + }); + }) + .then(wait(defs.ConnectionCloseOk)) + .then((_f) => { + send(defs.ConnectionCloseOk, {}); + }) + .then(succeed(done), fail(done)); + })); - test( - 'server error close', - connectionTest( - (c, done0) => { - const done = latch(2, done0); - c.on('close', succeed(done)); - c.on('error', succeed(done)); - c.open(OPEN_OPTS); - }, - (send, wait, done) => { - happy_open(send, wait) - .then((_f) => { - send(defs.ConnectionClose, { - replyText: 'Begone', - replyCode: defs.constants.INTERNAL_ERROR, - methodId: 0, - classId: 0, - }); - }) - .then(wait(defs.ConnectionCloseOk)) - .then(succeed(done), fail(done)); - }, - ), - ); + test('server error close', connectionTest((c, done0) => { + const done = latch(2, done0); + c.on('close', succeed(done)); + c.on('error', succeed(done)); + c.open(OPEN_OPTS); + }, (send, wait, done) => { + happy_open(send, wait) + .then((_f) => { + send(defs.ConnectionClose, { + replyText: 'Begone', + replyCode: defs.constants.INTERNAL_ERROR, + methodId: 0, + classId: 0, + }); + }) + .then(wait(defs.ConnectionCloseOk)) + .then(succeed(done), fail(done)); + })); - test( - 'operator-intiated close', - connectionTest( - (c, done) => { - c.on('close', succeed(done)); - c.on('error', fail(done)); - c.open(OPEN_OPTS); - }, - (send, wait, done) => { - happy_open(send, wait) - .then((_f) => { - send(defs.ConnectionClose, { - replyText: 'Begone', - replyCode: defs.constants.CONNECTION_FORCED, - methodId: 0, - classId: 0, - }); - }) - .then(wait(defs.ConnectionCloseOk)) - .then(succeed(done), fail(done)); - }, - ), - ); + test('operator-intiated close', connectionTest((c, done) => { + c.on('close', succeed(done)); + c.on('error', fail(done)); + c.open(OPEN_OPTS); + }, (send, wait, done) => { + happy_open(send, wait) + .then((_f) => { + send(defs.ConnectionClose, { + replyText: 'Begone', + replyCode: defs.constants.CONNECTION_FORCED, + methodId: 0, + classId: 0, + }); + }) + .then(wait(defs.ConnectionCloseOk)) + .then(succeed(done), fail(done)); + })); - test( - 'double close', - connectionTest( - (c, done) => { - c.open( - OPEN_OPTS, - kCallback(() => { - c.close(); - // NB no synchronisation, we do this straight away - assert.throws(() => { - c.close(); - }); - done(); - }, done), - ); - }, - (send, wait, done) => { - happy_open(send, wait) - .then(wait(defs.ConnectionClose)) - .then(() => { - send(defs.ConnectionCloseOk, {}); - }) - .then(succeed(done), fail(done)); - }, - ), - ); + test('double close', connectionTest((c, done) => { + c.open(OPEN_OPTS, kCallback(() => { + c.close(); + // NB no synchronisation, we do this straight away + assert.throws(() => { + c.close(); + }); + done(); + }, done)); + }, (send, wait, done) => { + happy_open(send, wait) + .then(wait(defs.ConnectionClose)) + .then(() => { + send(defs.ConnectionCloseOk, {}); + }) + .then(succeed(done), fail(done)); + })); }); suite('heartbeats', () => { @@ -403,50 +310,38 @@ suite('heartbeats', () => { heartbeat.UNITS_TO_MS = 1000; }); - test( - 'send heartbeat after open', - connectionTest( - (c, done) => { - completes(() => { - const opts = Object.create(OPEN_OPTS); - opts.heartbeat = 1; - // Don't leave the error waiting to happen for the next test, this - // confuses mocha awfully - c.on('error', () => {}); - c.open(opts); - }, done); - }, - (send, wait, done, socket) => { - let timer; - happy_open(send, wait) - .then(() => { - timer = setInterval(() => { - socket.write(HB_BUF); - }, heartbeat.UNITS_TO_MS); - }) - .then(wait()) - .then((hb) => { - if (hb === HEARTBEAT) done(); - else done('Next frame after silence not a heartbeat'); - clearInterval(timer); - }); - }, - ), - ); + test('send heartbeat after open', connectionTest((c, done) => { + completes(() => { + const opts = Object.create(OPEN_OPTS); + opts.heartbeat = 1; + // Don't leave the error waiting to happen for the next test, this + // confuses mocha awfully + c.on('error', () => {}); + c.open(opts); + }, done); + }, (send, wait, done, socket) => { + let timer; + happy_open(send, wait) + .then(() => { + timer = setInterval(() => { + socket.write(HB_BUF); + }, heartbeat.UNITS_TO_MS); + }) + .then(wait()) + .then((hb) => { + if (hb === HEARTBEAT) done(); + else done('Next frame after silence not a heartbeat'); + clearInterval(timer); + }); + })); - test( - 'detect lack of heartbeats', - connectionTest( - (c, done) => { - const opts = Object.create(OPEN_OPTS); - opts.heartbeat = 1; - c.on('error', succeed(done)); - c.open(opts); - }, - (send, wait, done, _socket) => { - happy_open(send, wait).then(succeed(done), fail(done)); - // conspicuously not sending anything ... - }, - ), - ); + test('detect lack of heartbeats', connectionTest((c, done) => { + const opts = Object.create(OPEN_OPTS); + opts.heartbeat = 1; + c.on('error', succeed(done)); + c.open(opts); + }, (send, wait, done, _socket) => { + happy_open(send, wait).then(succeed(done), fail(done)); + // conspicuously not sending anything ... + })); }); From c9201124a0cce6f7fccd4a1ecaad96526711df0e Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Sat, 6 Sep 2025 23:57:05 +0100 Subject: [PATCH 8/8] Reformat test/frame.js --- test/frame.js | 78 ++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/test/frame.js b/test/frame.js index 08f684ba..cc555779 100644 --- a/test/frame.js +++ b/test/frame.js @@ -138,25 +138,16 @@ suite('Parsing', () => { .asTest({ times: 20 }); } - test( - 'Parse trace of methods', - testPartitioning((bufs) => bufs), - ); - - test( - "Parse concat'd methods", - testPartitioning((bufs) => [Buffer.concat(bufs)]), - ); - - test( - 'Parse partitioned methods', - testPartitioning((bufs) => { - const full = Buffer.concat(bufs); - const onethird = Math.floor(full.length / 3); - const twothirds = 2 * onethird; - return [full.subarray(0, onethird), full.subarray(onethird, twothirds), full.subarray(twothirds)]; - }), - ); + test('Parse trace of methods', testPartitioning((bufs) => bufs)); + + test("Parse concat'd methods", testPartitioning((bufs) => [Buffer.concat(bufs)])); + + test('Parse partitioned methods', testPartitioning((bufs) => { + const full = Buffer.concat(bufs); + const onethird = Math.floor(full.length / 3); + const twothirds = 2 * onethird; + return [full.subarray(0, onethird), full.subarray(onethird, twothirds), full.subarray(twothirds)]; + })); }); const FRAME_MAX_MAX = 4096 * 4; @@ -176,31 +167,28 @@ const Content = transform( ); suite('Content framing', () => { - test( - 'Adhere to frame max', - forAll(Content, FrameMax) - .satisfy((content, max) => { - const input = inputs(); - const frames = new Frames(input); - frames.frameMax = max; - frames.sendMessage(0, defs.BasicDeliver, content.method, defs.BasicProperties, content.header, content.body); - let _i = 0; - let largest = 0; - let frame = input.read(); - while (frame) { - _i++; - if (frame.length > largest) largest = frame.length; - if (frame.length > max) { - return false; - } - frame = input.read(); + test('Adhere to frame max', forAll(Content, FrameMax) + .satisfy((content, max) => { + const input = inputs(); + const frames = new Frames(input); + frames.frameMax = max; + frames.sendMessage(0, defs.BasicDeliver, content.method, defs.BasicProperties, content.header, content.body); + let _i = 0; + let largest = 0; + let frame = input.read(); + while (frame) { + _i++; + if (frame.length > largest) largest = frame.length; + if (frame.length > max) { + return false; } - // The ratio of frames to 'contents' should always be >= 2 - // (one properties frame and at least one content frame); > 2 - // indicates fragmentation. The largest is always, of course <= frame max - //console.log('Frames: %d; frames per message: %d; largest frame %d', _i, _i / t.length, largest); - return true; - }) - .asTest(), - ); + frame = input.read(); + } + // The ratio of frames to 'contents' should always be >= 2 + // (one properties frame and at least one content frame); > 2 + // indicates fragmentation. The largest is always, of course <= frame max + //console.log('Frames: %d; frames per message: %d; largest frame %d', _i, _i / t.length, largest); + return true; + }) + .asTest()); });