Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Oct 20, 2023
1 parent a8be10d commit 9cc5ba3
Showing 1 changed file with 14 additions and 45 deletions.
59 changes: 14 additions & 45 deletions tests/discovery/mdns.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,12 @@ test('mdns - discovery and sharing of data', (t) => {
const str = 'hi'

mdnsDiscovery1.on('connection', (stream) => {
stream.on('error', (e) => {
// We expected connections to be closed when duplicates happen. On the
// closing side the error will be ERR_DUPLICATE, but on the other side
// the error will be an ECONNRESET - the error is not sent over the
// connection
const expectedError =
e.message === ERR_DUPLICATE || e.code === 'ECONNRESET'
t.ok(expectedError, 'connection closed with expected error')
})
stream.on('error', handleConnectionError.bind(null, t))
stream.write(str)
})

mdnsDiscovery2.on('connection', (stream) => {
stream.on('error', (e) => {
// We expected connections to be closed when duplicates happen. On the
// closing side the error will be ERR_DUPLICATE, but on the other side
// the error will be an ECONNRESET - the error is not sent over the
// connection
const expectedError =
e.message === ERR_DUPLICATE || e.code === 'ECONNRESET'
t.ok(expectedError, 'connection closed with expected error')
})
stream.on('error', handleConnectionError.bind(null, t))
stream.on('data', (d) => {
t.is(d.toString(), str, 'expected data written')
Promise.all([
Expand Down Expand Up @@ -72,31 +56,15 @@ test('deduplicate incoming connections', async (t) => {
await discovery.start()

discovery.on('connection', (conn) => {
conn.on('error', (e) => {
// We expected connections to be closed when duplicates happen. On the
// closing side the error will be ERR_DUPLICATE, but on the other side
// the error will be an ECONNRESET - the error is not sent over the
// connection
const expectedError =
e.message === ERR_DUPLICATE || e.code === 'ECONNRESET'
t.ok(expectedError, 'connection closed with expected error')
})
conn.on('error', handleConnectionError.bind(null, t))
localConnections.add(conn)
conn.on('close', () => localConnections.delete(conn))
})

const addrInfo = discovery.address()
for (let i = 0; i < 20; i++) {
noiseConnect(addrInfo, remoteKp).then((conn) => {
conn.on('error', (e) => {
// We expected connections to be closed when duplicates happen. On the
// closing side the error will be ERR_DUPLICATE, but on the other side
// the error will be an ECONNRESET - the error is not sent over the
// connection
const expectedError =
e.message === ERR_DUPLICATE || e.code === 'ECONNRESET'
t.ok(expectedError, 'connection closed with expected error')
})
conn.on('error', handleConnectionError.bind(null, t))
conn.on('connect', () => remoteConnections.add(conn))
conn.on('close', () => remoteConnections.delete(conn))
})
Expand Down Expand Up @@ -151,15 +119,7 @@ async function testMultiple(t, { period, nPeers = 20 }) {
const conns = []
connsById.set(peerId, conns)
discovery.on('connection', (conn) => {
conn.on('error', (e) => {
// We expected connections to be closed when duplicates happen. On the
// closing side the error will be ERR_DUPLICATE, but on the other side
// the error will be an ECONNRESET - the error is not sent over the
// connection
const expectedError =
e.message === ERR_DUPLICATE || e.code === 'ECONNRESET'
t.ok(expectedError, 'connection closed with expected error')
})
conn.on('error', handleConnectionError.bind(null, t))
conns.push(conn)
if (conns.length >= nPeers - 1) onConnected()
})
Expand Down Expand Up @@ -203,3 +163,12 @@ async function testMultiple(t, { period, nPeers = 20 }) {
await Promise.all(stopPromises)
t.pass('teardown complete')
}

function handleConnectionError(t, e) {
// We expected connections to be closed when duplicates happen. On the
// closing side the error will be ERR_DUPLICATE, but on the other side
// the error will be an ECONNRESET - the error is not sent over the
// connection
const expectedError = e.message === ERR_DUPLICATE || e.code === 'ECONNRESET'
t.ok(expectedError, 'connection closed with expected error')
}

0 comments on commit 9cc5ba3

Please sign in to comment.