@@ -59,10 +59,32 @@ checkInvocations(statwatcher1, { init: 1 }, |
59 | 59 | 'watcher1: when started to watch second file'); |
60 | 60 | checkWatcherStart('watcher2', statwatcher2); |
61 | 61 |
|
62 | | -setTimeout(() => fs.writeFileSync(file1, 'foo++'), |
63 | | - common.platformTimeout(100)); |
| 62 | +let w2Initialized = false; |
| 63 | +let writeFile2AfterW1 = false; |
| 64 | + |
| 65 | +const onW2Initialized = (curr, prev) => { |
| 66 | + if (curr.nlink !== 0 || prev.nlink !== 0) |
| 67 | + return; |
| 68 | + |
| 69 | + w2.removeListener('change', onW2Initialized); |
| 70 | + w2Initialized = true; |
| 71 | + if (writeFile2AfterW1) { |
| 72 | + setTimeout(() => fs.writeFileSync(file2, 'bar++'), |
| 73 | + common.platformTimeout(100)); |
| 74 | + } |
| 75 | +}; |
| 76 | +w2.on('change', onW2Initialized); |
| 77 | + |
64 | 78 | w1.on('change', common.mustCallAtLeast((curr, prev) => { |
65 | 79 | console.log('w1 change to', curr, 'from', prev); |
| 80 | + // Wait for the initial ENOENT poll before creating the file. Otherwise the |
| 81 | + // first stat can race with the write and use the created file as the baseline. |
| 82 | + if (curr.nlink === 0 && prev.nlink === 0) { |
| 83 | + setTimeout(() => fs.writeFileSync(file1, 'foo++'), |
| 84 | + common.platformTimeout(100)); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
66 | 88 | // Wait until we get the write above. |
67 | 89 | if (prev.size !== 0 || curr.size !== 5) |
68 | 90 | return; |
@@ -74,8 +96,11 @@ w1.on('change', common.mustCallAtLeast((curr, prev) => { |
74 | 96 | checkInvocations(statwatcher2, { init: 1 }, |
75 | 97 | 'watcher2: when unwatched first file'); |
76 | 98 |
|
77 | | - setTimeout(() => fs.writeFileSync(file2, 'bar++'), |
78 | | - common.platformTimeout(100)); |
| 99 | + writeFile2AfterW1 = true; |
| 100 | + if (w2Initialized) { |
| 101 | + setTimeout(() => fs.writeFileSync(file2, 'bar++'), |
| 102 | + common.platformTimeout(100)); |
| 103 | + } |
79 | 104 | w2.on('change', common.mustCallAtLeast((curr, prev) => { |
80 | 105 | console.log('w2 change to', curr, 'from', prev); |
81 | 106 | // Wait until we get the write above. |
|