blob: 7374bb140590be7d6e30711ddf0e333a3273eab0 [file] [log] [blame]
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +00001/*
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
kwibergbfefb032016-05-01 14:53:46 -070011#include <memory>
12
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000013#include "webrtc/call.h"
Peter Boströmd3c94472015-12-09 11:20:58 +010014#include "webrtc/system_wrappers/include/clock.h"
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000015#include "webrtc/test/fake_network_pipe.h"
kwibergac9f8762016-09-30 22:29:43 -070016#include "webrtc/test/gmock.h"
17#include "webrtc/test/gtest.h"
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000018
19using ::testing::_;
20using ::testing::AnyNumber;
21using ::testing::Return;
22using ::testing::Invoke;
23
24namespace webrtc {
25
philipela2c55232016-01-26 08:41:53 -080026class TestReceiver : public PacketReceiver {
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000027 public:
philipela2c55232016-01-26 08:41:53 -080028 TestReceiver() {}
29 virtual ~TestReceiver() {}
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000030
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000031 void IncomingPacket(const uint8_t* data, size_t length) {
stefan68786d22015-09-08 05:36:15 -070032 DeliverPacket(MediaType::ANY, data, length, PacketTime());
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000033 delete [] data;
34 }
35
philipela2c55232016-01-26 08:41:53 -080036 virtual MOCK_METHOD4(
stefan68786d22015-09-08 05:36:15 -070037 DeliverPacket,
38 DeliveryStatus(MediaType, const uint8_t*, size_t, const PacketTime&));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000039};
40
philipela2c55232016-01-26 08:41:53 -080041class 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.orgeaf7cf22012-12-11 11:47:22 +000058class FakeNetworkPipeTest : public ::testing::Test {
Peter Boströmd3c94472015-12-09 11:20:58 +010059 public:
60 FakeNetworkPipeTest() : fake_clock_(12345) {}
61
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000062 protected:
63 virtual void SetUp() {
philipela2c55232016-01-26 08:41:53 -080064 receiver_.reset(new TestReceiver());
stefan68786d22015-09-08 05:36:15 -070065 ON_CALL(*receiver_, DeliverPacket(_, _, _, _))
stefan@webrtc.org1ccff342014-08-06 15:41:58 +000066 .WillByDefault(Return(PacketReceiver::DELIVERY_OK));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000067 }
68
69 virtual void TearDown() {
70 }
71
philipela2c55232016-01-26 08:41:53 -080072 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) {
73 RTC_DCHECK_GE(packet_size, static_cast<int>(sizeof(int)));
kwibergbfefb032016-05-01 14:53:46 -070074 std::unique_ptr<uint8_t[]> packet(new uint8_t[packet_size]);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000075 for (int i = 0; i < number_packets; ++i) {
philipela2c55232016-01-26 08:41:53 -080076 // 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.orgeaf7cf22012-12-11 11:47:22 +000080 }
81 }
82
philipela2c55232016-01-26 08:41:53 -080083 int PacketTimeMs(int capacity_kbps, int packet_size) const {
84 return 8 * packet_size / capacity_kbps;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000085 }
86
Peter Boströmd3c94472015-12-09 11:20:58 +010087 SimulatedClock fake_clock_;
kwibergbfefb032016-05-01 14:53:46 -070088 std::unique_ptr<TestReceiver> receiver_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000089};
90
91void DeleteMemory(uint8_t* data, int length) { delete [] data; }
92
93// Test the capacity link and verify we get as many packets as we expect.
94TEST_F(FakeNetworkPipeTest, CapacityTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000095 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +000096 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000097 config.link_capacity_kbps = 80;
kwibergbfefb032016-05-01 14:53:46 -070098 std::unique_ptr<FakeNetworkPipe> pipe(
Peter Boströmd3c94472015-12-09 11:20:58 +010099 new FakeNetworkPipe(&fake_clock_, config));
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000100 pipe->SetReceiver(receiver_.get());
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000101
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.org7acb65a2012-12-13 15:53:11 +0000109 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
110 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000111
112 // Time haven't increased yet, so we souldn't get any packets.
stefan68786d22015-09-08 05:36:15 -0700113 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000114 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000115
116 // Advance enough time to release one packet.
Peter Boströmd3c94472015-12-09 11:20:58 +0100117 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
stefan68786d22015-09-08 05:36:15 -0700118 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000119 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000120
121 // Release all but one packet
Peter Boströmd3c94472015-12-09 11:20:58 +0100122 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1);
stefan68786d22015-09-08 05:36:15 -0700123 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(8);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000124 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000125
126 // And the last one.
Peter Boströmd3c94472015-12-09 11:20:58 +0100127 fake_clock_.AdvanceTimeMilliseconds(1);
stefan68786d22015-09-08 05:36:15 -0700128 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000129 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000130}
131
132// Test the extra network delay.
133TEST_F(FakeNetworkPipeTest, ExtraDelayTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000134 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000135 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000136 config.queue_delay_ms = 100;
137 config.link_capacity_kbps = 80;
kwibergbfefb032016-05-01 14:53:46 -0700138 std::unique_ptr<FakeNetworkPipe> pipe(
Peter Boströmd3c94472015-12-09 11:20:58 +0100139 new FakeNetworkPipe(&fake_clock_, config));
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000140 pipe->SetReceiver(receiver_.get());
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000141
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.org7acb65a2012-12-13 15:53:11 +0000147 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
148 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000149
150 // Increase more than kPacketTimeMs, but not more than the extra delay.
Peter Boströmd3c94472015-12-09 11:20:58 +0100151 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
stefan68786d22015-09-08 05:36:15 -0700152 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000153 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000154
155 // Advance the network delay to get the first packet.
Peter Boströmd3c94472015-12-09 11:20:58 +0100156 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
stefan68786d22015-09-08 05:36:15 -0700157 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000158 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000159
160 // Advance one more kPacketTimeMs to get the last packet.
Peter Boströmd3c94472015-12-09 11:20:58 +0100161 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
stefan68786d22015-09-08 05:36:15 -0700162 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000163 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000164}
165
166// Test the number of buffers and packets are dropped when sending too many
167// packets too quickly.
168TEST_F(FakeNetworkPipeTest, QueueLengthTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000169 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000170 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000171 config.link_capacity_kbps = 80;
kwibergbfefb032016-05-01 14:53:46 -0700172 std::unique_ptr<FakeNetworkPipe> pipe(
Peter Boströmd3c94472015-12-09 11:20:58 +0100173 new FakeNetworkPipe(&fake_clock_, config));
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000174 pipe->SetReceiver(receiver_.get());
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000175
176 const int kPacketSize = 1000;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000177 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
178 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000179
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ömd3c94472015-12-09 11:20:58 +0100185 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs);
stefan68786d22015-09-08 05:36:15 -0700186 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000187 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000188}
189
190// Test we get statistics as expected.
191TEST_F(FakeNetworkPipeTest, StatisticsTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000192 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000193 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000194 config.queue_delay_ms = 20;
195 config.link_capacity_kbps = 80;
kwibergbfefb032016-05-01 14:53:46 -0700196 std::unique_ptr<FakeNetworkPipe> pipe(
Peter Boströmd3c94472015-12-09 11:20:58 +0100197 new FakeNetworkPipe(&fake_clock_, config));
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000198 pipe->SetReceiver(receiver_.get());
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000199
200 const int kPacketSize = 1000;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000201 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
202 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000203
204 // Send three packets and verify only 2 are delivered.
205 SendPackets(pipe.get(), 3, kPacketSize);
Peter Boströmd3c94472015-12-09 11:20:58 +0100206 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs +
207 config.queue_delay_ms);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000208
stefan68786d22015-09-08 05:36:15 -0700209 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000210 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000211
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000212 // Packet 1: kPacketTimeMs + config.queue_delay_ms,
213 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average.
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000214 EXPECT_EQ(pipe->AverageDelay(), 170);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000215 EXPECT_EQ(pipe->sent_packets(), 2u);
216 EXPECT_EQ(pipe->dropped_packets(), 1u);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000217 EXPECT_EQ(pipe->PercentageLoss(), 1/3.f);
218}
219
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000220// Change the link capacity half-way through the test and verify that the
221// delivery times change accordingly.
222TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) {
223 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000224 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000225 config.link_capacity_kbps = 80;
kwibergbfefb032016-05-01 14:53:46 -0700226 std::unique_ptr<FakeNetworkPipe> pipe(
Peter Boströmd3c94472015-12-09 11:20:58 +0100227 new FakeNetworkPipe(&fake_clock_, config));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000228 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.
stefan68786d22015-09-08 05:36:15 -0700240 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000241 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ömd3c94472015-12-09 11:20:58 +0100245 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
stefan68786d22015-09-08 05:36:15 -0700246 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000247 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.
stefan68786d22015-09-08 05:36:15 -0700262 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000263 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ömd3c94472015-12-09 11:20:58 +0100267 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
stefan68786d22015-09-08 05:36:15 -0700268 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000269 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ömd3c94472015-12-09 11:20:58 +0100274 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess());
stefan68786d22015-09-08 05:36:15 -0700275 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000276 pipe->Process();
277}
278
279// Change the link capacity half-way through the test and verify that the
280// delivery times change accordingly.
281TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) {
282 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000283 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000284 config.link_capacity_kbps = 80;
kwibergbfefb032016-05-01 14:53:46 -0700285 std::unique_ptr<FakeNetworkPipe> pipe(
Peter Boströmd3c94472015-12-09 11:20:58 +0100286 new FakeNetworkPipe(&fake_clock_, config));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000287 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.
stefan68786d22015-09-08 05:36:15 -0700309 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000310 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ömd3c94472015-12-09 11:20:58 +0100314 fake_clock_.AdvanceTimeMilliseconds(packet_time_1_ms);
stefan68786d22015-09-08 05:36:15 -0700315 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000316 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ömd3c94472015-12-09 11:20:58 +0100321 fake_clock_.AdvanceTimeMilliseconds(packet_time_2_ms);
stefan68786d22015-09-08 05:36:15 -0700322 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000323 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ömd3c94472015-12-09 11:20:58 +0100328 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess());
stefan68786d22015-09-08 05:36:15 -0700329 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000330 pipe->Process();
331}
philipela2c55232016-01-26 08:41:53 -0800332
333// At first disallow reordering and then allow reordering.
334TEST_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;
kwibergbfefb032016-05-01 14:53:46 -0700340 std::unique_ptr<FakeNetworkPipe> pipe(
philipela2c55232016-01-26 08:41:53 -0800341 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}
philipel536378b2016-05-31 03:20:23 -0700381
382TEST_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.orgeaf7cf22012-12-11 11:47:22 +0000422} // namespace webrtc