|
| 1 | +// Flags: --expose-gc --no-warnings |
| 2 | + |
| 3 | +// Test that a runtime warning is emitted when a Dir object |
| 4 | +// is allowed to close on garbage collection. In the future, this |
| 5 | +// test should verify that closing on garbage collection throws a |
| 6 | +// process fatal exception. |
| 7 | + |
| 8 | +import { expectWarning, mustCall } from '../common/index.mjs'; |
| 9 | +import { rejects } from 'node:assert'; |
| 10 | +import { opendir } from 'node:fs/promises'; |
| 11 | +import { setImmediate } from 'node:timers'; |
| 12 | + |
| 13 | +const warning = |
| 14 | + 'Closing a Dir object on garbage collection is deprecated. ' + |
| 15 | + 'Please close Dir objects explicitly using Dir.prototype.close(), ' + |
| 16 | + "or by using explicit resource management ('using' keyword). " + |
| 17 | + 'In the future, an error will be thrown if a directory is closed during garbage collection.'; |
| 18 | + |
| 19 | +// Test that closing Dir automatically using iterator doesn't emit warning |
| 20 | +{ |
| 21 | + const dir = await opendir(import.meta.dirname); |
| 22 | + for await (const _ of dir) {} |
| 23 | + await rejects(dir.close(), { code: 'ERR_DIR_CLOSED' }); |
| 24 | +} |
| 25 | + |
| 26 | +{ |
| 27 | + expectWarning({ |
| 28 | + Warning: [['Closing directory handle on garbage collection']], |
| 29 | + DeprecationWarning: [[warning, 'DEP0200']], |
| 30 | + }); |
| 31 | + |
| 32 | + await opendir(import.meta.dirname).then(mustCall(() => { |
| 33 | + setImmediate(() => { |
| 34 | + // The dir is out of scope now |
| 35 | + globalThis.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(mustCall()); |
| 40 | + }); |
| 41 | + })); |
| 42 | +} |
0 commit comments