[test] Fix async hooks test which was swallowing a promise rejection
Updated the test so that it uses assertPromiseResult, which makes sure that a promise rejection is not swallowed. The change is reflected in the actual async ids, checked in the test. Bug: v8:8300 Change-Id: Ie227ca74a8cf4e0e079809b21c3abc5a5f87c11a Reviewed-on: https://chromium-review.googlesource.com/c/1278388 Commit-Queue: Maya Lekova <mslekova@chromium.org> Reviewed-by: Benedikt Meurer <bmeurer@chromium.org> Cr-Commit-Position: refs/heads/master@{#56608}
This commit is contained in:
parent
84b3b702fd
commit
fa8af5a775
@ -41,8 +41,9 @@
|
||||
});
|
||||
ah.enable();
|
||||
|
||||
// Simplified version of Node.js util.promisify(setTimeout)
|
||||
function sleep(callback, timeout) {
|
||||
// Simplified version of Node.js util.promisify(setTimeout),
|
||||
// but d8 ignores the timeout of setTimeout.
|
||||
function sleep0() {
|
||||
const promise = new Promise(function(resolve, reject) {
|
||||
try {
|
||||
setTimeout((err, ...values) => {
|
||||
@ -51,7 +52,7 @@
|
||||
} else {
|
||||
resolve(values[0]);
|
||||
}
|
||||
}, timeout);
|
||||
}, 0);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
@ -60,15 +61,15 @@
|
||||
}
|
||||
|
||||
async function foo() {
|
||||
await sleep(10);
|
||||
await sleep0();
|
||||
}
|
||||
|
||||
foo().then(function() {
|
||||
assertEquals(asyncIds.length, 6);
|
||||
assertEquals(triggerIds.length, 6);
|
||||
assertEquals(triggerIds[2], asyncIds[0]);
|
||||
assertEquals(triggerIds[3], asyncIds[2]);
|
||||
assertEquals(triggerIds[4], asyncIds[0]);
|
||||
assertEquals(triggerIds[5], asyncIds[1]);
|
||||
});
|
||||
assertPromiseResult(
|
||||
foo().then(function() {
|
||||
assertEquals(triggerIds[2], asyncIds[0]);
|
||||
assertEquals(triggerIds[3], asyncIds[2]);
|
||||
assertEquals(triggerIds[4], asyncIds[0]);
|
||||
assertEquals(triggerIds[5], asyncIds[4]);
|
||||
assertEquals(triggerIds[7], asyncIds[6]);
|
||||
}));
|
||||
})();
|
||||
|
@ -40,9 +40,13 @@
|
||||
let createdPromise = new Promise(function(resolve) {
|
||||
resolve(42);
|
||||
}).then(function() {
|
||||
assertEquals(asyncIds.length, 2, 'Exactly 2 promises should be inited');
|
||||
assertEquals(triggerIds.length, 2, 'Exactly 2 promises should be inited');
|
||||
assertEquals(3, asyncIds.length, 'Exactly 3 promises should be inited');
|
||||
assertEquals(3, triggerIds.length, 'Exactly 3 promises should be inited');
|
||||
assertEquals(triggerIds[1], asyncIds[0],
|
||||
"Parent promise asyncId doesn't correspond to child triggerAsyncId");
|
||||
}).catch((err) => {
|
||||
setTimeout(() => {
|
||||
throw err;
|
||||
}, 0);
|
||||
});
|
||||
})();
|
||||
|
@ -29,27 +29,27 @@
|
||||
|
||||
// Check for correct execution of available hooks and asyncIds
|
||||
(function() {
|
||||
let inited = false, resolved = false, before = false, after = false;
|
||||
let storedAsyncId;
|
||||
let calledHooks = [];
|
||||
let rootAsyncId = 0;
|
||||
|
||||
let ah = async_hooks.createHook({
|
||||
init(asyncId, type, triggerAsyncId, resource) {
|
||||
init: function init(asyncId, type, triggerAsyncId, resource) {
|
||||
if (type !== 'PROMISE') {
|
||||
return;
|
||||
}
|
||||
inited = true;
|
||||
storedAsyncId = asyncId;
|
||||
if (triggerAsyncId === 0) {
|
||||
rootAsyncId = asyncId;
|
||||
}
|
||||
calledHooks.push(['init', asyncId]);
|
||||
},
|
||||
promiseResolve(asyncId) {
|
||||
assertEquals(asyncId, storedAsyncId, 'AsyncId mismatch in resolve hook');
|
||||
resolved = true;
|
||||
promiseResolve: function promiseResolve(asyncId) {
|
||||
calledHooks.push(['resolve', asyncId]);
|
||||
},
|
||||
before(asyncId) {
|
||||
assertEquals(asyncId, storedAsyncId, 'AsyncId mismatch in before hook');
|
||||
before = true;
|
||||
before: function before(asyncId) {
|
||||
calledHooks.push(['before', asyncId]);
|
||||
},
|
||||
after(asyncId) {
|
||||
assertEquals(asyncId, storedAsyncId, 'AsyncId mismatch in after hook');
|
||||
after = true;
|
||||
after: function after(asyncId) {
|
||||
calledHooks.push(['after', asyncId]);
|
||||
},
|
||||
});
|
||||
ah.enable();
|
||||
@ -57,9 +57,21 @@
|
||||
new Promise(function(resolve) {
|
||||
resolve(42);
|
||||
}).then(function() {
|
||||
assertTrue(inited, "Didn't call init hook");
|
||||
assertTrue(resolved, "Didn't call resolve hook");
|
||||
assertTrue(before, "Didn't call before hook before the callback");
|
||||
assertFalse(after, "Called after hook before the callback");
|
||||
// [hook type, async Id]
|
||||
const expectedHooks = [
|
||||
['init', rootAsyncId], // the promise that we create initially
|
||||
['resolve', rootAsyncId],
|
||||
['init', rootAsyncId + 1], // the chained promise with the assertions
|
||||
['init', rootAsyncId + 2], // the chained promise from the catch block
|
||||
['before', rootAsyncId + 1],
|
||||
// ['after', rootAsyncId + 1] will get called after the assertions
|
||||
];
|
||||
|
||||
assertArrayEquals(expectedHooks, calledHooks,
|
||||
'Mismatch in async hooks execution order');
|
||||
}).catch((err) => {
|
||||
setTimeout(() => {
|
||||
throw err;
|
||||
}, 0);
|
||||
});
|
||||
})();
|
||||
|
@ -56,6 +56,10 @@
|
||||
}).then(() => {
|
||||
assertNotEquals(outerExecutionAsyncId, async_hooks.executionAsyncId());
|
||||
assertNotEquals(outerTriggerAsyncId, async_hooks.triggerAsyncId());
|
||||
}).catch((err) => {
|
||||
setTimeout(() => {
|
||||
throw err;
|
||||
}, 0);
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user