Move test_client.h to webrtc namespace
Bug: webrtc:42232595
Change-Id: Ie469504499563f6df54580a94706b1900a949a1f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/380560
Auto-Submit: Evan Shrubsole <eshr@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Evan Shrubsole <eshr@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#44086}
diff --git a/p2p/test/nat_unittest.cc b/p2p/test/nat_unittest.cc
index 5e1d2be..4efd32f 100644
--- a/p2p/test/nat_unittest.cc
+++ b/p2p/test/nat_unittest.cc
@@ -45,7 +45,7 @@
namespace rtc {
namespace {
-bool CheckReceive(TestClient* client,
+bool CheckReceive(webrtc::TestClient* client,
bool should_receive,
const char* buf,
size_t size) {
@@ -53,14 +53,14 @@
: client->CheckNoPacket();
}
-TestClient* CreateTestClient(SocketFactory* factory,
- const SocketAddress& local_addr) {
- return new TestClient(
+webrtc::TestClient* CreateTestClient(SocketFactory* factory,
+ const SocketAddress& local_addr) {
+ return new webrtc::TestClient(
absl::WrapUnique(AsyncUDPSocket::Create(factory, local_addr)));
}
-TestClient* CreateTCPTestClient(Socket* socket) {
- return new TestClient(std::make_unique<AsyncTCPSocket>(socket));
+webrtc::TestClient* CreateTCPTestClient(Socket* socket) {
+ return new webrtc::TestClient(std::make_unique<AsyncTCPSocket>(socket));
}
// Tests that when sending from internal_addr to external_addrs through the
@@ -86,10 +86,10 @@
NATSocketFactory* natsf = new NATSocketFactory(
internal, nat->internal_udp_address(), nat->internal_tcp_address());
- TestClient* in;
+ webrtc::TestClient* in;
th_int.BlockingCall([&] { in = CreateTestClient(natsf, internal_addr); });
- TestClient* out[4];
+ webrtc::TestClient* out[4];
th_ext.BlockingCall([&] {
for (int i = 0; i < 4; i++)
out[i] = CreateTestClient(external, external_addrs[i]);
@@ -147,10 +147,10 @@
NATSocketFactory* natsf = new NATSocketFactory(
internal, nat->internal_udp_address(), nat->internal_tcp_address());
- TestClient* in = nullptr;
+ webrtc::TestClient* in = nullptr;
th_int.BlockingCall([&] { in = CreateTestClient(natsf, internal_addr); });
- TestClient* out[4];
+ webrtc::TestClient* out[4];
th_ext.BlockingCall([&] {
for (int i = 0; i < 4; i++)
out[i] = CreateTestClient(external, external_addrs[i]);
@@ -413,8 +413,9 @@
EXPECT_EQ(client_->GetRemoteAddress(), server_->GetLocalAddress());
EXPECT_EQ(accepted_->GetRemoteAddress().ipaddr(), ext_addr_.ipaddr());
- std::unique_ptr<rtc::TestClient> in(CreateTCPTestClient(client_.release()));
- std::unique_ptr<rtc::TestClient> out(
+ std::unique_ptr<webrtc::TestClient> in(
+ CreateTCPTestClient(client_.release()));
+ std::unique_ptr<webrtc::TestClient> out(
CreateTCPTestClient(accepted_.release()));
const char* buf = "test_packet";
diff --git a/rtc_base/socket_unittest.cc b/rtc_base/socket_unittest.cc
index 11b40ee..6fe4b07 100644
--- a/rtc_base/socket_unittest.cc
+++ b/rtc_base/socket_unittest.cc
@@ -1060,9 +1060,9 @@
delete socket;
// Test send/receive behavior.
- auto client1 = std::make_unique<TestClient>(
+ auto client1 = std::make_unique<webrtc::TestClient>(
absl::WrapUnique(AsyncUDPSocket::Create(socket_factory_, addr1)));
- auto client2 = std::make_unique<TestClient>(
+ auto client2 = std::make_unique<webrtc::TestClient>(
absl::WrapUnique(AsyncUDPSocket::Create(socket_factory_, empty)));
SocketAddress addr2;
@@ -1075,7 +1075,7 @@
EXPECT_EQ(addr3, addr1);
// TODO: figure out what the intent is here
for (int i = 0; i < 10; ++i) {
- client2 = std::make_unique<TestClient>(
+ client2 = std::make_unique<webrtc::TestClient>(
absl::WrapUnique(AsyncUDPSocket::Create(socket_factory_, empty)));
SocketAddress addr4;
@@ -1102,7 +1102,7 @@
SocketAddress test_addr(dest, 2345);
// Test send
- auto client = std::make_unique<TestClient>(
+ auto client = std::make_unique<webrtc::TestClient>(
absl::WrapUnique(AsyncUDPSocket::Create(socket_factory_, empty)));
int test_packet_size = 1200;
std::unique_ptr<char[]> test_packet(new char[test_packet_size]);
@@ -1282,20 +1282,22 @@
SocketAddress address = socket->GetLocalAddress();
socket = nullptr;
- auto client1 = std::make_unique<TestClient>(
+ auto client1 = std::make_unique<webrtc::TestClient>(
absl::WrapUnique(AsyncUDPSocket::Create(socket_factory_, address)));
- auto client2 = std::make_unique<TestClient>(
+ auto client2 = std::make_unique<webrtc::TestClient>(
absl::WrapUnique(AsyncUDPSocket::Create(socket_factory_, empty)));
SocketAddress addr2;
client2->SendTo("foo", 3, address);
- std::unique_ptr<TestClient::Packet> packet_1 = client1->NextPacket(10000);
+ std::unique_ptr<webrtc::TestClient::Packet> packet_1 =
+ client1->NextPacket(10000);
ASSERT_TRUE(packet_1 != nullptr);
EXPECT_NEAR(packet_1->packet_time->us(), rtc::TimeMicros(), 1000'000);
Thread::SleepMs(100);
client2->SendTo("bar", 3, address);
- std::unique_ptr<TestClient::Packet> packet_2 = client1->NextPacket(10000);
+ std::unique_ptr<webrtc::TestClient::Packet> packet_2 =
+ client1->NextPacket(10000);
ASSERT_TRUE(packet_2 != nullptr);
EXPECT_GT(packet_2->packet_time->us(), packet_1->packet_time->us());
EXPECT_NEAR(packet_2->packet_time->us(), rtc::TimeMicros(), 1000'000);
diff --git a/rtc_base/test_client.cc b/rtc_base/test_client.cc
index 062da32..58409ea 100644
--- a/rtc_base/test_client.cc
+++ b/rtc_base/test_client.cc
@@ -28,17 +28,17 @@
#include "rtc_base/thread.h"
#include "rtc_base/time_utils.h"
-namespace rtc {
+namespace webrtc {
// DESIGN: Each packet received is put it into a list of packets.
// Callers can retrieve received packets from any thread by calling
// NextPacket.
-TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket)
+TestClient::TestClient(std::unique_ptr<rtc::AsyncPacketSocket> socket)
: TestClient(std::move(socket), nullptr) {}
-TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket,
- ThreadProcessingFakeClock* fake_clock)
+TestClient::TestClient(std::unique_ptr<rtc::AsyncPacketSocket> socket,
+ rtc::ThreadProcessingFakeClock* fake_clock)
: fake_clock_(fake_clock), socket_(std::move(socket)) {
socket_->RegisterReceivedPacketCallback(
[&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) {
@@ -49,10 +49,10 @@
TestClient::~TestClient() {}
-bool TestClient::CheckConnState(AsyncPacketSocket::State state) {
+bool TestClient::CheckConnState(rtc::AsyncPacketSocket::State state) {
// Wait for our timeout value until the socket reaches the desired state.
- int64_t end = TimeAfter(kTimeoutMs);
- while (socket_->GetState() != state && TimeUntil(end) > 0) {
+ int64_t end = rtc::TimeAfter(kTimeoutMs);
+ while (socket_->GetState() != state && rtc::TimeUntil(end) > 0) {
AdvanceTime(1);
}
return (socket_->GetState() == state);
@@ -65,7 +65,7 @@
int TestClient::SendTo(const char* buf,
size_t size,
- const SocketAddress& dest) {
+ const rtc::SocketAddress& dest) {
rtc::PacketOptions options;
return socket_->SendTo(buf, size, dest, options);
}
@@ -82,10 +82,10 @@
// Pumping another thread's queue could lead to messages being dispatched from
// the wrong thread to non-thread-safe objects.
- int64_t end = TimeAfter(timeout_ms);
- while (TimeUntil(end) > 0) {
+ int64_t end = rtc::TimeAfter(timeout_ms);
+ while (rtc::TimeUntil(end) > 0) {
{
- webrtc::MutexLock lock(&mutex_);
+ MutexLock lock(&mutex_);
if (!packets_.empty()) {
break;
}
@@ -95,7 +95,7 @@
// Return the first packet placed in the queue.
std::unique_ptr<Packet> packet;
- webrtc::MutexLock lock(&mutex_);
+ MutexLock lock(&mutex_);
if (!packets_.empty()) {
packet = std::move(packets_.front());
packets_.erase(packets_.begin());
@@ -106,7 +106,7 @@
bool TestClient::CheckNextPacket(const char* buf,
size_t size,
- SocketAddress* addr) {
+ rtc::SocketAddress* addr) {
bool res = false;
std::unique_ptr<Packet> packet = NextPacket(kTimeoutMs);
if (packet) {
@@ -119,8 +119,7 @@
return res;
}
-bool TestClient::CheckTimestamp(
- std::optional<webrtc::Timestamp> packet_timestamp) {
+bool TestClient::CheckTimestamp(std::optional<Timestamp> packet_timestamp) {
bool res = true;
if (!packet_timestamp) {
res = false;
@@ -142,7 +141,7 @@
fake_clock_->AdvanceTime(webrtc ::TimeDelta ::Millis(1));
};
} else {
- Thread::Current()->ProcessMessages(1);
+ rtc::Thread::Current()->ProcessMessages(1);
}
}
@@ -154,17 +153,17 @@
return socket_->GetError();
}
-int TestClient::SetOption(Socket::Option opt, int value) {
+int TestClient::SetOption(rtc::Socket::Option opt, int value) {
return socket_->SetOption(opt, value);
}
-void TestClient::OnPacket(AsyncPacketSocket* socket,
+void TestClient::OnPacket(rtc::AsyncPacketSocket* socket,
const rtc::ReceivedPacket& received_packet) {
- webrtc::MutexLock lock(&mutex_);
+ MutexLock lock(&mutex_);
packets_.push_back(std::make_unique<Packet>(received_packet));
}
-void TestClient::OnReadyToSend(AsyncPacketSocket* socket) {
+void TestClient::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
++ready_to_send_count_;
}
@@ -179,4 +178,4 @@
buf(p.buf.data(), p.buf.size()),
packet_time(p.packet_time) {}
-} // namespace rtc
+} // namespace webrtc
diff --git a/rtc_base/test_client.h b/rtc_base/test_client.h
index 56c4b7b..1915c80 100644
--- a/rtc_base/test_client.h
+++ b/rtc_base/test_client.h
@@ -21,7 +21,7 @@
#include "rtc_base/network/received_packet.h"
#include "rtc_base/synchronization/mutex.h"
-namespace rtc {
+namespace webrtc {
// A simple client that can send TCP or UDP data and check that it receives
// what it expects to receive. Useful for testing server functionality.
@@ -32,9 +32,9 @@
Packet(const rtc::ReceivedPacket& received_packet);
Packet(const Packet& p);
- SocketAddress addr;
+ rtc::SocketAddress addr;
Buffer buf;
- std::optional<webrtc::Timestamp> packet_time;
+ std::optional<Timestamp> packet_time;
};
// Default timeout for NextPacket reads.
@@ -42,33 +42,35 @@
// Creates a client that will send and receive with the given socket and
// will post itself messages with the given thread.
- explicit TestClient(std::unique_ptr<AsyncPacketSocket> socket);
+ explicit TestClient(std::unique_ptr<rtc::AsyncPacketSocket> socket);
// Create a test client that will use a fake clock. NextPacket needs to wait
// for a packet to be received, and thus it needs to advance the fake clock
// if the test is using one, rather than just sleeping.
- TestClient(std::unique_ptr<AsyncPacketSocket> socket,
- ThreadProcessingFakeClock* fake_clock);
+ TestClient(std::unique_ptr<rtc::AsyncPacketSocket> socket,
+ rtc::ThreadProcessingFakeClock* fake_clock);
~TestClient() override;
TestClient(const TestClient&) = delete;
TestClient& operator=(const TestClient&) = delete;
- SocketAddress address() const { return socket_->GetLocalAddress(); }
- SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
+ rtc::SocketAddress address() const { return socket_->GetLocalAddress(); }
+ rtc::SocketAddress remote_address() const {
+ return socket_->GetRemoteAddress();
+ }
// Checks that the socket moves to the specified connect state.
- bool CheckConnState(AsyncPacketSocket::State state);
+ bool CheckConnState(rtc::AsyncPacketSocket::State state);
// Checks that the socket is connected to the remote side.
bool CheckConnected() {
- return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
+ return CheckConnState(rtc::AsyncPacketSocket::STATE_CONNECTED);
}
// Sends using the clients socket.
int Send(const char* buf, size_t size);
// Sends using the clients socket to the given destination.
- int SendTo(const char* buf, size_t size, const SocketAddress& dest);
+ int SendTo(const char* buf, size_t size, const rtc::SocketAddress& dest);
// Returns the next packet received by the client or null if none is received
// within the specified timeout.
@@ -76,13 +78,13 @@
// Checks that the next packet has the given contents. Returns the remote
// address that the packet was sent from.
- bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
+ bool CheckNextPacket(const char* buf, size_t len, rtc::SocketAddress* addr);
// Checks that no packets have arrived or will arrive in the next second.
bool CheckNoPacket();
int GetError();
- int SetOption(Socket::Option opt, int value);
+ int SetOption(rtc::Socket::Option opt, int value);
bool ready_to_send() const { return ready_to_send_count() > 0; }
@@ -93,22 +95,28 @@
// Timeout for reads when no packet is expected.
static const int kNoPacketTimeoutMs = 1000;
// Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
- Socket::ConnState GetState();
+ rtc::Socket::ConnState GetState();
- void OnPacket(AsyncPacketSocket* socket,
+ void OnPacket(rtc::AsyncPacketSocket* socket,
const rtc::ReceivedPacket& received_packet);
- void OnReadyToSend(AsyncPacketSocket* socket);
- bool CheckTimestamp(std::optional<webrtc::Timestamp> packet_timestamp);
+ void OnReadyToSend(rtc::AsyncPacketSocket* socket);
+ bool CheckTimestamp(std::optional<Timestamp> packet_timestamp);
void AdvanceTime(int ms);
- ThreadProcessingFakeClock* fake_clock_ = nullptr;
- webrtc::Mutex mutex_;
- std::unique_ptr<AsyncPacketSocket> socket_;
+ rtc::ThreadProcessingFakeClock* fake_clock_ = nullptr;
+ Mutex mutex_;
+ std::unique_ptr<rtc::AsyncPacketSocket> socket_;
std::vector<std::unique_ptr<Packet>> packets_;
int ready_to_send_count_ = 0;
- std::optional<webrtc::Timestamp> prev_packet_timestamp_;
+ std::optional<Timestamp> prev_packet_timestamp_;
};
+} // namespace webrtc
+
+// Re-export symbols from the webrtc namespace for backwards compatibility.
+// TODO(bugs.webrtc.org/4222596): Remove once all references are updated.
+namespace rtc {
+using ::webrtc::TestClient;
} // namespace rtc
#endif // RTC_BASE_TEST_CLIENT_H_
diff --git a/rtc_base/test_client_unittest.cc b/rtc_base/test_client_unittest.cc
index 3bb79fc..3b3a8e0 100644
--- a/rtc_base/test_client_unittest.cc
+++ b/rtc_base/test_client_unittest.cc
@@ -24,47 +24,49 @@
#include "rtc_base/thread.h"
#include "test/gtest.h"
-namespace rtc {
-namespace {
-
#define MAYBE_SKIP_IPV4 \
- if (!HasIPv4Enabled()) { \
+ if (!::rtc::HasIPv4Enabled()) { \
RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
return; \
}
#define MAYBE_SKIP_IPV6 \
- if (!HasIPv6Enabled()) { \
+ if (!::rtc::HasIPv6Enabled()) { \
RTC_LOG(LS_INFO) << "No IPv6... skipping"; \
return; \
}
-void TestUdpInternal(const SocketAddress& loopback) {
+namespace webrtc {
+namespace {
+
+void TestUdpInternal(const rtc::SocketAddress& loopback) {
rtc::PhysicalSocketServer socket_server;
rtc::AutoSocketServerThread main_thread(&socket_server);
- Socket* socket = socket_server.CreateSocket(loopback.family(), SOCK_DGRAM);
+ rtc::Socket* socket =
+ socket_server.CreateSocket(loopback.family(), SOCK_DGRAM);
socket->Bind(loopback);
- TestClient client(std::make_unique<AsyncUDPSocket>(socket));
- SocketAddress addr = client.address(), from;
+ TestClient client(std::make_unique<rtc::AsyncUDPSocket>(socket));
+ rtc::SocketAddress addr = client.address(), from;
EXPECT_EQ(3, client.SendTo("foo", 3, addr));
EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
EXPECT_EQ(from, addr);
EXPECT_TRUE(client.CheckNoPacket());
}
-void TestTcpInternal(const SocketAddress& loopback) {
+void TestTcpInternal(const rtc::SocketAddress& loopback) {
rtc::PhysicalSocketServer socket_server;
rtc::AutoSocketServerThread main_thread(&socket_server);
webrtc::TestEchoServer server(&main_thread, loopback);
- Socket* socket = socket_server.CreateSocket(loopback.family(), SOCK_STREAM);
- std::unique_ptr<AsyncTCPSocket> tcp_socket = absl::WrapUnique(
- AsyncTCPSocket::Create(socket, loopback, server.address()));
+ rtc::Socket* socket =
+ socket_server.CreateSocket(loopback.family(), SOCK_STREAM);
+ std::unique_ptr<rtc::AsyncTCPSocket> tcp_socket = absl::WrapUnique(
+ rtc::AsyncTCPSocket::Create(socket, loopback, server.address()));
ASSERT_TRUE(tcp_socket != nullptr);
TestClient client(std::move(tcp_socket));
- SocketAddress addr = client.address(), from;
+ rtc::SocketAddress addr = client.address(), from;
EXPECT_TRUE(client.CheckConnected());
EXPECT_EQ(3, client.Send("foo", 3));
EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
@@ -75,7 +77,7 @@
// Tests whether the TestClient can send UDP to itself.
TEST(TestClientTest, TestUdpIPv4) {
MAYBE_SKIP_IPV4;
- TestUdpInternal(SocketAddress("127.0.0.1", 0));
+ TestUdpInternal(rtc::SocketAddress("127.0.0.1", 0));
}
#if defined(WEBRTC_LINUX)
@@ -85,13 +87,13 @@
#endif
TEST(TestClientTest, MAYBE_TestUdpIPv6) {
MAYBE_SKIP_IPV6;
- TestUdpInternal(SocketAddress("::1", 0));
+ TestUdpInternal(rtc::SocketAddress("::1", 0));
}
// Tests whether the TestClient can connect to a server and exchange data.
TEST(TestClientTest, TestTcpIPv4) {
MAYBE_SKIP_IPV4;
- TestTcpInternal(SocketAddress("127.0.0.1", 0));
+ TestTcpInternal(rtc::SocketAddress("127.0.0.1", 0));
}
#if defined(WEBRTC_LINUX)
@@ -101,8 +103,8 @@
#endif
TEST(TestClientTest, MAYBE_TestTcpIPv6) {
MAYBE_SKIP_IPV6;
- TestTcpInternal(SocketAddress("::1", 0));
+ TestTcpInternal(rtc::SocketAddress("::1", 0));
}
} // namespace
-} // namespace rtc
+} // namespace webrtc
diff --git a/rtc_base/virtual_socket_unittest.cc b/rtc_base/virtual_socket_unittest.cc
index 13e267e..8c43b57 100644
--- a/rtc_base/virtual_socket_unittest.cc
+++ b/rtc_base/virtual_socket_unittest.cc
@@ -183,7 +183,7 @@
socket->Bind(EmptySocketAddressWithFamily(default_address.family()));
SocketAddress client1_any_addr = socket->GetLocalAddress();
EXPECT_TRUE(client1_any_addr.IsAnyIP());
- auto client1 = std::make_unique<TestClient>(
+ auto client1 = std::make_unique<webrtc::TestClient>(
std::make_unique<AsyncUDPSocket>(socket), &fake_clock_);
// Create client2 bound to the address route.
@@ -191,7 +191,7 @@
socket2->Bind(SocketAddress(default_address, 0));
SocketAddress client2_addr = socket2->GetLocalAddress();
EXPECT_FALSE(client2_addr.IsAnyIP());
- auto client2 = std::make_unique<TestClient>(
+ auto client2 = std::make_unique<webrtc::TestClient>(
std::make_unique<AsyncUDPSocket>(socket2), &fake_clock_);
// Client1 sends to client2, client2 should see the default address as
@@ -214,10 +214,10 @@
// Make sure VSS didn't switch families on us.
EXPECT_EQ(server_addr.family(), initial_addr.family());
- auto client1 = std::make_unique<TestClient>(
+ auto client1 = std::make_unique<webrtc::TestClient>(
std::make_unique<AsyncUDPSocket>(socket), &fake_clock_);
Socket* socket2 = ss_.CreateSocket(initial_addr.family(), SOCK_DGRAM);
- auto client2 = std::make_unique<TestClient>(
+ auto client2 = std::make_unique<webrtc::TestClient>(
std::make_unique<AsyncUDPSocket>(socket2), &fake_clock_);
SocketAddress client2_addr;
@@ -231,7 +231,7 @@
SocketAddress empty = EmptySocketAddressWithFamily(initial_addr.family());
for (int i = 0; i < 10; i++) {
- client2 = std::make_unique<TestClient>(
+ client2 = std::make_unique<webrtc::TestClient>(
absl::WrapUnique(AsyncUDPSocket::Create(&ss_, empty)), &fake_clock_);
SocketAddress next_client2_addr;
@@ -814,12 +814,12 @@
Socket* socket = ss_.CreateSocket(AF_INET, SOCK_DGRAM);
socket->Bind(server_addr);
SocketAddress bound_server_addr = socket->GetLocalAddress();
- auto client1 = std::make_unique<TestClient>(
+ auto client1 = std::make_unique<webrtc::TestClient>(
std::make_unique<AsyncUDPSocket>(socket), &fake_clock_);
Socket* socket2 = ss_.CreateSocket(AF_INET, SOCK_DGRAM);
socket2->Bind(client_addr);
- auto client2 = std::make_unique<TestClient>(
+ auto client2 = std::make_unique<webrtc::TestClient>(
std::make_unique<AsyncUDPSocket>(socket2), &fake_clock_);
SocketAddress client2_addr;
@@ -1024,7 +1024,7 @@
absl::WrapUnique(ss_.CreateSocket(kIPv4AnyAddress.family(), SOCK_DGRAM));
socket1->Bind(kIPv4AnyAddress);
socket2->Bind(kIPv4AnyAddress);
- auto client1 = std::make_unique<TestClient>(
+ auto client1 = std::make_unique<webrtc::TestClient>(
std::make_unique<AsyncUDPSocket>(socket1), &fake_clock_);
ss_.SetSendingBlocked(true);