Bug 1412837 - damp fix draft
authorFred Lin <gasolin@gmail.com>
Tue, 07 Nov 2017 12:57:04 +0800
changeset 693927 4375153f40797e9ddd7c423e0cd5efb3e255adb0
parent 693926 77068f53090527e57c901eab823167a81420c02b
child 739201 f4ae9d9f85351ca3ee80f3e82dab319a90d8ca9f
push id87983
push userbmo:gasolin@mozilla.com
push dateTue, 07 Nov 2017 04:57:48 +0000
bugs1412837
milestone58.0a1
Bug 1412837 - damp fix MozReview-Commit-ID: 8Re5mtdjTMm
testing/talos/talos/tests/devtools/addon/content/damp.js
--- a/testing/talos/talos/tests/devtools/addon/content/damp.js
+++ b/testing/talos/talos/tests/devtools/addon/content/damp.js
@@ -1,11 +1,51 @@
 const { Services } = Components.utils.import("resource://gre/modules/Services.jsm", {});
 const { Task } = Cu.import("resource://gre/modules/Task.jsm", {});
 const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
+const gMgr = Cc['@mozilla.org/memory-reporter-manager;1'].getService(Ci.nsIMemoryReporterManager);
+
+// Flush any action pending on the event loop
+// Return the number of processed events, if any.
+function processPendingEvents() {
+  let loops = 0;
+  while (Services.tm.currentThread.hasPendingEvents()) {
+    Services.tm.currentThread.processNextEvent(true);
+    loops++;
+  }
+  return loops;
+}
+// Garbage collect everything.
+function garbageCollect() {
+  dump("Garbage collect\n");
+
+  // Minimize memory usage
+  Services.obs.notifyObservers(null, "child-mmu-request");
+  return new Promise(done => {
+    gMgr.minimizeMemoryUsage(done);
+  });
+}
+// Function to help waiting for all previous asynchronous action to be finished,
+// and also garbage collect things to avoid having a GC cause by previous test.
+async function settle() {
+  let start = performance.now();
+  await garbageCollect();
+
+  dump("Flush event loop\n");
+  // Wait for the event loop to be empty 10 times in a row for 500ms
+  let wait = 10;
+  while(wait > 0) {
+    while(processPendingEvents() > 0) {
+      wait = 10;
+    }
+    await new Promise(done => setTimeout(done, 50));
+    wait--;
+  }
+  dump("Settled in " + Math.round(performance.now() - start) + "ms\n");
+}
 
 XPCOMUtils.defineLazyGetter(this, "require", function() {
   let { require } =
     Components.utils.import("resource://devtools/shared/Loader.jsm", {});
   return require;
 });
 XPCOMUtils.defineLazyGetter(this, "gDevTools", function() {
   let { gDevTools } = require("devtools/client/framework/devtools");
@@ -442,16 +482,17 @@ async _consoleOpenWithCachedMessagesTest
     let {time} = await this.closeToolbox();
     this._results.push({name: name + ".close.DAMP", value: time });
   },
 
   async reloadPageAndLog(name, onReload) {
     dump("Reload page on '" + name + "'\n");
     let {time} = await this.reloadPage(onReload);
     this._results.push({name: name + ".reload.DAMP", value: time });
+    await settle();
   },
 
   async _coldInspectorOpen() {
     await this.testSetup(SIMPLE_URL);
     await this.openToolboxAndLog("cold.inspector", "inspector");
     await this.closeToolbox();
     await this.testTeardown();
   },
@@ -578,20 +619,21 @@ async _consoleOpenWithCachedMessagesTest
   testSetup: Task.async(function* (url) {
     let tab = yield this.addTab(url);
     yield new Promise(resolve => {
       setTimeout(resolve, this._config.rest);
     });
     return tab;
   }),
 
-  testTeardown: Task.async(function* (url) {
+  async testTeardown(url) {
     this.closeCurrentTab();
+    await settle();
     this._nextCommand();
-  }),
+  },
 
   // Everything below here are common pieces needed for the test runner to function,
   // just copy and pasted from Tart with /s/TART/DAMP
 
   _win: undefined,
   _dampTab: undefined,
   _results: [],
   _config: {subtests: [], repeat: 1, rest: 100},