Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: webrtc/base/socket_unittest.cc

Issue 2859373003: Refactor TestClient to use std::unique_ptr, and fix VirtualSocketServerTest leaks. (Closed)
Patch Set: git cl format Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2007 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2007 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <memory> 11 #include <memory>
12 12
13 #include "webrtc/base/socket_unittest.h" 13 #include "webrtc/base/socket_unittest.h"
14 14
15 #include "webrtc/base/arraysize.h" 15 #include "webrtc/base/arraysize.h"
16 #include "webrtc/base/asyncudpsocket.h"
16 #include "webrtc/base/buffer.h" 17 #include "webrtc/base/buffer.h"
17 #include "webrtc/base/asyncudpsocket.h"
18 #include "webrtc/base/gunit.h" 18 #include "webrtc/base/gunit.h"
19 #include "webrtc/base/nethelpers.h" 19 #include "webrtc/base/nethelpers.h"
20 #include "webrtc/base/ptr_util.h"
20 #include "webrtc/base/socketserver.h" 21 #include "webrtc/base/socketserver.h"
21 #include "webrtc/base/testclient.h" 22 #include "webrtc/base/testclient.h"
22 #include "webrtc/base/testutils.h" 23 #include "webrtc/base/testutils.h"
23 #include "webrtc/base/thread.h" 24 #include "webrtc/base/thread.h"
24 25
25 namespace rtc { 26 namespace rtc {
26 27
27 using webrtc::testing::SSE_CLOSE; 28 using webrtc::testing::SSE_CLOSE;
28 using webrtc::testing::SSE_ERROR; 29 using webrtc::testing::SSE_ERROR;
29 using webrtc::testing::SSE_OPEN; 30 using webrtc::testing::SSE_OPEN;
(...skipping 866 matching lines...) Expand 10 before | Expand all | Expand 10 after
896 EXPECT_EQ(0, socket->Bind(SocketAddress(loopback, 0))); 897 EXPECT_EQ(0, socket->Bind(SocketAddress(loopback, 0)));
897 SocketAddress addr1 = socket->GetLocalAddress(); 898 SocketAddress addr1 = socket->GetLocalAddress();
898 EXPECT_EQ(0, socket->Connect(addr1)); 899 EXPECT_EQ(0, socket->Connect(addr1));
899 EXPECT_EQ(AsyncSocket::CS_CONNECTED, socket->GetState()); 900 EXPECT_EQ(AsyncSocket::CS_CONNECTED, socket->GetState());
900 socket->Close(); 901 socket->Close();
901 EXPECT_EQ(AsyncSocket::CS_CLOSED, socket->GetState()); 902 EXPECT_EQ(AsyncSocket::CS_CLOSED, socket->GetState());
902 delete socket; 903 delete socket;
903 904
904 // Test send/receive behavior. 905 // Test send/receive behavior.
905 std::unique_ptr<TestClient> client1( 906 std::unique_ptr<TestClient> client1(
906 new TestClient(AsyncUDPSocket::Create(ss_, addr1))); 907 new TestClient(WrapUnique(AsyncUDPSocket::Create(ss_, addr1))));
907 std::unique_ptr<TestClient> client2( 908 std::unique_ptr<TestClient> client2(
908 new TestClient(AsyncUDPSocket::Create(ss_, empty))); 909 new TestClient(WrapUnique(AsyncUDPSocket::Create(ss_, empty))));
909 910
910 SocketAddress addr2; 911 SocketAddress addr2;
911 EXPECT_EQ(3, client2->SendTo("foo", 3, addr1)); 912 EXPECT_EQ(3, client2->SendTo("foo", 3, addr1));
912 EXPECT_TRUE(client1->CheckNextPacket("foo", 3, &addr2)); 913 EXPECT_TRUE(client1->CheckNextPacket("foo", 3, &addr2));
913 914
914 SocketAddress addr3; 915 SocketAddress addr3;
915 EXPECT_EQ(6, client1->SendTo("bizbaz", 6, addr2)); 916 EXPECT_EQ(6, client1->SendTo("bizbaz", 6, addr2));
916 EXPECT_TRUE(client2->CheckNextPacket("bizbaz", 6, &addr3)); 917 EXPECT_TRUE(client2->CheckNextPacket("bizbaz", 6, &addr3));
917 EXPECT_EQ(addr3, addr1); 918 EXPECT_EQ(addr3, addr1);
918 // TODO: figure out what the intent is here 919 // TODO: figure out what the intent is here
919 for (int i = 0; i < 10; ++i) { 920 for (int i = 0; i < 10; ++i) {
920 client2.reset(new TestClient(AsyncUDPSocket::Create(ss_, empty))); 921 client2.reset(
922 new TestClient(WrapUnique(AsyncUDPSocket::Create(ss_, empty))));
921 923
922 SocketAddress addr4; 924 SocketAddress addr4;
923 EXPECT_EQ(3, client2->SendTo("foo", 3, addr1)); 925 EXPECT_EQ(3, client2->SendTo("foo", 3, addr1));
924 EXPECT_TRUE(client1->CheckNextPacket("foo", 3, &addr4)); 926 EXPECT_TRUE(client1->CheckNextPacket("foo", 3, &addr4));
925 EXPECT_EQ(addr4.ipaddr(), addr2.ipaddr()); 927 EXPECT_EQ(addr4.ipaddr(), addr2.ipaddr());
926 928
927 SocketAddress addr5; 929 SocketAddress addr5;
928 EXPECT_EQ(6, client1->SendTo("bizbaz", 6, addr4)); 930 EXPECT_EQ(6, client1->SendTo("bizbaz", 6, addr4));
929 EXPECT_TRUE(client2->CheckNextPacket("bizbaz", 6, &addr5)); 931 EXPECT_TRUE(client2->CheckNextPacket("bizbaz", 6, &addr5));
930 EXPECT_EQ(addr5, addr1); 932 EXPECT_EQ(addr5, addr1);
931 933
932 addr2 = addr4; 934 addr2 = addr4;
933 } 935 }
934 } 936 }
935 937
936 void SocketTest::UdpReadyToSend(const IPAddress& loopback) { 938 void SocketTest::UdpReadyToSend(const IPAddress& loopback) {
937 SocketAddress empty = EmptySocketAddressWithFamily(loopback.family()); 939 SocketAddress empty = EmptySocketAddressWithFamily(loopback.family());
938 // RFC 5737 - The blocks 192.0.2.0/24 (TEST-NET-1) ... are provided for use in 940 // RFC 5737 - The blocks 192.0.2.0/24 (TEST-NET-1) ... are provided for use in
939 // documentation. 941 // documentation.
940 // RFC 3849 - 2001:DB8::/32 as a documentation-only prefix. 942 // RFC 3849 - 2001:DB8::/32 as a documentation-only prefix.
941 std::string dest = (loopback.family() == AF_INET6) ? 943 std::string dest = (loopback.family() == AF_INET6) ?
942 "2001:db8::1" : "192.0.2.0"; 944 "2001:db8::1" : "192.0.2.0";
943 SocketAddress test_addr(dest, 2345); 945 SocketAddress test_addr(dest, 2345);
944 946
945 // Test send 947 // Test send
946 std::unique_ptr<TestClient> client( 948 std::unique_ptr<TestClient> client(
947 new TestClient(AsyncUDPSocket::Create(ss_, empty))); 949 new TestClient(WrapUnique(AsyncUDPSocket::Create(ss_, empty))));
948 int test_packet_size = 1200; 950 int test_packet_size = 1200;
949 std::unique_ptr<char[]> test_packet(new char[test_packet_size]); 951 std::unique_ptr<char[]> test_packet(new char[test_packet_size]);
950 // Init the test packet just to avoid memcheck warning. 952 // Init the test packet just to avoid memcheck warning.
951 memset(test_packet.get(), 0, test_packet_size); 953 memset(test_packet.get(), 0, test_packet_size);
952 // Set the send buffer size to the same size as the test packet to have a 954 // Set the send buffer size to the same size as the test packet to have a
953 // better chance to get EWOULDBLOCK. 955 // better chance to get EWOULDBLOCK.
954 int send_buffer_size = test_packet_size; 956 int send_buffer_size = test_packet_size;
955 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) 957 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
956 send_buffer_size /= 2; 958 send_buffer_size /= 2;
957 #endif 959 #endif
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
1059 socket->RecvFrom(buffer, 3, nullptr, &recv_timestamp_2); 1061 socket->RecvFrom(buffer, 3, nullptr, &recv_timestamp_2);
1060 1062
1061 int64_t system_time_diff = send_time_2 - send_time_1; 1063 int64_t system_time_diff = send_time_2 - send_time_1;
1062 int64_t recv_timestamp_diff = recv_timestamp_2 - recv_timestamp_1; 1064 int64_t recv_timestamp_diff = recv_timestamp_2 - recv_timestamp_1;
1063 // Compare against the system time at the point of sending, because 1065 // Compare against the system time at the point of sending, because
1064 // SleepMs may not sleep for exactly the requested time. 1066 // SleepMs may not sleep for exactly the requested time.
1065 EXPECT_NEAR(system_time_diff, recv_timestamp_diff, 10000); 1067 EXPECT_NEAR(system_time_diff, recv_timestamp_diff, 10000);
1066 } 1068 }
1067 1069
1068 } // namespace rtc 1070 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698