Switch QuicTestClient to use QuicDefaultClient instead of QuicClient

PiperOrigin-RevId: 465535182
diff --git a/quiche/quic/core/http/end_to_end_test.cc b/quiche/quic/core/http/end_to_end_test.cc
index 0e2c867..50fe6a3 100644
--- a/quiche/quic/core/http/end_to_end_test.cc
+++ b/quiche/quic/core/http/end_to_end_test.cc
@@ -26,7 +26,7 @@
 #include "quiche/quic/core/quic_connection.h"
 #include "quiche/quic/core/quic_constants.h"
 #include "quiche/quic/core/quic_data_writer.h"
-#include "quiche/quic/core/quic_epoll_connection_helper.h"
+#include "quiche/quic/core/quic_default_clock.h"
 #include "quiche/quic/core/quic_error_codes.h"
 #include "quiche/quic/core/quic_framer.h"
 #include "quiche/quic/core/quic_packet_creator.h"
@@ -35,7 +35,6 @@
 #include "quiche/quic/core/quic_session.h"
 #include "quiche/quic/core/quic_types.h"
 #include "quiche/quic/core/quic_utils.h"
-#include "quiche/quic/platform/api/quic_epoll.h"
 #include "quiche/quic/platform/api/quic_expect_bug.h"
 #include "quiche/quic/platform/api/quic_flags.h"
 #include "quiche/quic/platform/api/quic_logging.h"
@@ -199,15 +198,15 @@
 
 class ClientDelegate : public PacketDroppingTestWriter::Delegate {
  public:
-  explicit ClientDelegate(QuicClient* client) : client_(client) {}
+  explicit ClientDelegate(QuicDefaultClient* client) : client_(client) {}
   ~ClientDelegate() override = default;
   void OnCanWrite() override {
-    QuicEpollEvent event(EPOLLOUT);
-    client_->epoll_network_helper()->OnEvent(client_->GetLatestFD(), &event);
+    client_->default_network_helper()->OnSocketEvent(
+        nullptr, client_->GetLatestFD(), kSocketEventWritable);
   }
 
  private:
-  QuicClient* client_;
+  QuicDefaultClient* client_;
 };
 
 class EndToEndTest : public QuicTestWithParam<TestParams> {
@@ -258,11 +257,12 @@
   }
 
   QuicTestClient* CreateQuicClient(QuicPacketWriterWrapper* writer) {
-    QuicTestClient* client =
-        new QuicTestClient(server_address_, server_hostname_, client_config_,
-                           client_supported_versions_,
-                           crypto_test_utils::ProofVerifierForTesting(),
-                           std::make_unique<QuicClientSessionCache>());
+    QuicTestClient* client = new QuicTestClient(
+        server_address_, server_hostname_, client_config_,
+        client_supported_versions_,
+        crypto_test_utils::ProofVerifierForTesting(),
+        std::make_unique<QuicClientSessionCache>(),
+        GetParam().event_loop->Create(QuicDefaultClock::Get()));
     client->SetUserAgentID(kTestUserAgentId);
     client->UseWriter(writer);
     if (!pre_shared_key_client_.empty()) {
@@ -449,7 +449,6 @@
       ADD_FAILURE() << "Missing MockableQuicClient";
       return false;
     }
-    static QuicEpollEvent event(EPOLLOUT);
     if (client_writer_ != nullptr) {
       QuicConnection* client_connection = GetClientConnection();
       if (client_connection == nullptr) {
@@ -2256,7 +2255,7 @@
   // Regression test for b/14677858.
   // Test that the resume write alarm is not set in QuicConnection::OnCanWrite
   // if currently connection level flow control blocked. If set, this results in
-  // an infinite loop in the EpollServer, as the alarm fires and is immediately
+  // an infinite loop in the EventLoop, as the alarm fires and is immediately
   // rescheduled.
   ASSERT_TRUE(Initialize());
   EXPECT_TRUE(client_->client()->WaitForOneRttKeysAvailable());
@@ -3452,10 +3451,12 @@
 
   // Create a new socket before closing the old one, which will result in a new
   // ephemeral port.
-  QuicClientPeer::CreateUDPSocketAndBind(client_->client());
+  client_->client()->network_helper()->CreateUDPSocketAndBind(
+      client_->client()->server_address(), client_->client()->bind_to_address(),
+      client_->client()->local_port());
 
   // Stop listening and close the old FD.
-  QuicClientPeer::CleanUpUDPSocket(client_->client(), old_fd);
+  client_->client()->default_network_helper()->CleanUpUDPSocket(old_fd);
 
   // The packet writer needs to be updated to use the new FD.
   client_->client()->network_helper()->CreateQuicPacketWriter();
@@ -3467,19 +3468,13 @@
   // FD rather than any more complex NAT rebinding simulation.
   int new_port =
       client_->client()->network_helper()->GetLatestClientAddress().port();
-  QuicClientPeer::SetClientPort(client_->client(), new_port);
+  client_->client()->default_network_helper()->SetClientPort(new_port);
   QuicConnection* client_connection = GetClientConnection();
   ASSERT_TRUE(client_connection);
   QuicConnectionPeer::SetSelfAddress(
       client_connection,
       QuicSocketAddress(client_connection->self_address().host(), new_port));
 
-  // Register the new FD for epoll events.
-  int new_fd = client_->client()->GetLatestFD();
-  QuicEpollServer* eps = client_->epoll_server();
-  eps->RegisterFD(new_fd, client_->client()->epoll_network_helper(),
-                  EPOLLIN | EPOLLOUT | EPOLLET);
-
   // Send a second request, using the new FD.
   SendSynchronousBarRequestAndCheckResponse();
 
@@ -4707,9 +4702,6 @@
   ServerStreamThatDropsBodyFactory stream_factory;
   SetSpdyStreamFactory(&stream_factory);
   ASSERT_TRUE(Initialize());
-  // Set client's epoll server's time out to 0 to make this test be finished
-  // within a short time.
-  client_->epoll_server()->set_timeout_in_us(0);
 
   EXPECT_TRUE(client_->client()->WaitForOneRttKeysAvailable());
   SetPacketLossPercentage(1);
@@ -4760,7 +4752,6 @@
   client->UseWriter(client_writer_);
   client->Connect();
   client_.reset(client);
-  static QuicEpollEvent event(EPOLLOUT);
   QuicConnection* client_connection = GetClientConnection();
   ASSERT_TRUE(client_connection);
   client_writer_->Initialize(
diff --git a/quiche/quic/test_tools/quic_test_client.cc b/quiche/quic/test_tools/quic_test_client.cc
index 2a82038..32b61ed 100644
--- a/quiche/quic/test_tools/quic_test_client.cc
+++ b/quiche/quic/test_tools/quic_test_client.cc
@@ -14,7 +14,8 @@
 #include "quiche/quic/core/crypto/proof_verifier.h"
 #include "quiche/quic/core/http/quic_spdy_client_stream.h"
 #include "quiche/quic/core/http/spdy_utils.h"
-#include "quiche/quic/core/quic_epoll_connection_helper.h"
+#include "quiche/quic/core/io/quic_default_event_loop.h"
+#include "quiche/quic/core/quic_default_clock.h"
 #include "quiche/quic/core/quic_packet_writer_wrapper.h"
 #include "quiche/quic/core/quic_server_id.h"
 #include "quiche/quic/core/quic_utils.h"
@@ -133,17 +134,17 @@
 };
 }  // namespace
 
-class MockableQuicClientEpollNetworkHelper
-    : public QuicClientEpollNetworkHelper {
+class MockableQuicClientDefaultNetworkHelper
+    : public QuicClientDefaultNetworkHelper {
  public:
-  using QuicClientEpollNetworkHelper::QuicClientEpollNetworkHelper;
-  ~MockableQuicClientEpollNetworkHelper() override = default;
+  using QuicClientDefaultNetworkHelper::QuicClientDefaultNetworkHelper;
+  ~MockableQuicClientDefaultNetworkHelper() override = default;
 
   void ProcessPacket(const QuicSocketAddress& self_address,
                      const QuicSocketAddress& peer_address,
                      const QuicReceivedPacket& packet) override {
-    QuicClientEpollNetworkHelper::ProcessPacket(self_address, peer_address,
-                                                packet);
+    QuicClientDefaultNetworkHelper::ProcessPacket(self_address, peer_address,
+                                                  packet);
     if (track_last_incoming_packet_) {
       last_incoming_packet_ = packet.Clone();
     }
@@ -151,7 +152,7 @@
 
   QuicPacketWriter* CreateQuicPacketWriter() override {
     QuicPacketWriter* writer =
-        QuicClientEpollNetworkHelper::CreateQuicPacketWriter();
+        QuicClientDefaultNetworkHelper::CreateQuicPacketWriter();
     if (!test_writer_) {
       return writer;
     }
@@ -188,35 +189,33 @@
 MockableQuicClient::MockableQuicClient(
     QuicSocketAddress server_address, const QuicServerId& server_id,
     const ParsedQuicVersionVector& supported_versions,
-    QuicEpollServer* epoll_server)
+    QuicEventLoop* event_loop)
     : MockableQuicClient(server_address, server_id, QuicConfig(),
-                         supported_versions, epoll_server) {}
+                         supported_versions, event_loop) {}
 
 MockableQuicClient::MockableQuicClient(
     QuicSocketAddress server_address, const QuicServerId& server_id,
     const QuicConfig& config, const ParsedQuicVersionVector& supported_versions,
-    QuicEpollServer* epoll_server)
+    QuicEventLoop* event_loop)
     : MockableQuicClient(server_address, server_id, config, supported_versions,
-                         epoll_server, nullptr) {}
+                         event_loop, nullptr) {}
 
 MockableQuicClient::MockableQuicClient(
     QuicSocketAddress server_address, const QuicServerId& server_id,
     const QuicConfig& config, const ParsedQuicVersionVector& supported_versions,
-    QuicEpollServer* epoll_server,
-    std::unique_ptr<ProofVerifier> proof_verifier)
+    QuicEventLoop* event_loop, std::unique_ptr<ProofVerifier> proof_verifier)
     : MockableQuicClient(server_address, server_id, config, supported_versions,
-                         epoll_server, std::move(proof_verifier), nullptr) {}
+                         event_loop, std::move(proof_verifier), nullptr) {}
 
 MockableQuicClient::MockableQuicClient(
     QuicSocketAddress server_address, const QuicServerId& server_id,
     const QuicConfig& config, const ParsedQuicVersionVector& supported_versions,
-    QuicEpollServer* epoll_server,
-    std::unique_ptr<ProofVerifier> proof_verifier,
+    QuicEventLoop* event_loop, std::unique_ptr<ProofVerifier> proof_verifier,
     std::unique_ptr<SessionCache> session_cache)
-    : QuicClient(
-          server_address, server_id, supported_versions, config, epoll_server,
-          std::make_unique<MockableQuicClientEpollNetworkHelper>(epoll_server,
-                                                                 this),
+    : QuicDefaultClient(
+          server_address, server_id, supported_versions, config, event_loop,
+          std::make_unique<MockableQuicClientDefaultNetworkHelper>(event_loop,
+                                                                   this),
           std::make_unique<RecordingProofVerifier>(std::move(proof_verifier)),
           std::move(session_cache)),
       override_server_connection_id_(EmptyQuicConnectionId()),
@@ -230,16 +229,16 @@
   }
 }
 
-MockableQuicClientEpollNetworkHelper*
+MockableQuicClientDefaultNetworkHelper*
 MockableQuicClient::mockable_network_helper() {
-  return static_cast<MockableQuicClientEpollNetworkHelper*>(
-      epoll_network_helper());
+  return static_cast<MockableQuicClientDefaultNetworkHelper*>(
+      default_network_helper());
 }
 
-const MockableQuicClientEpollNetworkHelper*
+const MockableQuicClientDefaultNetworkHelper*
 MockableQuicClient::mockable_network_helper() const {
-  return static_cast<const MockableQuicClientEpollNetworkHelper*>(
-      epoll_network_helper());
+  return static_cast<const MockableQuicClientDefaultNetworkHelper*>(
+      default_network_helper());
 }
 
 QuicConnectionId MockableQuicClient::GenerateNewConnectionId() {
@@ -250,7 +249,7 @@
     return QuicUtils::CreateRandomConnectionId(
         override_server_connection_id_length_);
   }
-  return QuicClient::GenerateNewConnectionId();
+  return QuicDefaultClient::GenerateNewConnectionId();
 }
 
 void MockableQuicClient::UseConnectionId(
@@ -272,7 +271,7 @@
     return QuicUtils::CreateRandomConnectionId(
         override_client_connection_id_length_);
   }
-  return QuicClient::GetClientConnectionId();
+  return QuicDefaultClient::GetClientConnectionId();
 }
 
 void MockableQuicClient::UseClientConnectionId(
@@ -314,10 +313,11 @@
 QuicTestClient::QuicTestClient(
     QuicSocketAddress server_address, const std::string& server_hostname,
     const QuicConfig& config, const ParsedQuicVersionVector& supported_versions)
-    : client_(new MockableQuicClient(
+    : event_loop_(GetDefaultEventLoop()->Create(QuicDefaultClock::Get())),
+      client_(new MockableQuicClient(
           server_address,
           QuicServerId(server_hostname, server_address.port(), false), config,
-          supported_versions, &epoll_server_)) {
+          supported_versions, event_loop_.get())) {
   Initialize();
 }
 
@@ -325,10 +325,11 @@
     QuicSocketAddress server_address, const std::string& server_hostname,
     const QuicConfig& config, const ParsedQuicVersionVector& supported_versions,
     std::unique_ptr<ProofVerifier> proof_verifier)
-    : client_(new MockableQuicClient(
+    : event_loop_(GetDefaultEventLoop()->Create(QuicDefaultClock::Get())),
+      client_(new MockableQuicClient(
           server_address,
           QuicServerId(server_hostname, server_address.port(), false), config,
-          supported_versions, &epoll_server_, std::move(proof_verifier))) {
+          supported_versions, event_loop_.get(), std::move(proof_verifier))) {
   Initialize();
 }
 
@@ -337,10 +338,26 @@
     const QuicConfig& config, const ParsedQuicVersionVector& supported_versions,
     std::unique_ptr<ProofVerifier> proof_verifier,
     std::unique_ptr<SessionCache> session_cache)
-    : client_(new MockableQuicClient(
+    : event_loop_(GetDefaultEventLoop()->Create(QuicDefaultClock::Get())),
+      client_(new MockableQuicClient(
           server_address,
           QuicServerId(server_hostname, server_address.port(), false), config,
-          supported_versions, &epoll_server_, std::move(proof_verifier),
+          supported_versions, event_loop_.get(), std::move(proof_verifier),
+          std::move(session_cache))) {
+  Initialize();
+}
+
+QuicTestClient::QuicTestClient(
+    QuicSocketAddress server_address, const std::string& server_hostname,
+    const QuicConfig& config, const ParsedQuicVersionVector& supported_versions,
+    std::unique_ptr<ProofVerifier> proof_verifier,
+    std::unique_ptr<SessionCache> session_cache,
+    std::unique_ptr<QuicEventLoop> event_loop)
+    : event_loop_(std::move(event_loop)),
+      client_(new MockableQuicClient(
+          server_address,
+          QuicServerId(server_hostname, server_address.port(), false), config,
+          supported_versions, event_loop_.get(), std::move(proof_verifier),
           std::move(session_cache))) {
   Initialize();
 }
@@ -636,24 +653,15 @@
 }
 
 bool QuicTestClient::WaitUntil(int timeout_ms, std::function<bool()> trigger) {
-  int64_t timeout_us = timeout_ms * kNumMicrosPerMilli;
-  int64_t old_timeout_us = epoll_server()->timeout_in_us_for_test();
-  if (timeout_us > 0) {
-    epoll_server()->set_timeout_in_us(timeout_us);
-  }
-  const QuicClock* clock =
-      QuicConnectionPeer::GetHelper(client()->session()->connection())
-          ->GetClock();
-  QuicTime end_waiting_time =
-      clock->Now() + QuicTime::Delta::FromMicroseconds(timeout_us);
+  QuicTime::Delta timeout = QuicTime::Delta::FromMilliseconds(timeout_ms);
+  const QuicClock* clock = client()->session()->connection()->clock();
+  QuicTime end_waiting_time = clock->Now() + timeout;
   while (connected() && !(trigger && trigger()) &&
-         (timeout_us < 0 || clock->Now() < end_waiting_time)) {
-    client_->WaitForEvents();
+         (timeout_ms < 0 || clock->Now() < end_waiting_time)) {
+    event_loop_->RunEventLoopOnce(timeout);
+    client_->WaitForEventsPostprocessing();
   }
   ReadNextResponse();
-  if (timeout_us > 0) {
-    epoll_server()->set_timeout_in_us(old_timeout_us);
-  }
   if (trigger && !trigger()) {
     QUIC_VLOG(1) << "Client WaitUntil returning with trigger returning false.";
     return false;
@@ -837,7 +845,7 @@
     bool fin, QuicTestClient* test_client,
     quiche::QuicheReferenceCountedPointer<QuicAckListenerInterface>
         ack_listener)
-    : QuicClient::QuicDataToResend(std::move(headers), body, fin),
+    : QuicDefaultClient::QuicDataToResend(std::move(headers), body, fin),
       test_client_(test_client),
       ack_listener_(std::move(ack_listener)) {}
 
diff --git a/quiche/quic/test_tools/quic_test_client.h b/quiche/quic/test_tools/quic_test_client.h
index 36cc46d..a1f7b13 100644
--- a/quiche/quic/test_tools/quic_test_client.h
+++ b/quiche/quic/test_tools/quic_test_client.h
@@ -10,13 +10,13 @@
 #include <string>
 
 #include "absl/strings/string_view.h"
+#include "quiche/quic/core/io/quic_event_loop.h"
 #include "quiche/quic/core/proto/cached_network_parameters_proto.h"
 #include "quiche/quic/core/quic_framer.h"
 #include "quiche/quic/core/quic_packet_creator.h"
 #include "quiche/quic/core/quic_packets.h"
-#include "quiche/quic/platform/api/quic_epoll.h"
 #include "quiche/quic/platform/api/quic_test.h"
-#include "quiche/quic/tools/quic_client.h"
+#include "quiche/quic/tools/quic_default_client.h"
 #include "quiche/common/quiche_linked_hash_map.h"
 #include "quiche/spdy/core/http2_header_block.h"
 
@@ -27,31 +27,31 @@
 
 namespace test {
 
-class MockableQuicClientEpollNetworkHelper;
+class MockableQuicClientDefaultNetworkHelper;
 
 // A quic client which allows mocking out reads and writes.
-class MockableQuicClient : public QuicClient {
+class MockableQuicClient : public QuicDefaultClient {
  public:
   MockableQuicClient(QuicSocketAddress server_address,
                      const QuicServerId& server_id,
                      const ParsedQuicVersionVector& supported_versions,
-                     QuicEpollServer* epoll_server);
+                     QuicEventLoop* event_loop);
 
   MockableQuicClient(QuicSocketAddress server_address,
                      const QuicServerId& server_id, const QuicConfig& config,
                      const ParsedQuicVersionVector& supported_versions,
-                     QuicEpollServer* epoll_server);
+                     QuicEventLoop* event_loop);
 
   MockableQuicClient(QuicSocketAddress server_address,
                      const QuicServerId& server_id, const QuicConfig& config,
                      const ParsedQuicVersionVector& supported_versions,
-                     QuicEpollServer* epoll_server,
+                     QuicEventLoop* event_loop,
                      std::unique_ptr<ProofVerifier> proof_verifier);
 
   MockableQuicClient(QuicSocketAddress server_address,
                      const QuicServerId& server_id, const QuicConfig& config,
                      const ParsedQuicVersionVector& supported_versions,
-                     QuicEpollServer* epoll_server,
+                     QuicEventLoop* event_loop,
                      std::unique_ptr<ProofVerifier> proof_verifier,
                      std::unique_ptr<SessionCache> session_cache);
   MockableQuicClient(const MockableQuicClient&) = delete;
@@ -73,9 +73,9 @@
   // If true, copy each packet from ProcessPacket into |last_incoming_packet|
   void set_track_last_incoming_packet(bool track);
 
-  // Casts the network helper to a MockableQuicClientEpollNetworkHelper.
-  MockableQuicClientEpollNetworkHelper* mockable_network_helper();
-  const MockableQuicClientEpollNetworkHelper* mockable_network_helper() const;
+  // Casts the network helper to a MockableQuicClientDefaultNetworkHelper.
+  MockableQuicClientDefaultNetworkHelper* mockable_network_helper();
+  const MockableQuicClientDefaultNetworkHelper* mockable_network_helper() const;
 
  private:
   // Server connection ID to use, if server_connection_id_overridden_
@@ -108,6 +108,12 @@
                  const ParsedQuicVersionVector& supported_versions,
                  std::unique_ptr<ProofVerifier> proof_verifier,
                  std::unique_ptr<SessionCache> session_cache);
+  QuicTestClient(QuicSocketAddress server_address,
+                 const std::string& server_hostname, const QuicConfig& config,
+                 const ParsedQuicVersionVector& supported_versions,
+                 std::unique_ptr<ProofVerifier> proof_verifier,
+                 std::unique_ptr<SessionCache> session_cache,
+                 std::unique_ptr<QuicEventLoop> event_loop);
 
   ~QuicTestClient() override;
 
@@ -284,7 +290,7 @@
 
   void WaitForWriteToFlush();
 
-  QuicEpollServer* epoll_server() { return &epoll_server_; }
+  QuicEventLoop* event_loop() { return event_loop_.get(); }
 
   size_t num_requests() const { return num_requests_; }
 
@@ -331,7 +337,7 @@
   QuicTestClient& operator=(const QuicTestClient&&) = delete;
 
  private:
-  class TestClientDataToResend : public QuicClient::QuicDataToResend {
+  class TestClientDataToResend : public QuicDefaultClient::QuicDataToResend {
    public:
     TestClientDataToResend(
         std::unique_ptr<spdy::Http2HeaderBlock> headers, absl::string_view body,
@@ -387,7 +393,7 @@
   // tracking its state.
   void SetLatestCreatedStream(QuicSpdyClientStream* stream);
 
-  QuicEpollServer epoll_server_;
+  std::unique_ptr<QuicEventLoop> event_loop_;
   std::unique_ptr<MockableQuicClient> client_;  // The actual client
   QuicSpdyClientStream* latest_created_stream_;
   std::map<QuicStreamId, QuicSpdyClientStream*> open_streams_;
diff --git a/quiche/quic/tools/quic_client_base.cc b/quiche/quic/tools/quic_client_base.cc
index 4a300c2..3a7dfd6 100644
--- a/quiche/quic/tools/quic_client_base.cc
+++ b/quiche/quic/tools/quic_client_base.cc
@@ -240,6 +240,10 @@
 
   network_helper_->RunEventLoop();
 
+  return WaitForEventsPostprocessing();
+}
+
+bool QuicClientBase::WaitForEventsPostprocessing() {
   QUICHE_DCHECK(session() != nullptr);
   ParsedQuicVersion version = UnsupportedQuicVersion();
   if (!connected() && CanReconnectWithDifferentVersion(&version)) {
diff --git a/quiche/quic/tools/quic_client_base.h b/quiche/quic/tools/quic_client_base.h
index 4f0c880..f241a5e 100644
--- a/quiche/quic/tools/quic_client_base.h
+++ b/quiche/quic/tools/quic_client_base.h
@@ -116,6 +116,10 @@
   // Returns true if there are any outstanding requests.
   bool WaitForEvents();
 
+  // Performs the part of WaitForEvents() that is done after the actual event
+  // loop call.
+  bool WaitForEventsPostprocessing();
+
   // Migrate to a new socket (new_host) during an active connection.
   bool MigrateSocket(const QuicIpAddress& new_host);