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: nodejs#37226
  • Loading branch information
Trott committed Feb 4, 2021
1 parent fe43bd8 commit eb1c465
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 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 @@ -350,16 +351,21 @@ process.on('multipleResolves', common.mustNotCall());
}

{
// Check that if we abort when we have some callbacks left,
// 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) {
await setTimeout(delay * 2);
controller.abort();
myEvent.once('myevent', () => {
controller.abort();
});
}
if (iterationNumber > totalIterations) {
totalIterations = iterationNumber;
Expand Down

0 comments on commit eb1c465

Please sign in to comment.