|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const fs = require("fs"); |
| 4 | +const path = require("path"); |
| 5 | +const Watchpack = require("../lib"); |
| 6 | +const TestHelper = require("./helpers/TestHelper"); |
| 7 | + |
| 8 | +const fixtures = path.join(__dirname, "fixtures"); |
| 9 | +const testHelper = new TestHelper(fixtures); |
| 10 | + |
| 11 | +// eslint-disable-next-line jest/no-confusing-set-timeout |
| 12 | +jest.setTimeout(20000); |
| 13 | + |
| 14 | +describe("Watcher limit of the system (#46)", () => { |
| 15 | + beforeEach((done) => { |
| 16 | + testHelper.before(done); |
| 17 | + }); |
| 18 | + |
| 19 | + /** @type {InstanceType<typeof Watchpack>[]} */ |
| 20 | + let watchers = []; |
| 21 | + |
| 22 | + afterEach((done) => { |
| 23 | + // closed here, not in the test, so a failure cannot leave one running |
| 24 | + for (const w of watchers) w.close(); |
| 25 | + watchers = []; |
| 26 | + jest.restoreAllMocks(); |
| 27 | + testHelper.after(done); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should poll a directory it cannot watch instead of reporting its files as removed", (done) => { |
| 31 | + const dir = path.join(fixtures, "dir"); |
| 32 | + const file = path.join(dir, "a"); |
| 33 | + testHelper.dir("dir"); |
| 34 | + testHelper.file(path.join("dir", "a")); |
| 35 | + const { watch } = fs; |
| 36 | + let watchCalls = 0; |
| 37 | + jest.spyOn(fs, "watch").mockImplementation((target, ...args) => { |
| 38 | + if (target === dir) { |
| 39 | + watchCalls++; |
| 40 | + throw Object.assign( |
| 41 | + new Error( |
| 42 | + `ENOSPC: System limit for number of file watchers reached, watch '${dir}'`, |
| 43 | + ), |
| 44 | + { code: "ENOSPC" }, |
| 45 | + ); |
| 46 | + } |
| 47 | + return /** @type {(...a: unknown[]) => fs.FSWatcher} */ (watch)( |
| 48 | + target, |
| 49 | + ...args, |
| 50 | + ); |
| 51 | + }); |
| 52 | + const errors = jest.spyOn(console, "error").mockImplementation(() => {}); |
| 53 | + testHelper.tick(1000, () => { |
| 54 | + const w = new Watchpack({ aggregateTimeout: 100 }); |
| 55 | + watchers.push(w); |
| 56 | + /** @type {string[]} */ |
| 57 | + const removed = []; |
| 58 | + w.on("remove", (item) => { |
| 59 | + removed.push(item); |
| 60 | + }); |
| 61 | + let edited = false; |
| 62 | + w.on("change", (item) => { |
| 63 | + // coarse timestamps can report the file in the initial scan |
| 64 | + if (!edited) return; |
| 65 | + expect(item).toBe(file); |
| 66 | + expect(removed).toEqual([]); |
| 67 | + // tried once, not again and again |
| 68 | + expect(watchCalls).toBeLessThanOrEqual(1); |
| 69 | + expect( |
| 70 | + errors.mock.calls.filter((call) => /ENOSPC/.test(String(call[0]))), |
| 71 | + ).toHaveLength(watchCalls); |
| 72 | + done(); |
| 73 | + }); |
| 74 | + w.watch({ files: [file], startTime: Date.now() }); |
| 75 | + testHelper.tick(1000, () => { |
| 76 | + edited = true; |
| 77 | + testHelper.file(path.join("dir", "a")); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments