Attempt to de-flake testSyncState.

(Cherry-pick from R CTS. Original CL decscription follows below.)
Bug: 170926877

- Don't assert on time differences between states. We only care about
the ordering of the states, the time difference between states is beyond
our control.

- Remove lock notifyAll() without a corresponding wait(). Also
synchronize on same lock for creation of SyncAdapter's CountDownLatch as
CountDownLoatch.countDown().

Fixes: 156691456
Test: atest --iterations=100 CtsStatsdHostTestCases:android.cts.statsd.atom.UidAtomTests#testSyncState
Change-Id: Ib5047c03ff8b8f3790e29682dadfc6b8146d9117
Merged-In: Ib5047c03ff8b8f3790e29682dadfc6b8146d9117
(cherry picked from commit 93d82cd81dbd119617c4328b3954552f35f4cce4)
diff --git a/hostsidetests/statsd/apps/statsdapp/src/com/android/server/cts/device/statsd/StatsdSyncAdapter.java b/hostsidetests/statsd/apps/statsdapp/src/com/android/server/cts/device/statsd/StatsdSyncAdapter.java
index 12f110b..6968307 100644
--- a/hostsidetests/statsd/apps/statsdapp/src/com/android/server/cts/device/statsd/StatsdSyncAdapter.java
+++ b/hostsidetests/statsd/apps/statsdapp/src/com/android/server/cts/device/statsd/StatsdSyncAdapter.java
@@ -58,7 +58,6 @@
         }
         synchronized (sLock) {
             Log.i(TAG, "onPerformSync");
-            sLock.notifyAll();
             if (sLatch != null) {
                 sLatch.countDown();
             } else {
@@ -79,8 +78,12 @@
         ContentResolver.requestSync(account, StatsdProvider.AUTHORITY, extras);
     }
 
-    public static synchronized CountDownLatch resetCountDownLatch() {
-        sLatch = new CountDownLatch(1);
+    public static CountDownLatch resetCountDownLatch() {
+        synchronized (sLock) {
+            if (sLatch == null || sLatch.getCount() == 0) {
+                sLatch = new CountDownLatch(1);
+            }
+        }
         return sLatch;
     }
 }
diff --git a/hostsidetests/statsd/src/android/cts/statsd/atom/UidAtomTests.java b/hostsidetests/statsd/src/android/cts/statsd/atom/UidAtomTests.java
index 584a834..2243abc 100644
--- a/hostsidetests/statsd/src/android/cts/statsd/atom/UidAtomTests.java
+++ b/hostsidetests/statsd/src/android/cts/statsd/atom/UidAtomTests.java
@@ -505,8 +505,9 @@
         List<EventMetricData> data = getEventMetricDataList();
 
         // Assert that the events happened in the expected order.
-        assertStatesOccurred(stateSet, data, WAIT_TIME_SHORT,
-                atom -> atom.getSyncStateChanged().getState().getNumber());
+        assertStatesOccurred(stateSet, data,
+            /* wait = */ 0 /* don't verify time differences between state changes */,
+            atom -> atom.getSyncStateChanged().getState().getNumber());
     }
 
     public void testWakelockState() throws Exception {