Skip to content

Commit

Permalink
[test] Fix flaky tests on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Sep 8, 2023
1 parent ae60ce0 commit fd3c64c
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2813,16 +2813,28 @@ describe('WebSocket', () => {
});

it('can be called from an error listener while connecting', (done) => {
const ws = new WebSocket('ws://localhost:1337');
const server = net.createServer();

ws.on('open', () => done(new Error("Unexpected 'open' event")));
ws.on('error', (err) => {
assert.ok(err instanceof Error);
assert.strictEqual(err.code, 'ECONNREFUSED');
ws.close();
ws.on('close', () => done());
server.on('connection', (socket) => {
socket.on('end', socket.end);
socket.resume();
socket.write(Buffer.from('foo\r\n'));
});
}).timeout(4000);

server.listen(0, () => {
const ws = new WebSocket(`ws://localhost:${server.address().port}`);

ws.on('open', () => done(new Error("Unexpected 'open' event")));
ws.on('error', (err) => {
assert.ok(err instanceof Error);
assert.strictEqual(err.code, 'HPE_INVALID_CONSTANT');
ws.close();
ws.on('close', () => {
server.close(done);
});
});
});
});

it("can be called from a listener of the 'redirect' event", (done) => {
const server = http.createServer();
Expand Down Expand Up @@ -3087,16 +3099,28 @@ describe('WebSocket', () => {
});

it('can be called from an error listener while connecting', (done) => {
const ws = new WebSocket('ws://localhost:1337');
const server = net.createServer();

ws.on('open', () => done(new Error("Unexpected 'open' event")));
ws.on('error', (err) => {
assert.ok(err instanceof Error);
assert.strictEqual(err.code, 'ECONNREFUSED');
ws.terminate();
ws.on('close', () => done());
server.on('connection', (socket) => {
socket.on('end', socket.end);
socket.resume();
socket.write(Buffer.from('foo\r\n'));
});
}).timeout(4000);

server.listen(0, function () {
const ws = new WebSocket(`ws://localhost:${server.address().port}`);

ws.on('open', () => done(new Error("Unexpected 'open' event")));
ws.on('error', (err) => {
assert.ok(err instanceof Error);
assert.strictEqual(err.code, 'HPE_INVALID_CONSTANT');
ws.terminate();
ws.on('close', () => {
server.close(done);
});
});
});
});

it("can be called from a listener of the 'redirect' event", (done) => {
const server = http.createServer();
Expand Down

0 comments on commit fd3c64c

Please sign in to comment.