1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:47:46 +00:00

test-js: Add a mark_as_garbage method to force GC to collect that object

This should fix the flaky tests of test-js.
It also fixes the tests when running with the -g flag since the values
will not be garbage collected too soon.
This commit is contained in:
davidot 2021-09-07 17:14:05 +02:00 committed by Linus Groh
parent 3373090993
commit 43b17f27a3
6 changed files with 71 additions and 12 deletions

View file

@ -3,11 +3,12 @@ test("length is 0", () => {
});
function registerInDifferentScope(registry) {
registry.register({}, {});
const target = {};
registry.register(target, {});
return target;
}
// Flaky test, investigate and fix :^)
test.skip("basic functionality", () => {
test("basic functionality", () => {
var registry = new FinalizationRegistry(() => {});
var count = 0;
@ -19,7 +20,8 @@ test.skip("basic functionality", () => {
expect(count).toBe(0);
registerInDifferentScope(registry);
const target = registerInDifferentScope(registry);
markAsGarbage("target");
gc();
registry.cleanupSome(increment);

View file

@ -21,10 +21,13 @@ test("invalid values", () => {
test("automatic removal of garbage-collected values", () => {
const weakMap = new WeakMap();
{
expect(weakMap.set({ a: 1 }, 1)).toBe(weakMap);
expect(getWeakMapSize(weakMap)).toBe(1);
}
const key = { e: 3 };
expect(weakMap.set(key, 1)).toBe(weakMap);
expect(getWeakMapSize(weakMap)).toBe(1);
markAsGarbage("key");
gc();
expect(getWeakMapSize(weakMap)).toBe(0);
});

View file

@ -17,10 +17,13 @@ test("invalid values", () => {
test("automatic removal of garbage-collected values", () => {
const weakSet = new WeakSet();
{
expect(weakSet.add({ a: 1 })).toBe(weakSet);
expect(getWeakSetSize(weakSet)).toBe(1);
}
const item = { a: 1 };
expect(weakSet.add(item)).toBe(weakSet);
expect(getWeakSetSize(weakSet)).toBe(1);
markAsGarbage("item");
gc();
expect(getWeakSetSize(weakSet)).toBe(0);
});