From 6040efd7dcf6e7edce9471799a81b23622d6926f Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 23 Jul 2016 14:11:50 -0700 Subject: [PATCH] test: fix flaky test-tls-wrap-timeout Competing timers were causing a race condition and thus the test was flaky. Instead, we check an object property on process exit. Fixes: https://github.com/nodejs/node/issues/7650 Backport-PR-URL: https://github.com/nodejs/node/pull/12567 PR-URL: https://github.com/nodejs/node/pull/7857 Reviewed-By: Santiago Gimeno Reviewed-By: Fedor Indutny Reviewed-By: jasnell - James M Snell --- test/parallel/test-tls-wrap-timeout.js | 50 ++++++++++++++++---------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/test/parallel/test-tls-wrap-timeout.js b/test/parallel/test-tls-wrap-timeout.js index ab0f307a97..0454242d92 100644 --- a/test/parallel/test-tls-wrap-timeout.js +++ b/test/parallel/test-tls-wrap-timeout.js @@ -1,42 +1,54 @@ 'use strict'; -var common = require('../common'); -var assert = require('assert'); +const common = require('../common'); if (!common.hasCrypto) { common.skip('missing crypto'); return; } -var tls = require('tls'); +const assert = require('assert'); +const tls = require('tls'); -var net = require('net'); -var fs = require('fs'); +const net = require('net'); +const fs = require('fs'); -var options = { +const options = { key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') }; -var server = tls.createServer(options, function(c) { - setTimeout(function() { - c.write('hello'); - setTimeout(function() { - c.destroy(); - server.close(); - }, 150); - }, 150); -}); +const server = tls.createServer(options, common.mustCall((c) => { + setImmediate(() => { + c.write('hello', () => { + setImmediate(() => { + c.destroy(); + server.close(); + }); + }); + }); +})); + +var socket; +var lastIdleStart; -server.listen(0, function() { - var socket = net.connect(this.address().port, function() { - var s = socket.setTimeout(common.platformTimeout(240), function() { +server.listen(0, () => { + socket = net.connect(server.address().port, function() { + const s = socket.setTimeout(Number.MAX_VALUE, function() { throw new Error('timeout'); }); assert.ok(s instanceof net.Socket); - var tsocket = tls.connect({ + assert.notStrictEqual(socket._idleTimeout, -1); + lastIdleStart = socket._idleStart; + + const tsocket = tls.connect({ socket: socket, rejectUnauthorized: false }); tsocket.resume(); }); }); + +process.on('exit', () => { + assert.strictEqual(socket._idleTimeout, -1); + assert(lastIdleStart < socket._idleStart); +});