1// Flags: --expose-gc --no-warnings 2'use strict'; 3 4// Test that a runtime warning is emitted when a FileHandle object 5// is allowed to close on garbage collection. In the future, this 6// test should verify that closing on garbage collection throws a 7// process fatal exception. 8 9const common = require('../common'); 10const assert = require('assert'); 11const { promises: fs } = require('fs'); 12 13const warning = 14 'Closing a FileHandle object on garbage collection is deprecated. ' + 15 'Please close FileHandle objects explicitly using ' + 16 'FileHandle.prototype.close(). In the future, an error will be ' + 17 'thrown if a file descriptor is closed during garbage collection.'; 18 19async function doOpen() { 20 const fh = await fs.open(__filename); 21 22 common.expectWarning({ 23 Warning: [[`Closing file descriptor ${fh.fd} on garbage collection`]], 24 DeprecationWarning: [[warning, 'DEP0137']] 25 }); 26 27 return fh; 28} 29 30doOpen().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})); 42