Ignore duplicate events from mDNS stack in CTS

It is too late to fix the duplicate events, and they may have existed
in JB-MR2 as well. So fixing CTS to ignore them for now.

Bug: 11049532
Change-Id: I0785a32dbac04eacb6994b428b12ce1ec27945d2
diff --git a/tests/tests/net/src/android/net/wifi/cts/NsdManagerTest.java b/tests/tests/net/src/android/net/wifi/cts/NsdManagerTest.java
index 482a4e3..d1e4c44 100644
--- a/tests/tests/net/src/android/net/wifi/cts/NsdManagerTest.java
+++ b/tests/tests/net/src/android/net/wifi/cts/NsdManagerTest.java
@@ -43,6 +43,12 @@
     NsdManager.ResolveListener mResolveListener;
 
     public NsdManagerTest() {
+        initRegistrationListener();
+        initDiscoveryListener();
+        initResolveListener();
+    }
+
+    private void initRegistrationListener() {
         mRegistrationListener = new NsdManager.RegistrationListener() {
             @Override
             public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
@@ -64,6 +70,9 @@
                 setEvent("onServiceUnregistered", serviceInfo);
             }
         };
+    }
+
+    private void initDiscoveryListener() {
         mDiscoveryListener = new NsdManager.DiscoveryListener() {
             @Override
             public void onStartDiscoveryFailed(String serviceType, int errorCode) {
@@ -99,6 +108,9 @@
                 setEvent("onServiceLost", serviceInfo);
             }
         };
+    }
+
+    private void initResolveListener() {
         mResolveListener = new NsdManager.ResolveListener() {
             @Override
             public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
@@ -112,6 +124,8 @@
         };
     }
 
+
+
     private final class EventData {
         EventData(String callbackName, NsdServiceInfo info) {
             mCallbackName = callbackName;
@@ -198,6 +212,29 @@
         }
     }
 
+    private EventData waitForNewEvents() throws InterruptedException {
+        if (DBG) Log.d(TAG, "Waiting for a bit, id=" + String.valueOf(mWaitId));
+
+        long startTime = android.os.SystemClock.uptimeMillis();
+        long elapsedTime = 0;
+        synchronized (mEventCache) {
+            int index = mEventCache.size();
+            while (elapsedTime < TIMEOUT ) {
+                // first check if we've received that event
+                for (; index < mEventCache.size(); index++) {
+                    EventData e = mEventCache.get(index);
+                    return e;
+                }
+
+                // Not yet received, just wait
+                mEventCache.wait(TIMEOUT - elapsedTime);
+                elapsedTime = android.os.SystemClock.uptimeMillis() - startTime;
+            }
+        }
+
+        return null;
+    }
+
     private String mServiceName;
 
     @Override
@@ -266,7 +303,9 @@
         assertTrue(lastEvent.mSucceeded);
 
         // Remove this event, so accounting becomes easier later
-        mEventCache.remove(lastEvent);
+        synchronized (mEventCache) {
+            mEventCache.remove(lastEvent);
+        }
 
         // Expect a service record to be discovered (and filter the ones
         // that are unrelated to this test)
@@ -292,7 +331,9 @@
 
             // Remove this event from the event cache, so it won't be found by subsequent
             // calls to waitForCallback
-            mEventCache.remove(lastEvent);
+            synchronized (mEventCache) {
+                mEventCache.remove(lastEvent);
+            }
         }
 
         assertTrue(found);
@@ -315,6 +356,7 @@
         assertTrue(lastEvent.mInfo.getPort() == localPort);
         assertTrue(eventCacheSize() == 1);
 
+        assertTrue(checkForAdditionalEvents());
         clearEventCache();
 
         // Unregister the service
@@ -333,12 +375,17 @@
         assertTrue(eventCacheSize() == 2);
 
         // Register service again to see if we discover it
+        checkForAdditionalEvents();
         clearEventCache();
 
         si = new NsdServiceInfo();
         si.setServiceType(SERVICE_TYPE);
         si.setServiceName(mServiceName);
         si.setPort(localPort);
+
+        // Create a new registration listener and register same service again
+        initRegistrationListener();
+
         mNsdManager.registerService(si, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
 
         lastEvent = waitForCallback("onServiceRegistered");                 // id = 7
@@ -358,67 +405,55 @@
                 lastEvent.mInfo.getServiceName());
 
         assertTrue(lastEvent.mInfo.getServiceName().equals(registeredName));
+        assertTrue(checkCacheSize(2));
 
-        assertTrue(eventCacheSize() == 2);
+        checkForAdditionalEvents();
         clearEventCache();
 
         mNsdManager.stopServiceDiscovery(mDiscoveryListener);
         lastEvent = waitForCallback("onDiscoveryStopped");                  // id = 9
         assertTrue(lastEvent != null);
         assertTrue(lastEvent.mSucceeded);
-        assertTrue(eventCacheSize() == 1);
+        assertTrue(checkCacheSize(1));
 
+        checkForAdditionalEvents();
         clearEventCache();
+
         mNsdManager.unregisterService(mRegistrationListener);
 
         lastEvent =  waitForCallback("onServiceUnregistered");              // id = 10
         assertTrue(lastEvent != null);
         assertTrue(lastEvent.mSucceeded);
-        assertTrue(eventCacheSize() == 1);
+        assertTrue(checkCacheSize(1));
+    }
+
+    boolean checkCacheSize(int size) {
+        synchronized (mEventCache) {
+            int cacheSize = mEventCache.size();
+            if (cacheSize != size) {
+                Log.d(TAG, "id = " + mWaitId + ": event cache size = " + cacheSize);
+                for (int i = 0; i < cacheSize; i++) {
+                    EventData e = mEventCache.get(i);
+                    String sname = (e.mInfo != null) ? "(" + e.mInfo.getServiceName() + ")" : "";
+                    Log.d(TAG, "eventName is " + e.mCallbackName + sname);
+                }
+            }
+            return (cacheSize == size);
+        }
+    }
+
+    boolean checkForAdditionalEvents() {
+        try {
+            EventData e = waitForNewEvents();
+            if (e != null) {
+                String sname = (e.mInfo != null) ? "(" + e.mInfo.getServiceName() + ")" : "";
+                Log.d(TAG, "ignoring unexpected event " + e.mCallbackName + sname);
+            }
+            return (e == null);
+        }
+        catch (InterruptedException ex) {
+            return false;
+        }
     }
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-