suspend: De-flake suspend tests

The events that happen on the sock stream can race with the isBlocked() checks because the autosuspend loop is a separate thread from the test thread. Add some buffer time to avoid flakiness in these tests.

Test: Treehugger
Bug: 255898234
Bug: 265513788
Bug: 266077359
Change-Id: Ie03b343e46dc4035c09cb0a5399b4a5da569f7d4
Merged-In: Ie03b343e46dc4035c09cb0a5399b4a5da569f7d4
diff --git a/suspend/1.0/default/SystemSuspendUnitTest.cpp b/suspend/1.0/default/SystemSuspendUnitTest.cpp
index f92e416..a4d58b5 100644
--- a/suspend/1.0/default/SystemSuspendUnitTest.cpp
+++ b/suspend/1.0/default/SystemSuspendUnitTest.cpp
@@ -198,8 +198,12 @@
     }
 
     virtual void TearDown() override {
+        // Allow some time for the autosuspend loop to happen, if unblocked
+        std::this_thread::sleep_for(100ms);
+
         if (!isReadBlocked(wakeupCountFd)) readFd(wakeupCountFd);
-        if (!isReadBlocked(stateFd)) readFd(stateFd).empty();
+        if (!isReadBlocked(stateFd)) readFd(stateFd);
+
         ASSERT_TRUE(isReadBlocked(wakeupCountFd));
         ASSERT_TRUE(isReadBlocked(stateFd));
     }
@@ -209,7 +213,12 @@
         ASSERT_TRUE(WriteStringToFd(wakeupCount, wakeupCountFd));
     }
 
-    bool isSystemSuspendBlocked(int timeout_ms = 20) { return isReadBlocked(stateFd, timeout_ms); }
+    bool isSystemSuspendBlocked(int timeout_ms = 20) {
+        // Allow some time for the autosuspend loop to happen, if unblocked
+        std::this_thread::sleep_for(100ms);
+
+        return isReadBlocked(stateFd, timeout_ms);
+    }
 
     std::shared_ptr<IWakeLock> acquireWakeLock(const std::string& name = "TestLock") {
         std::shared_ptr<IWakeLock> wl = nullptr;
@@ -305,15 +314,22 @@
 // Tests that autosuspend thread can only enabled again after its been disabled.
 TEST_F(SystemSuspendTest, EnableAutosuspendAfterDisableAutosuspend) {
     bool enabled = false;
-    unblockSystemSuspendFromWakeupCount();
-    systemSuspend->disableAutosuspend();
+
+    checkLoop(1);
     controlServiceInternal->enableAutosuspend(new BBinder(), &enabled);
-    ASSERT_EQ(enabled, true);
+    ASSERT_FALSE(enabled);
+
+    systemSuspend->disableAutosuspend();
+    unblockSystemSuspendFromWakeupCount();
+
+    controlServiceInternal->enableAutosuspend(new BBinder(), &enabled);
+    ASSERT_TRUE(enabled);
 }
 
 TEST_F(SystemSuspendTest, DisableAutosuspendBlocksSuspend) {
     checkLoop(1);
     systemSuspend->disableAutosuspend();
+    unblockSystemSuspendFromWakeupCount();
     ASSERT_TRUE(isSystemSuspendBlocked());
 
     // Re-enable autosuspend
@@ -425,10 +441,10 @@
         std::shared_ptr<IWakeLock> wl1 = acquireWakeLock();
         ASSERT_NE(wl1, nullptr);
         ASSERT_TRUE(isSystemSuspendBlocked());
-        unblockSystemSuspendFromWakeupCount();
         {
             std::shared_ptr<IWakeLock> wl2 = acquireWakeLock();
             ASSERT_NE(wl2, nullptr);
+            unblockSystemSuspendFromWakeupCount();
             ASSERT_TRUE(isSystemSuspendBlocked());
         }
         ASSERT_TRUE(isSystemSuspendBlocked());