OSDN Git Service

ConnectivityServiceTest: some fixes in CallbackInfo
authorHugo Benichi <hugobenichi@google.com>
Tue, 20 Dec 2016 08:05:06 +0000 (17:05 +0900)
committerHugo Benichi <hugobenichi@google.com>
Fri, 20 Jan 2017 07:56:41 +0000 (16:56 +0900)
- CallbackInfo becomes a static class so that it can be instantiated
  and looked at more easily than before.
- CallbackInfo gains hashCode() because it has equals().
- effectively final field are qualified final
- fixes bug when polling the next callback: poll() on a LinkedBlockingQueue
  returns null when timing out, which was not failing the test cleanly.

Test: ConnectivityServiceTest passes
Bug: 32561414

(cherry picked from commit 4e1619f034b256984ea85b4d89aa2e16ac6f795c)

Change-Id: Ica5f1609975e4b256f4e2f3dc2ac84362e762da2

tests/net/java/com/android/server/ConnectivityServiceTest.java

index d62c30d..46b6403 100644 (file)
@@ -1103,6 +1103,29 @@ public class ConnectivityServiceTest extends AndroidTestCase {
         UNAVAILABLE
     }
 
+    private static class CallbackInfo {
+        public final CallbackState state;
+        public final Network network;
+        public final Object arg;
+        public CallbackInfo(CallbackState s, Network n, Object o) {
+            state = s; network = n; arg = o;
+        }
+        public String toString() {
+            return String.format("%s (%s)", state, network);
+        }
+        @Override
+        public boolean equals(Object o) {
+            if (!(o instanceof CallbackInfo)) return false;
+            // Ignore timeMs, since it's unpredictable.
+            CallbackInfo other = (CallbackInfo) o;
+            return (state == other.state) && Objects.equals(network, other.network);
+        }
+        @Override
+        public int hashCode() {
+            return Objects.hash(state, network);
+        }
+    }
+
     /**
      * Utility NetworkCallback for testing. The caller must explicitly test for all the callbacks
      * this class receives, by calling expectCallback() exactly once each time a callback is
@@ -1114,21 +1137,6 @@ public class ConnectivityServiceTest extends AndroidTestCase {
         // the linger timeout.
         private final static int TIMEOUT_MS = 50;
 
-        private class CallbackInfo {
-            public final CallbackState state;
-            public final Network network;
-            public Object arg;
-            public CallbackInfo(CallbackState s, Network n, Object o) {
-                state = s; network = n; arg = o;
-            }
-            public String toString() { return String.format("%s (%s)", state, network); }
-            public boolean equals(Object o) {
-                if (!(o instanceof CallbackInfo)) return false;
-                // Ignore timeMs, since it's unpredictable.
-                CallbackInfo other = (CallbackInfo) o;
-                return state == other.state && Objects.equals(network, other.network);
-            }
-        }
         private final LinkedBlockingQueue<CallbackInfo> mCallbacks = new LinkedBlockingQueue<>();
 
         protected void setLastCallback(CallbackState state, Network network, Object o) {
@@ -1155,17 +1163,24 @@ public class ConnectivityServiceTest extends AndroidTestCase {
             setLastCallback(CallbackState.LOST, network, null);
         }
 
-        void expectCallback(CallbackState state, MockNetworkAgent mockAgent, int timeoutMs) {
-            CallbackInfo expected = new CallbackInfo(
-                    state, (mockAgent != null) ? mockAgent.getNetwork() : null, 0);
-            CallbackInfo actual;
+        CallbackInfo nextCallback(int timeoutMs) {
+            CallbackInfo cb = null;
             try {
-                actual = mCallbacks.poll(timeoutMs, TimeUnit.MILLISECONDS);
-                assertEquals("Unexpected callback:", expected, actual);
+                cb = mCallbacks.poll(timeoutMs, TimeUnit.MILLISECONDS);
             } catch (InterruptedException e) {
-                fail("Did not receive expected " + expected + " after " + TIMEOUT_MS + "ms");
-                actual = null;  // Or the compiler can't tell it's never used uninitialized.
             }
+            if (cb == null) {
+                // LinkedBlockingQueue.poll() returns null if it timeouts.
+                fail("Did not receive callback after " + timeoutMs + "ms");
+            }
+            return cb;
+        }
+
+        void expectCallback(CallbackState state, MockNetworkAgent mockAgent, int timeoutMs) {
+            CallbackInfo expected = new CallbackInfo(
+                    state, (mockAgent != null) ? mockAgent.getNetwork() : null, 0);
+            CallbackInfo actual = nextCallback(timeoutMs);
+            assertEquals("Unexpected callback:", expected, actual);
             if (state == CallbackState.LOSING) {
                 String msg = String.format(
                         "Invalid linger time value %d, must be between %d and %d",