Skip to content

Commit 46b17ae

Browse files
committed
test: recieve lots of data
Adds a test that opens a datachannel. When the remote receives notification of the new channel it writes lots of data into it and closes the channel. The local peer counts the received bytes and asserts that it received them all. This test currently fails intermittently, largely down to the amount of load the process is under at the time. You can run similar code in a browser here: https://codepen.io/achingbrain/pen/KwVPZBP?editors=0012 This works in Chrome and Firefox Nightly, though Firefox is a bit slow.
1 parent 2ef152d commit 46b17ae

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

test/fixtures/connect.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { eventPromise } from './event-promise';
2+
3+
export async function connect (peer1: RTCPeerConnection, peer2: RTCPeerConnection): Promise<void> {
4+
const dc: RTCDataChannel = peer1.createDataChannel('');
5+
6+
// Actions
7+
const peer1Offer = await peer1.createOffer();
8+
await peer2.setRemoteDescription(peer1Offer);
9+
10+
const peer2Answer = await peer2.createAnswer();
11+
await peer1.setRemoteDescription(peer2Answer);
12+
13+
peer1.addEventListener('icecandidate', (e: RTCPeerConnectionIceEvent) => {
14+
peer2.addIceCandidate(e.candidate);
15+
});
16+
17+
peer2.addEventListener('icecandidate', (e: RTCPeerConnectionIceEvent) => {
18+
peer1.addIceCandidate(e.candidate);
19+
});
20+
21+
await eventPromise(dc, 'open');
22+
23+
dc.close();
24+
25+
await eventPromise(dc, 'close');
26+
}

test/jest-tests/polyfill.test.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { expect, jest } from '@jest/globals';
33
import { RTCPeerConnection } from '../../src/polyfill/index';
44
import { PeerConnection } from '../../src/lib/index';
55
import { eventPromise } from '../fixtures/event-promise';
6+
import { connect } from '../fixtures/connect';
67

78
// Polyfill for Promise.withResolvers for Node < 20
89
if (!Promise.withResolvers) {
@@ -328,4 +329,101 @@ describe('polyfill', () => {
328329
expect(spy).toHaveBeenCalled();
329330
expect(connectionState).toEqual(originalFunc());
330331
});
332+
333+
test('it should send lots of data', async () => {
334+
const chunkSize = 1024;
335+
const count = 1024 * 10;
336+
const bytes = chunkSize * count;
337+
let received = 0;
338+
339+
const peer1 = new RTCPeerConnection();
340+
const peer2 = new RTCPeerConnection();
341+
342+
await connect(peer1, peer2);
343+
344+
const receivedAllMessages = Promise.withResolvers<void>();
345+
346+
const incomingChannelPromise = Promise.withResolvers<RTCDataChannel>();
347+
let incomingChannel
348+
349+
peer2.ondatachannel = async (evt): Promise<void> => {
350+
// prevent gc of channel
351+
incomingChannelPromise.resolve(evt.channel)
352+
incomingChannel = evt.channel;
353+
354+
incomingChannel.onmessage = (evt): void => {
355+
received += evt.data.byteLength;
356+
357+
if (received === bytes) {
358+
receivedAllMessages.resolve();
359+
}
360+
}
361+
};
362+
363+
const outgoingChannel = peer1.createDataChannel('');
364+
await eventPromise(outgoingChannel, 'open')
365+
366+
for (let i = 0; i < count; i++) {
367+
outgoingChannel.send(new Uint8Array(chunkSize));
368+
}
369+
370+
outgoingChannel.close();
371+
372+
await Promise.any([
373+
receivedAllMessages.promise,
374+
incomingChannelPromise.promise.then(channel => {
375+
return eventPromise(channel, 'close')
376+
})
377+
]);
378+
379+
expect(received).toEqual(bytes);
380+
381+
peer1.close();
382+
peer2.close();
383+
});
384+
385+
test('it should receive lots of data', async () => {
386+
const chunkSize = 1024;
387+
const count = 1024 * 10;
388+
const bytes = chunkSize * count;
389+
390+
const peer1 = new RTCPeerConnection();
391+
const peer2 = new RTCPeerConnection();
392+
393+
await connect(peer1, peer2);
394+
395+
const receivedAllMessages = Promise.withResolvers<void>();
396+
397+
peer2.ondatachannel = async (evt): Promise<void> => {
398+
const channel = evt.channel;
399+
channel.bufferedAmountLowThreshold = 0;
400+
401+
for (let i = 0; i < count; i++) {
402+
channel.send(new Uint8Array(chunkSize));
403+
}
404+
405+
channel.close();
406+
};
407+
408+
const dc = peer1.createDataChannel('');
409+
let received = 0;
410+
411+
dc.onmessage = (evt): void => {
412+
received += evt.data.byteLength;
413+
414+
if (received === bytes) {
415+
receivedAllMessages.resolve();
416+
}
417+
};
418+
419+
await Promise.any([
420+
receivedAllMessages.promise,
421+
eventPromise(dc, 'close')
422+
]);
423+
424+
expect(received).toEqual(bytes);
425+
426+
peer1.close();
427+
peer2.close();
428+
});
331429
});

0 commit comments

Comments
 (0)