Skip to content

Commit

Permalink
[test] Merge some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Dec 9, 2023
1 parent dd1994d commit 5a3036e
Showing 1 changed file with 19 additions and 74 deletions.
93 changes: 19 additions & 74 deletions test/receiver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1085,96 +1085,41 @@ describe('Receiver', () => {
receiver.write(Buffer.from([0x88, 0x03, 0x03, 0xe8, 0xf8]));
});

it("waits a microtask after each 'message' event", (done) => {
const messages = [];
const receiver = new Receiver();

receiver.on('message', (data, isBinary) => {
assert.ok(!isBinary);

const message = data.toString();
messages.push(message);

// `queueMicrotask()` is not available in Node.js < 11.
Promise.resolve().then(() => {
messages.push(`microtask ${message}`);

if (messages.length === 6) {
assert.deepStrictEqual(messages, [
'1',
'microtask 1',
'2',
'microtask 2',
'3',
'microtask 3'
]);

done();
}
});
});

receiver.write(Buffer.from('810131810132810133', 'hex'));
});

it("waits a microtask after each 'ping' event", (done) => {
it("waits a microtask after the 'message', and 'p{i,o}ng' events", (done) => {
const actual = [];
const receiver = new Receiver();
const expected = [
'1',
'microtask 1',
'2',
'microtask 2',
'3',
'microtask 3',
'4',
'microtask 4'
];

receiver.on('ping', (data) => {
function listener(data) {
const message = data.toString();
actual.push(message);

// `queueMicrotask()` is not available in Node.js < 11.
Promise.resolve().then(() => {
actual.push(`microtask ${message}`);

if (actual.length === 6) {
assert.deepStrictEqual(actual, [
'1',
'microtask 1',
'2',
'microtask 2',
'3',
'microtask 3'
]);

if (actual.length === 8) {
assert.deepStrictEqual(actual, expected);
done();
}
});
});

receiver.write(Buffer.from('890131890132890133', 'hex'));
});
}

it("waits a microtask after each 'pong' event", (done) => {
const actual = [];
const receiver = new Receiver();

receiver.on('pong', (data) => {
const message = data.toString();
actual.push(message);

// `queueMicrotask()` is not available in Node.js < 11.
Promise.resolve().then(() => {
actual.push(`microtask ${message}`);

if (actual.length === 6) {
assert.deepStrictEqual(actual, [
'1',
'microtask 1',
'2',
'microtask 2',
'3',
'microtask 3'
]);

done();
}
});
});
receiver.on('message', listener);
receiver.on('ping', listener);
receiver.on('pong', listener);

receiver.write(Buffer.from('8A01318A01328A0133', 'hex'));
receiver.write(Buffer.from('8101318901328a0133810134', 'hex'));
});

it('does not swallow errors thrown from event handlers', (done) => {
Expand Down

0 comments on commit 5a3036e

Please sign in to comment.