Bug 1512220 - Convert debugger server test helper to async functions. r=yulia
authorAlexandre Poirot <poirot.alex@gmail.com>
Tue, 11 Dec 2018 12:14:59 +0000
changeset 510207 943836f5e52865b4c4a39c127cdc274265d0bd72
parent 510206 9367d35f4dab2c99cf14cdbfe7f5fd6e07c5e4f8
child 510208 f9ae7fd32a86992d7e00807412df506c4f4d610f
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersyulia
bugs1512220
milestone66.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 1512220 - Convert debugger server test helper to async functions. r=yulia This cleanup will help next refactorings. Note that attachTestThread and attachTestTabAndResume are still using the callback style as it is still being used by tests that aren't using threadClientTest helper. MozReview-Commit-ID: 6FvoDtzEBmL Depends on D13901 Differential Revision: https://phabricator.services.mozilla.com/D13902
devtools/server/tests/unit/head_dbg.js
--- a/devtools/server/tests/unit/head_dbg.js
+++ b/devtools/server/tests/unit/head_dbg.js
@@ -370,74 +370,65 @@ function testGlobal(name) {
 function addTestGlobal(name, server = DebuggerServer) {
   const global = testGlobal(name);
   server.addTestGlobal(global);
   return global;
 }
 
 // List the DebuggerClient |client|'s tabs, look for one whose title is
 // |title|, and apply |callback| to the packet's entry for that tab.
-function getTestTab(client, title, callback) {
-  client.listTabs().then(function(response) {
-    for (const tab of response.tabs) {
-      if (tab.title === title) {
-        callback(tab);
-        return;
-      }
+async function getTestTab(client, title) {
+  const { tabs } = await client.mainRoot.listTabs();
+  for (const tab of tabs) {
+    if (tab.title === title) {
+      return tab;
     }
-    callback(null);
-  });
+  }
+  return null;
 }
 
-// Attach to |client|'s tab whose title is |title|; pass |callback| the
-// response packet and a TargetFront instance referring to that tab.
-function attachTestTab(client, title, callback) {
-  getTestTab(client, title, function(tab) {
-    client.attachTarget(tab).then(([response, targetFront]) => {
-      Assert.equal(response.type, "tabAttached");
-      Assert.ok(typeof response.threadActor === "string");
-      callback(response, targetFront);
-    });
-  });
+// Attach to |client|'s tab whose title is |title|; and return the targetFront instance
+// referring to that tab.
+async function attachTestTab(client, title) {
+  const tab = await getTestTab(client, title);
+  const [, targetFront] = await client.attachTarget(tab);
+  const response = await targetFront.attach();
+  Assert.equal(response.type, "tabAttached");
+  Assert.ok(typeof response.threadActor === "string");
+  return targetFront;
 }
 
 // Attach to |client|'s tab whose title is |title|, and then attach to
 // that tab's thread. Pass |callback| the thread attach response packet, a
 // TargetFront referring to the tab, and a ThreadClient referring to the
 // thread.
-function attachTestThread(client, title, callback) {
-  attachTestTab(client, title, function(tabResponse, targetFront) {
-    function onAttach([response, threadClient]) {
-      Assert.equal(threadClient.state, "paused", "Thread client is paused");
-      Assert.equal(response.type, "paused");
-      Assert.ok("why" in response);
-      Assert.equal(response.why.type, "attached");
-      callback(response, targetFront, threadClient, tabResponse);
-    }
-    targetFront.attachThread({
-      useSourceMaps: true,
-      autoBlackBox: true,
-    }).then(onAttach);
+async function attachTestThread(client, title, callback = () => {}) {
+  const targetFront = await attachTestTab(client, title);
+  const [response, threadClient] = await targetFront.attachThread({
+    useSourceMaps: true,
+    autoBlackBox: true,
   });
+  Assert.equal(threadClient.state, "paused", "Thread client is paused");
+  Assert.equal(response.type, "paused");
+  Assert.ok("why" in response);
+  Assert.equal(response.why.type, "attached");
+  callback(response, targetFront, threadClient);
+  return { targetFront, threadClient };
 }
 
 // Attach to |client|'s tab whose title is |title|, attach to the tab's
 // thread, and then resume it. Pass |callback| the thread's response to
 // the 'resume' packet, a TargetFront for the tab, and a ThreadClient for the
 // thread.
-function attachTestTabAndResume(client, title, callback = () => {}) {
-  return new Promise((resolve) => {
-    attachTestThread(client, title, function(response, targetFront, threadClient) {
-      threadClient.resume(function(response) {
-        Assert.equal(response.type, "resumed");
-        callback(response, targetFront, threadClient);
-        resolve([response, targetFront, threadClient]);
-      });
-    });
-  });
+async function attachTestTabAndResume(client, title, callback = () => {}) {
+  const { targetFront, threadClient } = await attachTestThread(client, title);
+  const response = await threadClient.resume();
+  Assert.equal(response.type, "resumed");
+  callback(response, targetFront, threadClient);
+  return { targetFront, threadClient };
 }
 
 /**
  * Initialize the testing debugger server.
  */
 function initTestDebuggerServer(server = DebuggerServer) {
   if (server === WorkerDebuggerServer) {
     const { createRootActor } = worker.require("xpcshell-test/testactors");
@@ -935,17 +926,17 @@ function threadClientTest(test, options 
     debuggee.__name = scriptName;
     server.addTestGlobal(debuggee);
 
     const client = new DebuggerClient(server.connectPipe());
     await client.connect();
 
     // Attach to the fake tab target and retrieve the ThreadClient instance.
     // Automatically resume as the thread is paused by default after attach.
-    const [, targetFront, threadClient] =
+    const { targetFront, threadClient } =
       await attachTestTabAndResume(client, scriptName);
 
     // Run the test function
     await test({ threadClient, debuggee, client, targetFront });
 
     // Cleanup the client after the test ran
     await client.close();