Skip to content

Commit eb608fa

Browse files
committedAug 6, 2020
improve test and cost function
1 parent 080646e commit eb608fa

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed
 

‎lib/reducePlan.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ module.exports = (plan, limit) => {
7474
for (const node of treeMap.values()) {
7575
if (node.entries <= 1 || !node.children) continue;
7676
if (node.children.length <= 1) continue;
77-
const cost = Math.abs(overLimit - node.entries + 1);
77+
// Try to select the node with has just a bit more entries than we need to reduce
78+
// When just a bit more is over 30% over the limit,
79+
// also consider just a bit less entries then we need to reduce
80+
const cost =
81+
node.entries - 1 >= overLimit
82+
? node.entries - 1 - overLimit
83+
: overLimit - node.entries + 1 + limit * 0.3;
7884
if (cost < bestCost) {
7985
bestNode = node;
8086
bestCost = cost;

‎lib/watchEventSource.js

+4
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,7 @@ exports.batch = fn => {
298298
execute();
299299
}
300300
};
301+
302+
exports.getNumberOfWatchers = () => {
303+
return watcherCount;
304+
};

‎test/ManyWatchers.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require("should");
55
const path = require("path");
66
const TestHelper = require("./helpers/TestHelper");
77
const Watchpack = require("../lib/watchpack");
8+
const watchEventSource = require("../lib/watchEventSource");
89

910
const fixtures = path.join(__dirname, "fixtures");
1011
const testHelper = new TestHelper(fixtures);
@@ -16,9 +17,15 @@ describe("ManyWatchers", function() {
1617

1718
it("should watch more than 4096 directories", done => {
1819
const files = [];
19-
for (let i = 0; i < 5000; i++) {
20-
const dir = `${Math.floor(i / 100)}/${i % 100}`;
21-
if (i % 100 === 0) testHelper.dir(`${Math.floor(i / 100)}`);
20+
for (let i = 1; i <= 5000; i++) {
21+
let highBit = 1;
22+
let j = i;
23+
while (j > 1) {
24+
highBit <<= 1;
25+
j >>= 1;
26+
}
27+
const dir = `${i & highBit}/${i & ~highBit}`;
28+
if (i === highBit) testHelper.dir(`${i}`);
2229
testHelper.dir(dir);
2330
testHelper.file(`${dir}/file`);
2431
files.push(path.join(fixtures, dir, "file"));
@@ -28,7 +35,9 @@ describe("ManyWatchers", function() {
2835
aggregateTimeout: 1000
2936
});
3037
w.on("aggregated", function(changes) {
31-
Array.from(changes).should.be.eql([path.join(fixtures, "49/49/file")]);
38+
Array.from(changes).should.be.eql([
39+
path.join(fixtures, "4096/900/file")
40+
]);
3241
w.close();
3342
done();
3443
});
@@ -39,7 +48,7 @@ describe("ManyWatchers", function() {
3948
}
4049
w.watch({ files });
4150
testHelper.tick(10000, () => {
42-
testHelper.file("49/49/file");
51+
testHelper.file("4096/900/file");
4352
});
4453
});
4554
});

‎test/helpers/TestHelper.js

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var path = require("path");
55
var rimraf = require("rimraf");
66
var writeFileAtomic = require("write-file-atomic");
77

8+
var watchEventSource = require("../../lib/watchEventSource");
9+
810
require("../../lib/getWatcherManager");
911
var watcherManagerModule =
1012
require.cache[require.resolve("../../lib/getWatcherManager")];
@@ -20,6 +22,7 @@ const checkAllWatcherClosed = () => {
2022
for (const watcherManager of allWatcherManager) {
2123
Array.from(watcherManager.directoryWatchers.keys()).should.be.eql([]);
2224
}
25+
watchEventSource.getNumberOfWatchers().should.be.eql(0);
2326
};
2427

2528
function TestHelper(testdir) {

0 commit comments

Comments
 (0)
Please sign in to comment.