Skip to content

Commit 5d0c407

Browse files
committed
test: mixed message types should be sent in order
Resolving blob content is done asynchronously so if two messages are sent synchronously where the first is a blob and the second is not a blob, the second message will get passed off to libdatachannel first.
1 parent 2ef152d commit 5d0c407

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

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: 36 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,39 @@ describe('polyfill', () => {
328329
expect(spy).toHaveBeenCalled();
329330
expect(connectionState).toEqual(originalFunc());
330331
});
332+
333+
test('it should send mixed types in order', async () => {
334+
const peer1 = new RTCPeerConnection();
335+
const peer2 = new RTCPeerConnection();
336+
337+
await connect(peer1, peer2);
338+
339+
const receivedAllMessages = Promise.withResolvers<any[]>();
340+
341+
peer2.ondatachannel = (evt): void => {
342+
const channel = evt.channel;
343+
const output = [];
344+
345+
channel.onmessage = (evt): void => {
346+
output.push(evt.data);
347+
348+
if (output.length === 2) {
349+
receivedAllMessages.resolve(output);
350+
}
351+
};
352+
};
353+
354+
const dc = peer1.createDataChannel('');
355+
356+
await eventPromise(dc, 'open');
357+
358+
dc.send(new Blob(['hello']));
359+
dc.send('world');
360+
361+
const messages = await receivedAllMessages.promise;
362+
363+
expect(messages[0]).toBeInstanceOf(ArrayBuffer);
364+
expect(new TextDecoder().decode(messages[0])).toEqual('hello');
365+
expect(messages[1]).toEqual('world');
366+
})
331367
});

0 commit comments

Comments
 (0)