Attempt to fix flaky tests and scripts.

Change-Id: Idcf9b85c72052924f990795c321606f9ae408ada
diff --git a/src/com/android/tradefed/device/DeviceManager.java b/src/com/android/tradefed/device/DeviceManager.java
index 08cf222..e38db7c 100644
--- a/src/com/android/tradefed/device/DeviceManager.java
+++ b/src/com/android/tradefed/device/DeviceManager.java
@@ -83,6 +83,8 @@
     /** the maximum number of no device runs that can be allocated at one time */
     private int mNumNullDevicesSupported = 1;
 
+    private boolean mSynchronousMode = false;
+
     /**
      * Package-private constructor, should only be used by this class and its associated unit test.
      * Use {@link #getInstance()} instead.
@@ -141,6 +143,17 @@
         addNullDevices();
     }
 
+    /**
+     * Instruct DeviceManager whether to use background threads or not.
+     * <p/>
+     * Exposed to make unit tests more deterministic.
+     *
+     * @param syncMode
+     */
+    void setSynchronousMode(boolean syncMode) {
+        mSynchronousMode = syncMode;
+    }
+
     private void checkInit() {
         if (!mIsInitialized) {
             throw new IllegalStateException("DeviceManager has not been initialized");
@@ -209,7 +222,7 @@
         mCheckDeviceMap.put(device.getSerialNumber(), monitor);
 
         final String threadName = String.format("Check device %s", device.getSerialNumber());
-        Thread checkThread = new Thread(threadName) {
+        Runnable checkRunnable = new Runnable() {
             @Override
             public void run() {
                 Log.d(LOG_TAG, String.format("checking new device %s responsiveness",
@@ -225,7 +238,11 @@
                 mCheckDeviceMap.remove(device.getSerialNumber());
             }
         };
-        checkThread.start();
+        if (mSynchronousMode ) {
+            checkRunnable.run();
+        } else {
+            new Thread(checkRunnable, threadName).start();
+        }
     }
 
     /**
diff --git a/tests/run_tradefed_func_tests.sh b/tests/run_tradefed_func_tests.sh
index fb22a6c..ce058d3 100755
--- a/tests/run_tradefed_func_tests.sh
+++ b/tests/run_tradefed_func_tests.sh
@@ -17,4 +17,4 @@
 # A simpler helper script that runs the Trade Federation functional tests
 
 # run the functional test suite that contains all the functional tests
-tradefed.sh run singleCommand --class com.android.tradefed.FuncTests "$@" host
+tradefed.sh run singleCommand host --class com.android.tradefed.FuncTests "$@"
diff --git a/tests/run_tradefed_tests.sh b/tests/run_tradefed_tests.sh
index 90eb77e..bf7a86f 100755
--- a/tests/run_tradefed_tests.sh
+++ b/tests/run_tradefed_tests.sh
@@ -19,5 +19,5 @@
 
 # A simpler helper script that runs the Trade Federation unit tests
 
-tradefed.sh run singleCommand -n --class  com.android.tradefed.UnitTests "$@" host
+tradefed.sh run singleCommand host -n --class  com.android.tradefed.UnitTests "$@"
 
diff --git a/tests/src/com/android/tradefed/device/DeviceManagerTest.java b/tests/src/com/android/tradefed/device/DeviceManagerTest.java
index 643ec87..9443a33 100644
--- a/tests/src/com/android/tradefed/device/DeviceManagerTest.java
+++ b/tests/src/com/android/tradefed/device/DeviceManagerTest.java
@@ -202,6 +202,7 @@
             }
         };
         mgr.setEnableLogcat(false);
+        mgr.setSynchronousMode(true);
         return mgr;
     }
 
@@ -609,10 +610,6 @@
 
         replayMocks();
         DeviceManager manager = createDeviceManager(mMockIDevice);
-        // allocate and free device to avoid race condition with waitForDeviceAvailable being
-        // called on background thread
-        assertEquals(mMockTestDevice, manager.allocateDevice());
-        manager.freeDevice(mMockTestDevice, FreeDeviceState.AVAILABLE);
         mDeviceListener.deviceChanged(mMockIDevice, IDevice.CHANGE_STATE);
         // verify device can still be allocated even though its in offline state
         // this is desired because then recovery can attempt to resurrect the device