Merge "Revert "ConnectivityServiceTest: eliminate remaining sleep()""
diff --git a/tests/net/java/com/android/server/ConnectivityServiceTest.java b/tests/net/java/com/android/server/ConnectivityServiceTest.java
index 158a7ce..09b9823 100644
--- a/tests/net/java/com/android/server/ConnectivityServiceTest.java
+++ b/tests/net/java/com/android/server/ConnectivityServiceTest.java
@@ -2803,17 +2803,19 @@
         NetworkRequest nr = new NetworkRequest.Builder().addTransportType(
                 NetworkCapabilities.TRANSPORT_WIFI).build();
         final TestNetworkCallback networkCallback = new TestNetworkCallback();
-        final int requestTimeoutMs = 50;
+        final int requestTimeoutMs = 100;
         mCm.requestNetwork(nr, networkCallback, requestTimeoutMs);
 
         mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
         mWiFiNetworkAgent.connect(false);
-        final int assertTimeoutMs = 100;
+        final int assertTimeoutMs = 150;
         networkCallback.expectAvailableCallbacks(mWiFiNetworkAgent, false, assertTimeoutMs);
+        sleepFor(20);
         mWiFiNetworkAgent.disconnect();
         networkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
 
-        // Validate that UNAVAILABLE is not called
+        // pass timeout and validate that UNAVAILABLE is not called
+        sleepFor(100);
         networkCallback.assertNoCallback();
     }
 
@@ -2840,20 +2842,24 @@
     }
 
     /**
-     * Validate that when a network request is unregistered (cancelled), no posterior event can
-     * trigger the callback.
+     * Validate that when a network request is unregistered (cancelled) the time-out for that
+     * request doesn't trigger the onUnavailable() callback.
      */
     @SmallTest
-    public void testNoCallbackAfterUnregisteredNetworkRequest() {
+    public void testTimedoutAfterUnregisteredNetworkRequest() {
         NetworkRequest nr = new NetworkRequest.Builder().addTransportType(
                 NetworkCapabilities.TRANSPORT_WIFI).build();
         final TestNetworkCallback networkCallback = new TestNetworkCallback();
         final int timeoutMs = 10;
-
         mCm.requestNetwork(nr, networkCallback, timeoutMs);
+
+        // remove request
         mCm.unregisterNetworkCallback(networkCallback);
-        // Regardless of the timeout, unregistering the callback in ConnectivityManager ensures
-        // that this callback will not be called.
+
+        // pass timeout and validate that no callbacks
+        // Note: doesn't validate that nothing called from CS since even if called the CM already
+        // unregisters the callback and won't pass it through!
+        sleepFor(15);
         networkCallback.assertNoCallback();
 
         // create a network satisfying request - validate that request not triggered
@@ -3355,4 +3361,13 @@
         assertEquals(String.format("expected array of length %s, but length was %s for %s",
                 expected, length, Arrays.toString(got)), expected, length);
     }
+
+    /* test utilities */
+    // TODO: eliminate all usages of sleepFor and replace by proper timeouts/waitForIdle.
+    static private void sleepFor(int ms) {
+        try {
+            Thread.sleep(ms);
+        } catch (InterruptedException e) {
+        }
+    }
 }