Skip to content

Commit 5100e84

Browse files
committed
test: fix flaky test-fs-promises-file-handle-close
Sometimes, the expected warnings were not emitted. To harden the test: - Instead of relying on a timeout that raced against the file being opened, wait until the file is opened and then schedule GC after that. - Explicitly keep the event loop alive for one turn to make sure the warning is being seen. PR-URL: #31687 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d0413ae commit 5100e84

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

test/parallel/test-fs-promises-file-handle-close.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ async function doOpen() {
2727
return fh;
2828
}
2929

30-
// Perform the file open assignment within a block.
31-
// When the block scope exits, the file handle will
32-
// be eligible for garbage collection.
33-
{
34-
doOpen().then(common.mustCall((fd) => {
35-
assert.strictEqual(typeof fd, 'object');
36-
}));
37-
}
38-
39-
setTimeout(() => global.gc(), 10);
30+
doOpen().then(common.mustCall((fd) => {
31+
assert.strictEqual(typeof fd, 'object');
32+
})).then(common.mustCall(() => {
33+
setImmediate(() => {
34+
// The FileHandle should be out-of-scope and no longer accessed now.
35+
global.gc();
36+
37+
// Wait an extra event loop turn, as the warning is emitted from the
38+
// native layer in an unref()'ed setImmediate() callback.
39+
setImmediate(common.mustCall());
40+
});
41+
}));

0 commit comments

Comments
 (0)