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();