Bug 1202706 - Part 4: Add tests for worker use counters; r=bzbarsky
authorEdgar Chen <echen@mozilla.com>
Wed, 27 Nov 2019 23:22:31 +0000
changeset 505567 2dfc53add3de16916c27818a904ab3e8adc250df
parent 505566 95b8b6b94213f4549642f0137d6e3ff0129ef68a
child 505568 93ec75efbf4117c8a4a6817c0627064d0a3ad31d
push id102347
push userechen@mozilla.com
push dateWed, 04 Dec 2019 23:23:46 +0000
treeherderautoland@2dfc53add3de [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1202706
milestone73.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1202706 - Part 4: Add tests for worker use counters; r=bzbarsky Differential Revision: https://phabricator.services.mozilla.com/D53829
dom/workers/test/browser.ini
dom/workers/test/browser_worker_use_counters.js
dom/workers/test/file_use_counter_service_worker.js
dom/workers/test/file_use_counter_shared_worker.js
dom/workers/test/file_use_counter_worker.html
dom/workers/test/file_use_counter_worker.js
--- a/dom/workers/test/browser.ini
+++ b/dom/workers/test/browser.ini
@@ -8,8 +8,14 @@ support-files =
 
 [browser_bug1047663.js]
 [browser_bug1104623.js]
 run-if = buildapp == 'browser'
 [browser_consoleSharedWorkers.js]
 support-files = sharedWorker_console.js empty.html
 [browser_fileURL.js]
 support-files = empty.html empty_worker.js
+[browser_worker_use_counters.js]
+support-files =
+  file_use_counter_worker.html
+  file_use_counter_worker.js
+  file_use_counter_shared_worker.js
+  file_use_counter_service_worker.js
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/browser_worker_use_counters.js
@@ -0,0 +1,158 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const gHttpTestRoot = "https://example.com/browser/dom/workers/test/";
+
+function grabHistogramsFromContent(
+  use_counter_name,
+  worker_type,
+  counter_before = null
+) {
+  let telemetry = Cc["@mozilla.org/base/telemetry;1"].getService(
+    Ci.nsITelemetry
+  );
+  let gather = () => {
+    let snapshots;
+    if (Services.appinfo.browserTabsRemoteAutostart) {
+      snapshots = telemetry.getSnapshotForHistograms("main", false).content;
+    } else {
+      snapshots = telemetry.getSnapshotForHistograms("main", false).parent;
+    }
+    let checkedGet = probe => {
+      return snapshots[probe] ? snapshots[probe].sum : 0;
+    };
+    return [
+      checkedGet(`USE_COUNTER2_${use_counter_name}_${worker_type}_WORKER`),
+      checkedGet(`${worker_type}_WORKER_DESTROYED`),
+    ];
+  };
+  return BrowserTestUtils.waitForCondition(() => {
+    return counter_before != gather()[0];
+  }).then(gather, gather);
+}
+
+var check_use_counter_worker = async function(
+  use_counter_name,
+  worker_type,
+  content_task
+) {
+  info(`checking ${use_counter_name} use counters for ${worker_type} worker`);
+
+  let newTab = BrowserTestUtils.addTab(gBrowser, "about:blank");
+  gBrowser.selectedTab = newTab;
+  newTab.linkedBrowser.stop();
+
+  // Hold on to the current values of the telemetry histograms we're
+  // interested in.
+  let [histogram_before, destructions_before] = await grabHistogramsFromContent(
+    use_counter_name,
+    worker_type
+  );
+
+  BrowserTestUtils.loadURI(
+    gBrowser.selectedBrowser,
+    gHttpTestRoot + "file_use_counter_worker.html"
+  );
+  await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
+  await content_task(gBrowser.selectedBrowser);
+
+  // Tear down the page.
+  let tabClosed = BrowserTestUtils.waitForTabClosing(newTab);
+  gBrowser.removeTab(newTab);
+  await tabClosed;
+
+  // Grab histograms again and compare.
+  let [histogram_after, destructions_after] = await grabHistogramsFromContent(
+    use_counter_name,
+    worker_type,
+    histogram_before
+  );
+
+  is(
+    histogram_after,
+    histogram_before + 1,
+    `histogram ${use_counter_name} counts for ${worker_type} worker are correct`
+  );
+  // There might be other workers created by prior tests get destroyed during
+  // this tests.
+  ok(
+    destructions_after > destructions_before,
+    `${worker_type} worker counts are correct`
+  );
+};
+
+add_task(async function test_dedicated_worker() {
+  await check_use_counter_worker("CONSOLE_LOG", "DEDICATED", async browser => {
+    await ContentTask.spawn(browser, {}, function() {
+      return new Promise(resolve => {
+        let worker = new content.Worker("file_use_counter_worker.js");
+        worker.onmessage = function(e) {
+          if (e.data === "DONE") {
+            worker.terminate();
+            resolve();
+          }
+        };
+      });
+    });
+  });
+});
+
+add_task(async function test_shared_worker() {
+  await check_use_counter_worker("CONSOLE_LOG", "SHARED", async browser => {
+    await ContentTask.spawn(browser, {}, function() {
+      return new Promise(resolve => {
+        let worker = new content.SharedWorker(
+          "file_use_counter_shared_worker.js"
+        );
+        worker.port.onmessage = function(e) {
+          if (e.data === "DONE") {
+            resolve();
+          }
+        };
+        worker.port.postMessage("RUN");
+      });
+    });
+  });
+});
+
+add_task(async function test_service_worker() {
+  await check_use_counter_worker("CONSOLE_LOG", "SERVICE", async browser => {
+    await ContentTask.spawn(browser, {}, function() {
+      let waitForActivated = async function(registration) {
+        return new Promise(resolve => {
+          let worker =
+            registration.installing ||
+            registration.waiting ||
+            registration.active;
+          if (worker.state === "activated") {
+            resolve(worker);
+            return;
+          }
+
+          worker.addEventListener("statechange", function onStateChange() {
+            if (worker.state === "activated") {
+              worker.removeEventListener("statechange", onStateChange);
+              resolve(worker);
+            }
+          });
+        });
+      };
+
+      return new Promise(resolve => {
+        content.navigator.serviceWorker
+          .register("file_use_counter_service_worker.js")
+          .then(async registration => {
+            content.navigator.serviceWorker.onmessage = function(e) {
+              if (e.data === "DONE") {
+                registration.unregister().then(resolve);
+              }
+            };
+            let worker = await waitForActivated(registration);
+            worker.postMessage("RUN");
+          });
+      });
+    });
+  });
+});
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/file_use_counter_service_worker.js
@@ -0,0 +1,9 @@
+onmessage = async function(e) {
+  if (e.data === "RUN") {
+    console.log("worker runs");
+    await clients.claim();
+    clients.matchAll().then(res => {
+      res.forEach(client => client.postMessage("DONE"));
+    });
+  }
+};
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/file_use_counter_shared_worker.js
@@ -0,0 +1,10 @@
+onconnect = function(e) {
+  let port = e.ports[0];
+  port.onmessage = function(m) {
+    if (m.data === "RUN") {
+      console.log("worker runs");
+      port.postMessage("DONE");
+      close();
+    }
+  };
+};
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/file_use_counter_worker.html
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1202706
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1202706</title>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1202706">Mozilla Bug 1202706</a>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/file_use_counter_worker.js
@@ -0,0 +1,2 @@
+console.log("worker runs");
+postMessage("DONE");