Skip to content

Commit

Permalink
Remove console.*, increase timeout, and poll gc() more aggressively.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Nov 10, 2023
1 parent dced7eb commit 46c6217
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
37 changes: 26 additions & 11 deletions packages/caches/src/tests/weak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ import { WeakCache } from "../weak.js";

describe("weak least-recently-used cache", function () {
it("can hold lots of elements", async function () {
this.timeout(5000);
this.timeout(10000);
const cache = new WeakCache();
const count = 1000000;
const keys = [];

console.time("filling up cache");
for (let i = 0; i < count; ++i) {
const key = {};
cache.set(key, String(i));
keys[i] = key;
}
console.timeEnd("filling up cache");
console.time("waiting for finalization");
await waitForCache(cache);
console.timeEnd("waiting for finalization");

cache.clean();

Expand Down Expand Up @@ -71,14 +67,33 @@ describe("weak least-recently-used cache", function () {
keys[i] = null;
}

global.gc!();
await new Promise((resolve) => setTimeout(resolve, 0));
global.gc!();
await new Promise((resolve) => setTimeout(resolve, 0));

assert.strictEqual(cache.size, 50);
return gcPromise(() => {
return cache.size > 50 ? null : () => {
assert.strictEqual(cache.size, 50);
assert.strictEqual(keys.length, 100);
assert.strictEqual(new Set(keys).size, 51);
};
});
});

function gcPromise(test: () => null | (() => void)) {
return new Promise<void>(function (resolve, reject) {
function pollGC() {
global.gc!();
const testCallback = test();
if (!testCallback) {
setTimeout(pollGC, 20);
} else try {
testCallback();
resolve();
} catch (e) {
reject(e);
}
}
pollGC();
});
}

it("can cope with small max values", async function () {
const cache = new WeakCache(2);
const keys = Array(10)
Expand Down
2 changes: 0 additions & 2 deletions packages/caches/src/weak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ export class WeakCache<K extends object = any, V = any>
}

private finalize = () => {
console.time("finalizing a chunk");
const iterator = this.unfinalizedNodes.values();
for (let i = 0; i < finalizationBatchSize; i++) {
const node = iterator.next().value;
Expand All @@ -197,6 +196,5 @@ export class WeakCache<K extends object = any, V = any>
} else {
this.finalizationScheduled = false;
}
console.timeEnd("finalizing a chunk");
};
}

0 comments on commit 46c6217

Please sign in to comment.