11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst { promiseHooks } = require('v8'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_cifor (const hook of ['init', 'before', 'after', 'settled']) { 71cb0ef41Sopenharmony_ci assert.throws(() => { 81cb0ef41Sopenharmony_ci promiseHooks.createHook({ 91cb0ef41Sopenharmony_ci [hook]: async function() { } 101cb0ef41Sopenharmony_ci }); 111cb0ef41Sopenharmony_ci }, new RegExp(`The "${hook}Hook" argument must be of type function`)); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci assert.throws(() => { 141cb0ef41Sopenharmony_ci promiseHooks.createHook({ 151cb0ef41Sopenharmony_ci [hook]: async function*() { } 161cb0ef41Sopenharmony_ci }); 171cb0ef41Sopenharmony_ci }, new RegExp(`The "${hook}Hook" argument must be of type function`)); 181cb0ef41Sopenharmony_ci} 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cilet init; 211cb0ef41Sopenharmony_cilet initParent; 221cb0ef41Sopenharmony_cilet before; 231cb0ef41Sopenharmony_cilet after; 241cb0ef41Sopenharmony_cilet settled; 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciconst stop = promiseHooks.createHook({ 271cb0ef41Sopenharmony_ci init: common.mustCall((promise, parent) => { 281cb0ef41Sopenharmony_ci init = promise; 291cb0ef41Sopenharmony_ci initParent = parent; 301cb0ef41Sopenharmony_ci }, 3), 311cb0ef41Sopenharmony_ci before: common.mustCall((promise) => { 321cb0ef41Sopenharmony_ci before = promise; 331cb0ef41Sopenharmony_ci }, 2), 341cb0ef41Sopenharmony_ci after: common.mustCall((promise) => { 351cb0ef41Sopenharmony_ci after = promise; 361cb0ef41Sopenharmony_ci }, 1), 371cb0ef41Sopenharmony_ci settled: common.mustCall((promise) => { 381cb0ef41Sopenharmony_ci settled = promise; 391cb0ef41Sopenharmony_ci }, 2) 401cb0ef41Sopenharmony_ci}); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci// Clears state on each check so only the delta needs to be checked. 431cb0ef41Sopenharmony_cifunction assertState(expectedInit, expectedInitParent, expectedBefore, 441cb0ef41Sopenharmony_ci expectedAfter, expectedSettled) { 451cb0ef41Sopenharmony_ci assert.strictEqual(init, expectedInit); 461cb0ef41Sopenharmony_ci assert.strictEqual(initParent, expectedInitParent); 471cb0ef41Sopenharmony_ci assert.strictEqual(before, expectedBefore); 481cb0ef41Sopenharmony_ci assert.strictEqual(after, expectedAfter); 491cb0ef41Sopenharmony_ci assert.strictEqual(settled, expectedSettled); 501cb0ef41Sopenharmony_ci init = undefined; 511cb0ef41Sopenharmony_ci initParent = undefined; 521cb0ef41Sopenharmony_ci before = undefined; 531cb0ef41Sopenharmony_ci after = undefined; 541cb0ef41Sopenharmony_ci settled = undefined; 551cb0ef41Sopenharmony_ci} 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ciconst parent = Promise.resolve(1); 581cb0ef41Sopenharmony_ci// After calling `Promise.resolve(...)`, the returned promise should have 591cb0ef41Sopenharmony_ci// produced an init event with no parent and a settled event. 601cb0ef41Sopenharmony_ciassertState(parent, undefined, undefined, undefined, parent); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ciconst child = parent.then(() => { 631cb0ef41Sopenharmony_ci // When a callback to `promise.then(...)` is called, the promise it resolves 641cb0ef41Sopenharmony_ci // to should have produced a before event to mark the start of this callback. 651cb0ef41Sopenharmony_ci assertState(undefined, undefined, child, undefined, undefined); 661cb0ef41Sopenharmony_ci}); 671cb0ef41Sopenharmony_ci// After calling `promise.then(...)`, the returned promise should have 681cb0ef41Sopenharmony_ci// produced an init event with a parent of the promise the `then(...)` 691cb0ef41Sopenharmony_ci// was called on. 701cb0ef41Sopenharmony_ciassertState(child, parent); 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ciconst grandChild = child.then(() => { 731cb0ef41Sopenharmony_ci // Since the check for the before event in the `then(...)` call producing the 741cb0ef41Sopenharmony_ci // `child` promise, there should have been both a before event for this 751cb0ef41Sopenharmony_ci // promise but also settled and after events for the `child` promise. 761cb0ef41Sopenharmony_ci assertState(undefined, undefined, grandChild, child, child); 771cb0ef41Sopenharmony_ci stop(); 781cb0ef41Sopenharmony_ci}); 791cb0ef41Sopenharmony_ci// After calling `promise.then(...)`, the returned promise should have 801cb0ef41Sopenharmony_ci// produced an init event with a parent of the promise the `then(...)` 811cb0ef41Sopenharmony_ci// was called on. 821cb0ef41Sopenharmony_ciassertState(grandChild, child); 83