test: don't compute knownGlobals lazily

Conditional globals like 'gc' should only be recognized when --expose_gc
is set.  The global.gc feature check works only when done eagerly, else
it lets through a leaked variable called 'gc'.
This commit is contained in:
Ben Noordhuis 2014-01-30 13:02:58 +01:00 committed by Fedor Indutny
parent 8874a31748
commit 2b6e078833
2 changed files with 91 additions and 63 deletions

View File

@ -89,13 +89,7 @@ exports.spawnPwd = function(options) {
} }
}; };
var knownGlobals = [setTimeout,
// Turn this off if the test should not check for global leaks.
exports.globalCheck = true;
process.on('exit', function() {
if (!exports.globalCheck) return;
var knownGlobals = [setTimeout,
setInterval, setInterval,
setImmediate, setImmediate,
clearTimeout, clearTimeout,
@ -107,11 +101,11 @@ process.on('exit', function() {
process, process,
global]; global];
if (global.gc) { if (global.gc) {
knownGlobals.push(gc); knownGlobals.push(gc);
} }
if (global.DTRACE_HTTP_SERVER_RESPONSE) { if (global.DTRACE_HTTP_SERVER_RESPONSE) {
knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE);
knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST);
knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE);
@ -120,17 +114,18 @@ process.on('exit', function() {
knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); knownGlobals.push(DTRACE_NET_SERVER_CONNECTION);
knownGlobals.push(DTRACE_NET_SOCKET_READ); knownGlobals.push(DTRACE_NET_SOCKET_READ);
knownGlobals.push(DTRACE_NET_SOCKET_WRITE); knownGlobals.push(DTRACE_NET_SOCKET_WRITE);
} }
if (global.COUNTER_NET_SERVER_CONNECTION) {
if (global.COUNTER_NET_SERVER_CONNECTION) {
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION); knownGlobals.push(COUNTER_NET_SERVER_CONNECTION);
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE); knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE);
knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST); knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST);
knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE); knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE);
knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST); knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST);
knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE); knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE);
} }
if (global.ArrayBuffer) { if (global.ArrayBuffer) {
knownGlobals.push(ArrayBuffer); knownGlobals.push(ArrayBuffer);
knownGlobals.push(Int8Array); knownGlobals.push(Int8Array);
knownGlobals.push(Uint8Array); knownGlobals.push(Uint8Array);
@ -142,23 +137,29 @@ process.on('exit', function() {
knownGlobals.push(Float32Array); knownGlobals.push(Float32Array);
knownGlobals.push(Float64Array); knownGlobals.push(Float64Array);
knownGlobals.push(DataView); knownGlobals.push(DataView);
} }
for (var x in global) { function leakedGlobals() {
var found = false; var leaked = [];
for (var y in knownGlobals) { for (var val in global)
if (global[x] === knownGlobals[y]) { if (-1 === knownGlobals.indexOf(global[val]))
found = true; leaked.push(val);
break;
}
}
if (!found) { return leaked;
console.error('Unknown global: %s', x); };
exports.leakedGlobals = leakedGlobals;
// Turn this off if the test should not check for global leaks.
exports.globalCheck = true;
process.on('exit', function() {
if (!exports.globalCheck) return;
var leaked = leakedGlobals();
if (leaked.length > 0) {
console.error('Unknown globals: %s', leaked);
assert.ok(false, 'Unknown global found'); assert.ok(false, 'Unknown global found');
} }
}
}); });

View File

@ -0,0 +1,27 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var common = require('../common');
var assert = require('assert');
common.globalCheck = false;
global.gc = 42; // Not a valid global unless --expose_gc is set.
assert.deepEqual(common.leakedGlobals(), ['gc']);