Skip to content

Commit 6da49ac

Browse files
TrottFishrock123
authored andcommitted
test: handle SmartOS bug in test-tls-session-cache
Sometimes, a SmartOS bug results in ECONNREFUSED when trying to connect to the TLS server that the test starts. Retry in that situation. Fixes: #5111 Refs: https://smartos.org/bugview/OS-2767 PR-URL: #7505 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
1 parent 9b5be44 commit 6da49ac

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

test/parallel/test-tls-session-cache.js

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
var common = require('../common');
2+
const common = require('../common');
33

44
if (!common.opensslCli) {
55
common.skip('node compiled without OpenSSL CLI.');
@@ -18,17 +18,17 @@ doTest({ tickets: false }, function() {
1818
});
1919

2020
function doTest(testOptions, callback) {
21-
var assert = require('assert');
22-
var tls = require('tls');
23-
var fs = require('fs');
24-
var join = require('path').join;
25-
var spawn = require('child_process').spawn;
21+
const assert = require('assert');
22+
const tls = require('tls');
23+
const fs = require('fs');
24+
const join = require('path').join;
25+
const spawn = require('child_process').spawn;
2626

27-
var keyFile = join(common.fixturesDir, 'agent.key');
28-
var certFile = join(common.fixturesDir, 'agent.crt');
29-
var key = fs.readFileSync(keyFile);
30-
var cert = fs.readFileSync(certFile);
31-
var options = {
27+
const keyFile = join(common.fixturesDir, 'agent.key');
28+
const certFile = join(common.fixturesDir, 'agent.crt');
29+
const key = fs.readFileSync(keyFile);
30+
const cert = fs.readFileSync(certFile);
31+
const options = {
3232
key: key,
3333
cert: cert,
3434
ca: [cert],
@@ -38,7 +38,7 @@ function doTest(testOptions, callback) {
3838
var resumeCount = 0;
3939
var session;
4040

41-
var server = tls.createServer(options, function(cleartext) {
41+
const server = tls.createServer(options, function(cleartext) {
4242
cleartext.on('error', function(er) {
4343
// We're ok with getting ECONNRESET in this test, but it's
4444
// timing-dependent, and thus unreliable. Any other errors
@@ -72,7 +72,7 @@ function doTest(testOptions, callback) {
7272
});
7373

7474
server.listen(0, function() {
75-
var args = [
75+
const args = [
7676
's_client',
7777
'-tls1',
7878
'-connect', `localhost:${this.address().port}`,
@@ -86,21 +86,35 @@ function doTest(testOptions, callback) {
8686
if (common.isWindows)
8787
args.push('-no_rand_screen');
8888

89-
var client = spawn(common.opensslCli, args, {
90-
stdio: [ 0, 1, 'pipe' ]
91-
});
92-
var err = '';
93-
client.stderr.setEncoding('utf8');
94-
client.stderr.on('data', function(chunk) {
95-
err += chunk;
96-
});
97-
client.on('exit', function(code) {
98-
console.error('done');
99-
assert.equal(code, 0);
100-
server.close(function() {
101-
setTimeout(callback, 100);
89+
function spawnClient() {
90+
const client = spawn(common.opensslCli, args, {
91+
stdio: [ 0, 1, 'pipe' ]
10292
});
103-
});
93+
var err = '';
94+
client.stderr.setEncoding('utf8');
95+
client.stderr.on('data', function(chunk) {
96+
err += chunk;
97+
});
98+
99+
client.on('exit', common.mustCall(function(code, signal) {
100+
if (code !== 0) {
101+
// If SmartOS and connection refused, then retry. See
102+
// https://github.com/nodejs/node/issues/2663.
103+
if (common.isSunOS && err.includes('Connection refused')) {
104+
requestCount = 0;
105+
spawnClient();
106+
return;
107+
}
108+
common.fail(`code: ${code}, signal: ${signal}, output: ${err}`);
109+
}
110+
assert.equal(code, 0);
111+
server.close(common.mustCall(function() {
112+
setTimeout(callback, 100);
113+
}));
114+
}));
115+
}
116+
117+
spawnClient();
104118
});
105119

106120
process.on('exit', function() {

0 commit comments

Comments
 (0)