Bug 1428076 - Add a mochitest r=mstange draft
authorJulien Wajsberg <felash@gmail.com>
Wed, 24 Jan 2018 11:31:50 +0100
changeset 724177 93d2052a81d0b3cb7abc0fdc27b3a325f67886ac
parent 724090 72dcdcf152762be0238fc0cb66ce06ee8446cf61
child 747080 ee167a689333118ddf6c2d2747df5d3293e877f6
push id96673
push userbmo:felash@gmail.com
push dateWed, 24 Jan 2018 16:11:55 +0000
reviewersmstange
bugs1428076
milestone60.0a1
Bug 1428076 - Add a mochitest r=mstange MozReview-Commit-ID: EhNoJuMhrAS
tools/profiler/moz.build
tools/profiler/tests/chrome/.eslintrc.js
tools/profiler/tests/chrome/chrome.ini
tools/profiler/tests/chrome/test_profile_worker_bug_1428076.html
--- a/tools/profiler/moz.build
+++ b/tools/profiler/moz.build
@@ -129,16 +129,17 @@ if CONFIG['MOZ_TASK_TRACER']:
         'tasktracer/TracedTaskCommon.h',
     ]
     UNIFIED_SOURCES += [
         'tasktracer/GeckoTaskTracer.cpp',
         'tasktracer/TracedTaskCommon.cpp',
     ]
 
 XPCSHELL_TESTS_MANIFESTS += ['tests/xpcshell.ini']
+MOCHITEST_CHROME_MANIFESTS += ['tests/chrome/chrome.ini']
 
 if CONFIG['CC_TYPE'] in ('clang', 'gcc'):
     CXXFLAGS += [
         '-Wno-error=shadow',
         '-Wno-ignored-qualifiers', # due to use of breakpad headers
     ]
 
 with Files('**'):
new file mode 100644
--- /dev/null
+++ b/tools/profiler/tests/chrome/.eslintrc.js
@@ -0,0 +1,6 @@
+"use strict";
+
+module.exports = {
+  // Extend from the shared list of defined globals for mochitests.
+  "extends": "plugin:mozilla/chrome-test"
+};
new file mode 100644
--- /dev/null
+++ b/tools/profiler/tests/chrome/chrome.ini
@@ -0,0 +1,1 @@
+[test_profile_worker_bug_1428076.html]
new file mode 100644
--- /dev/null
+++ b/tools/profiler/tests/chrome/test_profile_worker_bug_1428076.html
@@ -0,0 +1,108 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1428076
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1428076</title>
+  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
+  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1428076">Mozilla Bug 1428076</a>
+
+<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+<script type="application/javascript">
+
+/** Test for Bug 1428076 **/
+"use strict";
+Components.utils.import("resource://gre/modules/Services.jsm");
+
+function startProfiler() {
+  const settings = {
+    entries: 1000000, // 9MB
+    interval: 1, // ms
+    features: ["js", "threads", "leaf", "stackwalk"],
+    threads: ["GeckoMain", "Compositor", "Worker"] // most common combination
+  };
+
+  Services.profiler.StartProfiler(
+    settings.entries,
+    settings.interval,
+    settings.features,
+    settings.features.length,
+    settings.threads,
+    settings.threads.length
+  );
+
+  info("Profiler has started");
+}
+
+function getProfile() {
+  const profile = Services.profiler.getProfileData();
+  info("We got a profile");
+
+  // Run the mochitest with `--keep-open true` to see the logged profile in the
+  // Web console.
+  console.log(profile);
+
+  return profile;
+}
+
+function stopProfiler() {
+  Services.profiler.StopProfiler();
+  info("Profiler has stopped");
+}
+
+function end(error) {
+  if (error) {
+    ok(false, `We got an error: ${error}`);
+  } else {
+    ok(true, "We ran the whole process");
+  }
+  SimpleTest.finish();
+}
+
+function workload() {
+  // We use a Blob for the worker content to avoid an external JS file, and data
+  // URLs seem to be blocked in a chrome environment.
+  const workerContent = new Blob(
+    [ "console.log('hello world!')" ],
+    { type: "application/javascript" }
+  );
+  const blobURL = URL.createObjectURL(workerContent);
+
+  // We start a worker and then terminate it right away to trigger our bug.
+  info("Starting the worker, and terminate it right away.");
+  const myWorker = new Worker(blobURL);
+  myWorker.terminate();
+
+  URL.revokeObjectURL(blobURL);
+
+  // We're deferring some little time so that the worker has the time to be
+  // properly cleaned up and the profiler actually saves the worker data.
+  return new Promise(resolve => {
+    setTimeout(resolve, 50);
+  });
+}
+
+SimpleTest.waitForExplicitFinish();
+(async function() {
+  try {
+    await startProfiler();
+    await workload();
+    await getProfile();
+    await stopProfiler();
+    await end();
+  } catch (e) {
+    // By catching and handling the error, we're being nice to mochitest
+    // runners: instead of waiting for the timeout, we fail right away.
+    await end(e);
+  }
+})();
+
+
+</script>
+</body>
+</html>