Bug 1892041 - Part 3: Update test exclusions. r=spidermonkey-reviewers,dminor
[gecko.git] / testing / mochitest / ShutdownLeaksCollector.sys.mjs
blob44b2025429eb87408475a64fc65c9ea2f16d52b3
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 import { setTimeout } from "resource://gre/modules/Timer.sys.mjs";
7 // This listens for the message "browser-test:collect-request". When it gets it,
8 // it runs some GCs and CCs, then prints out a message indicating the collections
9 // are complete. Mochitest uses this information to determine when windows and
10 // docshells should be destroyed.
12 export var ContentCollector = {
13   init() {
14     let processType = Services.appinfo.processType;
15     if (processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
16       // In the main process, we handle triggering collections in browser-test.js
17       return;
18     }
20     Services.cpmm.addMessageListener("browser-test:collect-request", this);
21   },
23   receiveMessage(aMessage) {
24     switch (aMessage.name) {
25       case "browser-test:collect-request":
26         Services.obs.notifyObservers(null, "memory-pressure", "heap-minimize");
28         Cu.forceGC();
29         Cu.forceCC();
31         let shutdownCleanup = aCallback => {
32           Cu.schedulePreciseShrinkingGC(() => {
33             // Run the GC and CC a few times to make sure that as much
34             // as possible is freed.
35             Cu.forceGC();
36             Cu.forceCC();
37             aCallback();
38           });
39         };
41         shutdownCleanup(() => {
42           setTimeout(() => {
43             shutdownCleanup(() => {
44               this.finish();
45             });
46           }, 1000);
47         });
49         break;
50     }
51   },
53   finish() {
54     let pid = Services.appinfo.processID;
55     dump("Completed ShutdownLeaks collections in process " + pid + "\n");
57     Services.cpmm.removeMessageListener("browser-test:collect-request", this);
58   },
61 ContentCollector.init();