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

chore: fix various type errors in LocalDiscovery tests #601

Merged
merged 1 commit into from
May 2, 2024
Merged
Changes from all 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
16 changes: 13 additions & 3 deletions tests/discovery/local-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
LocalDiscovery,
} from '../../src/discovery/local-discovery.js'
import NoiseSecretStream from '@hyperswarm/secret-stream'
/** @typedef {import('../../src/utils.js').OpenedNoiseStream} OpenedNoiseStream */

test('peer discovery - discovery and sharing of data', async (t) => {
const deferred = pDefer()
Expand Down Expand Up @@ -115,7 +116,9 @@ async function noiseConnect({ port, address }, keyPair) {
* @param {number} [opts.nPeers] Number of peers to spawn (default 20)
*/
async function testMultiple(t, { period, nPeers = 20 }) {
/** @type {Map<string, LocalDiscovery>} */
const peersById = new Map()
/** @type {Map<string, OpenedNoiseStream[]>} */
const connsById = new Map()
// t.plan(3 * nPeers + 1)

Expand All @@ -136,6 +139,7 @@ async function testMultiple(t, { period, nPeers = 20 }) {
const discovery = new LocalDiscovery({ identityKeypair })
const peerId = keyToPublicId(discovery.publicKey)
peersById.set(peerId, discovery)
/** @type {OpenedNoiseStream[]} */
const conns = []
connsById.set(peerId, conns)
discovery.on('connection', (conn) => {
Expand Down Expand Up @@ -172,8 +176,9 @@ async function testMultiple(t, { period, nPeers = 20 }) {

for (const peerId of peerIds) {
const expected = peerIds.filter((id) => id !== peerId).sort()
const actual = connsById
.get(peerId)
const conns = connsById.get(peerId)
if (!conns) throw new Error('Expected connections for peer ID ' + peerId)
const actual = conns
.filter((conn) => !conn.destroyed)
.map((conn) => keyToPublicId(conn.remotePublicKey))
.sort()
Expand All @@ -187,11 +192,16 @@ async function testMultiple(t, { period, nPeers = 20 }) {
}
}

/**
* @param {import('brittle').TestInstance} t
* @param {Error} e
*/
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'
const expectedError =
e.message === ERR_DUPLICATE || ('code' in e && e.code === 'ECONNRESET')
t.ok(expectedError, 'connection closed with expected error')
}
Loading