blob: 9c4a3bf75584fae7e03f6e2dd4f57d02c24cced0 [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
Erik Språng09708512018-03-14 15:16:50 +010011#include "call/fake_network_pipe.h"
12
kwibergbfefb032016-05-01 14:53:46 -070013#include <memory>
Sebastian Jansson487c09b2019-02-21 16:21:51 +010014#include <utility>
kwibergbfefb032016-05-01 14:53:46 -070015
Artem Titove23b8a92018-08-16 15:51:07 +020016#include "call/simulated_network.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "system_wrappers/include/clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "test/gmock.h"
19#include "test/gtest.h"
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000020
21using ::testing::_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000022
23namespace webrtc {
24
minyue20c84cc2017-04-10 16:57:57 -070025class MockReceiver : public PacketReceiver {
26 public:
Danil Chapovalov292a73e2017-12-07 17:00:40 +010027 MOCK_METHOD3(DeliverPacket,
Niels Möller70082872018-08-07 11:03:12 +020028 DeliveryStatus(MediaType, rtc::CopyOnWriteBuffer, int64_t));
Sebastian Jansson09408112018-04-24 14:41:22 +020029 virtual ~MockReceiver() = default;
30};
31
32class ReorderTestReceiver : public MockReceiver {
33 public:
34 DeliveryStatus DeliverPacket(MediaType media_type,
35 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +020036 int64_t /* packet_time_us */) override {
Sebastian Jansson09408112018-04-24 14:41:22 +020037 RTC_DCHECK_GE(packet.size(), sizeof(int));
38 int seq_num;
39 memcpy(&seq_num, packet.data<uint8_t>(), sizeof(int));
40 delivered_sequence_numbers_.push_back(seq_num);
41 return DeliveryStatus::DELIVERY_OK;
42 }
43 std::vector<int> delivered_sequence_numbers_;
minyue20c84cc2017-04-10 16:57:57 -070044};
45
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000046class FakeNetworkPipeTest : public ::testing::Test {
Peter Boströmd3c94472015-12-09 11:20:58 +010047 public:
48 FakeNetworkPipeTest() : fake_clock_(12345) {}
49
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000050 protected:
philipela2c55232016-01-26 08:41:53 -080051 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) {
kwiberg352444f2016-11-28 15:58:53 -080052 RTC_DCHECK_GE(packet_size, sizeof(int));
kwibergbfefb032016-05-01 14:53:46 -070053 std::unique_ptr<uint8_t[]> packet(new uint8_t[packet_size]);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000054 for (int i = 0; i < number_packets; ++i) {
philipela2c55232016-01-26 08:41:53 -080055 // Set a sequence number for the packets by
56 // using the first bytes in the packet.
57 memcpy(packet.get(), &i, sizeof(int));
Sebastian Jansson09408112018-04-24 14:41:22 +020058 rtc::CopyOnWriteBuffer buffer(packet.get(), packet_size);
Niels Möller70082872018-08-07 11:03:12 +020059 pipe->DeliverPacket(MediaType::ANY, buffer, /* packet_time_us */ -1);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000060 }
61 }
62
philipela2c55232016-01-26 08:41:53 -080063 int PacketTimeMs(int capacity_kbps, int packet_size) const {
64 return 8 * packet_size / capacity_kbps;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000065 }
66
Peter Boströmd3c94472015-12-09 11:20:58 +010067 SimulatedClock fake_clock_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000068};
69
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000070// Test the capacity link and verify we get as many packets as we expect.
71TEST_F(FakeNetworkPipeTest, CapacityTest) {
Artem Titov75e36472018-10-08 12:28:56 +020072 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +000073 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000074 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +020075 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +020076 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 13:00:54 +020077 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
78 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000079
80 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
81 // get through the pipe.
82 const int kNumPackets = 10;
83 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 16:57:57 -070084 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000085
86 // Time to get one packet through the link.
Erik Språng09708512018-03-14 15:16:50 +010087 const int kPacketTimeMs =
88 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000089
90 // Time haven't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 14:41:22 +020091 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000092 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000093
94 // Advance enough time to release one packet.
Peter Boströmd3c94472015-12-09 11:20:58 +010095 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 14:41:22 +020096 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000097 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000098
99 // Release all but one packet
Peter Boströmd3c94472015-12-09 11:20:58 +0100100 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1);
Sebastian Jansson09408112018-04-24 14:41:22 +0200101 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(8);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000102 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000103
104 // And the last one.
Peter Boströmd3c94472015-12-09 11:20:58 +0100105 fake_clock_.AdvanceTimeMilliseconds(1);
Sebastian Jansson09408112018-04-24 14:41:22 +0200106 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000107 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000108}
109
110// Test the extra network delay.
111TEST_F(FakeNetworkPipeTest, ExtraDelayTest) {
Artem Titov75e36472018-10-08 12:28:56 +0200112 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000113 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000114 config.queue_delay_ms = 100;
115 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +0200116 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200117 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 13:00:54 +0200118 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
119 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000120
121 const int kNumPackets = 2;
122 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 16:57:57 -0700123 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000124
125 // Time to get one packet through the link.
Erik Språng09708512018-03-14 15:16:50 +0100126 const int kPacketTimeMs =
127 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000128
129 // Increase more than kPacketTimeMs, but not more than the extra delay.
Peter Boströmd3c94472015-12-09 11:20:58 +0100130 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 14:41:22 +0200131 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000132 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000133
134 // Advance the network delay to get the first packet.
Peter Boströmd3c94472015-12-09 11:20:58 +0100135 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
Sebastian Jansson09408112018-04-24 14:41:22 +0200136 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000137 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000138
139 // Advance one more kPacketTimeMs to get the last packet.
Peter Boströmd3c94472015-12-09 11:20:58 +0100140 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 14:41:22 +0200141 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000142 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000143}
144
145// Test the number of buffers and packets are dropped when sending too many
146// packets too quickly.
147TEST_F(FakeNetworkPipeTest, QueueLengthTest) {
Artem Titov75e36472018-10-08 12:28:56 +0200148 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000149 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000150 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +0200151 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200152 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 13:00:54 +0200153 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
154 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000155
156 const int kPacketSize = 1000;
Erik Språng09708512018-03-14 15:16:50 +0100157 const int kPacketTimeMs =
158 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000159
160 // Send three packets and verify only 2 are delivered.
161 SendPackets(pipe.get(), 3, kPacketSize);
162
163 // Increase time enough to deliver all three packets, verify only two are
164 // delivered.
Peter Boströmd3c94472015-12-09 11:20:58 +0100165 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs);
Sebastian Jansson09408112018-04-24 14:41:22 +0200166 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000167 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000168}
169
170// Test we get statistics as expected.
171TEST_F(FakeNetworkPipeTest, StatisticsTest) {
Artem Titov75e36472018-10-08 12:28:56 +0200172 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000173 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000174 config.queue_delay_ms = 20;
175 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +0200176 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200177 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 13:00:54 +0200178 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
179 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000180
181 const int kPacketSize = 1000;
Erik Språng09708512018-03-14 15:16:50 +0100182 const int kPacketTimeMs =
183 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000184
185 // Send three packets and verify only 2 are delivered.
186 SendPackets(pipe.get(), 3, kPacketSize);
Peter Boströmd3c94472015-12-09 11:20:58 +0100187 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs +
188 config.queue_delay_ms);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000189
Sebastian Jansson09408112018-04-24 14:41:22 +0200190 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000191 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000192
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000193 // Packet 1: kPacketTimeMs + config.queue_delay_ms,
194 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average.
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000195 EXPECT_EQ(pipe->AverageDelay(), 170);
Erik Språng09708512018-03-14 15:16:50 +0100196 EXPECT_EQ(pipe->SentPackets(), 2u);
197 EXPECT_EQ(pipe->DroppedPackets(), 1u);
198 EXPECT_EQ(pipe->PercentageLoss(), 1 / 3.f);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000199}
200
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000201// Change the link capacity half-way through the test and verify that the
202// delivery times change accordingly.
203TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) {
Artem Titov75e36472018-10-08 12:28:56 +0200204 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000205 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000206 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +0200207 MockReceiver receiver;
Artem Titove23b8a92018-08-16 15:51:07 +0200208 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
209 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 14:41:22 +0200210 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 17:02:20 +0200211 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000212
213 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
214 // get through the pipe.
215 const int kNumPackets = 10;
216 const int kPacketSize = 1000;
217 SendPackets(pipe.get(), kNumPackets, kPacketSize);
218
219 // Time to get one packet through the link.
220 int packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
221
222 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 14:41:22 +0200223 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000224 pipe->Process();
225
226 // Advance time in steps to release one packet at a time.
227 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 11:20:58 +0100228 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
Sebastian Jansson09408112018-04-24 14:41:22 +0200229 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000230 pipe->Process();
231 }
232
233 // Change the capacity.
234 config.link_capacity_kbps /= 2; // Reduce to 50%.
Artem Titove23b8a92018-08-16 15:51:07 +0200235 simulated_network->SetConfig(config);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000236
237 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
238 // seconds to get them through the pipe.
239 SendPackets(pipe.get(), kNumPackets, kPacketSize);
240
241 // Time to get one packet through the link.
242 packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
243
244 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 14:41:22 +0200245 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000246 pipe->Process();
247
248 // Advance time in steps to release one packet at a time.
249 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 11:20:58 +0100250 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
Sebastian Jansson09408112018-04-24 14:41:22 +0200251 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000252 pipe->Process();
253 }
254
255 // Check that all the packets were sent.
Erik Språng09708512018-03-14 15:16:50 +0100256 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->SentPackets());
Sebastian Jansson487c09b2019-02-21 16:21:51 +0100257 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value());
258 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 14:41:22 +0200259 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000260 pipe->Process();
261}
262
263// Change the link capacity half-way through the test and verify that the
264// delivery times change accordingly.
265TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) {
Artem Titov75e36472018-10-08 12:28:56 +0200266 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000267 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000268 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +0200269 MockReceiver receiver;
Artem Titove23b8a92018-08-16 15:51:07 +0200270 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
271 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 14:41:22 +0200272 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 17:02:20 +0200273 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000274
Christoffer Rodbro813c79b2019-01-31 09:25:12 +0100275 // Add 20 packets of 1000 bytes, = 80 kb.
276 const int kNumPackets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000277 const int kPacketSize = 1000;
278 SendPackets(pipe.get(), kNumPackets, kPacketSize);
279
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000280 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 14:41:22 +0200281 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000282 pipe->Process();
283
Christoffer Rodbro813c79b2019-01-31 09:25:12 +0100284 // Advance time in steps to release half of the packets one at a time.
285 int step_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
286 for (int i = 0; i < kNumPackets / 2; ++i) {
287 fake_clock_.AdvanceTimeMilliseconds(step_ms);
Sebastian Jansson09408112018-04-24 14:41:22 +0200288 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000289 pipe->Process();
290 }
291
Christoffer Rodbro813c79b2019-01-31 09:25:12 +0100292 // Change the capacity.
293 config.link_capacity_kbps *= 2; // Double the capacity.
294 simulated_network->SetConfig(config);
295
296 // Advance time in steps to release remaining packets one at a time.
297 step_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
298 for (int i = 0; i < kNumPackets / 2; ++i) {
299 fake_clock_.AdvanceTimeMilliseconds(step_ms);
Sebastian Jansson09408112018-04-24 14:41:22 +0200300 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000301 pipe->Process();
302 }
303
304 // Check that all the packets were sent.
Christoffer Rodbro813c79b2019-01-31 09:25:12 +0100305 EXPECT_EQ(static_cast<size_t>(kNumPackets), pipe->SentPackets());
Sebastian Jansson487c09b2019-02-21 16:21:51 +0100306 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value());
307 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 14:41:22 +0200308 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000309 pipe->Process();
310}
philipela2c55232016-01-26 08:41:53 -0800311
312// At first disallow reordering and then allow reordering.
313TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) {
Artem Titov75e36472018-10-08 12:28:56 +0200314 BuiltInNetworkBehaviorConfig config;
philipela2c55232016-01-26 08:41:53 -0800315 config.queue_length_packets = 1000;
316 config.link_capacity_kbps = 800;
317 config.queue_delay_ms = 100;
318 config.delay_standard_deviation_ms = 10;
Sebastian Jansson09408112018-04-24 14:41:22 +0200319 ReorderTestReceiver receiver;
Artem Titove23b8a92018-08-16 15:51:07 +0200320 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
321 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 14:41:22 +0200322 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 17:02:20 +0200323 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
philipela2c55232016-01-26 08:41:53 -0800324
325 const uint32_t kNumPackets = 100;
326 const int kPacketSize = 10;
327 SendPackets(pipe.get(), kNumPackets, kPacketSize);
328 fake_clock_.AdvanceTimeMilliseconds(1000);
329 pipe->Process();
330
331 // Confirm that all packets have been delivered in order.
Sebastian Jansson09408112018-04-24 14:41:22 +0200332 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 08:41:53 -0800333 int last_seq_num = -1;
Sebastian Jansson09408112018-04-24 14:41:22 +0200334 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 08:41:53 -0800335 EXPECT_GT(seq_num, last_seq_num);
336 last_seq_num = seq_num;
337 }
338
339 config.allow_reordering = true;
Artem Titove23b8a92018-08-16 15:51:07 +0200340 simulated_network->SetConfig(config);
philipela2c55232016-01-26 08:41:53 -0800341 SendPackets(pipe.get(), kNumPackets, kPacketSize);
342 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 14:41:22 +0200343 receiver.delivered_sequence_numbers_.clear();
philipela2c55232016-01-26 08:41:53 -0800344 pipe->Process();
345
346 // Confirm that all packets have been delivered
347 // and that reordering has occured.
Sebastian Jansson09408112018-04-24 14:41:22 +0200348 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 08:41:53 -0800349 bool reordering_has_occured = false;
350 last_seq_num = -1;
Sebastian Jansson09408112018-04-24 14:41:22 +0200351 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 08:41:53 -0800352 if (last_seq_num > seq_num) {
353 reordering_has_occured = true;
354 break;
355 }
356 last_seq_num = seq_num;
357 }
358 EXPECT_TRUE(reordering_has_occured);
359}
philipel536378b2016-05-31 03:20:23 -0700360
361TEST_F(FakeNetworkPipeTest, BurstLoss) {
362 const int kLossPercent = 5;
363 const int kAvgBurstLength = 3;
364 const int kNumPackets = 10000;
365 const int kPacketSize = 10;
366
Artem Titov75e36472018-10-08 12:28:56 +0200367 BuiltInNetworkBehaviorConfig config;
philipel536378b2016-05-31 03:20:23 -0700368 config.queue_length_packets = kNumPackets;
369 config.loss_percent = kLossPercent;
370 config.avg_burst_loss_length = kAvgBurstLength;
Sebastian Jansson09408112018-04-24 14:41:22 +0200371 ReorderTestReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200372 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titovc7ea8522018-08-29 09:07:27 +0200373 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
374 &fake_clock_, std::move(simulated_network), &receiver));
philipel536378b2016-05-31 03:20:23 -0700375
376 SendPackets(pipe.get(), kNumPackets, kPacketSize);
377 fake_clock_.AdvanceTimeMilliseconds(1000);
378 pipe->Process();
379
380 // Check that the average loss is |kLossPercent| percent.
Sebastian Jansson09408112018-04-24 14:41:22 +0200381 int lost_packets = kNumPackets - receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 03:20:23 -0700382 double loss_fraction = lost_packets / static_cast<double>(kNumPackets);
383
384 EXPECT_NEAR(kLossPercent / 100.0, loss_fraction, 0.05);
385
386 // Find the number of bursts that has occurred.
Sebastian Jansson09408112018-04-24 14:41:22 +0200387 size_t received_packets = receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 03:20:23 -0700388 int num_bursts = 0;
389 for (size_t i = 0; i < received_packets - 1; ++i) {
Sebastian Jansson09408112018-04-24 14:41:22 +0200390 int diff = receiver.delivered_sequence_numbers_[i + 1] -
391 receiver.delivered_sequence_numbers_[i];
philipel536378b2016-05-31 03:20:23 -0700392 if (diff > 1)
393 ++num_bursts;
394 }
395
396 double average_burst_length = static_cast<double>(lost_packets) / num_bursts;
397
398 EXPECT_NEAR(kAvgBurstLength, average_burst_length, 0.3);
399}
minyue20c84cc2017-04-10 16:57:57 -0700400
401TEST_F(FakeNetworkPipeTest, SetReceiver) {
Artem Titov75e36472018-10-08 12:28:56 +0200402 BuiltInNetworkBehaviorConfig config;
Sebastian Jansson09408112018-04-24 14:41:22 +0200403 config.link_capacity_kbps = 800;
404 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200405 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 13:00:54 +0200406 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
407 &fake_clock_, std::move(simulated_network), &receiver));
minyue20c84cc2017-04-10 16:57:57 -0700408
Sebastian Jansson09408112018-04-24 14:41:22 +0200409 const int kPacketSize = 1000;
410 const int kPacketTimeMs =
411 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
412 SendPackets(pipe.get(), 1, kPacketSize);
413 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
414 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
415 pipe->Process();
minyue20c84cc2017-04-10 16:57:57 -0700416
Sebastian Jansson09408112018-04-24 14:41:22 +0200417 MockReceiver new_receiver;
418 pipe->SetReceiver(&new_receiver);
minyue20c84cc2017-04-10 16:57:57 -0700419
Sebastian Jansson09408112018-04-24 14:41:22 +0200420 SendPackets(pipe.get(), 1, kPacketSize);
421 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
422 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
423 EXPECT_CALL(new_receiver, DeliverPacket(_, _, _)).Times(1);
424 pipe->Process();
minyue20c84cc2017-04-10 16:57:57 -0700425}
426
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000427} // namespace webrtc