2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-12-04 20:21:37 +01:00
|
|
|
const common = require('../common');
|
|
|
|
const net = require('net');
|
2013-10-25 23:05:39 +02:00
|
|
|
|
2016-12-04 20:21:37 +01:00
|
|
|
process.once('beforeExit', common.mustCall(tryImmediate));
|
2013-10-25 23:05:39 +02:00
|
|
|
|
|
|
|
function tryImmediate() {
|
2016-12-04 20:21:37 +01:00
|
|
|
setImmediate(common.mustCall(() => {
|
|
|
|
process.once('beforeExit', common.mustCall(tryTimer));
|
|
|
|
}));
|
2013-10-25 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function tryTimer() {
|
2016-12-04 20:21:37 +01:00
|
|
|
setTimeout(common.mustCall(() => {
|
|
|
|
process.once('beforeExit', common.mustCall(tryListen));
|
|
|
|
}), 1);
|
2013-10-25 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function tryListen() {
|
|
|
|
net.createServer()
|
2016-05-29 09:06:56 +02:00
|
|
|
.listen(0)
|
2016-12-04 20:21:37 +01:00
|
|
|
.on('listening', common.mustCall(function() {
|
2013-10-25 23:05:39 +02:00
|
|
|
this.close();
|
2017-01-03 02:23:31 +01:00
|
|
|
process.once('beforeExit', common.mustCall(tryRepeatedTimer));
|
2016-12-04 20:21:37 +01:00
|
|
|
}));
|
2013-10-25 23:05:39 +02:00
|
|
|
}
|
2017-01-03 02:23:31 +01:00
|
|
|
|
|
|
|
// test that a function invoked from the beforeExit handler can use a timer
|
|
|
|
// to keep the event loop open, which can use another timer to keep the event
|
|
|
|
// loop open, etc.
|
|
|
|
function tryRepeatedTimer() {
|
|
|
|
const N = 5;
|
|
|
|
let n = 0;
|
|
|
|
const repeatedTimer = common.mustCall(function() {
|
|
|
|
if (++n < N)
|
|
|
|
setTimeout(repeatedTimer, 1);
|
|
|
|
}, N);
|
|
|
|
setTimeout(repeatedTimer, 1);
|
|
|
|
}
|