Use RunLoop in p2p/ tests

Bug: webrtc:469327588
Change-Id: I4765d009202bba7bee45a0e944f797766a6a6964
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/461380
Auto-Submit: Evan Shrubsole <eshr@webrtc.org>
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Commit-Queue: Per Kjellander <perkj@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#47311}
diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn
index 8ff2369..344d127 100644
--- a/p2p/BUILD.gn
+++ b/p2p/BUILD.gn
@@ -1075,6 +1075,7 @@
       "../rtc_base/network:received_packet",
       "../rtc_base/network:sent_packet",
       "../rtc_base/synchronization:mutex",
+      "../test:run_loop",
       "../test:test_support",
       "//third_party/abseil-cpp/absl/base:core_headers",
       "//third_party/abseil-cpp/absl/memory",
@@ -1285,6 +1286,7 @@
       "../rtc_base:ssl_adapter",
       "../rtc_base:threading",
       "../test:create_test_field_trials",
+      "../test:run_loop",
       "../test:test_support",
       "../test:wait_until",
       "//third_party/abseil-cpp/absl/algorithm:container",
diff --git a/p2p/base/async_stun_tcp_socket_unittest.cc b/p2p/base/async_stun_tcp_socket_unittest.cc
index 574561c..7857cc3 100644
--- a/p2p/base/async_stun_tcp_socket_unittest.cc
+++ b/p2p/base/async_stun_tcp_socket_unittest.cc
@@ -26,11 +26,11 @@
 #include "rtc_base/network/sent_packet.h"
 #include "rtc_base/socket.h"
 #include "rtc_base/socket_address.h"
-#include "rtc_base/thread.h"
 #include "rtc_base/virtual_socket_server.h"
 #include "test/create_test_environment.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 namespace webrtc {
 
@@ -151,7 +151,7 @@
   }
 
   std::unique_ptr<VirtualSocketServer> vss_;
-  AutoSocketServerThread thread_;
+  test::RunLoop thread_;
   std::unique_ptr<AsyncStunTCPSocket> send_socket_;
   std::unique_ptr<AsyncListenSocket> listen_socket_;
   std::unique_ptr<AsyncPacketSocket> recv_socket_;
diff --git a/p2p/base/p2p_transport_channel_unittest.cc b/p2p/base/p2p_transport_channel_unittest.cc
index 97a9feb..8b596e0 100644
--- a/p2p/base/p2p_transport_channel_unittest.cc
+++ b/p2p/base/p2p_transport_channel_unittest.cc
@@ -96,6 +96,7 @@
 #include "test/create_test_field_trials.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/time_controller/simulated_time_controller.h"
 #include "test/wait_until.h"
 
@@ -5404,7 +5405,7 @@
   const Environment env = CreateTestEnvironment();
   ResolverFactoryFixture resolver_fixture;
   std::unique_ptr<SocketServer> socket_server = CreateDefaultSocketServer();
-  AutoSocketServerThread main_thread(socket_server.get());
+  test::RunLoop main_thread(socket_server.get());
   FakePortAllocator allocator(env, socket_server.get());
   IceTransportInit init(env);
   init.set_port_allocator(&allocator);
@@ -6378,7 +6379,7 @@
 TEST(P2PTransportChannelIceControllerTest, InjectIceController) {
   const Environment env = CreateTestEnvironment();
   std::unique_ptr<SocketServer> socket_server = CreateDefaultSocketServer();
-  AutoSocketServerThread main_thread(socket_server.get());
+  test::RunLoop main_thread(socket_server.get());
   MockIceControllerFactory factory;
   FakePortAllocator pa(env, socket_server.get());
   EXPECT_CALL(factory, RecordIceControllerCreated()).Times(1);
@@ -6393,7 +6394,7 @@
 TEST(P2PTransportChannel, InjectActiveIceController) {
   const Environment env = CreateTestEnvironment();
   std::unique_ptr<SocketServer> socket_server = CreateDefaultSocketServer();
-  AutoSocketServerThread main_thread(socket_server.get());
+  test::RunLoop main_thread(socket_server.get());
   MockActiveIceControllerFactory factory;
   FakePortAllocator pa(env, socket_server.get());
   EXPECT_CALL(factory, RecordActiveIceControllerCreated()).Times(1);
diff --git a/p2p/base/port_allocator_unittest.cc b/p2p/base/port_allocator_unittest.cc
index 9e68874..10f9d2a 100644
--- a/p2p/base/port_allocator_unittest.cc
+++ b/p2p/base/port_allocator_unittest.cc
@@ -22,9 +22,9 @@
 #include "rtc_base/ip_address.h"
 #include "rtc_base/net_helper.h"
 #include "rtc_base/socket_address.h"
-#include "rtc_base/thread.h"
 #include "rtc_base/virtual_socket_server.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 using ::webrtc::CreateEnvironment;
 using ::webrtc::IceCandidateType;
@@ -98,7 +98,7 @@
   }
 
   std::unique_ptr<webrtc::VirtualSocketServer> vss_;
-  webrtc::AutoSocketServerThread main_;
+  webrtc::test::RunLoop main_;
   std::unique_ptr<webrtc::FakePortAllocator> allocator_;
   webrtc::SocketAddress stun_server_1{"11.11.11.11", 3478};
   webrtc::SocketAddress stun_server_2{"22.22.22.22", 3478};
diff --git a/p2p/base/tcp_port_unittest.cc b/p2p/base/tcp_port_unittest.cc
index 6d50e41..97de11c 100644
--- a/p2p/base/tcp_port_unittest.cc
+++ b/p2p/base/tcp_port_unittest.cc
@@ -34,10 +34,10 @@
 #include "rtc_base/network/sent_packet.h"
 #include "rtc_base/socket.h"
 #include "rtc_base/socket_address.h"
-#include "rtc_base/thread.h"
 #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"
 
 using ::testing::Eq;
@@ -105,7 +105,7 @@
                                          int port_number = 0) {
     auto port = std::unique_ptr<TCPPort>(
         TCPPort::Create({.env = env_,
-                         .network_thread = &main_,
+                         .network_thread = main_.task_queue(),
                          .socket_factory = &socket_factory_,
                          .network = MakeNetwork(addr),
                          .ice_username_fragment = username_,
@@ -118,7 +118,7 @@
   std::unique_ptr<TCPPort> CreateTCPPort(const webrtc::Network* network) {
     auto port = std::unique_ptr<TCPPort>(
         TCPPort::Create({.env = env_,
-                         .network_thread = &main_,
+                         .network_thread = main_.task_queue(),
                          .socket_factory = &socket_factory_,
                          .network = network,
                          .ice_username_fragment = username_,
@@ -135,7 +135,7 @@
   // vector so that when it grows, pointers aren't invalidated.
   std::list<webrtc::Network> networks_;
   std::unique_ptr<webrtc::VirtualSocketServer> ss_;
-  webrtc::AutoSocketServerThread main_;
+  webrtc::test::RunLoop main_;
   webrtc::BasicPacketSocketFactory socket_factory_;
   std::string username_;
   std::string password_;
@@ -431,7 +431,7 @@
                         {.timeout = webrtc::TimeDelta::Millis(kTimeout)}),
       webrtc::IsRtcOk());
   // Wait a bit for the Stun response to be received.
-  webrtc::Thread::Current()->ProcessMessages(100);
+  main_.RunFor(webrtc::TimeDelta::Millis(100));
 
   // After the Stun Ping response has been received, packets can be sent again
   // and SignalSentPacket should be invoked.
diff --git a/p2p/base/wrapping_active_ice_controller_unittest.cc b/p2p/base/wrapping_active_ice_controller_unittest.cc
index 459af04..41c6056 100644
--- a/p2p/base/wrapping_active_ice_controller_unittest.cc
+++ b/p2p/base/wrapping_active_ice_controller_unittest.cc
@@ -26,10 +26,10 @@
 #include "p2p/test/mock_ice_controller.h"
 #include "rtc_base/event.h"
 #include "rtc_base/fake_clock.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"
 
 namespace webrtc {
 namespace {
@@ -55,7 +55,7 @@
 constexpr TimeDelta kTick = TimeDelta::Millis(1);
 
 TEST(WrappingActiveIceControllerTest, CreateLegacyIceControllerFromFactory) {
-  AutoThread main;
+  test::RunLoop main;
   MockIceAgent agent;
   IceControllerFactoryArgs args = {.env = CreateTestEnvironment()};
   MockIceControllerFactory legacy_controller_factory;
@@ -65,7 +65,7 @@
 }
 
 TEST(WrappingActiveIceControllerTest, PassthroughIceControllerInterface) {
-  AutoThread main;
+  test::RunLoop main;
   MockIceAgent agent;
   auto will_move = std::make_unique<MockIceController>();
   MockIceController* wrapped = will_move.get();
@@ -100,7 +100,7 @@
 }
 
 TEST(WrappingActiveIceControllerTest, HandlesImmediateSwitchRequest) {
-  AutoThread main;
+  test::RunLoop main;
   ScopedFakeClock clock;
   NiceMock<MockIceAgent> agent;
   auto will_move = std::make_unique<NiceMockIceController>();
@@ -144,7 +144,7 @@
 }
 
 TEST(WrappingActiveIceControllerTest, HandlesImmediateSortAndSwitchRequest) {
-  AutoThread main;
+  test::RunLoop main;
   ScopedFakeClock clock;
   NiceMock<MockIceAgent> agent;
   auto will_move = std::make_unique<NiceMockIceController>();
@@ -195,7 +195,7 @@
 }
 
 TEST(WrappingActiveIceControllerTest, HandlesSortAndSwitchRequest) {
-  AutoThread main;
+  test::RunLoop main;
   ScopedFakeClock clock;
 
   // Block the main task queue until ready.
@@ -239,7 +239,7 @@
 }
 
 TEST(WrappingActiveIceControllerTest, StartPingingAfterSortAndSwitch) {
-  AutoThread main;
+  test::RunLoop main;
   ScopedFakeClock clock;
 
   // Block the main task queue until ready.
diff --git a/p2p/dtls/dtls_transport_unittest.cc b/p2p/dtls/dtls_transport_unittest.cc
index 98b0ba5..3d3dd6a 100644
--- a/p2p/dtls/dtls_transport_unittest.cc
+++ b/p2p/dtls/dtls_transport_unittest.cc
@@ -57,11 +57,11 @@
 #include "rtc_base/ssl_identity.h"
 #include "rtc_base/ssl_stream_adapter.h"
 #include "rtc_base/stream.h"
-#include "rtc_base/thread.h"
 #include "system_wrappers/include/metrics.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 {
@@ -728,7 +728,7 @@
   }
 
  protected:
-  AutoThread main_thread_;
+  test::RunLoop main_thread_;
   ScopedFakeClock fake_clock_;
   DtlsTestClient client1_;
   DtlsTestClient client2_;
diff --git a/p2p/test/nat_unittest.cc b/p2p/test/nat_unittest.cc
index f50ea73..502e0f2 100644
--- a/p2p/test/nat_unittest.cc
+++ b/p2p/test/nat_unittest.cc
@@ -36,6 +36,7 @@
 #include "rtc_base/virtual_socket_server.h"
 #include "test/create_test_environment.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 #include "test/wait_until.h"
 
 namespace webrtc {
@@ -231,13 +232,13 @@
 }
 
 void TestPhysicalInternal(const SocketAddress& int_addr) {
-  AutoThread main_thread;
+  test::RunLoop main_thread;
   PhysicalSocketServer socket_server;
   const Environment env = CreateTestEnvironment();
   BasicNetworkManager network_manager(env, &socket_server);
   network_manager.StartUpdating();
   // Process pending messages so the network list is updated.
-  Thread::Current()->ProcessMessages(0);
+  main_thread.Flush();
 
   std::vector<const Network*> networks = network_manager.GetNetworks();
   std::erase_if(networks, [](const Network* network) {
@@ -301,7 +302,7 @@
 }  // namespace
 
 void TestVirtualInternal(int family) {
-  AutoThread main_thread;
+  test::RunLoop main_thread;
   const Environment env = CreateTestEnvironment();
   std::unique_ptr<TestVirtualSocketServer> int_vss(
       new TestVirtualSocketServer());
diff --git a/p2p/test/turn_server_unittest.cc b/p2p/test/turn_server_unittest.cc
index 2b63b13..455e1da 100644
--- a/p2p/test/turn_server_unittest.cc
+++ b/p2p/test/turn_server_unittest.cc
@@ -14,13 +14,13 @@
 
 #include "api/environment/environment.h"
 #include "p2p/base/basic_packet_socket_factory.h"
-#include "p2p/base/port_interface.h"
 #include "rtc_base/async_packet_socket.h"
+#include "rtc_base/net_helper.h"
 #include "rtc_base/socket_address.h"
-#include "rtc_base/thread.h"
 #include "rtc_base/virtual_socket_server.h"
 #include "test/create_test_environment.h"
 #include "test/gtest.h"
+#include "test/run_loop.h"
 
 // NOTE: This is a work in progress. Currently this file only has tests for
 // TurnServerConnection, a primitive class used by TurnServer.
@@ -48,7 +48,7 @@
 
  protected:
   VirtualSocketServer vss_;
-  AutoSocketServerThread thread_;
+  test::RunLoop thread_;
   BasicPacketSocketFactory socket_factory_;
 };