Skip to content

Truly independent web browser

Tests/LibWeb: Add a helper that collects until WeakRefs are cleared

Shannon Booth90d5369223c8
diff --git a/Tests/LibWeb/Text/input/Fetch/completed-fetch-is-collectable.html b/Tests/LibWeb/Text/input/Fetch/completed-fetch-is-collectable.html
index a2558e65a9e..7cf2e7ed750 100644
--- a/Tests/LibWeb/Text/input/Fetch/completed-fetch-is-collectable.html
+++ b/Tests/LibWeb/Text/input/Fetch/completed-fetch-is-collectable.html
@@ -24,13 +24,7 @@
}
})();
- const maximumGcRoundsWhileRequestTeardownTasksDrain = 100;
- for (let round = 0; round < maximumGcRoundsWhileRequestTeardownTasksDrain; ++round) {
- await timeout(0);
- await internals.gcAsync();
- if (weakResponses.every(weak => weak.deref() === undefined))
- break;
- }
+ await collectGarbageUntilCleared(weakResponses);
const collected = weakResponses.filter(weak => weak.deref() === undefined).length;
println(`collected ${collected} of ${weakResponses.length} completed responses`);
diff --git a/Tests/LibWeb/Text/input/Fetch/document-with-aborted-fetches-is-collectable.html b/Tests/LibWeb/Text/input/Fetch/document-with-aborted-fetches-is-collectable.html
index aa4e39d8a35..84ed46fa369 100644
--- a/Tests/LibWeb/Text/input/Fetch/document-with-aborted-fetches-is-collectable.html
+++ b/Tests/LibWeb/Text/input/Fetch/document-with-aborted-fetches-is-collectable.html
@@ -85,13 +85,7 @@
return weakDocuments;
})();
- const maximumGcRoundsWhileRequestTeardownTasksDrain = 100;
- for (let round = 0; round < maximumGcRoundsWhileRequestTeardownTasksDrain; ++round) {
- await timeout(0);
- await internals.gcAsync();
- if (weakDocuments.every(weak => weak.deref() === undefined))
- break;
- }
+ await collectGarbageUntilCleared(weakDocuments);
const collected = weakDocuments.filter(weak => weak.deref() === undefined).length;
println(`collected ${collected} of ${weakDocuments.length} documents with aborted fetches`);
diff --git a/Tests/LibWeb/Text/input/Fetch/document-with-ongoing-fetches-is-collectable.html b/Tests/LibWeb/Text/input/Fetch/document-with-ongoing-fetches-is-collectable.html
index 0a7bc8437c7..43bf95e5ca7 100644
--- a/Tests/LibWeb/Text/input/Fetch/document-with-ongoing-fetches-is-collectable.html
+++ b/Tests/LibWeb/Text/input/Fetch/document-with-ongoing-fetches-is-collectable.html
@@ -74,13 +74,7 @@
return weakDocuments;
})();
- const maximumGcRoundsWhileRequestTeardownTasksDrain = 100;
- for (let round = 0; round < maximumGcRoundsWhileRequestTeardownTasksDrain; ++round) {
- await timeout(0);
- await internals.gcAsync();
- if (weakDocuments.every(weak => weak.deref() === undefined))
- break;
- }
+ await collectGarbageUntilCleared(weakDocuments);
const collected = weakDocuments.filter(weak => weak.deref() === undefined).length;
println(`collected ${collected} of ${weakDocuments.length} documents with ongoing fetches`);
diff --git a/Tests/LibWeb/Text/input/HTML/broadcast-channel-kept-alive-while-listening.html b/Tests/LibWeb/Text/input/HTML/broadcast-channel-kept-alive-while-listening.html
index 93168d8db8f..c5ad819f557 100644
--- a/Tests/LibWeb/Text/input/HTML/broadcast-channel-kept-alive-while-listening.html
+++ b/Tests/LibWeb/Text/input/HTML/broadcast-channel-kept-alive-while-listening.html
@@ -53,12 +53,7 @@
"open, with its message listener removed",
"open, with its onmessage handler cleared",
];
- for (let round = 0; round < 100; ++round) {
- await timeout(0);
- await internals.gcAsync();
- if (collectable.every(description => weakChannels.get(description).deref() === undefined))
- break;
- }
+ await collectGarbageUntilCleared(collectable.map(description => weakChannels.get(description)));
for (const [description, weakChannel] of weakChannels)
println(`${description}: ${weakChannel.deref() === undefined ? "collected" : "kept alive"}`);
diff --git a/Tests/LibWeb/Text/input/HTML/replaced-document-with-listening-broadcast-channel-is-collectable.html b/Tests/LibWeb/Text/input/HTML/replaced-document-with-listening-broadcast-channel-is-collectable.html
index ab712236eab..6bf0f7a34f2 100644
--- a/Tests/LibWeb/Text/input/HTML/replaced-document-with-listening-broadcast-channel-is-collectable.html
+++ b/Tests/LibWeb/Text/input/HTML/replaced-document-with-listening-broadcast-channel-is-collectable.html
@@ -35,13 +35,7 @@
return weakDocuments;
})();
- const maximumGcRoundsWhileTeardownTasksDrain = 100;
- for (let round = 0; round < maximumGcRoundsWhileTeardownTasksDrain; ++round) {
- await timeout(0);
- await internals.gcAsync();
- if (weakDocuments.every(weak => weak.deref() === undefined))
- break;
- }
+ await collectGarbageUntilCleared(weakDocuments);
const collected = weakDocuments.filter(weak => weak.deref() === undefined).length;
println(`collected ${collected} of ${weakDocuments.length} replaced documents`);
diff --git a/Tests/LibWeb/Text/input/HTML/replaced-document-with-source-element-media-is-collectable.html b/Tests/LibWeb/Text/input/HTML/replaced-document-with-source-element-media-is-collectable.html
index 3436cc28f84..a2759fdb43f 100644
--- a/Tests/LibWeb/Text/input/HTML/replaced-document-with-source-element-media-is-collectable.html
+++ b/Tests/LibWeb/Text/input/HTML/replaced-document-with-source-element-media-is-collectable.html
@@ -35,13 +35,7 @@
return weakDocuments;
})();
- const maximumGcRoundsWhileTeardownTasksDrain = 100;
- for (let round = 0; round < maximumGcRoundsWhileTeardownTasksDrain; ++round) {
- await timeout(0);
- await internals.gcAsync();
- if (weakDocuments.every(weak => weak.deref() === undefined))
- break;
- }
+ await collectGarbageUntilCleared(weakDocuments);
const collected = weakDocuments.filter(weak => weak.deref() === undefined).length;
println(`collected ${collected} of ${weakDocuments.length} replaced documents`);
diff --git a/Tests/LibWeb/Text/input/HTML/replaced-hidden-document-with-pending-animation-frame-is-collectable.html b/Tests/LibWeb/Text/input/HTML/replaced-hidden-document-with-pending-animation-frame-is-collectable.html
index 5de3410207e..765f9862610 100644
--- a/Tests/LibWeb/Text/input/HTML/replaced-hidden-document-with-pending-animation-frame-is-collectable.html
+++ b/Tests/LibWeb/Text/input/HTML/replaced-hidden-document-with-pending-animation-frame-is-collectable.html
@@ -37,13 +37,7 @@
return weakDocuments;
})();
- const maximumGcRoundsWhileTeardownTasksDrain = 100;
- for (let round = 0; round < maximumGcRoundsWhileTeardownTasksDrain; ++round) {
- await timeout(0);
- await internals.gcAsync();
- if (weakDocuments.every(weak => weak.deref() === undefined))
- break;
- }
+ await collectGarbageUntilCleared(weakDocuments);
const collected = weakDocuments.filter(weak => weak.deref() === undefined).length;
println(`collected ${collected} of ${weakDocuments.length} replaced documents`);
diff --git a/Tests/LibWeb/Text/input/HTML/replaced-hidden-document-with-pending-idle-callback-is-collectable.html b/Tests/LibWeb/Text/input/HTML/replaced-hidden-document-with-pending-idle-callback-is-collectable.html
index af29872da76..31c27c5445a 100644
--- a/Tests/LibWeb/Text/input/HTML/replaced-hidden-document-with-pending-idle-callback-is-collectable.html
+++ b/Tests/LibWeb/Text/input/HTML/replaced-hidden-document-with-pending-idle-callback-is-collectable.html
@@ -37,13 +37,7 @@
return weakDocuments;
})();
- const maximumGcRoundsWhileTeardownTasksDrain = 100;
- for (let round = 0; round < maximumGcRoundsWhileTeardownTasksDrain; ++round) {
- await timeout(0);
- await internals.gcAsync();
- if (weakDocuments.every(weak => weak.deref() === undefined))
- break;
- }
+ await collectGarbageUntilCleared(weakDocuments);
const collected = weakDocuments.filter(weak => weak.deref() === undefined).length;
println(`collected ${collected} of ${weakDocuments.length} replaced documents`);
diff --git a/Tests/LibWeb/Text/input/css/unused-font-document-collectable.html b/Tests/LibWeb/Text/input/css/unused-font-document-collectable.html
index c414b89852e..cadce551ea9 100644
--- a/Tests/LibWeb/Text/input/css/unused-font-document-collectable.html
+++ b/Tests/LibWeb/Text/input/css/unused-font-document-collectable.html
@@ -37,14 +37,7 @@ promiseTest(async () => {
return { weakDocument, weakFace };
}
const { weakDocument, weakFace } = await makeRemovedDocument();
- // Collect outside the creating JS job, after its locals and WeakRef kept-alive objects are released.
- const maximumGcRoundsForConservativeRoots = 100;
- for (let round = 0; round < maximumGcRoundsForConservativeRoots; ++round) {
- await timeout(0);
- await internals.gcAsync();
- if (weakDocument.deref() === undefined && weakFace.deref() === undefined)
- break;
- }
+ await collectGarbageUntilCleared([weakDocument, weakFace]);
println(`removed document collected: ${weakDocument.deref() === undefined}`);
println(`unused face collected: ${weakFace.deref() === undefined}`);
});
diff --git a/Tests/LibWeb/Text/input/include.js b/Tests/LibWeb/Text/input/include.js
index ea5fc446781..6c3a5d9c275 100644
--- a/Tests/LibWeb/Text/input/include.js
+++ b/Tests/LibWeb/Text/input/include.js
@@ -60,6 +60,16 @@ async function waitForCondition(condition, attempts = 600, interval = 25) {
return true;
}
+// Each round runs in a task of its own, so that pending tasks can drop their references and the objects that WeakRefs
+// kept alive for an earlier job are released.
+async function collectGarbageUntilCleared(weakRefs, rounds = 100) {
+ for (let round = 0; round < rounds; ++round) {
+ await timeout(0);
+ await internals.gcAsync();
+ if (weakRefs.every(weakRef => weakRef.deref() === undefined)) return;
+ }
+}
+
async function setSystemVisibilityState(state) {
// Tests assume that the document reflects the UI process's system visibility state and that
// no previous visibility state change is still pending.
diff --git a/Tests/LibWeb/Text/input/navigation/replaced-document-is-collectable.html b/Tests/LibWeb/Text/input/navigation/replaced-document-is-collectable.html
index cd5da71a527..05db6fe62b2 100644
--- a/Tests/LibWeb/Text/input/navigation/replaced-document-is-collectable.html
+++ b/Tests/LibWeb/Text/input/navigation/replaced-document-is-collectable.html
@@ -41,13 +41,7 @@
return weakDocuments;
})();
- const maximumGcRoundsWhileRequestTeardownTasksDrain = 100;
- for (let round = 0; round < maximumGcRoundsWhileRequestTeardownTasksDrain; ++round) {
- await timeout(0);
- await internals.gcAsync();
- if (weakDocuments.every(weak => weak.deref() === undefined))
- break;
- }
+ await collectGarbageUntilCleared(weakDocuments);
const collected = weakDocuments.filter(weak => weak.deref() === undefined).length;
println(`collected ${collected} of ${weakDocuments.length} replaced documents`);