11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests that closing a watcher when the underlying handle is
41cb0ef41Sopenharmony_ci// already destroyed will result in a noop instead of a crash.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciif (common.isIBMi)
91cb0ef41Sopenharmony_ci  common.skip('IBMi does not support `fs.watch()`');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
121cb0ef41Sopenharmony_ciconst fs = require('fs');
131cb0ef41Sopenharmony_ciconst path = require('path');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_citmpdir.refresh();
161cb0ef41Sopenharmony_ciconst root = path.join(tmpdir.path, 'watched-directory');
171cb0ef41Sopenharmony_cifs.mkdirSync(root);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst watcher = fs.watch(root, { persistent: false, recursive: false });
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci// The following listeners may or may not be invoked.
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciwatcher.addListener('error', () => {
241cb0ef41Sopenharmony_ci  setTimeout(
251cb0ef41Sopenharmony_ci    () => { watcher.close(); },  // Should not crash if it's invoked
261cb0ef41Sopenharmony_ci    common.platformTimeout(10)
271cb0ef41Sopenharmony_ci  );
281cb0ef41Sopenharmony_ci});
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciwatcher.addListener('change', () => {
311cb0ef41Sopenharmony_ci  setTimeout(
321cb0ef41Sopenharmony_ci    () => { watcher.close(); },
331cb0ef41Sopenharmony_ci    common.platformTimeout(10)
341cb0ef41Sopenharmony_ci  );
351cb0ef41Sopenharmony_ci});
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_cifs.rmdirSync(root);
381cb0ef41Sopenharmony_ci// Wait for the listener to hit
391cb0ef41Sopenharmony_cisetTimeout(
401cb0ef41Sopenharmony_ci  common.mustCall(),
411cb0ef41Sopenharmony_ci  common.platformTimeout(100)
421cb0ef41Sopenharmony_ci);
43