Skip to content

Commit

Permalink
Ensure watcher tests exit cleanly
Browse files Browse the repository at this point in the history
Always await the last pending state when the watch runs have completed.

Use a teardown hook to ensure the watcher is aborted and has exited before ending the test.

Ensure the item is always an object, even if it didn't come from the generator.
  • Loading branch information
novemberborn committed Jan 11, 2024
1 parent 783f62b commit 70a6e25
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions test/watch-mode/helpers/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,40 @@ export const withFixture = fixture => async (t, task) => {
t.fail('Watcher performed a test run while it should have been idle');
}
}

return {};
});
}).then(() => ({}));
idlePromise = promise;

await promise;
};

let state = {};
let pendingState;
let process;

t.teardown(async () => {
if (process?.connected) {
process.send('abort-watcher');
}

// Sending the `abort-watcher` message should suffice, but on Linux
// the recursive watch handle does not close properly. See
// <https://github.com/nodejs/node/issues/48437> but there seem to be
// other isues.
setTimeout(() => {
process.kill('SIGKILL');
}, 1000).unref();

try {
await process;
} catch {}
});

const results = run(args, options);
try {
let nextResult = results.next();
while (true) { // eslint-disable-line no-constant-condition
while (!isDone) { // eslint-disable-line no-unmodified-loop-condition
const item = await Promise.race([nextResult, idlePromise, donePromise]); // eslint-disable-line no-await-in-loop
process ??= item.value?.process;

if (item.value) {
failedIdleAssertion ||= assertingIdle;
Expand All @@ -124,8 +142,8 @@ export const withFixture = fixture => async (t, task) => {
}
}

if (item.done || isDone) {
item.value?.process.send('abort-watcher');
if (item.done) {
await pendingState; // eslint-disable-line no-await-in-loop
break;
}
}
Expand Down

0 comments on commit 70a6e25

Please sign in to comment.