Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaced tests timeouts with flush helper #165

Merged
merged 6 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions test/firewall.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const test = require('brittle')
const createTestnet = require('hyperdht/testnet')
const { timeout } = require('./helpers')
const { timeout, flushConnections } = require('./helpers')

const Hyperswarm = require('..')

const CONNECTION_TIMEOUT = 100
const BACKOFFS = [
100,
200,
Expand Down Expand Up @@ -32,8 +31,8 @@ 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()
await flushConnections(swarm1)

t.alike(serverConnections, 0, 'server did not receive an incoming connection')

Expand Down Expand Up @@ -61,8 +60,8 @@ test('firewalled client - bad server is rejected', async (t) => {
await swarm1.join(topic, { client: false, server: true }).flushed()

HDegroote marked this conversation as resolved.
Show resolved Hide resolved
swarm2.join(topic, { client: true, server: false })

await timeout(CONNECTION_TIMEOUT)
await swarm2.flush()
await flushConnections(swarm2)

t.alike(clientConnections, 0, 'client did not receive an incoming connection')

Expand Down
4 changes: 4 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -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()))
}
44 changes: 24 additions & 20 deletions test/swarm.js
Original file line number Diff line number Diff line change
@@ -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('..')

Expand Down Expand Up @@ -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)
})
mafintosh marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -196,14 +199,10 @@ test('two servers, two clients - simple deduplication', async (t) => {
await swarm2.join(topic).flushed()

await swarm2.flush()
await swarm1.flush()
await timeout(250)
await flushConnections(swarm2)
HDegroote marked this conversation as resolved.
Show resolved Hide resolved

t.is(s1Connections, 1)
t.is(s2Connections, 1)

await swarm1.destroy()
await swarm2.destroy()
await swarm1.flush()
await flushConnections(swarm1)
})

test('one server, two clients - topic multiplexing', async (t) => {
Expand Down Expand Up @@ -283,15 +282,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)
})

Expand All @@ -304,15 +303,19 @@ 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 flushConnections(swarm2)

await swarm1.leave(topic)
await swarm1.flush()
await flushConnections(swarm1)

swarm3.join(topic, { client: true, server: false })

await swarm3.flush()
await timeout(CONNECTION_TIMEOUT)
t.is(serverConnections, 1)
await flushConnections(swarm3)

t.is(swarm2.connections.size, 1)
t.is(swarm3.connections.size, 0)

await swarm1.destroy()
await swarm2.destroy()
Expand Down Expand Up @@ -340,16 +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 timeout(CONNECTION_TIMEOUT)
await flushConnections(swarm3)
t.is(clientConnections, 1)

await swarm2.join(topic).flushed()
await swarm2.flush()
await timeout(CONNECTION_TIMEOUT)
await flushConnections(swarm2)
t.is(clientConnections, 1)

await discovery.refresh()
await swarm3.flush()
await flushConnections(swarm3)
t.is(clientConnections, 2)

await swarm1.destroy()
Expand Down