Skip to content

Commit

Permalink
fixup! test: re-implement promises.setInterval() test robustly
Browse files Browse the repository at this point in the history
  • Loading branch information
Trott committed Feb 6, 2021
1 parent c78b44a commit d676d1d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions test/parallel/test-timers-promisified.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,14 @@ process.on('multipleResolves', common.mustNotCall());
}

{
async function runInterval(fn, intervalTime, signal) {
async function runInterval(fn, intervalTime, signal, emitter) {
const input = 'foobar';
const interval = setInterval(intervalTime, input, { signal });
let iteration = 0;
for await (const value of interval) {
if (emitter) {
emitter.emit('myevent');
}
assert.strictEqual(value, input);
iteration++;
await fn(iteration);
Expand Down Expand Up @@ -359,18 +362,21 @@ process.on('multipleResolves', common.mustNotCall());
const delay = 10;
let totalIterations = 0;
const timeoutLoop = runInterval(async (iterationNumber) => {
myEvent.emit('myevent');
// The call to abort() will typically happen while we're awaiting here.
await setTimeout(delay);
if (iterationNumber <= 2) {
assert.strictEqual(signal.aborted, false);
}
if (iterationNumber === 2) {
myEvent.once('myevent', () => {
controller.abort();
});
}
if (iterationNumber > 2) {
assert.strictEqual(signal.aborted, true);
}
if (iterationNumber > totalIterations) {
totalIterations = iterationNumber;
}
}, delay, signal);
}, delay, signal, myEvent);

timeoutLoop.catch(common.mustCall(() => {
assert.ok(totalIterations >= 3, `iterations was ${totalIterations} < 3`);
Expand Down

0 comments on commit d676d1d

Please sign in to comment.