Bug 1469054 - Make sure the adb server stops. r?jdescottes draft
authorHiroyuki Ikezoe <hikezoe@mozilla.com>
Thu, 09 Aug 2018 14:27:55 +0900
changeset 827775 91699dcd898d6618bb4c7deb59bec9e7820043f9
parent 827774 17955c7c342140cef7b0bfe1b570664fd58a03f6
child 827776 fc048c7724b2aabad3de40a131c5efbb65046358
push id118583
push userhikezoe@mozilla.com
push dateThu, 09 Aug 2018 06:03:03 +0000
reviewersjdescottes
bugs1469054
milestone63.0a1
Bug 1469054 - Make sure the adb server stops. r?jdescottes Without this change, the test in this patch does fail since when the test starts, the adb server is still running for the previous test, thus ADB.start() doesn't start a new adb server, then after that when we check that the adb server is running by adb-running-checker, it tells us the adb server is NOT running at that moment. MozReview-Commit-ID: KRo30WmAsAY
devtools/shared/adb/adb.js
devtools/shared/adb/test/test_adb.js
--- a/devtools/shared/adb/adb.js
+++ b/devtools/shared/adb/adb.js
@@ -9,16 +9,18 @@
 const { Cc, Ci } = require("chrome");
 const EventEmitter = require("devtools/shared/event-emitter");
 const client = require("./adb-client");
 const { getFileForBinary } = require("./adb-binary");
 const { setTimeout } = require("resource://gre/modules/Timer.jsm");
 const { PromiseUtils } = require("resource://gre/modules/PromiseUtils.jsm");
 const { OS } = require("resource://gre/modules/osfile.jsm");
 const { Services } = require("resource://gre/modules/Services.jsm");
+loader.lazyRequireGetter(this, "check",
+                         "devtools/shared/adb/adb-running-checker", true);
 
 let ready = false;
 let didRunInitially = false;
 
 const OKAY = 0x59414b4f;
 // const FAIL = 0x4c494146;
 // const STAT = 0x54415453;
 const DATA = 0x41544144;
@@ -52,17 +54,17 @@ const ADB = {
   start() {
     return new Promise(async (resolve, reject) => {
       let onSuccessfulStart = () => {
         Services.obs.notifyObservers(null, "adb-ready");
         this.ready = true;
         resolve();
       };
 
-      let isAdbRunning = await require("./adb-running-checker").check();
+      let isAdbRunning = await check();
       if (isAdbRunning) {
         this.didRunInitially = false;
         console.log("Found ADB process running, not restarting");
         onSuccessfulStart();
         return;
       }
       console.log("Didn't find ADB process running, restarting");
 
@@ -106,16 +108,27 @@ const ADB = {
    *        In case, we do need to kill the server, this param is passed through
    *        to kill to determine whether it's a sync operation.
    */
   async stop(sync) {
     if (!this.didRunInitially) {
       return; // We didn't start the server, nothing to do
     }
     await this.kill(sync);
+    // Make sure the ADB server stops listening.
+    //
+    // kill() above doesn't mean that the ADB server stops, it just means that
+    // 'adb kill-server' command finished, so that it's possible that the ADB
+    // server is still alive there.
+    while (true) {
+      const isAdbRunning = await check();
+      if (!isAdbRunning) {
+        break;
+      }
+    }
   },
 
   /**
    * Kill the ADB server.  We do this by running ADB again, passing it
    * the "kill-server" argument.
    *
    * @param {Boolean} sync
    *        Whether or not to kill the server synchronously.  In general,
--- a/devtools/shared/adb/test/test_adb.js
+++ b/devtools/shared/adb/test/test_adb.js
@@ -175,8 +175,49 @@ add_task(async function testStartAndStop
   ok(await check(), "adb is now running");
 
   await ADB.stop(true /* sync */);
   ok(!ADB.ready);
 
   await extension.unload();
 });
 
+add_task(async function testTrackDevices() {
+  const extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      version: "1.0",
+      applications: {
+        gecko: { id: "adb@mozilla.org" }
+      }
+    },
+    files: {
+      "adb.json": JSON.stringify(ADB_JSON),
+      "linux/adb": adbMock,
+      "linux64/adb": adbMock,
+      "mac64/adb": adbMock,
+      "win32/adb.exe": adbMock,
+      "win32/AdbWinApi.dll": "dummy",
+      "win32/AdbWinUsbApi.dll": "dummy"
+    },
+  });
+
+  await extension.startup();
+
+  await ADB.start();
+  ok(ADB.ready);
+
+  ok(await check(), "adb is now running");
+
+  const receivedDeviceId = await new Promise(resolve => {
+    EventEmitter.on(ADB, "device-connected", deviceId => {
+      resolve(deviceId);
+    });
+    ADB.trackDevices();
+  });
+
+  equal(receivedDeviceId, "1234567890");
+
+  await ADB.stop(true /* sync */);
+  ok(!ADB.ready);
+
+  await extension.unload();
+});
+