Use RunLoop in pc/ tests

Bug: webrtc:469327588
Change-Id: I0648024795ba1e490e7b0a583aeec1996a6a6964
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/461120
Commit-Queue: Evan Shrubsole <eshr@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Auto-Submit: Evan Shrubsole <eshr@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#47304}
diff --git a/pc/BUILD.gn b/pc/BUILD.gn
index 9b04a2f..91cc293 100644
--- a/pc/BUILD.gn
+++ b/pc/BUILD.gn
@@ -2425,6 +2425,7 @@
       "../p2p:transport_description",
       "../rtc_base:checks",
       "../rtc_base:crypto_random",
+      "../rtc_base:net_helper",
       "../rtc_base:rtc_base_tests_utils",
       "../rtc_base:socket_address",
       "../rtc_base:socket_factory",
@@ -2433,6 +2434,7 @@
       "../system_wrappers",
       "../test:create_test_environment",
       "../test:create_test_field_trials",
+      "../test:run_loop",
       "../test:test_support",
       "../test:wait_until",
       "//third_party/abseil-cpp/absl/strings",
@@ -2492,6 +2494,7 @@
       "../rtc_base:logging",
       "../rtc_base:rtc_base_tests_utils",
       "../rtc_base:socket_address",
+      "../test:run_loop",
       "../test:test_support",
       "../test:wait_until",
       "//third_party/abseil-cpp/absl/algorithm:container",
@@ -3103,6 +3106,7 @@
       "../rtc_base/task_utils:repeating_task",
       "../system_wrappers",
       "../test:frame_generator_capturer",
+      "../test:run_loop",
       "../test:test_support",
       "../test:wait_until",
       "//testing/gmock",
@@ -3158,6 +3162,7 @@
       "../rtc_base:logging",
       "../rtc_base/containers:flat_map",
       "../system_wrappers",
+      "../test:run_loop",
       "../test:test_support",
       "../test/pc/e2e/analyzer/video:default_video_quality_analyzer",
       "../test/pc/e2e/analyzer/video:default_video_quality_analyzer_shared",
diff --git a/pc/channel_unittest.cc b/pc/channel_unittest.cc
index 1705685..8a91c47 100644
--- a/pc/channel_unittest.cc
+++ b/pc/channel_unittest.cc
@@ -63,6 +63,7 @@
 #include "test/create_test_field_trials.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 namespace {
 
@@ -1616,9 +1617,7 @@
       thread->ProcessMessages(0);
     }
   }
-  static void FlushCurrentThread() {
-    webrtc::Thread::Current()->ProcessMessages(0);
-  }
+  void FlushCurrentThread() { main_thread_.Flush(); }
   void WaitForThreads(std::span<webrtc::Thread*> threads) {
     // `threads` and current thread post packets to network thread.
     for (webrtc::Thread* thread : threads) {
@@ -1679,7 +1678,7 @@
         channel2_->media_receive_channel());
   }
 
-  webrtc::AutoThread main_thread_;
+  webrtc::test::RunLoop main_thread_;
   // TODO(pbos): Remove playout from all media channels and let renderers mute
   // themselves.
   const bool verify_playout_;
diff --git a/pc/dtls_srtp_transport_integrationtest.cc b/pc/dtls_srtp_transport_integrationtest.cc
index 7a88f7e..4c3144a 100644
--- a/pc/dtls_srtp_transport_integrationtest.cc
+++ b/pc/dtls_srtp_transport_integrationtest.cc
@@ -40,10 +40,10 @@
 #include "rtc_base/ssl_fingerprint.h"
 #include "rtc_base/ssl_identity.h"
 #include "rtc_base/ssl_stream_adapter.h"
-#include "rtc_base/thread.h"
 #include "test/create_test_environment.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 namespace webrtc {
@@ -130,15 +130,15 @@
     client_ice_transport_->SetDestination(server_ice_transport_.get());
 
     // Wait for the DTLS connection to be up.
-    EXPECT_THAT(WaitUntil(
-                    [&] {
-                      return client_dtls_transport_->writable() &&
-                             server_dtls_transport_->writable();
-                    },
-                    IsTrue(),
-                    {.timeout = TimeDelta::Millis(kTimeout),
-                     .clock = &fake_clock_}),
-                IsRtcOk());
+    EXPECT_THAT(
+        WaitUntil(
+            [&] {
+              return client_dtls_transport_->writable() &&
+                     server_dtls_transport_->writable();
+            },
+            IsTrue(),
+            {.timeout = TimeDelta::Millis(kTimeout), .clock = &fake_clock_}),
+        IsRtcOk());
     EXPECT_EQ(client_dtls_transport_->dtls_state(),
               DtlsTransportState::kConnected);
     EXPECT_EQ(server_dtls_transport_->dtls_state(),
@@ -220,7 +220,7 @@
   }
 
  private:
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   ScopedFakeClock fake_clock_;
   const Environment env_ = CreateTestEnvironment();
 
diff --git a/pc/dtls_srtp_transport_unittest.cc b/pc/dtls_srtp_transport_unittest.cc
index e42d9b7..ab58a8c 100644
--- a/pc/dtls_srtp_transport_unittest.cc
+++ b/pc/dtls_srtp_transport_unittest.cc
@@ -38,9 +38,9 @@
 #include "rtc_base/copy_on_write_buffer.h"
 #include "rtc_base/rtc_certificate.h"
 #include "rtc_base/ssl_identity.h"
-#include "rtc_base/thread.h"
 #include "test/create_test_field_trials.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 namespace webrtc {
 namespace {
@@ -267,7 +267,7 @@
     SendRecvRtcpPackets();
   }
 
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport1_;
   std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport2_;
   TransportObserver transport_observer1_;
diff --git a/pc/dtls_transport_unittest.cc b/pc/dtls_transport_unittest.cc
index 480357b..44e17d4 100644
--- a/pc/dtls_transport_unittest.cc
+++ b/pc/dtls_transport_unittest.cc
@@ -25,9 +25,9 @@
 #include "rtc_base/fake_ssl_identity.h"
 #include "rtc_base/rtc_certificate.h"
 #include "rtc_base/ssl_identity.h"
-#include "rtc_base/thread.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 namespace webrtc {
@@ -99,7 +99,7 @@
     fake_dtls1->SetDestination(fake_dtls2.get());
   }
 
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   scoped_refptr<DtlsTransport> transport_;
   std::unique_ptr<FakeDtlsTransport> internal_transport_;
   TestDtlsTransportObserver observer_;
diff --git a/pc/ice_transport_unittest.cc b/pc/ice_transport_unittest.cc
index 91d04a0..9b91e86 100644
--- a/pc/ice_transport_unittest.cc
+++ b/pc/ice_transport_unittest.cc
@@ -22,9 +22,9 @@
 #include "p2p/test/fake_port_allocator.h"
 #include "rtc_base/internal/default_socket_server.h"
 #include "rtc_base/socket_server.h"
-#include "rtc_base/thread.h"
 #include "test/create_test_environment.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 namespace webrtc {
 
@@ -38,7 +38,7 @@
 
  private:
   std::unique_ptr<SocketServer> socket_server_;
-  AutoSocketServerThread main_thread_;
+  test::RunLoop main_thread_;
 };
 
 TEST_F(IceTransportTest, CreateNonSelfDeletingTransport) {
diff --git a/pc/jsep_transport_controller_unittest.cc b/pc/jsep_transport_controller_unittest.cc
index e271753..136d2e4 100644
--- a/pc/jsep_transport_controller_unittest.cc
+++ b/pc/jsep_transport_controller_unittest.cc
@@ -67,6 +67,7 @@
 #include "test/create_test_field_trials.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 namespace webrtc {
@@ -357,7 +358,7 @@
 
   FieldTrials field_trials_ = CreateTestFieldTrials();
   Environment env_;
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   // Information received from signals from transport controller.
   IceConnectionState connection_state_ = kIceConnectionConnecting;
   PeerConnectionInterface::IceConnectionState ice_connection_state_ =
diff --git a/pc/jsep_transport_unittest.cc b/pc/jsep_transport_unittest.cc
index 3f49e7a..b72267d 100644
--- a/pc/jsep_transport_unittest.cc
+++ b/pc/jsep_transport_unittest.cc
@@ -53,9 +53,9 @@
 #include "rtc_base/ssl_fingerprint.h"
 #include "rtc_base/ssl_identity.h"
 #include "rtc_base/ssl_stream_adapter.h"
-#include "rtc_base/thread.h"
 #include "test/create_test_field_trials.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 namespace webrtc {
 namespace {
@@ -186,7 +186,7 @@
 
   void OnRtcpMuxActive() { signal_rtcp_mux_active_received_ = true; }
 
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   std::unique_ptr<JsepTransport> jsep_transport_;
   bool signal_rtcp_mux_active_received_ = false;
   FieldTrials field_trials_ = CreateTestFieldTrials();
diff --git a/pc/legacy_stats_collector_unittest.cc b/pc/legacy_stats_collector_unittest.cc
index f039115..94f7810 100644
--- a/pc/legacy_stats_collector_unittest.cc
+++ b/pc/legacy_stats_collector_unittest.cc
@@ -67,6 +67,7 @@
 #include "test/create_test_environment.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 namespace webrtc {
 
@@ -695,7 +696,7 @@
       TimeDelta::Seconds(int64_t{365} * 50 * 86400)};  // 50 years offset.
 
  private:
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
 };
 
 static scoped_refptr<MockRtpSenderInternal> CreateMockSender(
diff --git a/pc/media_stream_unittest.cc b/pc/media_stream_unittest.cc
index de82b20..8611ee4 100644
--- a/pc/media_stream_unittest.cc
+++ b/pc/media_stream_unittest.cc
@@ -20,6 +20,7 @@
 #include "rtc_base/thread.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 static const char kStreamId1[] = "local_stream_1";
 static const char kVideoTrackId[] = "dummy_video_cam_1";
@@ -82,7 +83,7 @@
     EXPECT_FALSE(track->enabled());
   }
 
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   scoped_refptr<MediaStreamInterface> stream_;
   scoped_refptr<AudioTrackInterface> audio_track_;
   scoped_refptr<VideoTrackInterface> video_track_;
diff --git a/pc/peer_connection_bundle_unittest.cc b/pc/peer_connection_bundle_unittest.cc
index 192de54..6d4dc98 100644
--- a/pc/peer_connection_bundle_unittest.cc
+++ b/pc/peer_connection_bundle_unittest.cc
@@ -63,6 +63,7 @@
 #include "rtc_base/virtual_socket_server.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 #ifdef WEBRTC_ANDROID
@@ -264,7 +265,7 @@
   }
 
   VirtualSocketServer vss_;
-  AutoSocketServerThread main_;
+  test::RunLoop main_;
   const SdpSemantics sdp_semantics_;
 };
 
diff --git a/pc/peer_connection_crypto_unittest.cc b/pc/peer_connection_crypto_unittest.cc
index 20d8553..f7fee87 100644
--- a/pc/peer_connection_crypto_unittest.cc
+++ b/pc/peer_connection_crypto_unittest.cc
@@ -52,6 +52,7 @@
 #include "rtc_base/thread.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 #ifdef WEBRTC_ANDROID
 #include "pc/test/android_test_initializer.h"
@@ -156,7 +157,7 @@
   }
 
   std::unique_ptr<VirtualSocketServer> vss_;
-  AutoSocketServerThread main_;
+  test::RunLoop main_;
   scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
   const SdpSemantics sdp_semantics_;
 };
diff --git a/pc/peer_connection_data_channel_unittest.cc b/pc/peer_connection_data_channel_unittest.cc
index 694f207..e086ce0 100644
--- a/pc/peer_connection_data_channel_unittest.cc
+++ b/pc/peer_connection_data_channel_unittest.cc
@@ -34,6 +34,7 @@
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/pc/sctp/fake_sctp_transport.h"
+#include "test/run_loop.h"
 
 #ifdef WEBRTC_ANDROID
 #include "pc/test/android_test_initializer.h"
@@ -158,7 +159,7 @@
   }
 
   std::unique_ptr<VirtualSocketServer> vss_;
-  AutoSocketServerThread main_;
+  test::RunLoop main_;
   const SdpSemantics sdp_semantics_;
 };
 
diff --git a/pc/peer_connection_end_to_end_unittest.cc b/pc/peer_connection_end_to_end_unittest.cc
index 53c8daf..ee6799c 100644
--- a/pc/peer_connection_end_to_end_unittest.cc
+++ b/pc/peer_connection_end_to_end_unittest.cc
@@ -48,6 +48,7 @@
 #include "test/create_test_environment.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 #ifdef WEBRTC_ANDROID
@@ -246,7 +247,7 @@
   }
 
  protected:
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   PhysicalSocketServer pss_;
   Environment env_;
   std::unique_ptr<Thread> network_thread_;
@@ -287,13 +288,13 @@
       .WillRepeatedly([dec] { return dec->Channels(); });
   EXPECT_CALL(*mock_decoder, DecodeInternal(_, _, _, _, _))
       .Times(AtLeast(1))
-      .WillRepeatedly(
-          [dec](const uint8_t* encoded, size_t encoded_len, int sample_rate_hz,
-                int16_t* decoded, AudioDecoder::SpeechType* speech_type) {
-            return dec->Decode(encoded, encoded_len, sample_rate_hz,
-                               std::numeric_limits<size_t>::max(), decoded,
-                               speech_type);
-          });
+      .WillRepeatedly([dec](const uint8_t* encoded, size_t encoded_len,
+                            int sample_rate_hz, int16_t* decoded,
+                            AudioDecoder::SpeechType* speech_type) {
+        return dec->Decode(encoded, encoded_len, sample_rate_hz,
+                           std::numeric_limits<size_t>::max(), decoded,
+                           speech_type);
+      });
   EXPECT_CALL(*mock_decoder, Die());
   EXPECT_CALL(*mock_decoder, HasDecodePlc()).WillRepeatedly([dec] {
     return dec->HasDecodePlc();
@@ -326,15 +327,14 @@
       });
   EXPECT_CALL(*mock_decoder_factory, Create)
       .Times(AtLeast(2))
-      .WillRepeatedly(
-          [real_decoder_factory](const Environment& env,
-                                 const SdpAudioFormat& format,
-                                 std::optional<AudioCodecPairId> /* pair */) {
-            auto real_decoder = real_decoder_factory->Create(env, format);
-            return real_decoder
-                       ? CreateForwardingMockDecoder(std::move(real_decoder))
-                       : nullptr;
-          });
+      .WillRepeatedly([real_decoder_factory](
+                          const Environment& env, const SdpAudioFormat& format,
+                          std::optional<AudioCodecPairId> /* pair */) {
+        auto real_decoder = real_decoder_factory->Create(env, format);
+        return real_decoder
+                   ? CreateForwardingMockDecoder(std::move(real_decoder))
+                   : nullptr;
+      });
   return mock_decoder_factory;
 }
 
@@ -690,7 +690,7 @@
 
   // Wait for a bit longer so the remote data channel will receive the
   // close message and be destroyed.
-  Thread::Current()->ProcessMessages(100);
+  main_thread_.RunFor(webrtc::TimeDelta::Millis(100));
 }
 
 // Test behavior of creating too many datachannels.
diff --git a/pc/peer_connection_factory_unittest.cc b/pc/peer_connection_factory_unittest.cc
index ed93e64..346125c 100644
--- a/pc/peer_connection_factory_unittest.cc
+++ b/pc/peer_connection_factory_unittest.cc
@@ -68,6 +68,7 @@
 #include "rtc_base/thread.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 #ifdef WEBRTC_ANDROID
 #include "pc/test/android_test_initializer.h"
@@ -262,7 +263,7 @@
   }
 
   std::unique_ptr<SocketServer> socket_server_;
-  AutoSocketServerThread main_thread_;
+  test::RunLoop main_thread_;
   scoped_refptr<PeerConnectionFactoryInterface> factory_;
   NullPeerConnectionObserver observer_;
   std::unique_ptr<FakePortAllocator> port_allocator_;
diff --git a/pc/peer_connection_field_trial_tests.cc b/pc/peer_connection_field_trial_tests.cc
index 4977bdd..fa32367 100644
--- a/pc/peer_connection_field_trial_tests.cc
+++ b/pc/peer_connection_field_trial_tests.cc
@@ -36,6 +36,7 @@
 #include "system_wrappers/include/clock.h"
 #include "test/create_test_field_trials.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 #ifdef WEBRTC_ANDROID
 #include "pc/test/android_test_initializer.h"
@@ -93,7 +94,7 @@
 
   Clock* const clock_;
   std::unique_ptr<SocketServer> socket_server_;
-  AutoSocketServerThread main_thread_;
+  test::RunLoop main_thread_;
   scoped_refptr<PeerConnectionFactoryInterface> pc_factory_ = nullptr;
   PeerConnectionInterface::RTCConfiguration config_;
 };
diff --git a/pc/peer_connection_header_extension_unittest.cc b/pc/peer_connection_header_extension_unittest.cc
index efd84f9..a81cd9b 100644
--- a/pc/peer_connection_header_extension_unittest.cc
+++ b/pc/peer_connection_header_extension_unittest.cc
@@ -40,6 +40,7 @@
 #include "test/create_test_field_trials.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 namespace webrtc {
 
@@ -116,7 +117,7 @@
   }
 
   std::unique_ptr<SocketServer> socket_server_;
-  AutoSocketServerThread main_thread_;
+  test::RunLoop main_thread_;
   std::vector<RtpHeaderExtensionCapability> extensions_;
 };
 
diff --git a/pc/peer_connection_histogram_unittest.cc b/pc/peer_connection_histogram_unittest.cc
index 53b034c..251d46d 100644
--- a/pc/peer_connection_histogram_unittest.cc
+++ b/pc/peer_connection_histogram_unittest.cc
@@ -44,6 +44,7 @@
 #include "system_wrappers/include/metrics.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 namespace webrtc {
@@ -344,7 +345,7 @@
 
   int next_local_address_ = 0;
   VirtualSocketServer vss_;
-  AutoSocketServerThread main_;
+  test::RunLoop main_;
 };
 
 TEST_F(PeerConnectionUsageHistogramTest, UsageFingerprintHistogramFromTimeout) {
diff --git a/pc/peer_connection_interface_unittest.cc b/pc/peer_connection_interface_unittest.cc
index 15f4d31..7135acb 100644
--- a/pc/peer_connection_interface_unittest.cc
+++ b/pc/peer_connection_interface_unittest.cc
@@ -92,6 +92,7 @@
 #include "rtc_base/weak_ptr.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 #ifdef WEBRTC_ANDROID
@@ -1290,7 +1291,7 @@
   SocketServer* socket_server() const { return vss_.get(); }
 
   std::unique_ptr<VirtualSocketServer> vss_;
-  AutoSocketServerThread main_;
+  test::RunLoop main_;
   std::unique_ptr<Thread> network_thread_;
   std::unique_ptr<Thread> worker_thread_;
   scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
@@ -3978,6 +3979,7 @@
 
   std::unique_ptr<Thread> network_thread_;
   std::unique_ptr<Thread> worker_thread_;
+  test::RunLoop signaling_thread_;
   scoped_refptr<PeerConnectionFactoryForTest> pcf_;
   MockPeerConnectionObserver observer_;
 };
diff --git a/pc/peer_connection_media_unittest.cc b/pc/peer_connection_media_unittest.cc
index a4671c9..ea2a077 100644
--- a/pc/peer_connection_media_unittest.cc
+++ b/pc/peer_connection_media_unittest.cc
@@ -55,6 +55,7 @@
 #include "rtc_base/ref_counted_object.h"
 #include "rtc_base/thread.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #ifdef WEBRTC_ANDROID
 #include "pc/test/android_test_initializer.h"
 #endif
@@ -245,7 +246,7 @@
   }
 
   std::unique_ptr<VirtualSocketServer> vss_;
-  AutoSocketServerThread main_;
+  test::RunLoop main_;
   const SdpSemantics sdp_semantics_;
 };
 
@@ -1576,11 +1577,10 @@
   auto codecs =
       caller->pc_factory()->GetRtpSenderCapabilities(MediaType::AUDIO).codecs;
   auto codecs_only_rtx_red_fec = codecs;
-  std::erase_if(
-      codecs_only_rtx_red_fec, [](const RtpCodecCapability& codec) {
-        return !(codec.name == kRtxCodecName || codec.name == kRedCodecName ||
-                 codec.name == kUlpfecCodecName);
-      });
+  std::erase_if(codecs_only_rtx_red_fec, [](const RtpCodecCapability& codec) {
+    return !(codec.name == kRtxCodecName || codec.name == kRedCodecName ||
+             codec.name == kUlpfecCodecName);
+  });
   ASSERT_THAT(codecs_only_rtx_red_fec.size(), Gt(0));
   auto result = transceiver->SetCodecPreferences(codecs_only_rtx_red_fec);
   EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, result.type());
@@ -1652,11 +1652,10 @@
   auto codecs =
       caller->pc_factory()->GetRtpSenderCapabilities(MediaType::VIDEO).codecs;
   auto codecs_only_rtx_red_fec = codecs;
-  std::erase_if(
-      codecs_only_rtx_red_fec, [](const RtpCodecCapability& codec) {
-        return !(codec.name == kRtxCodecName || codec.name == kRedCodecName ||
-                 codec.name == kUlpfecCodecName);
-      });
+  std::erase_if(codecs_only_rtx_red_fec, [](const RtpCodecCapability& codec) {
+    return !(codec.name == kRtxCodecName || codec.name == kRedCodecName ||
+             codec.name == kUlpfecCodecName);
+  });
 
   auto result = transceiver->SetCodecPreferences(codecs_only_rtx_red_fec);
   EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, result.type());
diff --git a/pc/peer_connection_rampup_tests.cc b/pc/peer_connection_rampup_tests.cc
index e967536..b0c76c2 100644
--- a/pc/peer_connection_rampup_tests.cc
+++ b/pc/peer_connection_rampup_tests.cc
@@ -40,7 +40,6 @@
 #include "api/video_codecs/video_encoder_factory_template_libvpx_vp8_adapter.h"
 #include "api/video_codecs/video_encoder_factory_template_libvpx_vp9_adapter.h"
 #include "api/video_codecs/video_encoder_factory_template_open_h264_adapter.h"
-#include "p2p/base/port_interface.h"
 #include "p2p/test/test_turn_server.h"
 #include "pc/peer_connection.h"
 #include "pc/peer_connection_wrapper.h"
@@ -51,6 +50,7 @@
 #include "rtc_base/crypto_random.h"
 #include "rtc_base/fake_network.h"
 #include "rtc_base/firewall_socket_server.h"
+#include "rtc_base/net_helper.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/socket_factory.h"
 #include "rtc_base/task_queue_for_test.h"
diff --git a/pc/peer_connection_signaling_unittest.cc b/pc/peer_connection_signaling_unittest.cc
index 368ef4a..6bc9c29 100644
--- a/pc/peer_connection_signaling_unittest.cc
+++ b/pc/peer_connection_signaling_unittest.cc
@@ -65,6 +65,7 @@
 #include "rtc_base/virtual_socket_server.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 #ifdef WEBRTC_ANDROID
@@ -198,7 +199,7 @@
   }
 
   std::unique_ptr<VirtualSocketServer> vss_;
-  AutoSocketServerThread main_;
+  test::RunLoop main_;
   scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
   const SdpSemantics sdp_semantics_;
 };
diff --git a/pc/rtp_transport_unittest.cc b/pc/rtp_transport_unittest.cc
index adbe708..65762fa 100644
--- a/pc/rtp_transport_unittest.cc
+++ b/pc/rtp_transport_unittest.cc
@@ -32,7 +32,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/network/sent_packet.h"
 #include "rtc_base/network_route.h"
-#include "rtc_base/thread.h"
 #include "test/create_test_field_trials.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
@@ -240,7 +239,7 @@
 TEST(RtpTransportTest, RtcpPacketSentOverCorrectTransport) {
   // If the RTCP-mux is not enabled, RTCP packets are expected to be sent over
   // the RtcpPacketTransport.
-  AutoThread thread;
+  test::RunLoop thread;
   RtpTransport transport(kMuxDisabled, CreateTestFieldTrials());
   FakePacketTransport fake_rtcp("fake_rtcp");
   FakePacketTransport fake_rtp("fake_rtp");
@@ -291,7 +290,7 @@
 }
 
 TEST(RtpTransportTest, RegisterAndUnregisterRtpHeaderExtensionMap) {
-  AutoThread thread;
+  test::RunLoop thread;
   RtpTransport transport(kMuxDisabled, CreateTestFieldTrials());
   RtpHeaderExtensions extensions1 = {
       RtpExtension("urn:ietf:params:rtp-hdrext:ssrc-audio-level", 1)};
@@ -394,7 +393,7 @@
 // Test that SignalPacketReceived fires with rtcp=true when a RTCP packet is
 // received.
 TEST(RtpTransportTest, SignalDemuxedRtcp) {
-  AutoThread thread;
+  test::RunLoop thread;
   RtpTransport transport(kMuxDisabled, CreateTestFieldTrials());
   FakePacketTransport fake_rtp("fake_rtp");
   fake_rtp.SetDestination(&fake_rtp, true);
@@ -418,7 +417,7 @@
 // Test that SignalPacketReceived fires with rtcp=false when a RTP packet with a
 // handled payload type is received.
 TEST(RtpTransportTest, SignalHandledRtpPayloadType) {
-  AutoThread thread;
+  test::RunLoop thread;
   RtpTransport transport(kMuxDisabled, CreateTestFieldTrials());
   FakePacketTransport fake_rtp("fake_rtp");
   fake_rtp.SetDestination(&fake_rtp, true);
@@ -443,7 +442,7 @@
 }
 
 TEST(RtpTransportTest, ReceivedPacketEcnMarkingPropagatedToDemuxedPacket) {
-  AutoThread thread;
+  test::RunLoop thread;
   RtpTransport transport(kMuxDisabled, CreateTestFieldTrials());
   // Setup FakePacketTransport to send packets to itself.
   FakePacketTransport fake_rtp("fake_rtp");
@@ -468,7 +467,7 @@
 }
 
 TEST(RtpTransportTest, RtcpSentAsEct1IfReceivedRtpPacketAsEct1) {
-  AutoThread thread;
+  test::RunLoop thread;
   RtpTransport transport(kMuxDisabled, CreateTestFieldTrials());
   // Setup FakePacketTransport to send packets to itself.
   FakePacketTransport fake_rtp("fake_rtp");
@@ -504,7 +503,7 @@
 // Test that SignalPacketReceived does not fire when a RTP packet with an
 // unhandled payload type is received.
 TEST(RtpTransportTest, DontSignalUnhandledRtpPayloadType) {
-  AutoThread thread;
+  test::RunLoop thread;
   RtpTransport transport(kMuxDisabled, CreateTestFieldTrials());
   FakePacketTransport fake_rtp("fake_rtp");
   fake_rtp.SetDestination(&fake_rtp, true);
@@ -530,7 +529,7 @@
 
 TEST(RtpTransportTest, DontChangeReadyToSendStateOnSendFailure) {
   // ReadyToSendState should only care about if transport is writable.
-  AutoThread thread;
+  test::RunLoop thread;
   RtpTransport transport(kMuxEnabled, CreateTestFieldTrials());
   TransportObserver observer(&transport);
 
diff --git a/pc/sctp_transport_unittest.cc b/pc/sctp_transport_unittest.cc
index c7a8a4b..d3b796e 100644
--- a/pc/sctp_transport_unittest.cc
+++ b/pc/sctp_transport_unittest.cc
@@ -32,9 +32,9 @@
 #include "p2p/dtls/fake_dtls_transport.h"
 #include "pc/dtls_transport.h"
 #include "rtc_base/copy_on_write_buffer.h"
-#include "rtc_base/thread.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 namespace webrtc {
@@ -161,7 +161,7 @@
     return static_cast<FakeSctpTransportInternal*>(transport_->internal());
   }
 
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   scoped_refptr<SctpTransport> transport_;
   scoped_refptr<DtlsTransport> dtls_transport_;
   std::unique_ptr<FakeDtlsTransport> internal_transport_;
@@ -169,7 +169,7 @@
 };
 
 TEST(SctpTransportSimpleTest, CreateClearDelete) {
-  AutoThread main_thread;
+  test::RunLoop main_thread;
   std::unique_ptr<DtlsTransportInternal> internal_transport =
       std::make_unique<FakeDtlsTransport>("audio", ICE_CANDIDATE_COMPONENT_RTP);
   scoped_refptr<DtlsTransport> dtls_transport =
diff --git a/pc/sdp_munging_detector_unittest.cc b/pc/sdp_munging_detector_unittest.cc
index 2c8430c..369837d 100644
--- a/pc/sdp_munging_detector_unittest.cc
+++ b/pc/sdp_munging_detector_unittest.cc
@@ -66,6 +66,7 @@
 #include "test/create_test_field_trials.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 // This file contains unit tests that relate to the behavior of the
@@ -149,7 +150,7 @@
   scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
 
  private:
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
 };
 
 TEST_F(SdpMungingTest, DISABLED_ReportUMAMetricsWithNoMunging) {
diff --git a/pc/sdp_offer_answer_unittest.cc b/pc/sdp_offer_answer_unittest.cc
index a4a9c62..0749978 100644
--- a/pc/sdp_offer_answer_unittest.cc
+++ b/pc/sdp_offer_answer_unittest.cc
@@ -57,6 +57,7 @@
 #include "test/create_test_field_trials.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 // This file contains unit tests that relate to the behavior of the
 // SdpOfferAnswer module.
@@ -153,7 +154,7 @@
   scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
 
  private:
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
 };
 
 TEST_F(SdpOfferAnswerTest, OnTrackReturnsProxiedObject) {
diff --git a/pc/srtp_transport_unittest.cc b/pc/srtp_transport_unittest.cc
index 20a7c45..163c509 100644
--- a/pc/srtp_transport_unittest.cc
+++ b/pc/srtp_transport_unittest.cc
@@ -33,9 +33,9 @@
 #include "rtc_base/containers/flat_set.h"
 #include "rtc_base/copy_on_write_buffer.h"
 #include "rtc_base/ssl_stream_adapter.h"
-#include "rtc_base/thread.h"
 #include "test/create_test_field_trials.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 using ::webrtc::kSrtpAeadAes128Gcm;
 using ::webrtc::kTestKey1;
@@ -280,7 +280,7 @@
     TestSendRecvPacketWithEncryptedHeaderExtension(crypto_suite,
                                                    encrypted_headers);
   }
-  AutoThread main_thread;
+  test::RunLoop main_thread;
 
   std::unique_ptr<SrtpTransport> srtp_transport1_;
   std::unique_ptr<SrtpTransport> srtp_transport2_;
diff --git a/pc/test/fake_audio_capture_module_unittest.cc b/pc/test/fake_audio_capture_module_unittest.cc
index 42d1a72..63d3580 100644
--- a/pc/test/fake_audio_capture_module_unittest.cc
+++ b/pc/test/fake_audio_capture_module_unittest.cc
@@ -19,9 +19,9 @@
 #include "api/test/rtc_error_matchers.h"
 #include "api/units/time_delta.h"
 #include "rtc_base/synchronization/mutex.h"
-#include "rtc_base/thread.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 class FakeAdmTest : public ::testing::Test, public webrtc::AudioTransport {
@@ -127,7 +127,7 @@
     return min_buffer_size;
   }
 
-  webrtc::AutoThread main_thread_;
+  webrtc::test::RunLoop main_thread_;
 
   mutable webrtc::Mutex mutex_;
 
diff --git a/pc/track_media_info_map_unittest.cc b/pc/track_media_info_map_unittest.cc
index fd46ed5..2b50e40 100644
--- a/pc/track_media_info_map_unittest.cc
+++ b/pc/track_media_info_map_unittest.cc
@@ -33,6 +33,7 @@
 #include "rtc_base/thread.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 using ::testing::ElementsAre;
 
@@ -210,7 +211,7 @@
   }
 
  private:
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   VoiceMediaInfo voice_media_info_;
   VideoMediaInfo video_media_info_;
 
diff --git a/pc/video_track_unittest.cc b/pc/video_track_unittest.cc
index a0a078c..259d415 100644
--- a/pc/video_track_unittest.cc
+++ b/pc/video_track_unittest.cc
@@ -22,6 +22,7 @@
 #include "pc/test/fake_video_track_source.h"
 #include "rtc_base/thread.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 namespace webrtc {
 namespace {
@@ -38,7 +39,7 @@
   }
 
  protected:
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   scoped_refptr<FakeVideoTrackSource> video_track_source_;
   scoped_refptr<VideoTrack> video_track_;
   FakeFrameSource frame_source_;