Skip to content

Commit f600111

Browse files
committed
test: cache lazy properties, fix style nits
inFreeBSDJail involves an execSync() and is used by localhost_ipv4 so will be unnecessarily expensive, so cache both values and reuse rather than re-evaluate each time. Renamed localhost_ipv4 to localhostIPv4 for style consistency. PR-URL: #1196 Reviewed-By: Johan Bergström <[email protected]>
1 parent 3038b8e commit f600111

6 files changed

+31
-22
lines changed

test/common.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,44 @@ if (process.env.TEST_THREAD_ID) {
2020
exports.tmpDir = path.join(exports.testDir, exports.tmpDirName);
2121

2222
var opensslCli = null;
23+
var inFreeBSDJail = null;
24+
var localhostIPv4 = null;
2325

2426
Object.defineProperty(exports, 'inFreeBSDJail', {
2527
get: function() {
28+
if (inFreeBSDJail !== null) return inFreeBSDJail;
29+
2630
if (process.platform === 'freebsd' &&
2731
child_process.execSync('sysctl -n security.jail.jailed').toString() ===
2832
'1\n') {
29-
return true;
33+
inFreeBSDJail = true;
3034
} else {
31-
return false;
35+
inFreeBSDJail = false;
3236
}
37+
return inFreeBSDJail;
3338
}
3439
});
3540

36-
Object.defineProperty(exports, 'localhost_ipv4', {
41+
Object.defineProperty(exports, 'localhostIPv4', {
3742
get: function() {
43+
if (localhostIPv4 !== null) return localhostIPv4;
44+
3845
if (exports.inFreeBSDJail) {
3946
// Jailed network interfaces are a bit special - since we need to jump
4047
// through loops, as well as this being an exception case, assume the
4148
// user will provide this instead.
42-
if (process.env.LOCALHOST)
43-
return process.env.LOCALHOST;
44-
45-
console.error('Looks like we\'re in a FreeBSD Jail. ' +
46-
'Please provide your default interface address ' +
47-
'as LOCALHOST or expect some tests to fail.');
49+
if (process.env.LOCALHOST) {
50+
localhostIPv4 = process.env.LOCALHOST;
51+
} else {
52+
console.error('Looks like we\'re in a FreeBSD Jail. ' +
53+
'Please provide your default interface address ' +
54+
'as LOCALHOST or expect some tests to fail.');
55+
}
4856
}
49-
return '127.0.0.1';
57+
58+
if (localhostIPv4 === null) localhostIPv4 = '127.0.0.1';
59+
60+
return localhostIPv4;
5061
}
5162
});
5263

test/parallel/test-dgram-address.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ var assert = require('assert');
33
var dgram = require('dgram');
44

55
// IPv4 Test
6-
var localhost_ipv4 = common.localhost_ipv4;
76
var socket_ipv4 = dgram.createSocket('udp4');
87
var family_ipv4 = 'IPv4';
98

109
socket_ipv4.on('listening', function() {
1110
var address_ipv4 = socket_ipv4.address();
12-
assert.strictEqual(address_ipv4.address, localhost_ipv4);
11+
assert.strictEqual(address_ipv4.address, common.localhostIPv4);
1312
assert.strictEqual(address_ipv4.port, common.PORT);
1413
assert.strictEqual(address_ipv4.family, family_ipv4);
1514
socket_ipv4.close();
@@ -20,7 +19,7 @@ socket_ipv4.on('error', function(e) {
2019
socket_ipv4.close();
2120
});
2221

23-
socket_ipv4.bind(common.PORT, localhost_ipv4);
22+
socket_ipv4.bind(common.PORT, common.localhostIPv4);
2423

2524
// IPv6 Test
2625
var localhost_ipv6 = '::1';

test/parallel/test-dgram-udp4.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ server = dgram.createSocket('udp4');
1111
server.on('message', function(msg, rinfo) {
1212
console.log('server got: ' + msg +
1313
' from ' + rinfo.address + ':' + rinfo.port);
14-
assert.strictEqual(rinfo.address, common.localhost_ipv4);
14+
assert.strictEqual(rinfo.address, common.localhostIPv4);
1515
assert.strictEqual(msg.toString(), message_to_send.toString());
1616
server.send(msg, 0, msg.length, rinfo.port, rinfo.address);
1717
});
@@ -22,7 +22,7 @@ server.on('listening', function() {
2222
client.on('message', function(msg, rinfo) {
2323
console.log('client got: ' + msg +
2424
' from ' + rinfo.address + ':' + address.port);
25-
assert.strictEqual(rinfo.address, common.localhost_ipv4);
25+
assert.strictEqual(rinfo.address, common.localhostIPv4);
2626
assert.strictEqual(rinfo.port, server_port);
2727
assert.strictEqual(msg.toString(), message_to_send.toString());
2828
client.close();

test/parallel/test-net-local-address-port.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ var conns = 0, conns_closed = 0;
66

77
var server = net.createServer(function(socket) {
88
conns++;
9-
assert.equal(common.localhost_ipv4, socket.localAddress);
9+
assert.equal(common.localhostIPv4, socket.localAddress);
1010
assert.equal(socket.localPort, common.PORT);
1111
socket.on('end', function() {
1212
server.close();
1313
});
1414
socket.resume();
1515
});
1616

17-
server.listen(common.PORT, common.localhost_ipv4, function() {
18-
var client = net.createConnection(common.PORT, common.localhost_ipv4);
17+
server.listen(common.PORT, common.localhostIPv4, function() {
18+
var client = net.createConnection(common.PORT, common.localhostIPv4);
1919
client.on('connect', function() {
2020
client.end();
2121
});

test/parallel/test-net-remote-address-port.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var net = require('net');
55

66
var conns = 0, conns_closed = 0;
77

8-
var remoteAddrCandidates = [ common.localhost_ipv4 ];
8+
var remoteAddrCandidates = [ common.localhostIPv4 ];
99
if (common.hasIPv6) remoteAddrCandidates.push('::ffff:127.0.0.1');
1010

1111
var remoteFamilyCandidates = ['IPv4'];

test/sequential/test-net-server-address.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ var assert = require('assert');
33
var net = require('net');
44

55
// Test on IPv4 Server
6-
var localhost_ipv4 = common.localhost_ipv4;
76
var family_ipv4 = 'IPv4';
87
var server_ipv4 = net.createServer();
98

109
server_ipv4.on('error', function(e) {
1110
console.log('Error on ipv4 socket: ' + e.toString());
1211
});
1312

14-
server_ipv4.listen(common.PORT, localhost_ipv4, function() {
13+
server_ipv4.listen(common.PORT, common.localhostIPv4, function() {
1514
var address_ipv4 = server_ipv4.address();
16-
assert.strictEqual(address_ipv4.address, localhost_ipv4);
15+
assert.strictEqual(address_ipv4.address, common.localhostIPv4);
1716
assert.strictEqual(address_ipv4.port, common.PORT);
1817
assert.strictEqual(address_ipv4.family, family_ipv4);
1918
server_ipv4.close();

0 commit comments

Comments
 (0)