From 105163439fd8c9ac335e0064f60f0c55391fb584 Mon Sep 17 00:00:00 2001 From: rafapaezbas Date: Thu, 7 Dec 2023 11:05:54 +0100 Subject: [PATCH 1/6] replaced tests timeouts with flush helper --- test/firewall.js | 7 ++----- test/swarm.js | 32 ++++++++++++-------------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/test/firewall.js b/test/firewall.js index 9a4486c..23614dc 100644 --- a/test/firewall.js +++ b/test/firewall.js @@ -4,7 +4,6 @@ const { timeout } = require('./helpers') const Hyperswarm = require('..') -const CONNECTION_TIMEOUT = 100 const BACKOFFS = [ 100, 200, @@ -32,8 +31,7 @@ test('firewalled server - bad client is rejected', async (t) => { await swarm2.join(topic, { client: false, server: true }).flushed() swarm1.join(topic, { client: true, server: false }) - - await timeout(CONNECTION_TIMEOUT) + await swarm1.flush() t.alike(serverConnections, 0, 'server did not receive an incoming connection') @@ -61,8 +59,7 @@ test('firewalled client - bad server is rejected', async (t) => { await swarm1.join(topic, { client: false, server: true }).flushed() swarm2.join(topic, { client: true, server: false }) - - await timeout(CONNECTION_TIMEOUT) + await swarm2.flush() t.alike(clientConnections, 0, 'client did not receive an incoming connection') diff --git a/test/swarm.js b/test/swarm.js index 8d4886e..6952274 100644 --- a/test/swarm.js +++ b/test/swarm.js @@ -174,20 +174,23 @@ test('one server, one client - banned peer does not reconnect', async (t) => { }) test('two servers, two clients - simple deduplication', async (t) => { + t.plan(2) const { bootstrap } = await createTestnet(3, t.teardown) const swarm1 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) const swarm2 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) - let s1Connections = 0 - let s2Connections = 0 + t.teardown(async () => { + await swarm1.destroy() + await swarm2.destroy() + }) swarm1.on('connection', (conn) => { - s1Connections++ + t.pass('Swarm 1 connection') conn.on('error', noop) }) swarm2.on('connection', (conn) => { - s2Connections++ + t.pass('Swarm 2 connection') conn.on('error', noop) }) @@ -197,13 +200,6 @@ test('two servers, two clients - simple deduplication', async (t) => { await swarm2.flush() await swarm1.flush() - await timeout(250) - - t.is(s1Connections, 1) - t.is(s2Connections, 1) - - await swarm1.destroy() - await swarm2.destroy() }) test('one server, two clients - topic multiplexing', async (t) => { @@ -283,15 +279,15 @@ test('one server, two clients - first connection', async (t) => { }) test('one server, two clients - if a second client joins after the server leaves, they will not connect', async (t) => { + t.plan(2) + const { bootstrap } = await createTestnet(3, t.teardown) const swarm1 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) const swarm2 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) const swarm3 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) - let serverConnections = 0 swarm1.on('connection', (conn) => { - serverConnections++ conn.on('error', noop) }) @@ -304,15 +300,13 @@ test('one server, two clients - if a second client joins after the server leaves swarm2.join(topic, { client: true, server: false }) await swarm2.flush() - await timeout(CONNECTION_TIMEOUT) - t.is(serverConnections, 1) await swarm1.leave(topic) swarm3.join(topic, { client: true, server: false }) - await swarm3.flush() - await timeout(CONNECTION_TIMEOUT) - t.is(serverConnections, 1) + + t.is(swarm2.connections.size, 1) + t.is(swarm3.connections.size, 0) await swarm1.destroy() await swarm2.destroy() @@ -340,12 +334,10 @@ test('two servers, one client - refreshing a peer discovery instance discovers n const discovery = swarm3.join(topic, { client: true, server: false }) await swarm3.flush() - await timeout(CONNECTION_TIMEOUT) t.is(clientConnections, 1) await swarm2.join(topic).flushed() await swarm2.flush() - await timeout(CONNECTION_TIMEOUT) t.is(clientConnections, 1) await discovery.refresh() From e571faabdb4215d6ab25383bed9c35eb121561a1 Mon Sep 17 00:00:00 2001 From: rafapaezbas Date: Thu, 7 Dec 2023 11:33:43 +0100 Subject: [PATCH 2/6] added flushConnections test helper --- test/firewall.js | 4 +++- test/helpers/index.js | 4 ++++ test/swarm.js | 14 +++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/test/firewall.js b/test/firewall.js index 23614dc..223aab7 100644 --- a/test/firewall.js +++ b/test/firewall.js @@ -1,6 +1,6 @@ const test = require('brittle') const createTestnet = require('hyperdht/testnet') -const { timeout } = require('./helpers') +const { timeout, flushConnections } = require('./helpers') const Hyperswarm = require('..') @@ -32,6 +32,7 @@ test('firewalled server - bad client is rejected', async (t) => { swarm1.join(topic, { client: true, server: false }) await swarm1.flush() + await flushConnections(swarm1) t.alike(serverConnections, 0, 'server did not receive an incoming connection') @@ -60,6 +61,7 @@ test('firewalled client - bad server is rejected', async (t) => { swarm2.join(topic, { client: true, server: false }) await swarm2.flush() + await flushConnections(swarm2) t.alike(clientConnections, 0, 'client did not receive an incoming connection') diff --git a/test/helpers/index.js b/test/helpers/index.js index 459ab63..16e1202 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -1,3 +1,7 @@ exports.timeout = function timeout (ms) { return new Promise((resolve) => setTimeout(resolve, ms)) } + +exports.flushConnections = function (swarm) { + return Promise.all(Array.from(swarm.connections).map(e => e.flush())) +} diff --git a/test/swarm.js b/test/swarm.js index 6952274..a560e9b 100644 --- a/test/swarm.js +++ b/test/swarm.js @@ -1,6 +1,6 @@ const test = require('brittle') const createTestnet = require('hyperdht/testnet') -const { timeout } = require('./helpers') +const { timeout, flushConnections } = require('./helpers') const Hyperswarm = require('..') @@ -199,7 +199,10 @@ test('two servers, two clients - simple deduplication', async (t) => { await swarm2.join(topic).flushed() await swarm2.flush() + await flushConnections(swarm2) + await swarm1.flush() + await flushConnections(swarm1) }) test('one server, two clients - topic multiplexing', async (t) => { @@ -300,10 +303,16 @@ test('one server, two clients - if a second client joins after the server leaves swarm2.join(topic, { client: true, server: false }) await swarm2.flush() + await flushConnections(swarm2) await swarm1.leave(topic) + await swarm1.flush() + await flushConnections(swarm1) + swarm3.join(topic, { client: true, server: false }) + await swarm3.flush() + await flushConnections(swarm3) t.is(swarm2.connections.size, 1) t.is(swarm3.connections.size, 0) @@ -334,14 +343,17 @@ test('two servers, one client - refreshing a peer discovery instance discovers n const discovery = swarm3.join(topic, { client: true, server: false }) await swarm3.flush() + await flushConnections(swarm3) t.is(clientConnections, 1) await swarm2.join(topic).flushed() await swarm2.flush() + await flushConnections(swarm2) t.is(clientConnections, 1) await discovery.refresh() await swarm3.flush() + await flushConnections(swarm3) t.is(clientConnections, 2) await swarm1.destroy() From 471ab803a2f51b8d495afea5c2a9a4b64a689fe1 Mon Sep 17 00:00:00 2001 From: rafapaezbas Date: Thu, 7 Dec 2023 11:47:21 +0100 Subject: [PATCH 3/6] added swarm flush to flushConnections helper --- test/firewall.js | 2 -- test/helpers/index.js | 3 ++- test/swarm.js | 21 ++++++++------------- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/test/firewall.js b/test/firewall.js index 223aab7..5cf3681 100644 --- a/test/firewall.js +++ b/test/firewall.js @@ -31,7 +31,6 @@ test('firewalled server - bad client is rejected', async (t) => { await swarm2.join(topic, { client: false, server: true }).flushed() swarm1.join(topic, { client: true, server: false }) - await swarm1.flush() await flushConnections(swarm1) t.alike(serverConnections, 0, 'server did not receive an incoming connection') @@ -60,7 +59,6 @@ test('firewalled client - bad server is rejected', async (t) => { await swarm1.join(topic, { client: false, server: true }).flushed() swarm2.join(topic, { client: true, server: false }) - await swarm2.flush() await flushConnections(swarm2) t.alike(clientConnections, 0, 'client did not receive an incoming connection') diff --git a/test/helpers/index.js b/test/helpers/index.js index 16e1202..f352ca0 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -2,6 +2,7 @@ exports.timeout = function timeout (ms) { return new Promise((resolve) => setTimeout(resolve, ms)) } -exports.flushConnections = function (swarm) { +exports.flushConnections = async function (swarm) { + await swarm.flush() return Promise.all(Array.from(swarm.connections).map(e => e.flush())) } diff --git a/test/swarm.js b/test/swarm.js index a560e9b..99d1a48 100644 --- a/test/swarm.js +++ b/test/swarm.js @@ -174,7 +174,12 @@ test('one server, one client - banned peer does not reconnect', async (t) => { }) test('two servers, two clients - simple deduplication', async (t) => { - t.plan(2) + const connection1Test = t.test('connection1') + const connection2Test = t.test('connection2') + + connection1Test.plan(1) + connection2Test.plan(1) + const { bootstrap } = await createTestnet(3, t.teardown) const swarm1 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) @@ -186,11 +191,11 @@ test('two servers, two clients - simple deduplication', async (t) => { }) swarm1.on('connection', (conn) => { - t.pass('Swarm 1 connection') + connection1Test.pass('Swarm 1 connection') conn.on('error', noop) }) swarm2.on('connection', (conn) => { - t.pass('Swarm 2 connection') + connection2Test.pass('Swarm 2 connection') conn.on('error', noop) }) @@ -198,10 +203,7 @@ test('two servers, two clients - simple deduplication', async (t) => { await swarm1.join(topic).flushed() await swarm2.join(topic).flushed() - await swarm2.flush() await flushConnections(swarm2) - - await swarm1.flush() await flushConnections(swarm1) }) @@ -302,16 +304,12 @@ test('one server, two clients - if a second client joins after the server leaves swarm2.join(topic, { client: true, server: false }) - await swarm2.flush() await flushConnections(swarm2) await swarm1.leave(topic) - await swarm1.flush() await flushConnections(swarm1) swarm3.join(topic, { client: true, server: false }) - - await swarm3.flush() await flushConnections(swarm3) t.is(swarm2.connections.size, 1) @@ -342,17 +340,14 @@ test('two servers, one client - refreshing a peer discovery instance discovers n await swarm1.join(topic).flushed() const discovery = swarm3.join(topic, { client: true, server: false }) - await swarm3.flush() await flushConnections(swarm3) t.is(clientConnections, 1) await swarm2.join(topic).flushed() - await swarm2.flush() await flushConnections(swarm2) t.is(clientConnections, 1) await discovery.refresh() - await swarm3.flush() await flushConnections(swarm3) t.is(clientConnections, 2) From d76918ca2739d67cddabe8dbfda926a3f989dcd4 Mon Sep 17 00:00:00 2001 From: rafapaezbas Date: Thu, 7 Dec 2023 13:30:17 +0100 Subject: [PATCH 4/6] refactor swarm tests --- test/helpers/index.js | 3 +- test/swarm.js | 109 +++++++++++++++++++++++------------------- 2 files changed, 62 insertions(+), 50 deletions(-) diff --git a/test/helpers/index.js b/test/helpers/index.js index f352ca0..9d77814 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -4,5 +4,6 @@ exports.timeout = function timeout (ms) { exports.flushConnections = async function (swarm) { await swarm.flush() - return Promise.all(Array.from(swarm.connections).map(e => e.flush())) + await Promise.all(Array.from(swarm.connections).map(e => e.flush())) + await new Promise(resolve => setImmediate(resolve)) } diff --git a/test/swarm.js b/test/swarm.js index 99d1a48..7a0acc0 100644 --- a/test/swarm.js +++ b/test/swarm.js @@ -18,11 +18,15 @@ test('one server, one client - first connection', async (t) => { const swarm1 = new Hyperswarm({ bootstrap }) const swarm2 = new Hyperswarm({ bootstrap }) - const connected = t.test('connection') - connected.plan(1) + t.plan(1) + + t.teardown(async () => { + await swarm1.destroy() + await swarm2.destroy() + }) swarm2.on('connection', (conn) => { - connected.pass('swarm2') + t.pass('swarm2') conn.on('error', noop) conn.end() }) @@ -35,12 +39,7 @@ test('one server, one client - first connection', async (t) => { await swarm1.join(topic, { server: true, client: false }).flushed() swarm2.join(topic, { client: true, server: false }) - await swarm2.flush() - - await connected - - await swarm1.destroy() - await swarm2.destroy() + await flushConnections(swarm2) }) test('two servers - first connection', async (t) => { @@ -49,17 +48,25 @@ test('two servers - first connection', async (t) => { const swarm1 = new Hyperswarm({ bootstrap }) const swarm2 = new Hyperswarm({ bootstrap }) - const connected = t.test('connection') - connected.plan(2) + const connection1Test = t.test('connection1') + const connection2Test = t.test('connection2') + + connection1Test.plan(1) + connection2Test.plan(1) + + t.teardown(async () => { + await swarm1.destroy() + await swarm2.destroy() + }) swarm1.on('connection', (conn) => { conn.on('error', noop) - connected.pass('swarm1') + connection1Test.pass('swarm1') conn.end() }) swarm2.on('connection', (conn) => { conn.on('error', noop) - connected.pass('swarm2') + connection2Test.pass('swarm2') conn.end() }) @@ -67,11 +74,6 @@ test('two servers - first connection', async (t) => { await swarm1.join(topic).flushed() await swarm2.join(topic).flushed() - - await connected - - await swarm1.destroy() - await swarm2.destroy() }) test('one server, one client - single reconnect', async (t) => { @@ -80,8 +82,16 @@ test('one server, one client - single reconnect', async (t) => { const swarm1 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) const swarm2 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) - const reconnected = t.test('reconnection') - reconnected.plan(2) + const reconnection1Test = t.test('reconnection1') + const reconnection2Test = t.test('reconnection2') + + reconnection1Test.plan(1) + reconnection2Test.plan(1) + + t.teardown(async () => { + await swarm1.destroy() + await swarm2.destroy() + }) let clientDisconnected = false let serverDisconnected = false @@ -93,7 +103,7 @@ test('one server, one client - single reconnect', async (t) => { conn.destroy() return } - reconnected.pass('client') + reconnection2Test.pass('client') conn.end() }) swarm1.on('connection', (conn) => { @@ -103,18 +113,13 @@ test('one server, one client - single reconnect', async (t) => { conn.destroy() return } - reconnected.pass('server') + reconnection1Test.pass('client') conn.end() }) const topic = Buffer.alloc(32).fill('hello world') await swarm1.join(topic, { client: false, server: true }).flushed() swarm2.join(topic, { client: true, server: false }) - - await reconnected - - await swarm1.destroy() - await swarm2.destroy() }) test('one server, one client - maximum reconnects', async (t) => { @@ -249,21 +254,32 @@ test('one server, two clients - first connection', async (t) => { const swarm2 = new Hyperswarm({ bootstrap }) const swarm3 = new Hyperswarm({ bootstrap }) - const connected = t.test('connection') - connected.plan(3) + const connection1Test = t.test('connection1') + const connection2Test = t.test('connection2') + const connection3Test = t.test('connection3') + + connection1Test.plan(1) + connection2Test.plan(1) + connection3Test.plan(1) + + t.teardown(async () => { + await swarm1.destroy() + await swarm2.destroy() + await swarm3.destroy() + }) swarm1.on('connection', (conn) => { - connected.pass('swarm1') + connection1Test.pass('swarm1') conn.on('error', noop) conn.destroy() }) swarm2.on('connection', (conn) => { - connected.pass('swarm2') + connection2Test.pass('swarm2') conn.on('error', noop) conn.destroy() }) swarm3.on('connection', (conn) => { - connected.pass('swarm3') + connection3Test.pass('swarm3') conn.on('error', noop) conn.destroy() }) @@ -275,12 +291,6 @@ test('one server, two clients - first connection', async (t) => { await swarm2.flush() await swarm3.flush() - - await connected - - await swarm1.destroy() - await swarm2.destroy() - await swarm3.destroy() }) test('one server, two clients - if a second client joins after the server leaves, they will not connect', async (t) => { @@ -532,16 +542,24 @@ test('multiple discovery sessions with different opts', async (t) => { const topic = Buffer.alloc(32).fill('hello world') - const connected = t.test('connection') - connected.plan(2) + const connection1Test = t.test('connection1') + const connection2Test = t.test('connection2') + + connection1Test.plan(1) + connection2Test.plan(1) + + t.teardown(async () => { + await swarm1.destroy() + await swarm2.destroy() + }) swarm1.on('connection', (conn) => { - connected.pass('swarm1') + connection1Test.pass('swarm1') conn.on('error', noop) }) swarm2.on('connection', (conn) => { - connected.pass('swarm2') + connection2Test.pass('swarm2') conn.on('error', noop) }) @@ -553,11 +571,6 @@ test('multiple discovery sessions with different opts', async (t) => { await discovery1.destroy() // should not prevent server connections await discovery2.flushed() - - await connected - - await swarm1.destroy() - await swarm2.destroy() }) test('closing all discovery sessions clears all peer-discovery objects', async t => { @@ -657,8 +670,6 @@ test('peer-discovery object deleted when corresponding connection closes (client swarm2.join(topic, { client: true, server: false }) await swarm2.flush() - await connected - t.is(swarm2.peers.size, 1) await swarm1.destroy() }) From 12a751ba994aa61c5afdcd0843bbe09791383ea1 Mon Sep 17 00:00:00 2001 From: rafapaezbas Date: Fri, 8 Dec 2023 09:47:23 +0100 Subject: [PATCH 5/6] removed uneeded test flushes --- test/swarm.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/test/swarm.js b/test/swarm.js index 7a0acc0..d711703 100644 --- a/test/swarm.js +++ b/test/swarm.js @@ -37,9 +37,7 @@ test('one server, one client - first connection', async (t) => { const topic = Buffer.alloc(32).fill('hello world') await swarm1.join(topic, { server: true, client: false }).flushed() - swarm2.join(topic, { client: true, server: false }) - await flushConnections(swarm2) }) test('two servers - first connection', async (t) => { @@ -207,9 +205,6 @@ test('two servers, two clients - simple deduplication', async (t) => { const topic = Buffer.alloc(32).fill('hello world') await swarm1.join(topic).flushed() await swarm2.join(topic).flushed() - - await flushConnections(swarm2) - await flushConnections(swarm1) }) test('one server, two clients - topic multiplexing', async (t) => { @@ -567,10 +562,9 @@ test('multiple discovery sessions with different opts', async (t) => { await swarm1.flush() const discovery1 = swarm2.join(topic, { client: true, server: false }) - const discovery2 = swarm2.join(topic, { client: false, server: true }) + swarm2.join(topic, { client: false, server: true }) await discovery1.destroy() // should not prevent server connections - await discovery2.flushed() }) test('closing all discovery sessions clears all peer-discovery objects', async t => { From fa38209307fc49b03f5410495b8222a23c322944 Mon Sep 17 00:00:00 2001 From: rafapaezbas Date: Fri, 8 Dec 2023 11:04:18 +0100 Subject: [PATCH 6/6] improved firewall test --- test/firewall.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/firewall.js b/test/firewall.js index 5cf3681..679141a 100644 --- a/test/firewall.js +++ b/test/firewall.js @@ -41,6 +41,7 @@ test('firewalled server - bad client is rejected', async (t) => { test('firewalled client - bad server is rejected', async (t) => { const { bootstrap } = await createTestnet(3, t.teardown) + t.plan(2) const swarm1 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) const swarm2 = new Hyperswarm({ @@ -48,7 +49,9 @@ test('firewalled client - bad server is rejected', async (t) => { backoffs: BACKOFFS, jitter: 0, firewall: remotePublicKey => { - return remotePublicKey.equals(swarm1.keyPair.publicKey) + const firewalled = remotePublicKey.equals(swarm1.keyPair.publicKey) + t.ok(firewalled, 'The peer got firewalled') + return firewalled } })