1'use strict';
2
3// This tests that closing a watcher when the underlying handle is
4// already destroyed will result in a noop instead of a crash.
5
6const common = require('../common');
7
8if (common.isIBMi)
9  common.skip('IBMi does not support `fs.watch()`');
10
11const tmpdir = require('../common/tmpdir');
12const fs = require('fs');
13const path = require('path');
14
15tmpdir.refresh();
16const root = path.join(tmpdir.path, 'watched-directory');
17fs.mkdirSync(root);
18
19const watcher = fs.watch(root, { persistent: false, recursive: false });
20
21// The following listeners may or may not be invoked.
22
23watcher.addListener('error', () => {
24  setTimeout(
25    () => { watcher.close(); },  // Should not crash if it's invoked
26    common.platformTimeout(10)
27  );
28});
29
30watcher.addListener('change', () => {
31  setTimeout(
32    () => { watcher.close(); },
33    common.platformTimeout(10)
34  );
35});
36
37fs.rmdirSync(root);
38// Wait for the listener to hit
39setTimeout(
40  common.mustCall(),
41  common.platformTimeout(100)
42);
43