From 5755a94b869116219f891e1c275379c7791ef170 Mon Sep 17 00:00:00 2001 From: Anny Gakhokidze Date: Fri, 28 Aug 2020 17:20:57 +0000 Subject: [PATCH] Bug 1589102 - Part 7: Fix test_browsing_context_structured_clone.js to instead schedule precise gc, r=kmag Differential Revision: https://phabricator.services.mozilla.com/D85086 --- .../test_browsing_context_structured_clone.js | 23 ++++++++----------- testing/xpcshell/head.js | 12 ++++++++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/docshell/test/unit/test_browsing_context_structured_clone.js b/docshell/test/unit/test_browsing_context_structured_clone.js index ebd27b09752b..d06f7aecf601 100644 --- a/docshell/test/unit/test_browsing_context_structured_clone.js +++ b/docshell/test/unit/test_browsing_context_structured_clone.js @@ -4,7 +4,11 @@ add_task(async function test_BrowsingContext_structured_clone() { let browser = Services.appShell.createWindowlessBrowser(false); let frame = browser.document.createElement("iframe"); - browser.document.body.appendChild(frame); + + await new Promise(r => { + frame.onload = () => r(); + browser.document.body.appendChild(frame); + }); let { browsingContext } = frame; @@ -39,11 +43,6 @@ add_task(async function test_BrowsingContext_structured_clone() { // Beyond that, we don't *have* to reload or destroy the parent // document, but we do anyway just to be safe. // - // Then comes the tricky bit: The WindowGlobal actors (which keep - // references to their BrowsingContexts) require an IPC round trip to - // be completely destroyed, even though they're in-process, so we make - // a quick trip through the event loop, and then run the CC in order - // to allow their (now snow-white) references to be collected. frame.remove(); frame = null; @@ -52,13 +51,11 @@ add_task(async function test_BrowsingContext_structured_clone() { browser.document.location.reload(); browser.close(); - Cu.forceGC(); - - // Give the IPC messages that destroy the actors a chance to be - // processed. - await new Promise(executeSoon); - - Cu.forceCC(); + // We will schedule a precise GC and do both GC and CC a few times, to make + // sure we have completely destroyed the WindowGlobal actors (which keep + // references to their BrowsingContexts) in order + // to allow their (now snow-white) references to be collected. + await schedulePreciseGCAndForceCC(3); // OK. We can be fairly confident that the BrowsingContext object // stored in our structured clone data has been destroyed. Make sure diff --git a/testing/xpcshell/head.js b/testing/xpcshell/head.js index ccab0d1175db..1bbadde6953f 100644 --- a/testing/xpcshell/head.js +++ b/testing/xpcshell/head.js @@ -1463,6 +1463,18 @@ function do_send_remote_message(name, data) { mm[sender](name, data); } +/** + * Schedules and awaits a precise GC, and forces CC, `maxCount` number of times. + * @param maxCount + * How many times GC and CC should be scheduled. + */ +async function schedulePreciseGCAndForceCC(maxCount) { + for (let count = 0; count < maxCount; count++) { + await new Promise(resolve => Cu.schedulePreciseGC(resolve)); + Cu.forceCC(); + } +} + /** * Add a test function to the list of tests that are to be run asynchronously. *