adb: guarantee that fdevent_run_on_main_thread happens last.

Make it so that we handle run_on_main_thread calls after regular socket
events, so that we can use it as a way to ensure we've processed all
pending socket events.

Test: adb_test
Test: wine adb_test.exe
Change-Id: Ic215c7fed19a8e1699e759970658b3775aa08c45
diff --git a/adb/fdevent_test.h b/adb/fdevent_test.h
index 1a2d41c..15ded21 100644
--- a/adb/fdevent_test.h
+++ b/adb/fdevent_test.h
@@ -21,6 +21,24 @@
 #include "socket.h"
 #include "sysdeps.h"
 
+static void WaitForFdeventLoop() {
+    // Sleep for a bit to make sure that network events have propagated.
+    std::this_thread::sleep_for(100ms);
+
+    // fdevent_run_on_main_thread has a guaranteed ordering, and is guaranteed to happen after
+    // socket events, so as soon as our function is called, we know that we've processed all
+    // previous events.
+    std::mutex mutex;
+    std::condition_variable cv;
+    std::unique_lock<std::mutex> lock(mutex);
+    fdevent_run_on_main_thread([&]() {
+        mutex.lock();
+        mutex.unlock();
+        cv.notify_one();
+    });
+    cv.wait(lock);
+}
+
 class FdeventTest : public ::testing::Test {
   protected:
     int dummy = -1;