Skip to content

Commit

Permalink
Fix instability in echo & send tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Dec 13, 2023
1 parent 48aaf01 commit a8c3fd0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
13 changes: 6 additions & 7 deletions test/integration/echo-steps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,26 @@ describe("Echo steps", function () {
const { answer } = await mockPeer.answerOffer(localOffer);
await localConnection.setRemoteDescription(answer);

let messages: Array<any> = [];
let messages: Array<string> = [];
dataChannel1.addEventListener('message', (event) => messages.push("1: " + event.data));

await waitForChannelOpen(dataChannel1);
dataChannel1.send('Test message 1');
dataChannel1.send('Test message 2');

const dataChannel2 = localConnection.createDataChannel("dataChannel2");
dataChannel2.addEventListener('message', (event) => messages.push("2: " + event.data));
await waitForChannelOpen(dataChannel2);
await delay(10); // Delay to guarantee ordering
dataChannel2.send('Test message 2');

await delay(10); // Delay to guarantee ordering
dataChannel1.send('Test message 3');
await delay(50); // Delay to guarantee ordering
dataChannel2.send('Test message 3');

await delay(50); // Delay to guarantee delivery

expect(messages).to.deep.equal([
'1: Test message 1',
'2: Test message 2',
'1: Test message 3',
'1: Test message 2',
'2: Test message 3',
]);
});

Expand Down
10 changes: 7 additions & 3 deletions test/integration/send-steps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {
MockRTC,
delay,
expect,
waitForChannelClose
} from '../test-setup';
Expand Down Expand Up @@ -32,13 +33,14 @@ describe("Send steps", function () {
await localConnection.setRemoteDescription(answer);

// Wait for a response:
let messages: Array<any> = [];
let messages: Array<string> = [];
dataChannel1.addEventListener('message', (event) => messages.push("1: " + event.data));
dataChannel2.addEventListener('message', (event) => messages.push("2: " + event.data));

await waitForChannelClose(dataChannel1);
await delay(1);

expect(messages).to.deep.equal([
expect(messages.sort()).to.deep.equal([
'1: Hello and goodbye',
'2: Hello and goodbye',
]);
Expand Down Expand Up @@ -66,6 +68,7 @@ describe("Send steps", function () {
dataChannel2.addEventListener('message', (event) => messages.push("2: " + event.data));

await waitForChannelClose(dataChannel1);
await delay(1);

// We only see a 2nd channel message:
expect(messages).to.deep.equal([
Expand Down Expand Up @@ -112,12 +115,13 @@ describe("Send steps", function () {
await localConnection.setRemoteDescription(answer);

// Wait for a response:
let messages: Array<any> = [];
let messages: Array<Buffer> = [];
dataChannel1.addEventListener('message', (event) => {
messages.push(Buffer.from(event.data)); // ArrayBuffer -> node Buffer
});

await waitForChannelClose(dataChannel1);
await delay(1);

expect(messages.map(m => m.toString('utf8'))).to.deep.equal([
'Hello from buffer'
Expand Down

0 comments on commit a8c3fd0

Please sign in to comment.