Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async_hooks,test: add test for async hooks parity for async/await #20626

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
async_hooks,test: refactor async/await test to use initHooks
Reword async/await test to make use of the initHooks utility
and an attempt to clarify the test's logic.

Refs: #20516
  • Loading branch information
MayaLekova committed May 11, 2018
commit dc98e8049630b6d897316b0e929f2b56bb8d662a
80 changes: 45 additions & 35 deletions test/async-hooks/test-async-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,80 @@ const common = require('../common');
// 2. Checking that for each 'before' corresponding hook 'after' hook is called

const assert = require('assert');
const asyncHooks = require('async_hooks');
const initHooks = require('./init-hooks');

const util = require('util');

const sleep = util.promisify(setTimeout);
const promiseCallbacks = new Map();
const resolvedPromises = new Set();

const asyncHook = asyncHooks.createHook({
init, before, after, promiseResolve
// either 'inited' or 'resolved'
const promisesInitState = new Map();
// either 'before' or 'after' AND asyncId must be present in the other map
const promisesExecutionState = new Map();

const hooks = initHooks({
oninit,
onbefore,
onafter,
ondestroy: null, // Intentionally not tested, since it will be removed soon
onpromiseResolve
});
asyncHook.enable();
hooks.enable();

function init(asyncId, type, triggerAsyncId, resource) {
function oninit(asyncId, type, triggerAsyncId, resource) {
if (type === 'PROMISE') {
promiseCallbacks.set(asyncId, 0);
promisesInitState.set(asyncId, 'inited');
}
}

function before(asyncId) {
if (promiseCallbacks.has(asyncId)) {
assert.strictEqual(promiseCallbacks.get(asyncId), 0,
'before hook called for promise without prior call' +
'to init hook');
promiseCallbacks.set(asyncId, 1);
function onbefore(asyncId) {
if (!promisesInitState.has(asyncId)) {
return;
}
promisesExecutionState.set(asyncId, 'before');
}

function after(asyncId) {
if (promiseCallbacks.has(asyncId)) {
assert.strictEqual(promiseCallbacks.get(asyncId), 1,
'after hook called for promise without prior call' +
'to before hook');
promiseCallbacks.set(asyncId, 0);
function onafter(asyncId) {
if (!promisesInitState.has(asyncId)) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should remove things from the map here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check if this still applies to the new version - now the maps are more meant to reflect the state of the promises created, so IMO no need to clean them in the hooks.


assert.strictEqual(promisesExecutionState.get(asyncId), 'before',
'after hook called for promise without prior call' +
'to before hook');
assert.strictEqual(promisesInitState.get(asyncId), 'resolved',
'after hook called for promise without prior call' +
'to resolve hook');
promisesExecutionState.set(asyncId, 'after');
}

function promiseResolve(asyncId) {
assert(promiseCallbacks.has(asyncId),
function onpromiseResolve(asyncId) {
assert(promisesInitState.has(asyncId),
'resolve hook called for promise without prior call to init hook');

resolvedPromises.add(asyncId);
promisesInitState.set(asyncId, 'resolved');
}

const timeout = common.platformTimeout(10);

function checkPromiseCallbacks() {
for (const balance of promiseCallbacks.values()) {
assert.strictEqual(balance, 0,
'mismatch between before and after hook calls');
function checkPromisesInitState() {
for (const initState of promisesInitState.values()) {
assert.strictEqual(initState, 'resolved',
'promise initialized without being resolved');
}
}

function checkPromiseResolution() {
for (const id of promiseCallbacks.keys()) {
assert(resolvedPromises.has(id),
'promise initialized without being resolved');
function checkPromisesExecutionState() {
for (const executionState of promisesExecutionState.values()) {
assert.strictEqual(executionState, 'after',
'mismatch between before and after hook calls');
}
}

process.on('beforeExit', common.mustCall(() => {
asyncHook.disable();
hooks.disable();

checkPromiseResolution();
checkPromiseCallbacks();
checkPromisesInitState();
checkPromisesExecutionState();
}));

async function asyncFunc(callback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused callback param

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, removed.

Expand Down