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

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

Issue 1362503003: Use suffixed {uint,int}{8,16,32,64}_t types. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: rebase + revert basictypes.h (to be landed separately just in case of a revert due to unexpected us… Created 5 years, 2 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
« no previous file with comments | « webrtc/base/unixfilesystem.cc ('k') | webrtc/base/virtualsocketserver.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
11 #include <math.h> 11 #include <math.h>
12 #include <time.h> 12 #include <time.h>
13 #if defined(WEBRTC_POSIX) 13 #if defined(WEBRTC_POSIX)
14 #include <netinet/in.h> 14 #include <netinet/in.h>
15 #endif 15 #endif
16 16
17 #include "webrtc/base/logging.h" 17 #include "webrtc/base/logging.h"
18 #include "webrtc/base/gunit.h" 18 #include "webrtc/base/gunit.h"
19 #include "webrtc/base/testclient.h" 19 #include "webrtc/base/testclient.h"
20 #include "webrtc/base/testutils.h" 20 #include "webrtc/base/testutils.h"
21 #include "webrtc/base/thread.h" 21 #include "webrtc/base/thread.h"
22 #include "webrtc/base/timeutils.h" 22 #include "webrtc/base/timeutils.h"
23 #include "webrtc/base/virtualsocketserver.h" 23 #include "webrtc/base/virtualsocketserver.h"
24 #include "webrtc/test/testsupport/gtest_disable.h" 24 #include "webrtc/test/testsupport/gtest_disable.h"
25 25
26 using namespace rtc; 26 using namespace rtc;
27 27
28 // Sends at a constant rate but with random packet sizes. 28 // Sends at a constant rate but with random packet sizes.
29 struct Sender : public MessageHandler { 29 struct Sender : public MessageHandler {
30 Sender(Thread* th, AsyncSocket* s, uint32 rt) 30 Sender(Thread* th, AsyncSocket* s, uint32_t rt)
31 : thread(th), socket(new AsyncUDPSocket(s)), 31 : thread(th),
32 done(false), rate(rt), count(0) { 32 socket(new AsyncUDPSocket(s)),
33 done(false),
34 rate(rt),
35 count(0) {
33 last_send = rtc::Time(); 36 last_send = rtc::Time();
34 thread->PostDelayed(NextDelay(), this, 1); 37 thread->PostDelayed(NextDelay(), this, 1);
35 } 38 }
36 39
37 uint32 NextDelay() { 40 uint32_t NextDelay() {
38 uint32 size = (rand() % 4096) + 1; 41 uint32_t size = (rand() % 4096) + 1;
39 return 1000 * size / rate; 42 return 1000 * size / rate;
40 } 43 }
41 44
42 void OnMessage(Message* pmsg) { 45 void OnMessage(Message* pmsg) {
43 ASSERT_EQ(1u, pmsg->message_id); 46 ASSERT_EQ(1u, pmsg->message_id);
44 47
45 if (done) 48 if (done)
46 return; 49 return;
47 50
48 uint32 cur_time = rtc::Time(); 51 uint32_t cur_time = rtc::Time();
49 uint32 delay = cur_time - last_send; 52 uint32_t delay = cur_time - last_send;
50 uint32 size = rate * delay / 1000; 53 uint32_t size = rate * delay / 1000;
51 size = std::min<uint32>(size, 4096); 54 size = std::min<uint32_t>(size, 4096);
52 size = std::max<uint32>(size, sizeof(uint32)); 55 size = std::max<uint32_t>(size, sizeof(uint32_t));
53 56
54 count += size; 57 count += size;
55 memcpy(dummy, &cur_time, sizeof(cur_time)); 58 memcpy(dummy, &cur_time, sizeof(cur_time));
56 socket->Send(dummy, size, options); 59 socket->Send(dummy, size, options);
57 60
58 last_send = cur_time; 61 last_send = cur_time;
59 thread->PostDelayed(NextDelay(), this, 1); 62 thread->PostDelayed(NextDelay(), this, 1);
60 } 63 }
61 64
62 Thread* thread; 65 Thread* thread;
63 scoped_ptr<AsyncUDPSocket> socket; 66 scoped_ptr<AsyncUDPSocket> socket;
64 rtc::PacketOptions options; 67 rtc::PacketOptions options;
65 bool done; 68 bool done;
66 uint32 rate; // bytes per second 69 uint32_t rate; // bytes per second
67 uint32 count; 70 uint32_t count;
68 uint32 last_send; 71 uint32_t last_send;
69 char dummy[4096]; 72 char dummy[4096];
70 }; 73 };
71 74
72 struct Receiver : public MessageHandler, public sigslot::has_slots<> { 75 struct Receiver : public MessageHandler, public sigslot::has_slots<> {
73 Receiver(Thread* th, AsyncSocket* s, uint32 bw) 76 Receiver(Thread* th, AsyncSocket* s, uint32_t bw)
74 : thread(th), socket(new AsyncUDPSocket(s)), bandwidth(bw), done(false), 77 : thread(th),
75 count(0), sec_count(0), sum(0), sum_sq(0), samples(0) { 78 socket(new AsyncUDPSocket(s)),
79 bandwidth(bw),
80 done(false),
81 count(0),
82 sec_count(0),
83 sum(0),
84 sum_sq(0),
85 samples(0) {
76 socket->SignalReadPacket.connect(this, &Receiver::OnReadPacket); 86 socket->SignalReadPacket.connect(this, &Receiver::OnReadPacket);
77 thread->PostDelayed(1000, this, 1); 87 thread->PostDelayed(1000, this, 1);
78 } 88 }
79 89
80 ~Receiver() { 90 ~Receiver() {
81 thread->Clear(this); 91 thread->Clear(this);
82 } 92 }
83 93
84 void OnReadPacket(AsyncPacketSocket* s, const char* data, size_t size, 94 void OnReadPacket(AsyncPacketSocket* s, const char* data, size_t size,
85 const SocketAddress& remote_addr, 95 const SocketAddress& remote_addr,
86 const PacketTime& packet_time) { 96 const PacketTime& packet_time) {
87 ASSERT_EQ(socket.get(), s); 97 ASSERT_EQ(socket.get(), s);
88 ASSERT_GE(size, 4U); 98 ASSERT_GE(size, 4U);
89 99
90 count += size; 100 count += size;
91 sec_count += size; 101 sec_count += size;
92 102
93 uint32 send_time = *reinterpret_cast<const uint32*>(data); 103 uint32_t send_time = *reinterpret_cast<const uint32_t*>(data);
94 uint32 recv_time = rtc::Time(); 104 uint32_t recv_time = rtc::Time();
95 uint32 delay = recv_time - send_time; 105 uint32_t delay = recv_time - send_time;
96 sum += delay; 106 sum += delay;
97 sum_sq += delay * delay; 107 sum_sq += delay * delay;
98 samples += 1; 108 samples += 1;
99 } 109 }
100 110
101 void OnMessage(Message* pmsg) { 111 void OnMessage(Message* pmsg) {
102 ASSERT_EQ(1u, pmsg->message_id); 112 ASSERT_EQ(1u, pmsg->message_id);
103 113
104 if (done) 114 if (done)
105 return; 115 return;
106 116
107 // It is always possible for us to receive more than expected because 117 // It is always possible for us to receive more than expected because
108 // packets can be further delayed in delivery. 118 // packets can be further delayed in delivery.
109 if (bandwidth > 0) 119 if (bandwidth > 0)
110 ASSERT_TRUE(sec_count <= 5 * bandwidth / 4); 120 ASSERT_TRUE(sec_count <= 5 * bandwidth / 4);
111 sec_count = 0; 121 sec_count = 0;
112 thread->PostDelayed(1000, this, 1); 122 thread->PostDelayed(1000, this, 1);
113 } 123 }
114 124
115 Thread* thread; 125 Thread* thread;
116 scoped_ptr<AsyncUDPSocket> socket; 126 scoped_ptr<AsyncUDPSocket> socket;
117 uint32 bandwidth; 127 uint32_t bandwidth;
118 bool done; 128 bool done;
119 size_t count; 129 size_t count;
120 size_t sec_count; 130 size_t sec_count;
121 double sum; 131 double sum;
122 double sum_sq; 132 double sum_sq;
123 uint32 samples; 133 uint32_t samples;
124 }; 134 };
125 135
126 class VirtualSocketServerTest : public testing::Test { 136 class VirtualSocketServerTest : public testing::Test {
127 public: 137 public:
128 VirtualSocketServerTest() : ss_(new VirtualSocketServer(NULL)), 138 VirtualSocketServerTest() : ss_(new VirtualSocketServer(NULL)),
129 kIPv4AnyAddress(IPAddress(INADDR_ANY), 0), 139 kIPv4AnyAddress(IPAddress(INADDR_ANY), 0),
130 kIPv6AnyAddress(IPAddress(in6addr_any), 0) { 140 kIPv6AnyAddress(IPAddress(in6addr_any), 0) {
131 } 141 }
132 142
133 void CheckPortIncrementalization(const SocketAddress& post, 143 void CheckPortIncrementalization(const SocketAddress& post,
134 const SocketAddress& pre) { 144 const SocketAddress& pre) {
135 EXPECT_EQ(post.port(), pre.port() + 1); 145 EXPECT_EQ(post.port(), pre.port() + 1);
136 IPAddress post_ip = post.ipaddr(); 146 IPAddress post_ip = post.ipaddr();
137 IPAddress pre_ip = pre.ipaddr(); 147 IPAddress pre_ip = pre.ipaddr();
138 EXPECT_EQ(pre_ip.family(), post_ip.family()); 148 EXPECT_EQ(pre_ip.family(), post_ip.family());
139 if (post_ip.family() == AF_INET) { 149 if (post_ip.family() == AF_INET) {
140 in_addr pre_ipv4 = pre_ip.ipv4_address(); 150 in_addr pre_ipv4 = pre_ip.ipv4_address();
141 in_addr post_ipv4 = post_ip.ipv4_address(); 151 in_addr post_ipv4 = post_ip.ipv4_address();
142 EXPECT_EQ(post_ipv4.s_addr, pre_ipv4.s_addr); 152 EXPECT_EQ(post_ipv4.s_addr, pre_ipv4.s_addr);
143 } else if (post_ip.family() == AF_INET6) { 153 } else if (post_ip.family() == AF_INET6) {
144 in6_addr post_ip6 = post_ip.ipv6_address(); 154 in6_addr post_ip6 = post_ip.ipv6_address();
145 in6_addr pre_ip6 = pre_ip.ipv6_address(); 155 in6_addr pre_ip6 = pre_ip.ipv6_address();
146 uint32* post_as_ints = reinterpret_cast<uint32*>(&post_ip6.s6_addr); 156 uint32_t* post_as_ints = reinterpret_cast<uint32_t*>(&post_ip6.s6_addr);
147 uint32* pre_as_ints = reinterpret_cast<uint32*>(&pre_ip6.s6_addr); 157 uint32_t* pre_as_ints = reinterpret_cast<uint32_t*>(&pre_ip6.s6_addr);
148 EXPECT_EQ(post_as_ints[3], pre_as_ints[3]); 158 EXPECT_EQ(post_as_ints[3], pre_as_ints[3]);
149 } 159 }
150 } 160 }
151 161
152 // Test a client can bind to the any address, and all sent packets will have 162 // Test a client can bind to the any address, and all sent packets will have
153 // the default route as the source address. Also, it can receive packets sent 163 // the default route as the source address. Also, it can receive packets sent
154 // to the default route. 164 // to the default route.
155 void TestDefaultRoute(const IPAddress& default_route) { 165 void TestDefaultRoute(const IPAddress& default_route) {
156 ss_->SetDefaultRoute(default_route); 166 ss_->SetDefaultRoute(default_route);
157 167
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 } 623 }
614 624
615 ss_->ProcessMessagesUntilIdle(); 625 ss_->ProcessMessagesUntilIdle();
616 626
617 for (char i = 0; i < cNumPackets; ++i) { 627 for (char i = 0; i < cNumPackets; ++i) {
618 EXPECT_EQ(1, b->Recv(buffer, sizeof(buffer))); 628 EXPECT_EQ(1, b->Recv(buffer, sizeof(buffer)));
619 EXPECT_EQ(static_cast<char>('0' + i), buffer[0]); 629 EXPECT_EQ(static_cast<char>('0' + i), buffer[0]);
620 } 630 }
621 631
622 // Next, deliver packets at random intervals 632 // Next, deliver packets at random intervals
623 const uint32 mean = 50; 633 const uint32_t mean = 50;
624 const uint32 stddev = 50; 634 const uint32_t stddev = 50;
625 635
626 ss_->set_delay_mean(mean); 636 ss_->set_delay_mean(mean);
627 ss_->set_delay_stddev(stddev); 637 ss_->set_delay_stddev(stddev);
628 ss_->UpdateDelayDistribution(); 638 ss_->UpdateDelayDistribution();
629 639
630 for (char i = 0; i < cNumPackets; ++i) { 640 for (char i = 0; i < cNumPackets; ++i) {
631 buffer[0] = 'A' + i; 641 buffer[0] = 'A' + i;
632 EXPECT_EQ(1, a->Send(buffer, 1)); 642 EXPECT_EQ(1, a->Send(buffer, 1));
633 } 643 }
634 644
(...skipping 12 matching lines...) Expand all
647 AsyncSocket* send_socket = 657 AsyncSocket* send_socket =
648 ss_->CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM); 658 ss_->CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM);
649 AsyncSocket* recv_socket = 659 AsyncSocket* recv_socket =
650 ss_->CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM); 660 ss_->CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM);
651 ASSERT_EQ(0, send_socket->Bind(initial_addr)); 661 ASSERT_EQ(0, send_socket->Bind(initial_addr));
652 ASSERT_EQ(0, recv_socket->Bind(initial_addr)); 662 ASSERT_EQ(0, recv_socket->Bind(initial_addr));
653 EXPECT_EQ(send_socket->GetLocalAddress().family(), initial_addr.family()); 663 EXPECT_EQ(send_socket->GetLocalAddress().family(), initial_addr.family());
654 EXPECT_EQ(recv_socket->GetLocalAddress().family(), initial_addr.family()); 664 EXPECT_EQ(recv_socket->GetLocalAddress().family(), initial_addr.family());
655 ASSERT_EQ(0, send_socket->Connect(recv_socket->GetLocalAddress())); 665 ASSERT_EQ(0, send_socket->Connect(recv_socket->GetLocalAddress()));
656 666
657 uint32 bandwidth = 64 * 1024; 667 uint32_t bandwidth = 64 * 1024;
658 ss_->set_bandwidth(bandwidth); 668 ss_->set_bandwidth(bandwidth);
659 669
660 Thread* pthMain = Thread::Current(); 670 Thread* pthMain = Thread::Current();
661 Sender sender(pthMain, send_socket, 80 * 1024); 671 Sender sender(pthMain, send_socket, 80 * 1024);
662 Receiver receiver(pthMain, recv_socket, bandwidth); 672 Receiver receiver(pthMain, recv_socket, bandwidth);
663 673
664 pthMain->ProcessMessages(5000); 674 pthMain->ProcessMessages(5000);
665 sender.done = true; 675 sender.done = true;
666 pthMain->ProcessMessages(5000); 676 pthMain->ProcessMessages(5000);
667 677
668 ASSERT_TRUE(receiver.count >= 5 * 3 * bandwidth / 4); 678 ASSERT_TRUE(receiver.count >= 5 * 3 * bandwidth / 4);
669 ASSERT_TRUE(receiver.count <= 6 * bandwidth); // queue could drain for 1s 679 ASSERT_TRUE(receiver.count <= 6 * bandwidth); // queue could drain for 1s
670 680
671 ss_->set_bandwidth(0); 681 ss_->set_bandwidth(0);
672 } 682 }
673 683
674 // It is important that initial_addr's port has to be 0 such that the 684 // It is important that initial_addr's port has to be 0 such that the
675 // incremental port behavior could ensure the 2 Binds result in different 685 // incremental port behavior could ensure the 2 Binds result in different
676 // address. 686 // address.
677 void DelayTest(const SocketAddress& initial_addr) { 687 void DelayTest(const SocketAddress& initial_addr) {
678 time_t seed = ::time(NULL); 688 time_t seed = ::time(NULL);
679 LOG(LS_VERBOSE) << "seed = " << seed; 689 LOG(LS_VERBOSE) << "seed = " << seed;
680 srand(static_cast<unsigned int>(seed)); 690 srand(static_cast<unsigned int>(seed));
681 691
682 const uint32 mean = 2000; 692 const uint32_t mean = 2000;
683 const uint32 stddev = 500; 693 const uint32_t stddev = 500;
684 694
685 ss_->set_delay_mean(mean); 695 ss_->set_delay_mean(mean);
686 ss_->set_delay_stddev(stddev); 696 ss_->set_delay_stddev(stddev);
687 ss_->UpdateDelayDistribution(); 697 ss_->UpdateDelayDistribution();
688 698
689 AsyncSocket* send_socket = 699 AsyncSocket* send_socket =
690 ss_->CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM); 700 ss_->CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM);
691 AsyncSocket* recv_socket = 701 AsyncSocket* recv_socket =
692 ss_->CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM); 702 ss_->CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM);
693 ASSERT_EQ(0, send_socket->Bind(initial_addr)); 703 ASSERT_EQ(0, send_socket->Bind(initial_addr));
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
1001 true); 1011 true);
1002 } 1012 }
1003 1013
1004 TEST_F(VirtualSocketServerTest, CanSendDatagramFromUnboundIPv6ToIPv4Any) { 1014 TEST_F(VirtualSocketServerTest, CanSendDatagramFromUnboundIPv6ToIPv4Any) {
1005 CrossFamilyDatagramTest(SocketAddress("::", 0), 1015 CrossFamilyDatagramTest(SocketAddress("::", 0),
1006 SocketAddress("0.0.0.0", 5000), 1016 SocketAddress("0.0.0.0", 5000),
1007 true); 1017 true);
1008 } 1018 }
1009 1019
1010 TEST_F(VirtualSocketServerTest, CreatesStandardDistribution) { 1020 TEST_F(VirtualSocketServerTest, CreatesStandardDistribution) {
1011 const uint32 kTestMean[] = { 10, 100, 333, 1000 }; 1021 const uint32_t kTestMean[] = {10, 100, 333, 1000};
1012 const double kTestDev[] = { 0.25, 0.1, 0.01 }; 1022 const double kTestDev[] = { 0.25, 0.1, 0.01 };
1013 // TODO: The current code only works for 1000 data points or more. 1023 // TODO: The current code only works for 1000 data points or more.
1014 const uint32 kTestSamples[] = { /*10, 100,*/ 1000 }; 1024 const uint32_t kTestSamples[] = {/*10, 100,*/ 1000};
1015 for (size_t midx = 0; midx < ARRAY_SIZE(kTestMean); ++midx) { 1025 for (size_t midx = 0; midx < ARRAY_SIZE(kTestMean); ++midx) {
1016 for (size_t didx = 0; didx < ARRAY_SIZE(kTestDev); ++didx) { 1026 for (size_t didx = 0; didx < ARRAY_SIZE(kTestDev); ++didx) {
1017 for (size_t sidx = 0; sidx < ARRAY_SIZE(kTestSamples); ++sidx) { 1027 for (size_t sidx = 0; sidx < ARRAY_SIZE(kTestSamples); ++sidx) {
1018 ASSERT_LT(0u, kTestSamples[sidx]); 1028 ASSERT_LT(0u, kTestSamples[sidx]);
1019 const uint32 kStdDev = 1029 const uint32_t kStdDev =
1020 static_cast<uint32>(kTestDev[didx] * kTestMean[midx]); 1030 static_cast<uint32_t>(kTestDev[didx] * kTestMean[midx]);
1021 VirtualSocketServer::Function* f = 1031 VirtualSocketServer::Function* f =
1022 VirtualSocketServer::CreateDistribution(kTestMean[midx], 1032 VirtualSocketServer::CreateDistribution(kTestMean[midx],
1023 kStdDev, 1033 kStdDev,
1024 kTestSamples[sidx]); 1034 kTestSamples[sidx]);
1025 ASSERT_TRUE(NULL != f); 1035 ASSERT_TRUE(NULL != f);
1026 ASSERT_EQ(kTestSamples[sidx], f->size()); 1036 ASSERT_EQ(kTestSamples[sidx], f->size());
1027 double sum = 0; 1037 double sum = 0;
1028 for (uint32 i = 0; i < f->size(); ++i) { 1038 for (uint32_t i = 0; i < f->size(); ++i) {
1029 sum += (*f)[i].second; 1039 sum += (*f)[i].second;
1030 } 1040 }
1031 const double mean = sum / f->size(); 1041 const double mean = sum / f->size();
1032 double sum_sq_dev = 0; 1042 double sum_sq_dev = 0;
1033 for (uint32 i = 0; i < f->size(); ++i) { 1043 for (uint32_t i = 0; i < f->size(); ++i) {
1034 double dev = (*f)[i].second - mean; 1044 double dev = (*f)[i].second - mean;
1035 sum_sq_dev += dev * dev; 1045 sum_sq_dev += dev * dev;
1036 } 1046 }
1037 const double stddev = sqrt(sum_sq_dev / f->size()); 1047 const double stddev = sqrt(sum_sq_dev / f->size());
1038 EXPECT_NEAR(kTestMean[midx], mean, 0.1 * kTestMean[midx]) 1048 EXPECT_NEAR(kTestMean[midx], mean, 0.1 * kTestMean[midx])
1039 << "M=" << kTestMean[midx] 1049 << "M=" << kTestMean[midx]
1040 << " SD=" << kStdDev 1050 << " SD=" << kStdDev
1041 << " N=" << kTestSamples[sidx]; 1051 << " N=" << kTestSamples[sidx];
1042 EXPECT_NEAR(kStdDev, stddev, 0.1 * kStdDev) 1052 EXPECT_NEAR(kStdDev, stddev, 0.1 * kStdDev)
1043 << "M=" << kTestMean[midx] 1053 << "M=" << kTestMean[midx]
1044 << " SD=" << kStdDev 1054 << " SD=" << kStdDev
1045 << " N=" << kTestSamples[sidx]; 1055 << " N=" << kTestSamples[sidx];
1046 delete f; 1056 delete f;
1047 } 1057 }
1048 } 1058 }
1049 } 1059 }
1050 } 1060 }
OLDNEW
« no previous file with comments | « webrtc/base/unixfilesystem.cc ('k') | webrtc/base/virtualsocketserver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698