1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 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 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 #include "webrtc/call.h" 15 #include "webrtc/system_wrappers/interface/scoped_ptr.h" 16 #include "webrtc/system_wrappers/interface/tick_util.h" 17 #include "webrtc/test/fake_network_pipe.h" 18 19 using ::testing::_; 20 using ::testing::AnyNumber; 21 using ::testing::Return; 22 using ::testing::Invoke; 23 24 namespace webrtc { 25 26 class MockReceiver : public PacketReceiver { 27 public: 28 MockReceiver() {} 29 virtual ~MockReceiver() {} 30 31 void IncomingPacket(const uint8_t* data, size_t length) { 32 DeliverPacket(data, length); 33 delete [] data; 34 } 35 36 MOCK_METHOD2(DeliverPacket, DeliveryStatus(const uint8_t*, size_t)); 37 }; 38 39 class FakeNetworkPipeTest : public ::testing::Test { 40 protected: 41 virtual void SetUp() { 42 TickTime::UseFakeClock(12345); 43 receiver_.reset(new MockReceiver()); 44 } 45 46 virtual void TearDown() { 47 } 48 49 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int kPacketSize) { 50 scoped_ptr<uint8_t[]> packet(new uint8_t[kPacketSize]); 51 for (int i = 0; i < number_packets; ++i) { 52 pipe->SendPacket(packet.get(), kPacketSize); 53 } 54 } 55 56 int PacketTimeMs(int capacity_kbps, int kPacketSize) const { 57 return 8 * kPacketSize / capacity_kbps; 58 } 59 60 scoped_ptr<MockReceiver> receiver_; 61 }; 62 63 void DeleteMemory(uint8_t* data, int length) { delete [] data; } 64 65 // Test the capacity link and verify we get as many packets as we expect. 66 TEST_F(FakeNetworkPipeTest, CapacityTest) { 67 FakeNetworkPipe::Config config; 68 config.queue_length = 20; 69 config.link_capacity_kbps = 80; 70 scoped_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(config)); 71 pipe->SetReceiver(receiver_.get()); 72 73 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to 74 // get through the pipe. 75 const int kNumPackets = 10; 76 const int kPacketSize = 1000; 77 SendPackets(pipe.get(), kNumPackets , kPacketSize); 78 79 // Time to get one packet through the link. 80 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, 81 kPacketSize); 82 83 // Time haven't increased yet, so we souldn't get any packets. 84 EXPECT_CALL(*receiver_, DeliverPacket(_, _)) 85 .Times(0); 86 pipe->Process(); 87 88 // Advance enough time to release one packet. 89 TickTime::AdvanceFakeClock(kPacketTimeMs); 90 EXPECT_CALL(*receiver_, DeliverPacket(_, _)) 91 .Times(1); 92 pipe->Process(); 93 94 // Release all but one packet 95 TickTime::AdvanceFakeClock(9 * kPacketTimeMs - 1); 96 EXPECT_CALL(*receiver_, DeliverPacket(_, _)) 97 .Times(8); 98 pipe->Process(); 99 100 // And the last one. 101 TickTime::AdvanceFakeClock(1); 102 EXPECT_CALL(*receiver_, DeliverPacket(_, _)) 103 .Times(1); 104 pipe->Process(); 105 } 106 107 // Test the extra network delay. 108 TEST_F(FakeNetworkPipeTest, ExtraDelayTest) { 109 FakeNetworkPipe::Config config; 110 config.queue_length = 20; 111 config.queue_delay_ms = 100; 112 config.link_capacity_kbps = 80; 113 scoped_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(config)); 114 pipe->SetReceiver(receiver_.get()); 115 116 const int kNumPackets = 2; 117 const int kPacketSize = 1000; 118 SendPackets(pipe.get(), kNumPackets , kPacketSize); 119 120 // Time to get one packet through the link. 121 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, 122 kPacketSize); 123 124 // Increase more than kPacketTimeMs, but not more than the extra delay. 125 TickTime::AdvanceFakeClock(kPacketTimeMs); 126 EXPECT_CALL(*receiver_, DeliverPacket(_, _)) 127 .Times(0); 128 pipe->Process(); 129 130 // Advance the network delay to get the first packet. 131 TickTime::AdvanceFakeClock(config.queue_delay_ms); 132 EXPECT_CALL(*receiver_, DeliverPacket(_, _)) 133 .Times(1); 134 pipe->Process(); 135 136 // Advance one more kPacketTimeMs to get the last packet. 137 TickTime::AdvanceFakeClock(kPacketTimeMs); 138 EXPECT_CALL(*receiver_, DeliverPacket(_, _)) 139 .Times(1); 140 pipe->Process(); 141 } 142 143 // Test the number of buffers and packets are dropped when sending too many 144 // packets too quickly. 145 TEST_F(FakeNetworkPipeTest, QueueLengthTest) { 146 FakeNetworkPipe::Config config; 147 config.queue_length = 2; 148 config.link_capacity_kbps = 80; 149 scoped_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(config)); 150 pipe->SetReceiver(receiver_.get()); 151 152 const int kPacketSize = 1000; 153 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, 154 kPacketSize); 155 156 // Send three packets and verify only 2 are delivered. 157 SendPackets(pipe.get(), 3, kPacketSize); 158 159 // Increase time enough to deliver all three packets, verify only two are 160 // delivered. 161 TickTime::AdvanceFakeClock(3 * kPacketTimeMs); 162 EXPECT_CALL(*receiver_, DeliverPacket(_, _)) 163 .Times(2); 164 pipe->Process(); 165 } 166 167 // Test we get statistics as expected. 168 TEST_F(FakeNetworkPipeTest, StatisticsTest) { 169 FakeNetworkPipe::Config config; 170 config.queue_length = 2; 171 config.queue_delay_ms = 20; 172 config.link_capacity_kbps = 80; 173 scoped_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(config)); 174 pipe->SetReceiver(receiver_.get()); 175 176 const int kPacketSize = 1000; 177 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, 178 kPacketSize); 179 180 // Send three packets and verify only 2 are delivered. 181 SendPackets(pipe.get(), 3, kPacketSize); 182 TickTime::AdvanceFakeClock(3 * kPacketTimeMs + config.queue_delay_ms); 183 184 EXPECT_CALL(*receiver_, DeliverPacket(_, _)) 185 .Times(2); 186 pipe->Process(); 187 188 // Packet 1: kPacketTimeMs + config.queue_delay_ms, 189 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average. 190 EXPECT_EQ(pipe->AverageDelay(), 170); 191 EXPECT_EQ(pipe->sent_packets(), 2u); 192 EXPECT_EQ(pipe->dropped_packets(), 1u); 193 EXPECT_EQ(pipe->PercentageLoss(), 1/3.f); 194 } 195 196 // Change the link capacity half-way through the test and verify that the 197 // delivery times change accordingly. 198 TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) { 199 FakeNetworkPipe::Config config; 200 config.queue_length = 20; 201 config.link_capacity_kbps = 80; 202 scoped_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(config)); 203 pipe->SetReceiver(receiver_.get()); 204 205 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to 206 // get through the pipe. 207 const int kNumPackets = 10; 208 const int kPacketSize = 1000; 209 SendPackets(pipe.get(), kNumPackets, kPacketSize); 210 211 // Time to get one packet through the link. 212 int packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); 213 214 // Time hasn't increased yet, so we souldn't get any packets. 215 EXPECT_CALL(*receiver_, DeliverPacket(_, _)).Times(0); 216 pipe->Process(); 217 218 // Advance time in steps to release one packet at a time. 219 for (int i = 0; i < kNumPackets; ++i) { 220 TickTime::AdvanceFakeClock(packet_time_ms); 221 EXPECT_CALL(*receiver_, DeliverPacket(_, _)).Times(1); 222 pipe->Process(); 223 } 224 225 // Change the capacity. 226 config.link_capacity_kbps /= 2; // Reduce to 50%. 227 pipe->SetConfig(config); 228 229 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two 230 // seconds to get them through the pipe. 231 SendPackets(pipe.get(), kNumPackets, kPacketSize); 232 233 // Time to get one packet through the link. 234 packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); 235 236 // Time hasn't increased yet, so we souldn't get any packets. 237 EXPECT_CALL(*receiver_, DeliverPacket(_, _)).Times(0); 238 pipe->Process(); 239 240 // Advance time in steps to release one packet at a time. 241 for (int i = 0; i < kNumPackets; ++i) { 242 TickTime::AdvanceFakeClock(packet_time_ms); 243 EXPECT_CALL(*receiver_, DeliverPacket(_, _)).Times(1); 244 pipe->Process(); 245 } 246 247 // Check that all the packets were sent. 248 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); 249 TickTime::AdvanceFakeClock(pipe->TimeUntilNextProcess()); 250 EXPECT_CALL(*receiver_, DeliverPacket(_, _)).Times(0); 251 pipe->Process(); 252 } 253 254 // Change the link capacity half-way through the test and verify that the 255 // delivery times change accordingly. 256 TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) { 257 FakeNetworkPipe::Config config; 258 config.queue_length = 20; 259 config.link_capacity_kbps = 80; 260 scoped_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(config)); 261 pipe->SetReceiver(receiver_.get()); 262 263 // Add 10 packets of 1000 bytes, = 80 kb. 264 const int kNumPackets = 10; 265 const int kPacketSize = 1000; 266 SendPackets(pipe.get(), kNumPackets, kPacketSize); 267 268 // Time to get one packet through the link at the initial speed. 269 int packet_time_1_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); 270 271 // Change the capacity. 272 config.link_capacity_kbps *= 2; // Double the capacity. 273 pipe->SetConfig(config); 274 275 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two 276 // seconds to get them through the pipe. 277 SendPackets(pipe.get(), kNumPackets, kPacketSize); 278 279 // Time to get one packet through the link at the new capacity. 280 int packet_time_2_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); 281 282 // Time hasn't increased yet, so we souldn't get any packets. 283 EXPECT_CALL(*receiver_, DeliverPacket(_, _)).Times(0); 284 pipe->Process(); 285 286 // Advance time in steps to release one packet at a time. 287 for (int i = 0; i < kNumPackets; ++i) { 288 TickTime::AdvanceFakeClock(packet_time_1_ms); 289 EXPECT_CALL(*receiver_, DeliverPacket(_, _)).Times(1); 290 pipe->Process(); 291 } 292 293 // Advance time in steps to release one packet at a time. 294 for (int i = 0; i < kNumPackets; ++i) { 295 TickTime::AdvanceFakeClock(packet_time_2_ms); 296 EXPECT_CALL(*receiver_, DeliverPacket(_, _)).Times(1); 297 pipe->Process(); 298 } 299 300 // Check that all the packets were sent. 301 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); 302 TickTime::AdvanceFakeClock(pipe->TimeUntilNextProcess()); 303 EXPECT_CALL(*receiver_, DeliverPacket(_, _)).Times(0); 304 pipe->Process(); 305 } 306 } // namespace webrtc 307