summaryrefslogtreecommitdiff
path: root/test/parallel/test-tls-socket-close.js
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2017-03-18 20:02:00 -0700
committerRich Trott <rtrott@gmail.com>2017-03-21 21:16:57 -0700
commit7bc893f0c667528c8f82e79cbff73ac09eafb8a4 (patch)
treed559e127a0357d55c7cd58fdaa43d7c19de04939 /test/parallel/test-tls-socket-close.js
parentd23123643d98165c67ad51d1fdecf06486b1c6d9 (diff)
downloadandroid-node-v8-7bc893f0c667528c8f82e79cbff73ac09eafb8a4.tar.gz
android-node-v8-7bc893f0c667528c8f82e79cbff73ac09eafb8a4.tar.bz2
android-node-v8-7bc893f0c667528c8f82e79cbff73ac09eafb8a4.zip
test: fix flaky test-tls-socket-close
Replace timer/timeout race with event-based ordering, eliminating test flakiness. PR-URL: https://github.com/nodejs/node/pull/11921 Fixes: https://github.com/nodejs/node/issues/11912 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-tls-socket-close.js')
-rw-r--r--test/parallel/test-tls-socket-close.js45
1 files changed, 28 insertions, 17 deletions
diff --git a/test/parallel/test-tls-socket-close.js b/test/parallel/test-tls-socket-close.js
index 4e7382f340..617062a4f2 100644
--- a/test/parallel/test-tls-socket-close.js
+++ b/test/parallel/test-tls-socket-close.js
@@ -13,35 +13,46 @@ 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();
+ const interval = setInterval(() => {
+ // Checking this way allows us to do the write at a time that causes a
+ // segmentation fault (not always, but often) in Node.js 7.7.3 and
+ // earlier. If we instead, for example, wait on the `close` event, then
+ // it will not segmentation fault, which is what this test is all about.
+ if (tlsSocket._handle._parent.bytesRead === 0) {
+ tlsSocket.write('bar');
+ clearInterval(interval);
+ }
+ }, 1);
+ }));
+ }));
}));