Skip to content

Commit

Permalink
test: fix flaky test-tls-socket-close
Browse files Browse the repository at this point in the history
Replace timer/timeout race with event-based ordering, eliminating test
flakiness.

Fixes: nodejs#11912
  • Loading branch information
Trott committed Mar 19, 2017
1 parent 5bda5fa commit 5db57ca
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions test/parallel/test-tls-socket-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,37 @@ const net = require('net');
const key = fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem');
const cert = fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem');

const T = 100;

let tlsSocket;
// tls server
const tlsServer = tls.createServer({ cert, key }, (socket) => {
setTimeout(() => {
socket.on('error', (error) => {
assert.strictEqual(error.code, 'EINVAL');
tlsServer.close();
netServer.close();
});
socket.write('bar');
}, T * 2);
tlsSocket = socket;
socket.on('error', common.mustCall((error) => {
assert.strictEqual(error.code, 'EINVAL');
tlsServer.close();
netServer.close();
}));
});

let netSocket;
// plain tcp server
const netServer = net.createServer((socket) => {
// if client wants to use tls
// if client wants to use tls
tlsServer.emit('connection', socket);

socket.setTimeout(T, () => {
// this breaks if TLSSocket is already managing the socket:
socket.destroy();
});
netSocket = socket;
}).listen(0, common.mustCall(function() {

// connect client
tls.connect({
host: 'localhost',
port: this.address().port,
rejectUnauthorized: false
}).write('foo');
}).write('foo', 'utf8', common.mustCall(() => {
assert(netSocket);
netSocket.setTimeout(1, common.mustCall(() => {
assert(tlsSocket);
// this breaks if TLSSocket is already managing the socket:
netSocket.destroy();
netSocket.on('close', () => { tlsSocket.write('bar'); });
}));
}));
}));

0 comments on commit 5db57ca

Please sign in to comment.