Skip to content

Commit

Permalink
chore: remove setTimeout from test
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Sep 10, 2024
1 parent 6d72709 commit c2bc7fe
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions .release-please.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"packages/peer-id": {},
"packages/peer-record": {},
"packages/peer-store": {},
"packages/pnet": {},
"packages/protocol-autonat": {},
"packages/protocol-dcutr": {},
"packages/protocol-echo": {},
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
"release:rc": "aegir release-rc",
"docs": "aegir docs",
"docs:no-publish": "aegir docs --publish false -- --exclude interop --exclude doc",
"postinstall": "patch-package"
"postinstall": "patch-package && rm -rf node_modules/@chainsafe/libp2p-yamux/node_modules node_modules/@chainsafe/libp2p-noise/node_modules node_modules/@chainsafe/libp2p-gossipsub/node_modules node_modules/@libp2p/daemon-client/node_modules node_modules/@libp2p/daemon-server/node_modules node_modules/@libp2p/daemon-protocol/node_modules node_modules/@libp2p/interop/node_modules"
},
"devDependencies": {
"aegir": "^44.0.1",
"npm-run-all": "^4.1.5",
"patch-package": "^8.0.0"
"patch-package": "^8.0.0",
"rimraf": "^6.0.1"
},
"eslintConfig": {
"extends": "ipfs",
Expand Down
17 changes: 13 additions & 4 deletions packages/kad-dht/test/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ describe('QueryManager', () => {
})
await manager.start()

const deferred = pDefer()
const controller = new AbortController()
let aborted

Expand All @@ -331,16 +332,24 @@ describe('QueryManager', () => {
aborted = true
})

deferred.resolve()

await delay(1000)

yield topology[peer.toString()].event
}

setTimeout(() => {
controller.abort()
}, 10)
// start the query
const queryPromise = all(manager.run(key, queryFunc, { signal: controller.signal }))

// wait for the query function to be invoked
await deferred.promise

// abort the query
controller.abort()

await expect(all(manager.run(key, queryFunc, { signal: controller.signal }))).to.eventually.be.rejected()
// the should have been aborted
await expect(queryPromise).to.eventually.be.rejected()
.with.property('name', 'QueryAbortedError')

expect(aborted).to.be.true()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export async function initiateConnection ({ rtcConfiguration, dataChannel, signa

onProgress?.(new CustomProgressEvent('webrtc:read-sdp-answer'))

log.trace('initiator read SDP answer')

// read answer
const answerMessage = await messageStream.read({
signal
Expand All @@ -147,7 +149,7 @@ export async function initiateConnection ({ rtcConfiguration, dataChannel, signa
throw new SDPHandshakeFailedError('Remote should send an SDP answer')
}

log.trace('initiator receive SDP answer %s', answerMessage.data)
log.trace('initiator received SDP answer %s', answerMessage.data)

const answerSdp = new RTCSessionDescription({ type: 'answer', sdp: answerMessage.data })
await peerConnection.setRemoteDescription(answerSdp).catch(err => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { pbStream } from 'it-protobuf-stream'
import { SDPHandshakeFailedError } from '../error.js'
import { type RTCPeerConnection, RTCSessionDescription } from '../webrtc/index.js'
import { Message } from './pb/message.js'
import { readCandidatesUntilConnected } from './util.js'
import { getConnectionState, readCandidatesUntilConnected } from './util.js'
import type { Logger } from '@libp2p/interface'
import type { IncomingStreamData } from '@libp2p/interface-internal'

Expand Down Expand Up @@ -40,6 +40,8 @@ export async function handleIncomingStream ({ peerConnection, stream, signal, co
})
}

log.trace('recipient read SDP offer')

// read an SDP offer
const pbOffer = await messageStream.read({
signal
Expand All @@ -49,7 +51,7 @@ export async function handleIncomingStream ({ peerConnection, stream, signal, co
throw new SDPHandshakeFailedError(`expected message type SDP_OFFER, received: ${pbOffer.type ?? 'undefined'} `)
}

log.trace('recipient receive SDP offer %s', pbOffer.data)
log.trace('recipient received SDP offer %s', pbOffer.data)

const offer = new RTCSessionDescription({
type: 'offer',
Expand Down Expand Up @@ -88,7 +90,7 @@ export async function handleIncomingStream ({ peerConnection, stream, signal, co
log
})
} catch (err: any) {
if (peerConnection.connectionState !== 'connected') {
if (getConnectionState(peerConnection) !== 'connected') {
log.error('error while handling signaling stream from peer %a', connection.remoteAddr, err)

peerConnection.close()
Expand Down
4 changes: 2 additions & 2 deletions packages/transport-webrtc/src/private-to-private/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ export const readCandidatesUntilConnected = async (pc: RTCPeerConnection, stream
} catch (err) {
options.log.error('%s error parsing ICE candidate', options.direction, err)

if (options.signal?.aborted === true) {
if (options.signal?.aborted === true && getConnectionState(pc) !== 'connected') {
throw err
}
}
}

function getConnectionState (pc: RTCPeerConnection): string {
export function getConnectionState (pc: RTCPeerConnection): string {
return isFirefox ? pc.iceConnectionState : pc.connectionState
}

Expand Down

0 comments on commit c2bc7fe

Please sign in to comment.