11cb0ef41Sopenharmony_ci// Flags: --expose-gc --no-warnings --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst path = require('path');
71cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
81cb0ef41Sopenharmony_ciconst fs = internalBinding('fs');
91cb0ef41Sopenharmony_ciconst { stringToFlags } = require('internal/fs/utils');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// Verifies that the FileHandle object is garbage collected and that a
121cb0ef41Sopenharmony_ci// warning is emitted if it is not closed.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cilet fdnum;
151cb0ef41Sopenharmony_ci{
161cb0ef41Sopenharmony_ci  const ctx = {};
171cb0ef41Sopenharmony_ci  fdnum = fs.openFileHandle(path.toNamespacedPath(__filename),
181cb0ef41Sopenharmony_ci                            stringToFlags('r'), 0o666, undefined, ctx).fd;
191cb0ef41Sopenharmony_ci  assert.strictEqual(ctx.errno, undefined);
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst deprecationWarning =
231cb0ef41Sopenharmony_ci  'Closing a FileHandle object on garbage collection is deprecated. ' +
241cb0ef41Sopenharmony_ci  'Please close FileHandle objects explicitly using ' +
251cb0ef41Sopenharmony_ci  'FileHandle.prototype.close(). In the future, an error will be ' +
261cb0ef41Sopenharmony_ci  'thrown if a file descriptor is closed during garbage collection.';
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cicommon.expectWarning({
291cb0ef41Sopenharmony_ci  'internal/test/binding': [
301cb0ef41Sopenharmony_ci    'These APIs are for internal testing only. Do not use them.',
311cb0ef41Sopenharmony_ci  ],
321cb0ef41Sopenharmony_ci  'Warning': [
331cb0ef41Sopenharmony_ci    `Closing file descriptor ${fdnum} on garbage collection`,
341cb0ef41Sopenharmony_ci  ],
351cb0ef41Sopenharmony_ci  'DeprecationWarning': [[deprecationWarning, 'DEP0137']]
361cb0ef41Sopenharmony_ci});
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciglobal.gc();
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cisetTimeout(() => {}, 10);
41