Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

fix: add browser-to-browser test for bi-directional communication #172

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
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
47 changes: 28 additions & 19 deletions examples/browser-to-browser/tests/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const sendBtn = '#send'
const output = '#output'
const listeningAddresses = '#multiaddrs'

const message = 'hello'
let url

// we spawn a js libp2p relay
Expand Down Expand Up @@ -68,37 +67,47 @@ test.describe('browser to browser example:', () => {
await page.goto(url)
})

test('should connect to a relay node', async ({ page, context }) => {
// first page dials the relay
const relayedAddress = await dialRelay(page, relayNodeAddr)
test('should connect to a relay node', async ({ page: pageA, context }) => {
// load second page
const pageB = await context.newPage()
await pageB.goto(url)

// load second page and use `peer` as the connectAddr
const pageTwo = await context.newPage();
await pageTwo.goto(url)
await dialPeerOverRelay(pageTwo, relayedAddress)
// connect both pages to the relay
const relayedAddressA = await dialRelay(pageA, relayNodeAddr)
const relayedAddressB = await dialRelay(pageB, relayNodeAddr)

// dial first page from second page over relay
await dialPeerOverRelay(pageA, relayedAddressB)
await dialPeerOverRelay(pageB, relayedAddressA)

// stop the relay
await relayNode.stop()

// send the message to the peer over webRTC
await pageTwo.fill(messageInput, message)
await pageTwo.click(sendBtn)
await echoMessagePeer(pageB, 'hello B')

// check the message was echoed back
const outputLocator = pageTwo.locator(output)
await expect(outputLocator).toHaveText(/Sending message/)
await expect(outputLocator).toHaveText(/Received message/, { timeout: 60000 })
await echoMessagePeer(pageA, 'hello A')
})
})

async function echoMessagePeer (page, message) {
// send the message to the peer over webRTC
await page.fill(messageInput, message)
await page.click(sendBtn)

// check the message was echoed back
const outputLocator = page.locator(output)
await expect(outputLocator).toContainText(`Sending message '${message}'`)
await expect(outputLocator).toContainText(`Received message '${message}'`)
}

async function dialRelay (page, address) {
// add the go libp2p multiaddress to the input field and submit
await page.fill(connectAddr, address)
await page.click(connectBtn)

const outputLocator = page.locator(output)
await expect(outputLocator).toHaveText(/Dialing/)
await expect(outputLocator).toHaveText(/Connected/)
await expect(outputLocator).toContainText(`Dialing '${address}'`)
await expect(outputLocator).toContainText(`Connected to '${address}'`)

const multiaddrsLocator = page.locator(listeningAddresses)
await expect(multiaddrsLocator).toHaveText(/webrtc/)
Expand All @@ -115,6 +124,6 @@ async function dialPeerOverRelay (page, address) {
await page.click(connectBtn)

const outputLocator = page.locator(output)
await expect(outputLocator).toHaveText(/Dialing/)
await expect(outputLocator).toHaveText(/Connected/)
await expect(outputLocator).toContainText(`Dialing '${address}'`)
await expect(outputLocator).toContainText(`Connected to '${address}'`)
}
6 changes: 6 additions & 0 deletions src/private-to-private/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ export class WebRTCTransport implements Transport, Startable {
// reset the stream in case of any error
signalingStream.reset()
throw err
} finally {
// Close the signaling connection
await connection.close()
}
}

Expand All @@ -143,6 +146,9 @@ export class WebRTCTransport implements Transport, Startable {
} catch (err) {
stream.reset()
throw err
} finally {
// Close the signaling connection
await connection.close()
}
}
}
Expand Down