Skip to content

Commit

Permalink
test: cache lazy properties, fix style nits
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rvagg committed Mar 19, 2015
1 parent 3038b8e commit f600111
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
31 changes: 21 additions & 10 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,44 @@ if (process.env.TEST_THREAD_ID) {
exports.tmpDir = path.join(exports.testDir, exports.tmpDirName);

var opensslCli = null;
var inFreeBSDJail = null;
var localhostIPv4 = null;

Object.defineProperty(exports, 'inFreeBSDJail', {
get: function() {
if (inFreeBSDJail !== null) return inFreeBSDJail;

if (process.platform === 'freebsd' &&
child_process.execSync('sysctl -n security.jail.jailed').toString() ===
'1\n') {
return true;
inFreeBSDJail = true;
} else {
return false;
inFreeBSDJail = false;
}
return inFreeBSDJail;
}
});

Object.defineProperty(exports, 'localhost_ipv4', {
Object.defineProperty(exports, 'localhostIPv4', {
get: function() {
if (localhostIPv4 !== null) return localhostIPv4;

if (exports.inFreeBSDJail) {
// Jailed network interfaces are a bit special - since we need to jump
// through loops, as well as this being an exception case, assume the
// user will provide this instead.
if (process.env.LOCALHOST)
return process.env.LOCALHOST;

console.error('Looks like we\'re in a FreeBSD Jail. ' +
'Please provide your default interface address ' +
'as LOCALHOST or expect some tests to fail.');
if (process.env.LOCALHOST) {
localhostIPv4 = process.env.LOCALHOST;
} else {
console.error('Looks like we\'re in a FreeBSD Jail. ' +
'Please provide your default interface address ' +
'as LOCALHOST or expect some tests to fail.');
}
}
return '127.0.0.1';

if (localhostIPv4 === null) localhostIPv4 = '127.0.0.1';

return localhostIPv4;
}
});

Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-dgram-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ var assert = require('assert');
var dgram = require('dgram');

// IPv4 Test
var localhost_ipv4 = common.localhost_ipv4;
var socket_ipv4 = dgram.createSocket('udp4');
var family_ipv4 = 'IPv4';

socket_ipv4.on('listening', function() {
var address_ipv4 = socket_ipv4.address();
assert.strictEqual(address_ipv4.address, localhost_ipv4);
assert.strictEqual(address_ipv4.address, common.localhostIPv4);
assert.strictEqual(address_ipv4.port, common.PORT);
assert.strictEqual(address_ipv4.family, family_ipv4);
socket_ipv4.close();
Expand All @@ -20,7 +19,7 @@ socket_ipv4.on('error', function(e) {
socket_ipv4.close();
});

socket_ipv4.bind(common.PORT, localhost_ipv4);
socket_ipv4.bind(common.PORT, common.localhostIPv4);

// IPv6 Test
var localhost_ipv6 = '::1';
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-dgram-udp4.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ server = dgram.createSocket('udp4');
server.on('message', function(msg, rinfo) {
console.log('server got: ' + msg +
' from ' + rinfo.address + ':' + rinfo.port);
assert.strictEqual(rinfo.address, common.localhost_ipv4);
assert.strictEqual(rinfo.address, common.localhostIPv4);
assert.strictEqual(msg.toString(), message_to_send.toString());
server.send(msg, 0, msg.length, rinfo.port, rinfo.address);
});
Expand All @@ -22,7 +22,7 @@ server.on('listening', function() {
client.on('message', function(msg, rinfo) {
console.log('client got: ' + msg +
' from ' + rinfo.address + ':' + address.port);
assert.strictEqual(rinfo.address, common.localhost_ipv4);
assert.strictEqual(rinfo.address, common.localhostIPv4);
assert.strictEqual(rinfo.port, server_port);
assert.strictEqual(msg.toString(), message_to_send.toString());
client.close();
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-net-local-address-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ var conns = 0, conns_closed = 0;

var server = net.createServer(function(socket) {
conns++;
assert.equal(common.localhost_ipv4, socket.localAddress);
assert.equal(common.localhostIPv4, socket.localAddress);
assert.equal(socket.localPort, common.PORT);
socket.on('end', function() {
server.close();
});
socket.resume();
});

server.listen(common.PORT, common.localhost_ipv4, function() {
var client = net.createConnection(common.PORT, common.localhost_ipv4);
server.listen(common.PORT, common.localhostIPv4, function() {
var client = net.createConnection(common.PORT, common.localhostIPv4);
client.on('connect', function() {
client.end();
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-net-remote-address-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var net = require('net');

var conns = 0, conns_closed = 0;

var remoteAddrCandidates = [ common.localhost_ipv4 ];
var remoteAddrCandidates = [ common.localhostIPv4 ];
if (common.hasIPv6) remoteAddrCandidates.push('::ffff:127.0.0.1');

var remoteFamilyCandidates = ['IPv4'];
Expand Down
5 changes: 2 additions & 3 deletions test/sequential/test-net-server-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ var assert = require('assert');
var net = require('net');

// Test on IPv4 Server
var localhost_ipv4 = common.localhost_ipv4;
var family_ipv4 = 'IPv4';
var server_ipv4 = net.createServer();

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

server_ipv4.listen(common.PORT, localhost_ipv4, function() {
server_ipv4.listen(common.PORT, common.localhostIPv4, function() {
var address_ipv4 = server_ipv4.address();
assert.strictEqual(address_ipv4.address, localhost_ipv4);
assert.strictEqual(address_ipv4.address, common.localhostIPv4);
assert.strictEqual(address_ipv4.port, common.PORT);
assert.strictEqual(address_ipv4.family, family_ipv4);
server_ipv4.close();
Expand Down

0 comments on commit f600111

Please sign in to comment.