From 0f60060c9ceaf2bf2142df25f32174112edf6ec9 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Mon, 12 Jun 2023 18:04:16 +0200 Subject: [PATCH] test: add a test for large transfers (#175) --- package.json | 2 +- test/basics.spec.ts | 68 +++++++++++++++++++++++++++++++-------------- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index cf29d0c7c6..2c1e8c841d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/test/basics.spec.ts b/test/basics.spec.ts index 2f9efedb1a..ec15c43703 100644 --- a/test/basics.spec.ts +++ b/test/basics.spec.ts @@ -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 { @@ -50,25 +51,12 @@ async function createNode (): Promise { } 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 { const remoteAddr = remoteNode.getMultiaddrs() .filter(ma => WebRTC.matches(ma)).pop() @@ -76,8 +64,6 @@ describe('basics', () => { throw new Error('Remote peer could not listen on relay') } - const echo = '/echo/1.0.0' - await remoteNode.handle(echo, ({ stream }) => { void pipe( stream, @@ -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) @@ -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)) }) })