mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 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 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 13 | #include "webrtc/call.h" |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 14 | #include "webrtc/system_wrappers/include/clock.h" |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 15 | #include "webrtc/test/fake_network_pipe.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 16 | #include "webrtc/test/gmock.h" |
| 17 | #include "webrtc/test/gtest.h" |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 18 | |
| 19 | using ::testing::_; |
| 20 | using ::testing::AnyNumber; |
| 21 | using ::testing::Return; |
| 22 | using ::testing::Invoke; |
| 23 | |
| 24 | namespace webrtc { |
| 25 | |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 26 | class TestReceiver : public PacketReceiver { |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 27 | public: |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 28 | TestReceiver() {} |
| 29 | virtual ~TestReceiver() {} |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 30 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 31 | void IncomingPacket(const uint8_t* data, size_t length) { |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 32 | DeliverPacket(MediaType::ANY, data, length, PacketTime()); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 33 | delete [] data; |
| 34 | } |
| 35 | |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 36 | virtual MOCK_METHOD4( |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 37 | DeliverPacket, |
| 38 | DeliveryStatus(MediaType, const uint8_t*, size_t, const PacketTime&)); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 41 | class ReorderTestReceiver : public TestReceiver { |
| 42 | public: |
| 43 | ReorderTestReceiver() {} |
| 44 | virtual ~ReorderTestReceiver() {} |
| 45 | |
| 46 | DeliveryStatus DeliverPacket(MediaType media_type, |
| 47 | const uint8_t* packet, |
| 48 | size_t length, |
| 49 | const PacketTime& packet_time) override { |
| 50 | int seq_num; |
| 51 | memcpy(&seq_num, packet, sizeof(int)); |
| 52 | delivered_sequence_numbers_.push_back(seq_num); |
| 53 | return PacketReceiver::DELIVERY_OK; |
| 54 | } |
| 55 | std::vector<int> delivered_sequence_numbers_; |
| 56 | }; |
| 57 | |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 58 | class FakeNetworkPipeTest : public ::testing::Test { |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 59 | public: |
| 60 | FakeNetworkPipeTest() : fake_clock_(12345) {} |
| 61 | |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 62 | protected: |
| 63 | virtual void SetUp() { |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 64 | receiver_.reset(new TestReceiver()); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 65 | ON_CALL(*receiver_, DeliverPacket(_, _, _, _)) |
stefan@webrtc.org | 1ccff34 | 2014-08-06 15:41:58 +0000 | [diff] [blame] | 66 | .WillByDefault(Return(PacketReceiver::DELIVERY_OK)); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | virtual void TearDown() { |
| 70 | } |
| 71 | |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 72 | void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) { |
| 73 | RTC_DCHECK_GE(packet_size, static_cast<int>(sizeof(int))); |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 74 | std::unique_ptr<uint8_t[]> packet(new uint8_t[packet_size]); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 75 | for (int i = 0; i < number_packets; ++i) { |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 76 | // Set a sequence number for the packets by |
| 77 | // using the first bytes in the packet. |
| 78 | memcpy(packet.get(), &i, sizeof(int)); |
| 79 | pipe->SendPacket(packet.get(), packet_size); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 83 | int PacketTimeMs(int capacity_kbps, int packet_size) const { |
| 84 | return 8 * packet_size / capacity_kbps; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 87 | SimulatedClock fake_clock_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 88 | std::unique_ptr<TestReceiver> receiver_; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | void DeleteMemory(uint8_t* data, int length) { delete [] data; } |
| 92 | |
| 93 | // Test the capacity link and verify we get as many packets as we expect. |
| 94 | TEST_F(FakeNetworkPipeTest, CapacityTest) { |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 95 | FakeNetworkPipe::Config config; |
stefan@webrtc.org | b8e9e44 | 2014-07-09 11:29:06 +0000 | [diff] [blame] | 96 | config.queue_length_packets = 20; |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 97 | config.link_capacity_kbps = 80; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 98 | std::unique_ptr<FakeNetworkPipe> pipe( |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 99 | new FakeNetworkPipe(&fake_clock_, config)); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 100 | pipe->SetReceiver(receiver_.get()); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 101 | |
| 102 | // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to |
| 103 | // get through the pipe. |
| 104 | const int kNumPackets = 10; |
| 105 | const int kPacketSize = 1000; |
| 106 | SendPackets(pipe.get(), kNumPackets , kPacketSize); |
| 107 | |
| 108 | // Time to get one packet through the link. |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 109 | const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, |
| 110 | kPacketSize); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 111 | |
| 112 | // Time haven't increased yet, so we souldn't get any packets. |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 113 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 114 | pipe->Process(); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 115 | |
| 116 | // Advance enough time to release one packet. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 117 | fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 118 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 119 | pipe->Process(); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 120 | |
| 121 | // Release all but one packet |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 122 | fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 123 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(8); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 124 | pipe->Process(); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 125 | |
| 126 | // And the last one. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 127 | fake_clock_.AdvanceTimeMilliseconds(1); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 128 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 129 | pipe->Process(); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Test the extra network delay. |
| 133 | TEST_F(FakeNetworkPipeTest, ExtraDelayTest) { |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 134 | FakeNetworkPipe::Config config; |
stefan@webrtc.org | b8e9e44 | 2014-07-09 11:29:06 +0000 | [diff] [blame] | 135 | config.queue_length_packets = 20; |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 136 | config.queue_delay_ms = 100; |
| 137 | config.link_capacity_kbps = 80; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 138 | std::unique_ptr<FakeNetworkPipe> pipe( |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 139 | new FakeNetworkPipe(&fake_clock_, config)); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 140 | pipe->SetReceiver(receiver_.get()); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 141 | |
| 142 | const int kNumPackets = 2; |
| 143 | const int kPacketSize = 1000; |
| 144 | SendPackets(pipe.get(), kNumPackets , kPacketSize); |
| 145 | |
| 146 | // Time to get one packet through the link. |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 147 | const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, |
| 148 | kPacketSize); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 149 | |
| 150 | // Increase more than kPacketTimeMs, but not more than the extra delay. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 151 | fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 152 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 153 | pipe->Process(); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 154 | |
| 155 | // Advance the network delay to get the first packet. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 156 | fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 157 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 158 | pipe->Process(); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 159 | |
| 160 | // Advance one more kPacketTimeMs to get the last packet. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 161 | fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 162 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 163 | pipe->Process(); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Test the number of buffers and packets are dropped when sending too many |
| 167 | // packets too quickly. |
| 168 | TEST_F(FakeNetworkPipeTest, QueueLengthTest) { |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 169 | FakeNetworkPipe::Config config; |
stefan@webrtc.org | b8e9e44 | 2014-07-09 11:29:06 +0000 | [diff] [blame] | 170 | config.queue_length_packets = 2; |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 171 | config.link_capacity_kbps = 80; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 172 | std::unique_ptr<FakeNetworkPipe> pipe( |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 173 | new FakeNetworkPipe(&fake_clock_, config)); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 174 | pipe->SetReceiver(receiver_.get()); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 175 | |
| 176 | const int kPacketSize = 1000; |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 177 | const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, |
| 178 | kPacketSize); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 179 | |
| 180 | // Send three packets and verify only 2 are delivered. |
| 181 | SendPackets(pipe.get(), 3, kPacketSize); |
| 182 | |
| 183 | // Increase time enough to deliver all three packets, verify only two are |
| 184 | // delivered. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 185 | fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 186 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(2); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 187 | pipe->Process(); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Test we get statistics as expected. |
| 191 | TEST_F(FakeNetworkPipeTest, StatisticsTest) { |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 192 | FakeNetworkPipe::Config config; |
stefan@webrtc.org | b8e9e44 | 2014-07-09 11:29:06 +0000 | [diff] [blame] | 193 | config.queue_length_packets = 2; |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 194 | config.queue_delay_ms = 20; |
| 195 | config.link_capacity_kbps = 80; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 196 | std::unique_ptr<FakeNetworkPipe> pipe( |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 197 | new FakeNetworkPipe(&fake_clock_, config)); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 198 | pipe->SetReceiver(receiver_.get()); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 199 | |
| 200 | const int kPacketSize = 1000; |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 201 | const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, |
| 202 | kPacketSize); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 203 | |
| 204 | // Send three packets and verify only 2 are delivered. |
| 205 | SendPackets(pipe.get(), 3, kPacketSize); |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 206 | fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs + |
| 207 | config.queue_delay_ms); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 208 | |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 209 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(2); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 210 | pipe->Process(); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 211 | |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 212 | // Packet 1: kPacketTimeMs + config.queue_delay_ms, |
| 213 | // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average. |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 214 | EXPECT_EQ(pipe->AverageDelay(), 170); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 215 | EXPECT_EQ(pipe->sent_packets(), 2u); |
| 216 | EXPECT_EQ(pipe->dropped_packets(), 1u); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 217 | EXPECT_EQ(pipe->PercentageLoss(), 1/3.f); |
| 218 | } |
| 219 | |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 220 | // Change the link capacity half-way through the test and verify that the |
| 221 | // delivery times change accordingly. |
| 222 | TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) { |
| 223 | FakeNetworkPipe::Config config; |
stefan@webrtc.org | b8e9e44 | 2014-07-09 11:29:06 +0000 | [diff] [blame] | 224 | config.queue_length_packets = 20; |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 225 | config.link_capacity_kbps = 80; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 226 | std::unique_ptr<FakeNetworkPipe> pipe( |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 227 | new FakeNetworkPipe(&fake_clock_, config)); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 228 | pipe->SetReceiver(receiver_.get()); |
| 229 | |
| 230 | // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to |
| 231 | // get through the pipe. |
| 232 | const int kNumPackets = 10; |
| 233 | const int kPacketSize = 1000; |
| 234 | SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 235 | |
| 236 | // Time to get one packet through the link. |
| 237 | int packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); |
| 238 | |
| 239 | // Time hasn't increased yet, so we souldn't get any packets. |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 240 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 241 | pipe->Process(); |
| 242 | |
| 243 | // Advance time in steps to release one packet at a time. |
| 244 | for (int i = 0; i < kNumPackets; ++i) { |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 245 | fake_clock_.AdvanceTimeMilliseconds(packet_time_ms); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 246 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 247 | pipe->Process(); |
| 248 | } |
| 249 | |
| 250 | // Change the capacity. |
| 251 | config.link_capacity_kbps /= 2; // Reduce to 50%. |
| 252 | pipe->SetConfig(config); |
| 253 | |
| 254 | // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two |
| 255 | // seconds to get them through the pipe. |
| 256 | SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 257 | |
| 258 | // Time to get one packet through the link. |
| 259 | packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); |
| 260 | |
| 261 | // Time hasn't increased yet, so we souldn't get any packets. |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 262 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 263 | pipe->Process(); |
| 264 | |
| 265 | // Advance time in steps to release one packet at a time. |
| 266 | for (int i = 0; i < kNumPackets; ++i) { |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 267 | fake_clock_.AdvanceTimeMilliseconds(packet_time_ms); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 268 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 269 | pipe->Process(); |
| 270 | } |
| 271 | |
| 272 | // Check that all the packets were sent. |
| 273 | EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 274 | fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess()); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 275 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 276 | pipe->Process(); |
| 277 | } |
| 278 | |
| 279 | // Change the link capacity half-way through the test and verify that the |
| 280 | // delivery times change accordingly. |
| 281 | TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) { |
| 282 | FakeNetworkPipe::Config config; |
stefan@webrtc.org | b8e9e44 | 2014-07-09 11:29:06 +0000 | [diff] [blame] | 283 | config.queue_length_packets = 20; |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 284 | config.link_capacity_kbps = 80; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 285 | std::unique_ptr<FakeNetworkPipe> pipe( |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 286 | new FakeNetworkPipe(&fake_clock_, config)); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 287 | pipe->SetReceiver(receiver_.get()); |
| 288 | |
| 289 | // Add 10 packets of 1000 bytes, = 80 kb. |
| 290 | const int kNumPackets = 10; |
| 291 | const int kPacketSize = 1000; |
| 292 | SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 293 | |
| 294 | // Time to get one packet through the link at the initial speed. |
| 295 | int packet_time_1_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); |
| 296 | |
| 297 | // Change the capacity. |
| 298 | config.link_capacity_kbps *= 2; // Double the capacity. |
| 299 | pipe->SetConfig(config); |
| 300 | |
| 301 | // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two |
| 302 | // seconds to get them through the pipe. |
| 303 | SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 304 | |
| 305 | // Time to get one packet through the link at the new capacity. |
| 306 | int packet_time_2_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); |
| 307 | |
| 308 | // Time hasn't increased yet, so we souldn't get any packets. |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 309 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 310 | pipe->Process(); |
| 311 | |
| 312 | // Advance time in steps to release one packet at a time. |
| 313 | for (int i = 0; i < kNumPackets; ++i) { |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 314 | fake_clock_.AdvanceTimeMilliseconds(packet_time_1_ms); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 315 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 316 | pipe->Process(); |
| 317 | } |
| 318 | |
| 319 | // Advance time in steps to release one packet at a time. |
| 320 | for (int i = 0; i < kNumPackets; ++i) { |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 321 | fake_clock_.AdvanceTimeMilliseconds(packet_time_2_ms); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 322 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 323 | pipe->Process(); |
| 324 | } |
| 325 | |
| 326 | // Check that all the packets were sent. |
| 327 | EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 328 | fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess()); |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 329 | EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 330 | pipe->Process(); |
| 331 | } |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 332 | |
| 333 | // At first disallow reordering and then allow reordering. |
| 334 | TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) { |
| 335 | FakeNetworkPipe::Config config; |
| 336 | config.queue_length_packets = 1000; |
| 337 | config.link_capacity_kbps = 800; |
| 338 | config.queue_delay_ms = 100; |
| 339 | config.delay_standard_deviation_ms = 10; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 340 | std::unique_ptr<FakeNetworkPipe> pipe( |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 341 | new FakeNetworkPipe(&fake_clock_, config)); |
| 342 | ReorderTestReceiver* receiver = new ReorderTestReceiver(); |
| 343 | receiver_.reset(receiver); |
| 344 | pipe->SetReceiver(receiver_.get()); |
| 345 | |
| 346 | const uint32_t kNumPackets = 100; |
| 347 | const int kPacketSize = 10; |
| 348 | SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 349 | fake_clock_.AdvanceTimeMilliseconds(1000); |
| 350 | pipe->Process(); |
| 351 | |
| 352 | // Confirm that all packets have been delivered in order. |
| 353 | EXPECT_EQ(kNumPackets, receiver->delivered_sequence_numbers_.size()); |
| 354 | int last_seq_num = -1; |
| 355 | for (int seq_num : receiver->delivered_sequence_numbers_) { |
| 356 | EXPECT_GT(seq_num, last_seq_num); |
| 357 | last_seq_num = seq_num; |
| 358 | } |
| 359 | |
| 360 | config.allow_reordering = true; |
| 361 | pipe->SetConfig(config); |
| 362 | SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 363 | fake_clock_.AdvanceTimeMilliseconds(1000); |
| 364 | receiver->delivered_sequence_numbers_.clear(); |
| 365 | pipe->Process(); |
| 366 | |
| 367 | // Confirm that all packets have been delivered |
| 368 | // and that reordering has occured. |
| 369 | EXPECT_EQ(kNumPackets, receiver->delivered_sequence_numbers_.size()); |
| 370 | bool reordering_has_occured = false; |
| 371 | last_seq_num = -1; |
| 372 | for (int seq_num : receiver->delivered_sequence_numbers_) { |
| 373 | if (last_seq_num > seq_num) { |
| 374 | reordering_has_occured = true; |
| 375 | break; |
| 376 | } |
| 377 | last_seq_num = seq_num; |
| 378 | } |
| 379 | EXPECT_TRUE(reordering_has_occured); |
| 380 | } |
philipel | 536378b | 2016-05-31 03:20:23 -0700 | [diff] [blame] | 381 | |
| 382 | TEST_F(FakeNetworkPipeTest, BurstLoss) { |
| 383 | const int kLossPercent = 5; |
| 384 | const int kAvgBurstLength = 3; |
| 385 | const int kNumPackets = 10000; |
| 386 | const int kPacketSize = 10; |
| 387 | |
| 388 | FakeNetworkPipe::Config config; |
| 389 | config.queue_length_packets = kNumPackets; |
| 390 | config.loss_percent = kLossPercent; |
| 391 | config.avg_burst_loss_length = kAvgBurstLength; |
| 392 | std::unique_ptr<FakeNetworkPipe> pipe( |
| 393 | new FakeNetworkPipe(&fake_clock_, config)); |
| 394 | ReorderTestReceiver* receiver = new ReorderTestReceiver(); |
| 395 | receiver_.reset(receiver); |
| 396 | pipe->SetReceiver(receiver_.get()); |
| 397 | |
| 398 | SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 399 | fake_clock_.AdvanceTimeMilliseconds(1000); |
| 400 | pipe->Process(); |
| 401 | |
| 402 | // Check that the average loss is |kLossPercent| percent. |
| 403 | int lost_packets = kNumPackets - receiver->delivered_sequence_numbers_.size(); |
| 404 | double loss_fraction = lost_packets / static_cast<double>(kNumPackets); |
| 405 | |
| 406 | EXPECT_NEAR(kLossPercent / 100.0, loss_fraction, 0.05); |
| 407 | |
| 408 | // Find the number of bursts that has occurred. |
| 409 | size_t received_packets = receiver->delivered_sequence_numbers_.size(); |
| 410 | int num_bursts = 0; |
| 411 | for (size_t i = 0; i < received_packets - 1; ++i) { |
| 412 | int diff = receiver->delivered_sequence_numbers_[i + 1] - |
| 413 | receiver->delivered_sequence_numbers_[i]; |
| 414 | if (diff > 1) |
| 415 | ++num_bursts; |
| 416 | } |
| 417 | |
| 418 | double average_burst_length = static_cast<double>(lost_packets) / num_bursts; |
| 419 | |
| 420 | EXPECT_NEAR(kAvgBurstLength, average_burst_length, 0.3); |
| 421 | } |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 422 | } // namespace webrtc |