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

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

Issue 2284903002: Adding ability to simulate EWOULDBLOCK/SignalReadyToSend. (Closed)
Patch Set: Created 4 years, 3 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 2006 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2006 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
(...skipping 1000 matching lines...) Expand 10 before | Expand all | Expand 10 after
1011 SocketAddress("::ffff:127.0.0.2", 5000), 1011 SocketAddress("::ffff:127.0.0.2", 5000),
1012 true); 1012 true);
1013 } 1013 }
1014 1014
1015 TEST_F(VirtualSocketServerTest, CanSendDatagramFromUnboundIPv6ToIPv4Any) { 1015 TEST_F(VirtualSocketServerTest, CanSendDatagramFromUnboundIPv6ToIPv4Any) {
1016 CrossFamilyDatagramTest(SocketAddress("::", 0), 1016 CrossFamilyDatagramTest(SocketAddress("::", 0),
1017 SocketAddress("0.0.0.0", 5000), 1017 SocketAddress("0.0.0.0", 5000),
1018 true); 1018 true);
1019 } 1019 }
1020 1020
1021 TEST_F(VirtualSocketServerTest, SetSendingBlockedWithUdpSocket) {
1022 AsyncSocket* socket1 =
1023 ss_->CreateAsyncSocket(kIPv4AnyAddress.family(), SOCK_DGRAM);
1024 AsyncSocket* socket2 =
1025 ss_->CreateAsyncSocket(kIPv4AnyAddress.family(), SOCK_DGRAM);
1026 socket1->Bind(kIPv4AnyAddress);
1027 socket2->Bind(kIPv4AnyAddress);
1028 TestClient* client1 = new TestClient(new AsyncUDPSocket(socket1));
1029
1030 ss_->SetSendingBlocked(true);
1031 EXPECT_EQ(-1, client1->SendTo("foo", 3, socket2->GetLocalAddress()));
1032 EXPECT_TRUE(socket1->IsBlocking());
1033 EXPECT_EQ(0, client1->ready_to_send_count());
1034
1035 ss_->SetSendingBlocked(false);
1036 EXPECT_EQ(1, client1->ready_to_send_count());
1037 EXPECT_EQ(3, client1->SendTo("foo", 3, socket2->GetLocalAddress()));
1038 }
1039
1040 TEST_F(VirtualSocketServerTest, SetSendingBlockedWithTcpSocket) {
1041 constexpr size_t kBufferSize = 1024;
1042 ss_->set_send_buffer_capacity(kBufferSize);
1043 ss_->set_recv_buffer_capacity(kBufferSize);
1044
1045 testing::StreamSink sink;
1046 AsyncSocket* socket1 =
1047 ss_->CreateAsyncSocket(kIPv4AnyAddress.family(), SOCK_STREAM);
1048 AsyncSocket* socket2 =
1049 ss_->CreateAsyncSocket(kIPv4AnyAddress.family(), SOCK_STREAM);
1050 sink.Monitor(socket1);
1051 sink.Monitor(socket2);
1052 socket1->Bind(kIPv4AnyAddress);
1053 socket2->Bind(kIPv4AnyAddress);
1054
1055 // Connect sockets.
1056 EXPECT_EQ(0, socket1->Connect(socket2->GetLocalAddress()));
1057 EXPECT_EQ(0, socket2->Connect(socket1->GetLocalAddress()));
1058 ss_->ProcessMessagesUntilIdle();
1059
1060 char data[kBufferSize] = {};
1061
1062 // First Send call will fill the send buffer but not send anything.
1063 ss_->SetSendingBlocked(true);
1064 EXPECT_EQ(static_cast<int>(kBufferSize), socket1->Send(data, kBufferSize));
1065 ss_->ProcessMessagesUntilIdle();
1066 EXPECT_FALSE(sink.Check(socket1, testing::SSE_WRITE));
1067 EXPECT_FALSE(sink.Check(socket2, testing::SSE_READ));
1068 EXPECT_FALSE(socket1->IsBlocking());
1069
1070 // Since the send buffer is full, next Send will result in EWOULDBLOCK.
1071 EXPECT_EQ(-1, socket1->Send(data, kBufferSize));
1072 EXPECT_FALSE(sink.Check(socket1, testing::SSE_WRITE));
1073 EXPECT_FALSE(sink.Check(socket2, testing::SSE_READ));
1074 EXPECT_TRUE(socket1->IsBlocking());
1075
1076 // When sending is unblocked, the buffered data should be sent and
1077 // SignalWriteEvent should fire.
1078 ss_->SetSendingBlocked(false);
1079 ss_->ProcessMessagesUntilIdle();
1080 EXPECT_TRUE(sink.Check(socket1, testing::SSE_WRITE));
1081 EXPECT_TRUE(sink.Check(socket2, testing::SSE_READ));
1082 }
1083
1021 TEST_F(VirtualSocketServerTest, CreatesStandardDistribution) { 1084 TEST_F(VirtualSocketServerTest, CreatesStandardDistribution) {
1022 const uint32_t kTestMean[] = {10, 100, 333, 1000}; 1085 const uint32_t kTestMean[] = {10, 100, 333, 1000};
1023 const double kTestDev[] = { 0.25, 0.1, 0.01 }; 1086 const double kTestDev[] = { 0.25, 0.1, 0.01 };
1024 // TODO: The current code only works for 1000 data points or more. 1087 // TODO(deadbeef): The current code only works for 1000 data points or more.
1025 const uint32_t kTestSamples[] = {/*10, 100,*/ 1000}; 1088 const uint32_t kTestSamples[] = {/*10, 100,*/ 1000};
1026 for (size_t midx = 0; midx < arraysize(kTestMean); ++midx) { 1089 for (size_t midx = 0; midx < arraysize(kTestMean); ++midx) {
1027 for (size_t didx = 0; didx < arraysize(kTestDev); ++didx) { 1090 for (size_t didx = 0; didx < arraysize(kTestDev); ++didx) {
1028 for (size_t sidx = 0; sidx < arraysize(kTestSamples); ++sidx) { 1091 for (size_t sidx = 0; sidx < arraysize(kTestSamples); ++sidx) {
1029 ASSERT_LT(0u, kTestSamples[sidx]); 1092 ASSERT_LT(0u, kTestSamples[sidx]);
1030 const uint32_t kStdDev = 1093 const uint32_t kStdDev =
1031 static_cast<uint32_t>(kTestDev[didx] * kTestMean[midx]); 1094 static_cast<uint32_t>(kTestDev[didx] * kTestMean[midx]);
1032 VirtualSocketServer::Function* f = 1095 VirtualSocketServer::Function* f =
1033 VirtualSocketServer::CreateDistribution(kTestMean[midx], 1096 VirtualSocketServer::CreateDistribution(kTestMean[midx],
1034 kStdDev, 1097 kStdDev,
(...skipping 17 matching lines...) Expand all
1052 << " N=" << kTestSamples[sidx]; 1115 << " N=" << kTestSamples[sidx];
1053 EXPECT_NEAR(kStdDev, stddev, 0.1 * kStdDev) 1116 EXPECT_NEAR(kStdDev, stddev, 0.1 * kStdDev)
1054 << "M=" << kTestMean[midx] 1117 << "M=" << kTestMean[midx]
1055 << " SD=" << kStdDev 1118 << " SD=" << kStdDev
1056 << " N=" << kTestSamples[sidx]; 1119 << " N=" << kTestSamples[sidx];
1057 delete f; 1120 delete f;
1058 } 1121 }
1059 } 1122 }
1060 } 1123 }
1061 } 1124 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698