Skip to content

Commit

Permalink
test: re-implement promises.setInterval() test robustly
Browse files Browse the repository at this point in the history
By using events, we can make sure the call to `controller.abort()`
doesn't happen before the next iteration starts.

Fixes: #37226
  • Loading branch information
Trott committed Feb 4, 2021
1 parent cfb2e06 commit 366054b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/parallel/test-timers-promisified.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const child_process = require('child_process');
const { NodeEventTarget } = require('internal/event_target');

const timerPromises = require('timers/promises');
const EventEmitter = require('events');

/* eslint-disable no-restricted-syntax */

Expand Down Expand Up @@ -348,6 +349,33 @@ process.on('multipleResolves', common.mustNotCall());
assert.strictEqual(loopCount, 5);
}));
}

{
// Check that if we abort when we have some unresolved callbacks,
// we actually call them.
const controller = new AbortController();
const myEvent = new EventEmitter();
const { signal } = controller;
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) {
myEvent.once('myevent', () => {
controller.abort();
});
}
if (iterationNumber > totalIterations) {
totalIterations = iterationNumber;
}
}, delay, signal);

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

{
Expand Down

0 comments on commit 366054b

Please sign in to comment.