gfe-relnote: Add a quic::test::ServerThread::WaitUntil method and use it in quic end_to_end_test. Test only.

This is another attempt to deflake BadPacketHeaderFlags.

PiperOrigin-RevId: 293642314
Change-Id: I1809b8b56c50df7ef2de60d73ac30e9a4e8b35df
diff --git a/quic/test_tools/server_thread.cc b/quic/test_tools/server_thread.cc
index da412dd..fa7e822 100644
--- a/quic/test_tools/server_thread.cc
+++ b/quic/test_tools/server_thread.cc
@@ -15,6 +15,7 @@
 ServerThread::ServerThread(QuicServer* server, const QuicSocketAddress& address)
     : QuicThread("server_thread"),
       server_(server),
+      clock_(server->epoll_server()),
       address_(address),
       port_(0),
       initialized_(false) {}
@@ -68,6 +69,24 @@
   confirmed_.WaitForNotification();
 }
 
+bool ServerThread::WaitUntil(std::function<bool()> termination_predicate,
+                             QuicTime::Delta timeout) {
+  const QuicTime deadline = clock_.Now() + timeout;
+  while (clock_.Now() < deadline) {
+    QuicNotification done_checking;
+    bool should_terminate = false;
+    Schedule([&] {
+      should_terminate = termination_predicate();
+      done_checking.Notify();
+    });
+    done_checking.WaitForNotification();
+    if (should_terminate) {
+      return true;
+    }
+  }
+  return false;
+}
+
 void ServerThread::Pause() {
   DCHECK(!pause_.HasBeenNotified());
   pause_.Notify();
diff --git a/quic/test_tools/server_thread.h b/quic/test_tools/server_thread.h
index d430297..eb47496 100644
--- a/quic/test_tools/server_thread.h
+++ b/quic/test_tools/server_thread.h
@@ -39,6 +39,13 @@
   // Waits for the handshake to be confirmed for the first session created.
   void WaitForCryptoHandshakeConfirmed();
 
+  // Wait until |termination_predicate| returns true in server thread, or
+  // reached |timeout|. Must be called from an external thread.
+  // Return whether the function returned after |termination_predicate| become
+  // true.
+  bool WaitUntil(std::function<bool()> termination_predicate,
+                 QuicTime::Delta timeout);
+
   // Pauses execution of the server until Resume() is called.  May only be
   // called once.
   void Pause();
@@ -71,6 +78,7 @@
   QuicNotification quit_;    // Notified when the server should quit.
 
   std::unique_ptr<QuicServer> server_;
+  QuicEpollClock clock_;
   QuicSocketAddress address_;
   mutable QuicMutex port_lock_;
   int port_ QUIC_GUARDED_BY(port_lock_);