Bug 1509315 - Refactor console attachConsole test helpers to async. r=yulia
authorAlexandre Poirot <poirot.alex@gmail.com>
Tue, 27 Nov 2018 21:48:05 +0000
changeset 504828 30bc5b0c60ebfe0444530fa9af86c5c8d5498ecd
parent 504827 3eadd3d0580012f95a9d8e8ac24befb60d93b3fd
child 504829 5e887c4ebd791fc491977bdc0ee8fc787f46b33a
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersyulia
bugs1509315
milestone65.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 1509315 - Refactor console attachConsole test helpers to async. r=yulia MozReview-Commit-ID: 2jIm73AiIPN Depends on D12733 Differential Revision: https://phabricator.services.mozilla.com/D12734
devtools/shared/webconsole/test/common.js
--- a/devtools/shared/webconsole/test/common.js
+++ b/devtools/shared/webconsole/test/common.js
@@ -21,110 +21,101 @@ function initCommon() {
 }
 
 function initDebuggerServer() {
   DebuggerServer.init();
   DebuggerServer.registerAllActors();
   DebuggerServer.allowChromeProcess = true;
 }
 
-function connectToDebugger() {
+async function connectToDebugger() {
   initCommon();
   initDebuggerServer();
 
   const transport = DebuggerServer.connectPipe();
   const client = new DebuggerClient(transport);
 
-  const dbgState = { dbgClient: client };
-  return new Promise(resolve => {
-    client.connect().then(response => resolve([dbgState, response]));
-  });
+  await client.connect();
+  return client;
 }
 
-function attachConsole(listeners, callback) {
-  _attachConsole(listeners, callback);
+async function attachConsole(listeners, callback) {
+  const { state, response } = await _attachConsole(listeners);
+  callback(state, response);
 }
-function attachConsoleToTab(listeners, callback) {
-  _attachConsole(listeners, callback, true);
+async function attachConsoleToTab(listeners, callback) {
+  const { state, response } = await _attachConsole(listeners, true);
+  callback(state, response);
 }
-function attachConsoleToWorker(listeners, callback) {
-  _attachConsole(listeners, callback, true, true);
+async function attachConsoleToWorker(listeners, callback) {
+  const { state, response } = await _attachConsole(listeners, true, true);
+  callback(state, response);
 }
 
 var _attachConsole = async function(
-  listeners, callback, attachToTab, attachToWorker
+  listeners, attachToTab, attachToWorker
 ) {
-  function _onAttachConsole(state, [response, webConsoleClient]) {
-    state.client = webConsoleClient;
+  try {
+    const client = await connectToDebugger();
 
-    callback(state, response);
-  }
-  function _onAttachError(state, response) {
-    console.error("attachConsole failed: " + response.error + " " +
-                  response.message);
-    callback(state, response);
-  }
+    function waitForMessage(target) {
+      return new Promise(resolve => {
+        target.addEventListener("message", resolve, { once: true });
+      });
+    }
 
-  function waitForMessage(target) {
-    return new Promise(resolve => {
-      target.addEventListener("message", resolve, { once: true });
-    });
-  }
-
-  let [state, response] = await connectToDebugger();
-  if (response.error) {
-    console.error("client.connect() failed: " + response.error + " " +
-                  response.message);
-    callback(state, response);
-    return;
-  }
+    // Fetch the console actor out of the expected target
+    // ParentProcessTarget / WorkerTarget / FrameTarget
+    let consoleActor, worker;
+    if (!attachToTab) {
+      const front = await client.mainRoot.getMainProcess();
+      await front.attach();
+      consoleActor = front.targetForm.consoleActor;
+    } else {
+      const response = await client.listTabs();
+      const tab = response.tabs[response.selected];
+      const [, targetFront] = await client.attachTarget(tab.actor);
+      if (attachToWorker) {
+        const workerName = "console-test-worker.js#" + new Date().getTime();
+        worker = new Worker(workerName);
+        await waitForMessage(worker);
 
-  if (!attachToTab) {
-    const front = await state.dbgClient.mainRoot.getMainProcess();
-    await front.attach();
-    const consoleActor = front.targetForm.consoleActor;
-    state.actor = consoleActor;
-    state.dbgClient.attachConsole(consoleActor, listeners)
-      .then(_onAttachConsole.bind(null, state), _onAttachError.bind(null, state));
-    return;
-  }
-  response = await state.dbgClient.listTabs();
-  if (response.error) {
-    console.error("listTabs failed: " + response.error + " " +
-                  response.message);
-    callback(state, response);
-    return;
+        const { workers } = await targetFront.listWorkers();
+        const workerTargetFront = workers.filter(w => w.url == workerName)[0];
+        if (!workerTargetFront) {
+          console.error("listWorkers failed. Unable to find the worker actor\n");
+          return null;
+        }
+        await workerTargetFront.attach();
+        await workerTargetFront.attachThread({});
+        consoleActor = workerTargetFront.targetForm.consoleActor;
+      } else {
+        consoleActor = tab.consoleActor;
+      }
+    }
+
+    // Instantiate the WebConsoleClient
+    const [ response, webConsoleClient ] = await client.attachConsole(consoleActor,
+      listeners);
+    return {
+      state: {
+        dbgClient: client,
+        client: webConsoleClient,
+        actor: consoleActor,
+        // Keep a strong reference to the Worker to avoid it being
+        // GCd during the test (bug 1237492).
+        // eslint-disable-next-line camelcase
+        _worker_ref: worker,
+      },
+      response,
+    };
+  } catch (error) {
+    console.error(`attachConsole failed: ${error.error} ${error.message}`);
   }
-  const tab = response.tabs[response.selected];
-  const [, targetFront] = await state.dbgClient.attachTarget(tab.actor);
-  if (attachToWorker) {
-    const workerName = "console-test-worker.js#" + new Date().getTime();
-    const worker = new Worker(workerName);
-    // Keep a strong reference to the Worker to avoid it being
-    // GCd during the test (bug 1237492).
-    // eslint-disable-next-line camelcase
-    state._worker_ref = worker;
-    await waitForMessage(worker);
-
-    const { workers } = await targetFront.listWorkers();
-    const workerTargetFront = workers.filter(w => w.url == workerName)[0];
-    if (!workerTargetFront) {
-      console.error("listWorkers failed. Unable to find the worker actor\n");
-      return;
-    }
-    await workerTargetFront.attach();
-    await workerTargetFront.attachThread({});
-    state.actor = workerTargetFront.targetForm.consoleActor;
-    state.dbgClient.attachConsole(workerTargetFront.targetForm.consoleActor, listeners)
-      .then(_onAttachConsole.bind(null, state), _onAttachError.bind(null, state));
-  } else {
-    state.actor = tab.consoleActor;
-    state.dbgClient.attachConsole(tab.consoleActor, listeners)
-      .then(_onAttachConsole.bind(null, state), _onAttachError.bind(null, state));
-  }
+  return null;
 };
 
 function closeDebugger(state, callback) {
   const onClose = state.dbgClient.close();
 
   state.dbgClient = null;
   state.client = null;