node/test/parallel/test-async-wrap-propagate-parent.js
Trevor Norris aeee956ac7 async_wrap: correctly pass parent to init callback
Previous logic didn't allow parent to propagate to the init callback
properly. The fix now allows the init callback to be called and receive
the parent if:

- async wrap callbacks are enabled and parent exists
- the init callback has been called on the parent and an init callback
  exists then it will be called regardless of whether async wrap
  callbacks are disabled.

Change the init/pre/post callback checks to see if it has been properly
set. This allows removal of the Environment "using_asyncwrap" variable.

Pass Isolate to a TryCatch instance.

Fixes: https://github.com/nodejs/node/issues/2986
PR-URL: https://github.com/nodejs/node/pull/3216
Reviewed-By: Rod Vagg <rod@vagg.org>
2015-10-07 13:27:46 -06:00

43 lines
952 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const async_wrap = process.binding('async_wrap');
let cntr = 0;
let server;
let client;
function init(type, parent) {
if (parent) {
cntr++;
// Cannot assert in init callback or will abort.
process.nextTick(() => {
assert.equal(parent, server._handle, 'server doesn\'t match parent');
assert.equal(this, client._handle, 'client doesn\'t match context');
});
}
}
function noop() { }
async_wrap.setupHooks(init, noop, noop);
async_wrap.enable();
server = net.createServer(function(c) {
client = c;
// Allow init callback to run before closing.
setImmediate(() => {
c.end();
this.close();
});
}).listen(common.PORT, function() {
net.connect(common.PORT, noop);
});
process.on('exit', function() {
// init should have only been called once with a parent.
assert.equal(cntr, 1);
});