Skip to content

Commit 0f60060

Browse files
authored
test: add a test for large transfers (libp2p#175)
1 parent 84bfe98 commit 0f60060

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
"it-pipe": "^3.0.1",
155155
"it-pushable": "^3.1.3",
156156
"it-stream-types": "^2.0.1",
157+
"it-to-buffer": "^4.0.2",
157158
"multiformats": "^11.0.2",
158159
"multihashes": "^4.0.3",
159160
"p-defer": "^4.0.0",
@@ -171,7 +172,6 @@
171172
"@types/sinon": "^10.0.14",
172173
"aegir": "^39.0.7",
173174
"delay": "^5.0.0",
174-
"it-all": "^3.0.2",
175175
"it-length": "^3.0.2",
176176
"it-map": "^3.0.3",
177177
"it-pair": "^2.0.6",

test/basics.spec.ts

+47-21
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import * as filter from '@libp2p/websockets/filters'
77
import { WebRTC } from '@multiformats/mafmt'
88
import { multiaddr } from '@multiformats/multiaddr'
99
import { expect } from 'aegir/chai'
10-
import all from 'it-all'
1110
import map from 'it-map'
1211
import { pipe } from 'it-pipe'
12+
import toBuffer from 'it-to-buffer'
1313
import { createLibp2p } from 'libp2p'
1414
import { circuitRelayTransport } from 'libp2p/circuit-relay'
1515
import { identifyService } from 'libp2p/identify'
1616
import { webRTC } from '../src/index.js'
17+
import type { Connection } from '@libp2p/interface-connection'
1718
import type { Libp2p } from '@libp2p/interface-libp2p'
1819

1920
async function createNode (): Promise<Libp2p> {
@@ -50,34 +51,19 @@ async function createNode (): Promise<Libp2p> {
5051
}
5152

5253
describe('basics', () => {
54+
const echo = '/echo/1.0.0'
55+
5356
let localNode: Libp2p
5457
let remoteNode: Libp2p
5558

56-
beforeEach(async () => {
57-
localNode = await createNode()
58-
remoteNode = await createNode()
59-
})
60-
61-
afterEach(async () => {
62-
if (localNode != null) {
63-
await localNode.stop()
64-
}
65-
66-
if (remoteNode != null) {
67-
await remoteNode.stop()
68-
}
69-
})
70-
71-
it('can dial through a relay', async () => {
59+
async function connectNodes (): Promise<Connection> {
7260
const remoteAddr = remoteNode.getMultiaddrs()
7361
.filter(ma => WebRTC.matches(ma)).pop()
7462

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

79-
const echo = '/echo/1.0.0'
80-
8167
await remoteNode.handle(echo, ({ stream }) => {
8268
void pipe(
8369
stream,
@@ -91,6 +77,27 @@ describe('basics', () => {
9177
await localNode.hangUp(multiaddr(process.env.RELAY_MULTIADDR))
9278
await remoteNode.hangUp(multiaddr(process.env.RELAY_MULTIADDR))
9379

80+
return connection
81+
}
82+
83+
beforeEach(async () => {
84+
localNode = await createNode()
85+
remoteNode = await createNode()
86+
})
87+
88+
afterEach(async () => {
89+
if (localNode != null) {
90+
await localNode.stop()
91+
}
92+
93+
if (remoteNode != null) {
94+
await remoteNode.stop()
95+
}
96+
})
97+
98+
it('can dial through a relay', async () => {
99+
const connection = await connectNodes()
100+
94101
// open a stream on the echo protocol
95102
const stream = await connection.newStream(echo)
96103

@@ -100,10 +107,29 @@ describe('basics', () => {
100107
input,
101108
stream,
102109
(source) => map(source, list => list.subarray()),
103-
async (source) => all(source)
110+
async (source) => toBuffer(source)
111+
)
112+
113+
// asset that we got the right data
114+
expect(output).to.equalBytes(toBuffer(input))
115+
})
116+
117+
it('can send a large file', async () => {
118+
const connection = await connectNodes()
119+
120+
// open a stream on the echo protocol
121+
const stream = await connection.newStream(echo)
122+
123+
// send and receive some data
124+
const input = new Array(5).fill(0).map(() => new Uint8Array(1024 * 1024))
125+
const output = await pipe(
126+
input,
127+
stream,
128+
(source) => map(source, list => list.subarray()),
129+
async (source) => toBuffer(source)
104130
)
105131

106132
// asset that we got the right data
107-
expect(output).to.deep.equal(input)
133+
expect(output).to.equalBytes(toBuffer(input))
108134
})
109135
})

0 commit comments

Comments
 (0)