Backed out changeset 713114c0331a (bug 1938707) by developer request CLOSED TREE
[gecko.git] / js / xpconnect / tests / unit / test_onGarbageCollection-02.js
blobfc3bf685ef2279ff7870d284bb7a3f26dd6ec97c
1 // Test multiple debuggers, GCs, and zones interacting with each other.
2 //
3 // Note: when observing both globals, but GC'ing in only one, we don't test that
4 // we *didn't* GC in the other zone because GCs are finicky and unreliable. That
5 // used to work when this was a jit-test, but in the process of migrating to
6 // xpcshell, we lost some amount of reliability and determinism.
8 const root1 = newGlobal();
9 const dbg1 = new Debugger();
10 dbg1.addDebuggee(root1)
12 const root2 = newGlobal();
13 const dbg2 = new Debugger();
14 dbg2.addDebuggee(root2)
16 let fired1 = false;
17 let fired2 = false;
18 dbg1.memory.onGarbageCollection = _ => fired1 = true;
19 dbg2.memory.onGarbageCollection = _ => fired2 = true;
21 Services.prefs.setBoolPref("security.allow_eval_with_system_principal", true);
22 registerCleanupFunction(() => {
23   Services.prefs.clearUserPref("security.allow_eval_with_system_principal");
24 });
26 function reset() {
27   fired1 = false;
28   fired2 = false;
31 function run_test() {
32   do_test_pending();
34   gc();
35   executeSoon(() => {
36     reset();
38     // GC 1 only
39     root1.eval(`gc(this)`);
40     executeSoon(() => {
41       equal(fired1, true);
43       // GC 2 only
44       reset();
45       root2.eval(`gc(this)`);
46       executeSoon(() => {
47         equal(fired2, true);
49         // Full GC
50         reset();
51         gc();
52         executeSoon(() => {
53           equal(fired1, true);
54           equal(fired2, true);
56           // Full GC with no debuggees
57           reset();
58           dbg1.removeAllDebuggees();
59           dbg2.removeAllDebuggees();
60           gc();
61           executeSoon(() => {
62             equal(fired1, false);
63             equal(fired2, false);
65             // One debugger with multiple debuggees in different zones.
67             dbg1.addDebuggee(root1);
68             dbg1.addDebuggee(root2);
70             // Just debuggee 1
71             reset();
72             root1.eval(`gc(this)`);
73             executeSoon(() => {
74               equal(fired1, true);
75               equal(fired2, false);
77               // Just debuggee 2
78               reset();
79               root2.eval(`gc(this)`);
80               executeSoon(() => {
81                 equal(fired1, true);
82                 equal(fired2, false);
84                 // All debuggees
85                 reset();
86                 gc();
87                 executeSoon(() => {
88                   equal(fired1, true);
89                   equal(fired2, false);
90                   do_test_finished();
91                 });
92               });
93             });
94           });
95         });
96       });
97     });
98   });