Skip to content

Commit

Permalink
feat: add webrtc-direct connect tests
Browse files Browse the repository at this point in the history
Adds tests that use webrtc-direct transports and also an `UnsupportedError`
error that can be thrown to skip the current test if it's not supported
by the current platform.
  • Loading branch information
achingbrain committed Jun 6, 2024
1 parent 6135583 commit b242cc4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import type { Daemon, DaemonFactory, NodeType, SpawnOptions, TransportType } fro

export function connectTests (factory: DaemonFactory): void {
const nodeTypes: NodeType[] = ['js', 'go']
const transportTypes: TransportType[] = ['tcp', 'webtransport']
const transportTypes: TransportType[] = ['tcp', 'webtransport', 'webrtc-direct']

for (const typeA of nodeTypes) {
for (const typeB of nodeTypes) {
transportTypes.forEach(transport => {
runConnectTests(
transport,
factory,
{ type: typeA, transport },
{ type: typeA, transport, noListen: true },
{ type: typeB, transport }
)
})
Expand All @@ -23,13 +23,23 @@ function runConnectTests (name: string, factory: DaemonFactory, optionsA: SpawnO
describe(`connection.${name}`, () => {
let daemonA: Daemon
let daemonB: Daemon
let skipped: boolean

// Start Daemons
before(async function () {
this.timeout(20 * 1000)

daemonA = await factory.spawn(optionsA)
daemonB = await factory.spawn(optionsB)
try {
daemonA = await factory.spawn(optionsA)
daemonB = await factory.spawn(optionsB)
} catch (err: any) {
if (err.name === 'UnsupportedError') {
skipped = true
return
}

throw err
}
})

// Stop daemons
Expand All @@ -44,6 +54,10 @@ function runConnectTests (name: string, factory: DaemonFactory, optionsA: SpawnO
it(`${optionsA.type} peer to ${optionsB.type} peer over ${name}`, async function () {
this.timeout(10 * 1000)

if (skipped) {
return this.skip()
}

const identify1 = await daemonA.client.identify()
const identify2 = await daemonB.client.identify()

Expand Down
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type PeerIdType = 'rsa' | 'ed25519' | 'secp256k1'
export type PubSubRouter = 'gossipsub' | 'floodsub'
export type Muxer = 'mplex' | 'yamux'
export type Encryption = 'noise' | 'tls'
export type TransportType = 'tcp' | 'webtransport'
export type TransportType = 'tcp' | 'webtransport' | 'webrtc-direct'

export interface SpawnOptions {
type: NodeType
Expand Down Expand Up @@ -95,3 +95,15 @@ export {
streamTests as streamInteropTests,
relayTests as relayInteropTests
}

/**
* Some tests allow skipping certain configurations. When this is necessary,
* `DaemonFactory.spawn` should thow an instance of this error.
*/
export class UnsupportedError extends Error {
constructor (message = 'Unsupported test configuration') {
super(message)

this.name = 'UnsupportedError'
}
}

0 comments on commit b242cc4

Please sign in to comment.