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

test: add a test for large transfers #175

Merged
merged 1 commit 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"it-pipe": "^3.0.1",
"it-pushable": "^3.1.3",
"it-stream-types": "^2.0.1",
"it-to-buffer": "^4.0.2",
"multiformats": "^11.0.2",
"multihashes": "^4.0.3",
"p-defer": "^4.0.0",
Expand All @@ -171,7 +172,6 @@
"@types/sinon": "^10.0.14",
"aegir": "^39.0.7",
"delay": "^5.0.0",
"it-all": "^3.0.2",
"it-length": "^3.0.2",
"it-map": "^3.0.3",
"it-pair": "^2.0.6",
Expand Down
68 changes: 47 additions & 21 deletions test/basics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import * as filter from '@libp2p/websockets/filters'
import { WebRTC } from '@multiformats/mafmt'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import all from 'it-all'
import map from 'it-map'
import { pipe } from 'it-pipe'
import toBuffer from 'it-to-buffer'
import { createLibp2p } from 'libp2p'
import { circuitRelayTransport } from 'libp2p/circuit-relay'
import { identifyService } from 'libp2p/identify'
import { webRTC } from '../src/index.js'
import type { Connection } from '@libp2p/interface-connection'
import type { Libp2p } from '@libp2p/interface-libp2p'

async function createNode (): Promise<Libp2p> {
Expand Down Expand Up @@ -50,34 +51,19 @@ async function createNode (): Promise<Libp2p> {
}

describe('basics', () => {
const echo = '/echo/1.0.0'

let localNode: Libp2p
let remoteNode: Libp2p

beforeEach(async () => {
localNode = await createNode()
remoteNode = await createNode()
})

afterEach(async () => {
if (localNode != null) {
await localNode.stop()
}

if (remoteNode != null) {
await remoteNode.stop()
}
})

it('can dial through a relay', async () => {
async function connectNodes (): Promise<Connection> {
const remoteAddr = remoteNode.getMultiaddrs()
.filter(ma => WebRTC.matches(ma)).pop()

if (remoteAddr == null) {
throw new Error('Remote peer could not listen on relay')
}

const echo = '/echo/1.0.0'

await remoteNode.handle(echo, ({ stream }) => {
void pipe(
stream,
Expand All @@ -91,6 +77,27 @@ describe('basics', () => {
await localNode.hangUp(multiaddr(process.env.RELAY_MULTIADDR))
await remoteNode.hangUp(multiaddr(process.env.RELAY_MULTIADDR))

return connection
}

beforeEach(async () => {
localNode = await createNode()
remoteNode = await createNode()
})

afterEach(async () => {
if (localNode != null) {
await localNode.stop()
}

if (remoteNode != null) {
await remoteNode.stop()
}
})

it('can dial through a relay', async () => {
const connection = await connectNodes()

// open a stream on the echo protocol
const stream = await connection.newStream(echo)

Expand All @@ -100,10 +107,29 @@ describe('basics', () => {
input,
stream,
(source) => map(source, list => list.subarray()),
async (source) => all(source)
async (source) => toBuffer(source)
)

// asset that we got the right data
expect(output).to.equalBytes(toBuffer(input))
})

it('can send a large file', async () => {
const connection = await connectNodes()

// open a stream on the echo protocol
const stream = await connection.newStream(echo)

// send and receive some data
const input = new Array(5).fill(0).map(() => new Uint8Array(1024 * 1024))
const output = await pipe(
input,
stream,
(source) => map(source, list => list.subarray()),
async (source) => toBuffer(source)
)

// asset that we got the right data
expect(output).to.deep.equal(input)
expect(output).to.equalBytes(toBuffer(input))
})
})