Skip to content

Commit

Permalink
refactor exit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed May 4, 2013
1 parent f37d431 commit 890382d
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,40 @@ var nextTick = typeof setImmediate !== 'undefined'
exports = module.exports = (function () {
var harness;
return function () {
if (!harness) {
harness = createHarness();
var stream = harness.createStream();
stream.pipe(createDefaultStream());

var ended = false;
stream.on('end', function () { ended = true });

if (process.exit && process._getActiveHandles) {
var iv = setInterval(function () {
if (process._getActiveHandles().length > 1) return;

clearInterval(iv);
setTimeout(function () {
if (!ended) {
for (var i = 0; i < harness._tests.length; i++) {
var t = harness._tests[i];
t._exit();
}
}
}, 100);

setTimeout(function () {
process.exit(harness._exitCode);
}, 110);
});
}
}
if (!harness) harness = createExitHarness();
return harness.apply(this, arguments);
};
})();

function createExitHarness () {
var harness = createHarness();
var stream = harness.createStream();
stream.pipe(createDefaultStream());

var ended = false;
stream.on('end', function () { ended = true });

if (process.exit && process._getActiveHandles) {
var iv = setInterval(function () {
if (process._getActiveHandles().length > 1) return;

clearInterval(iv);
setTimeout(function () {
if (ended) return;
for (var i = 0; i < harness._tests.length; i++) {
var t = harness._tests[i];
t._exit();
}
}, 100);

setTimeout(function () {
process.exit(harness._exitCode);
}, 105);
});
}
return harness;
}

exports.createHarness = createHarness;
exports.Test = Test;
exports.test = exports; // tap compat
Expand Down

0 comments on commit 890382d

Please sign in to comment.