11cb0ef41Sopenharmony_ci// Flags: --expose-gc --no-warnings
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// Test that a runtime warning is emitted when a FileHandle object
51cb0ef41Sopenharmony_ci// is allowed to close on garbage collection. In the future, this
61cb0ef41Sopenharmony_ci// test should verify that closing on garbage collection throws a
71cb0ef41Sopenharmony_ci// process fatal exception.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst common = require('../common');
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst { promises: fs } = require('fs');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst warning =
141cb0ef41Sopenharmony_ci  'Closing a FileHandle object on garbage collection is deprecated. ' +
151cb0ef41Sopenharmony_ci  'Please close FileHandle objects explicitly using ' +
161cb0ef41Sopenharmony_ci  'FileHandle.prototype.close(). In the future, an error will be ' +
171cb0ef41Sopenharmony_ci  'thrown if a file descriptor is closed during garbage collection.';
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciasync function doOpen() {
201cb0ef41Sopenharmony_ci  const fh = await fs.open(__filename);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  common.expectWarning({
231cb0ef41Sopenharmony_ci    Warning: [[`Closing file descriptor ${fh.fd} on garbage collection`]],
241cb0ef41Sopenharmony_ci    DeprecationWarning: [[warning, 'DEP0137']]
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  return fh;
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cidoOpen().then(common.mustCall((fd) => {
311cb0ef41Sopenharmony_ci  assert.strictEqual(typeof fd, 'object');
321cb0ef41Sopenharmony_ci})).then(common.mustCall(() => {
331cb0ef41Sopenharmony_ci  setImmediate(() => {
341cb0ef41Sopenharmony_ci    // The FileHandle should be out-of-scope and no longer accessed now.
351cb0ef41Sopenharmony_ci    global.gc();
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    // Wait an extra event loop turn, as the warning is emitted from the
381cb0ef41Sopenharmony_ci    // native layer in an unref()'ed setImmediate() callback.
391cb0ef41Sopenharmony_ci    setImmediate(common.mustCall());
401cb0ef41Sopenharmony_ci  });
411cb0ef41Sopenharmony_ci}));
42