Inject a random number generator into QuartcEndpoint and SendAlgorithms.

QuicSentPacketManager now takes a QuicRandom instance in its construtor.  It
shares the connection's random number generator, which may be injected through
the QuicConnectionHelperInterface.  This random number generator is only used to
create a send algorithm.

Rather than using the default instance of QuicRandom, QuartcEndpoint and
QuartcConnectionHelper now use an injected instance.  In production, this will
still be the default instance.  However, in tests it may be replaced with a
deterministic generator (for reproducible results).

In all tests that currently use the QUIC simulator and construct their own
endpoints, inject the simulator's random number generator.

In QuartcBidiTest, set the simulator to use a SimpleRandom generator (which
should give predictable output based on a fixed seed).

gfe-relnote: n/a (Quartc only)
PiperOrigin-RevId: 244280321
Change-Id: I1925f79e3cec3ff7f474f4b979b85c387751503f
diff --git a/quic/core/quic_connection.cc b/quic/core/quic_connection.cc
index fae53e8..eeebc20 100644
--- a/quic/core/quic_connection.cc
+++ b/quic/core/quic_connection.cc
@@ -328,6 +328,7 @@
       sent_packet_manager_(
           perspective,
           clock_,
+          random_generator_,
           &stats_,
           GetQuicReloadableFlag(quic_default_to_bbr) ? kBBR : kCubicBytes,
           kNack),
diff --git a/quic/core/quic_sent_packet_manager.cc b/quic/core/quic_sent_packet_manager.cc
index 456e81c..5a9cbb0 100644
--- a/quic/core/quic_sent_packet_manager.cc
+++ b/quic/core/quic_sent_packet_manager.cc
@@ -75,11 +75,13 @@
 QuicSentPacketManager::QuicSentPacketManager(
     Perspective perspective,
     const QuicClock* clock,
+    QuicRandom* random,
     QuicConnectionStats* stats,
     CongestionControlType congestion_control_type,
     LossDetectionType loss_type)
     : unacked_packets_(perspective),
       clock_(clock),
+      random_(random),
       stats_(stats),
       debug_delegate_(nullptr),
       network_change_visitor_(nullptr),
@@ -1054,8 +1056,8 @@
 void QuicSentPacketManager::SetSendAlgorithm(
     CongestionControlType congestion_control_type) {
   SetSendAlgorithm(SendAlgorithmInterface::Create(
-      clock_, &rtt_stats_, &unacked_packets_, congestion_control_type,
-      QuicRandom::GetInstance(), stats_, initial_congestion_window_));
+      clock_, &rtt_stats_, &unacked_packets_, congestion_control_type, random_,
+      stats_, initial_congestion_window_));
 }
 
 void QuicSentPacketManager::SetSendAlgorithm(
diff --git a/quic/core/quic_sent_packet_manager.h b/quic/core/quic_sent_packet_manager.h
index 7e10b77..f7f3c12 100644
--- a/quic/core/quic_sent_packet_manager.h
+++ b/quic/core/quic_sent_packet_manager.h
@@ -89,6 +89,7 @@
 
   QuicSentPacketManager(Perspective perspective,
                         const QuicClock* clock,
+                        QuicRandom* random,
                         QuicConnectionStats* stats,
                         CongestionControlType congestion_control_type,
                         LossDetectionType loss_type);
@@ -535,6 +536,7 @@
   PendingRetransmissionMap pending_retransmissions_;
 
   const QuicClock* clock_;
+  QuicRandom* random_;
   QuicConnectionStats* stats_;
 
   DebugDelegate* debug_delegate_;
diff --git a/quic/core/quic_sent_packet_manager_test.cc b/quic/core/quic_sent_packet_manager_test.cc
index 08e307c..6d0dd4c 100644
--- a/quic/core/quic_sent_packet_manager_test.cc
+++ b/quic/core/quic_sent_packet_manager_test.cc
@@ -80,7 +80,12 @@
 
  protected:
   QuicSentPacketManagerTest()
-      : manager_(Perspective::IS_SERVER, &clock_, &stats_, kCubicBytes, kNack),
+      : manager_(Perspective::IS_SERVER,
+                 &clock_,
+                 QuicRandom::GetInstance(),
+                 &stats_,
+                 kCubicBytes,
+                 kNack),
         send_algorithm_(new StrictMock<MockSendAlgorithm>),
         network_change_visitor_(new StrictMock<MockNetworkChangeVisitor>) {
     QuicSentPacketManagerPeer::SetSendAlgorithm(&manager_, send_algorithm_);
diff --git a/quic/quartc/quartc_connection_helper.cc b/quic/quartc/quartc_connection_helper.cc
index 6c6ee97..da74858 100644
--- a/quic/quartc/quartc_connection_helper.cc
+++ b/quic/quartc/quartc_connection_helper.cc
@@ -6,15 +6,16 @@
 
 namespace quic {
 
-QuartcConnectionHelper::QuartcConnectionHelper(const QuicClock* clock)
-    : clock_(clock) {}
+QuartcConnectionHelper::QuartcConnectionHelper(const QuicClock* clock,
+                                               QuicRandom* random)
+    : clock_(clock), random_(random) {}
 
 const QuicClock* QuartcConnectionHelper::GetClock() const {
   return clock_;
 }
 
 QuicRandom* QuartcConnectionHelper::GetRandomGenerator() {
-  return QuicRandom::GetInstance();
+  return random_;
 }
 
 QuicBufferAllocator* QuartcConnectionHelper::GetStreamSendBufferAllocator() {
diff --git a/quic/quartc/quartc_connection_helper.h b/quic/quartc/quartc_connection_helper.h
index 2b1d62f..184dacc 100644
--- a/quic/quartc/quartc_connection_helper.h
+++ b/quic/quartc/quartc_connection_helper.h
@@ -16,7 +16,7 @@
 // Simple implementation of QuicConnectionHelperInterface for Quartc.
 class QuartcConnectionHelper : public QuicConnectionHelperInterface {
  public:
-  explicit QuartcConnectionHelper(const QuicClock* clock);
+  QuartcConnectionHelper(const QuicClock* clock, QuicRandom* random);
 
   // QuicConnectionHelperInterface overrides.
   const QuicClock* GetClock() const override;
@@ -25,6 +25,7 @@
 
  private:
   const QuicClock* clock_;
+  QuicRandom* random_;
   SimpleBufferAllocator buffer_allocator_;
 };
 
diff --git a/quic/quartc/quartc_endpoint.cc b/quic/quartc/quartc_endpoint.cc
index 459ceba..fe87ca1 100644
--- a/quic/quartc/quartc_endpoint.cc
+++ b/quic/quartc/quartc_endpoint.cc
@@ -47,6 +47,7 @@
 QuartcClientEndpoint::QuartcClientEndpoint(
     QuicAlarmFactory* alarm_factory,
     const QuicClock* clock,
+    QuicRandom* random,
     QuartcEndpoint::Delegate* delegate,
     const QuartcSessionConfig& config,
     QuicStringPiece serialized_server_config,
@@ -60,7 +61,8 @@
                                              AllSupportedVersions())),
       create_session_alarm_(QuicWrapUnique(
           alarm_factory_->CreateAlarm(new CreateSessionDelegate(this)))),
-      connection_helper_(QuicMakeUnique<QuartcConnectionHelper>(clock)),
+      connection_helper_(
+          QuicMakeUnique<QuartcConnectionHelper>(clock_, random)),
       config_(config) {}
 
 void QuartcClientEndpoint::Connect(QuartcPacketTransport* packet_transport) {
@@ -79,6 +81,7 @@
 QuartcServerEndpoint::QuartcServerEndpoint(
     QuicAlarmFactory* alarm_factory,
     const QuicClock* clock,
+    QuicRandom* random,
     QuartcEndpoint::Delegate* delegate,
     const QuartcSessionConfig& config,
     std::unique_ptr<QuicVersionManager> version_manager)
@@ -88,7 +91,8 @@
       version_manager_(version_manager ? std::move(version_manager)
                                        : QuicMakeUnique<QuicVersionManager>(
                                              AllSupportedVersions())),
-      pre_connection_helper_(QuicMakeUnique<QuartcConnectionHelper>(clock)),
+      pre_connection_helper_(
+          QuicMakeUnique<QuartcConnectionHelper>(clock, random)),
       crypto_config_(
           CreateCryptoServerConfig(pre_connection_helper_->GetRandomGenerator(),
                                    clock,
diff --git a/quic/quartc/quartc_endpoint.h b/quic/quartc/quartc_endpoint.h
index 97f3060..1169403 100644
--- a/quic/quartc/quartc_endpoint.h
+++ b/quic/quartc/quartc_endpoint.h
@@ -65,6 +65,7 @@
   QuartcClientEndpoint(
       QuicAlarmFactory* alarm_factory,
       const QuicClock* clock,
+      QuicRandom* random,
       Delegate* delegate,
       const QuartcSessionConfig& config,
       QuicStringPiece serialized_server_config,
@@ -130,6 +131,7 @@
   QuartcServerEndpoint(
       QuicAlarmFactory* alarm_factory,
       const QuicClock* clock,
+      QuicRandom* random,
       QuartcEndpoint::Delegate* delegate,
       const QuartcSessionConfig& config,
       std::unique_ptr<QuicVersionManager> version_manager = nullptr);
diff --git a/quic/quartc/quartc_endpoint_test.cc b/quic/quartc/quartc_endpoint_test.cc
index 3d7d85e..5af7c87 100644
--- a/quic/quartc/quartc_endpoint_test.cc
+++ b/quic/quartc/quartc_endpoint_test.cc
@@ -33,17 +33,19 @@
         server_session_delegate_(&server_stream_delegate_,
                                  simulator_.GetClock()),
         server_endpoint_delegate_(&server_session_delegate_),
-        server_endpoint_(
-            QuicMakeUnique<QuartcServerEndpoint>(simulator_.GetAlarmFactory(),
-                                                 simulator_.GetClock(),
-                                                 &server_endpoint_delegate_,
-                                                 QuartcSessionConfig())),
+        server_endpoint_(QuicMakeUnique<QuartcServerEndpoint>(
+            simulator_.GetAlarmFactory(),
+            simulator_.GetClock(),
+            simulator_.GetRandomGenerator(),
+            &server_endpoint_delegate_,
+            QuartcSessionConfig())),
         client_session_delegate_(&client_stream_delegate_,
                                  simulator_.GetClock()),
         client_endpoint_delegate_(&client_session_delegate_),
         client_endpoint_(QuicMakeUnique<QuartcClientEndpoint>(
             simulator_.GetAlarmFactory(),
             simulator_.GetClock(),
+            simulator_.GetRandomGenerator(),
             &client_endpoint_delegate_,
             QuartcSessionConfig(),
             /*serialized_server_config=*/"")) {}
@@ -95,7 +97,8 @@
   client_versions.push_back({PROTOCOL_QUIC_CRYPTO, QUIC_VERSION_43});
   client_endpoint_ = QuicMakeUnique<QuartcClientEndpoint>(
       simulator_.GetAlarmFactory(), simulator_.GetClock(),
-      &client_endpoint_delegate_, QuartcSessionConfig(),
+      simulator_.GetRandomGenerator(), &client_endpoint_delegate_,
+      QuartcSessionConfig(),
       /*serialized_server_config=*/"",
       QuicMakeUnique<QuicVersionManager>(client_versions));
 
@@ -104,7 +107,8 @@
   server_versions.push_back({PROTOCOL_QUIC_CRYPTO, QUIC_VERSION_43});
   server_endpoint_ = QuicMakeUnique<QuartcServerEndpoint>(
       simulator_.GetAlarmFactory(), simulator_.GetClock(),
-      &server_endpoint_delegate_, QuartcSessionConfig(),
+      simulator_.GetRandomGenerator(), &server_endpoint_delegate_,
+      QuartcSessionConfig(),
       QuicMakeUnique<QuicVersionManager>(server_versions));
 
   // The endpoints should be able to establish a connection using version 46.
@@ -138,7 +142,8 @@
   client_versions.push_back({PROTOCOL_QUIC_CRYPTO, QUIC_VERSION_43});
   client_endpoint_ = QuicMakeUnique<QuartcClientEndpoint>(
       simulator_.GetAlarmFactory(), simulator_.GetClock(),
-      &client_endpoint_delegate_, QuartcSessionConfig(),
+      simulator_.GetRandomGenerator(), &client_endpoint_delegate_,
+      QuartcSessionConfig(),
       /*serialized_server_config=*/"",
       QuicMakeUnique<QuicVersionManager>(client_versions));
 
@@ -149,7 +154,8 @@
   server_versions.push_back({PROTOCOL_QUIC_CRYPTO, QUIC_VERSION_43});
   server_endpoint_ = QuicMakeUnique<QuartcServerEndpoint>(
       simulator_.GetAlarmFactory(), simulator_.GetClock(),
-      &server_endpoint_delegate_, QuartcSessionConfig(),
+      simulator_.GetRandomGenerator(), &server_endpoint_delegate_,
+      QuartcSessionConfig(),
       QuicMakeUnique<QuicVersionManager>(server_versions));
 
   // The endpoints should be able to establish a connection using version 46.
@@ -183,7 +189,8 @@
   client_versions.push_back({PROTOCOL_QUIC_CRYPTO, QUIC_VERSION_43});
   client_endpoint_ = QuicMakeUnique<QuartcClientEndpoint>(
       simulator_.GetAlarmFactory(), simulator_.GetClock(),
-      &client_endpoint_delegate_, QuartcSessionConfig(),
+      simulator_.GetRandomGenerator(), &client_endpoint_delegate_,
+      QuartcSessionConfig(),
       /*serialized_server_config=*/"",
       QuicMakeUnique<QuicVersionManager>(client_versions));
 
@@ -192,7 +199,8 @@
   server_versions.push_back({PROTOCOL_QUIC_CRYPTO, QUIC_VERSION_46});
   server_endpoint_ = QuicMakeUnique<QuartcServerEndpoint>(
       simulator_.GetAlarmFactory(), simulator_.GetClock(),
-      &server_endpoint_delegate_, QuartcSessionConfig(),
+      simulator_.GetRandomGenerator(), &server_endpoint_delegate_,
+      QuartcSessionConfig(),
       QuicMakeUnique<QuicVersionManager>(server_versions));
 
   // The endpoints should be unable to establish a connection.
diff --git a/quic/quartc/quartc_session_test.cc b/quic/quartc/quartc_session_test.cc
index 31abd57..936e4ed 100644
--- a/quic/quartc/quartc_session_test.cc
+++ b/quic/quartc/quartc_session_test.cc
@@ -74,12 +74,14 @@
     if (create_client_endpoint) {
       client_endpoint_ = QuicMakeUnique<QuartcClientEndpoint>(
           simulator_.GetAlarmFactory(), simulator_.GetClock(),
-          client_endpoint_delegate_.get(), quic::QuartcSessionConfig(),
+          simulator_.GetRandomGenerator(), client_endpoint_delegate_.get(),
+          quic::QuartcSessionConfig(),
           /*serialized_server_config=*/"");
     }
     server_endpoint_ = QuicMakeUnique<QuartcServerEndpoint>(
         simulator_.GetAlarmFactory(), simulator_.GetClock(),
-        server_endpoint_delegate_.get(), quic::QuartcSessionConfig());
+        simulator_.GetRandomGenerator(), server_endpoint_delegate_.get(),
+        quic::QuartcSessionConfig());
   }
 
   // Note that input session config will apply to both server and client.
@@ -512,7 +514,8 @@
 
   client_endpoint_ = QuicMakeUnique<QuartcClientEndpoint>(
       simulator_.GetAlarmFactory(), simulator_.GetClock(),
-      client_endpoint_delegate_.get(), QuartcSessionConfig(),
+      simulator_.GetRandomGenerator(), client_endpoint_delegate_.get(),
+      QuartcSessionConfig(),
       // This is the key line here. It passes through the server config
       // from the server to the client.
       server_endpoint_->server_crypto_config());
diff --git a/quic/quartc/test/bidi_test_runner.cc b/quic/quartc/test/bidi_test_runner.cc
index b185db6..2e9ba87 100644
--- a/quic/quartc/test/bidi_test_runner.cc
+++ b/quic/quartc/test/bidi_test_runner.cc
@@ -101,8 +101,8 @@
     server_delegate = server_interceptor_;
   }
   server_endpoint_ = QuicMakeUnique<QuartcServerEndpoint>(
-      simulator_->GetAlarmFactory(), simulator_->GetClock(), server_delegate,
-      QuartcSessionConfig());
+      simulator_->GetAlarmFactory(), simulator_->GetClock(),
+      simulator_->GetRandomGenerator(), server_delegate, QuartcSessionConfig());
 
   QuartcEndpoint::Delegate* client_delegate = client_peer_.get();
   if (client_interceptor_) {
@@ -110,8 +110,9 @@
     client_delegate = client_interceptor_;
   }
   client_endpoint_ = QuicMakeUnique<QuartcClientEndpoint>(
-      simulator_->GetAlarmFactory(), simulator_->GetClock(), client_delegate,
-      QuartcSessionConfig(), server_endpoint_->server_crypto_config());
+      simulator_->GetAlarmFactory(), simulator_->GetClock(),
+      simulator_->GetRandomGenerator(), client_delegate, QuartcSessionConfig(),
+      server_endpoint_->server_crypto_config());
 
   QuicTime start_time = simulator_->GetClock()->Now();
   server_endpoint_->Connect(server_transport_);
diff --git a/quic/quartc/test/quartc_bidi_test.cc b/quic/quartc/test/quartc_bidi_test.cc
index 7761a80..6ca9144 100644
--- a/quic/quartc/test/quartc_bidi_test.cc
+++ b/quic/quartc/test/quartc_bidi_test.cc
@@ -13,6 +13,7 @@
 #include "net/third_party/quiche/src/quic/quartc/test/bidi_test_runner.h"
 #include "net/third_party/quiche/src/quic/quartc/test/random_delay_link.h"
 #include "net/third_party/quiche/src/quic/quartc/test/random_packet_filter.h"
+#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
 #include "net/third_party/quiche/src/quic/test_tools/simulator/simulator.h"
 
 namespace quic {
@@ -21,7 +22,12 @@
 
 class QuartcBidiTest : public QuicTest {
  protected:
-  QuartcBidiTest() {}
+  QuartcBidiTest() {
+    uint64_t seed = QuicRandom::GetInstance()->RandUint64();
+    QUIC_LOG(INFO) << "Setting random seed to " << seed;
+    random_.set_seed(seed);
+    simulator_.set_random_generator(&random_);
+  }
 
   void CreateTransports(QuicBandwidth bandwidth,
                         QuicTime::Delta propagation_delay,
@@ -45,6 +51,7 @@
   }
 
   simulator::Simulator simulator_;
+  SimpleRandom random_;
 
   std::unique_ptr<simulator::SimulatedQuartcPacketTransport> client_transport_;
   std::unique_ptr<simulator::SimulatedQuartcPacketTransport> server_transport_;
diff --git a/quic/quartc/test/quartc_peer_test.cc b/quic/quartc/test/quartc_peer_test.cc
index cf1e2f1..5a3dbeb 100644
--- a/quic/quartc/test/quartc_peer_test.cc
+++ b/quic/quartc/test/quartc_peer_test.cc
@@ -49,10 +49,12 @@
     DCHECK(server_peer_);
 
     server_endpoint_ = QuicMakeUnique<QuartcServerEndpoint>(
-        simulator_.GetAlarmFactory(), simulator_.GetClock(), server_peer_.get(),
+        simulator_.GetAlarmFactory(), simulator_.GetClock(),
+        simulator_.GetRandomGenerator(), server_peer_.get(),
         QuartcSessionConfig());
     client_endpoint_ = QuicMakeUnique<QuartcClientEndpoint>(
-        simulator_.GetAlarmFactory(), simulator_.GetClock(), client_peer_.get(),
+        simulator_.GetAlarmFactory(), simulator_.GetClock(),
+        simulator_.GetRandomGenerator(), client_peer_.get(),
         QuartcSessionConfig(), server_endpoint_->server_crypto_config());
 
     server_endpoint_->Connect(&server_transport_);