blob: 9c4a3bf75584fae7e03f6e2dd4f57d02c24cced0 [file] [log] [blame]
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:221/*
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 14:16:5011#include "call/fake_network_pipe.h"
12
kwibergbfefb032016-05-01 21:53:4613#include <memory>
Sebastian Jansson487c09b2019-02-21 15:21:5114#include <utility>
kwibergbfefb032016-05-01 21:53:4615
Artem Titove23b8a92018-08-16 13:51:0716#include "call/simulated_network.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "system_wrappers/include/clock.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "test/gmock.h"
19#include "test/gtest.h"
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2220
21using ::testing::_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2222
23namespace webrtc {
24
minyue20c84cc2017-04-10 23:57:5725class MockReceiver : public PacketReceiver {
26 public:
Danil Chapovalov292a73e2017-12-07 16:00:4027 MOCK_METHOD3(DeliverPacket,
Niels Möller70082872018-08-07 09:03:1228 DeliveryStatus(MediaType, rtc::CopyOnWriteBuffer, int64_t));
Sebastian Jansson09408112018-04-24 12:41:2229 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 09:03:1236 int64_t /* packet_time_us */) override {
Sebastian Jansson09408112018-04-24 12:41:2237 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 23:57:5744};
45
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2246class FakeNetworkPipeTest : public ::testing::Test {
Peter Boströmd3c94472015-12-09 10:20:5847 public:
48 FakeNetworkPipeTest() : fake_clock_(12345) {}
49
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2250 protected:
philipela2c55232016-01-26 16:41:5351 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) {
kwiberg352444f2016-11-28 23:58:5352 RTC_DCHECK_GE(packet_size, sizeof(int));
kwibergbfefb032016-05-01 21:53:4653 std::unique_ptr<uint8_t[]> packet(new uint8_t[packet_size]);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2254 for (int i = 0; i < number_packets; ++i) {
philipela2c55232016-01-26 16:41:5355 // 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 12:41:2258 rtc::CopyOnWriteBuffer buffer(packet.get(), packet_size);
Niels Möller70082872018-08-07 09:03:1259 pipe->DeliverPacket(MediaType::ANY, buffer, /* packet_time_us */ -1);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2260 }
61 }
62
philipela2c55232016-01-26 16:41:5363 int PacketTimeMs(int capacity_kbps, int packet_size) const {
64 return 8 * packet_size / capacity_kbps;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2265 }
66
Peter Boströmd3c94472015-12-09 10:20:5867 SimulatedClock fake_clock_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2268};
69
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2270// Test the capacity link and verify we get as many packets as we expect.
71TEST_F(FakeNetworkPipeTest, CapacityTest) {
Artem Titov75e36472018-10-08 10:28:5672 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:0673 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:1174 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:2275 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:1876 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 11:00:5477 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
78 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2279
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 23:57:5784 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2285
86 // Time to get one packet through the link.
Erik Språng09708512018-03-14 14:16:5087 const int kPacketTimeMs =
88 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2289
90 // Time haven't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 12:41:2291 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:2592 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2293
94 // Advance enough time to release one packet.
Peter Boströmd3c94472015-12-09 10:20:5895 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 12:41:2296 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:2597 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2298
99 // Release all but one packet
Peter Boströmd3c94472015-12-09 10:20:58100 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1);
Sebastian Jansson09408112018-04-24 12:41:22101 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(8);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25102 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22103
104 // And the last one.
Peter Boströmd3c94472015-12-09 10:20:58105 fake_clock_.AdvanceTimeMilliseconds(1);
Sebastian Jansson09408112018-04-24 12:41:22106 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25107 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22108}
109
110// Test the extra network delay.
111TEST_F(FakeNetworkPipeTest, ExtraDelayTest) {
Artem Titov75e36472018-10-08 10:28:56112 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06113 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11114 config.queue_delay_ms = 100;
115 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22116 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:18117 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 11:00:54118 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
119 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22120
121 const int kNumPackets = 2;
122 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 23:57:57123 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22124
125 // Time to get one packet through the link.
Erik Språng09708512018-03-14 14:16:50126 const int kPacketTimeMs =
127 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22128
129 // Increase more than kPacketTimeMs, but not more than the extra delay.
Peter Boströmd3c94472015-12-09 10:20:58130 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 12:41:22131 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25132 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22133
134 // Advance the network delay to get the first packet.
Peter Boströmd3c94472015-12-09 10:20:58135 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
Sebastian Jansson09408112018-04-24 12:41:22136 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25137 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22138
139 // Advance one more kPacketTimeMs to get the last packet.
Peter Boströmd3c94472015-12-09 10:20:58140 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 12:41:22141 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25142 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22143}
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 10:28:56148 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06149 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11150 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22151 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:18152 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 11:00:54153 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
154 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22155
156 const int kPacketSize = 1000;
Erik Språng09708512018-03-14 14:16:50157 const int kPacketTimeMs =
158 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22159
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 10:20:58165 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs);
Sebastian Jansson09408112018-04-24 12:41:22166 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25167 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22168}
169
170// Test we get statistics as expected.
171TEST_F(FakeNetworkPipeTest, StatisticsTest) {
Artem Titov75e36472018-10-08 10:28:56172 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06173 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11174 config.queue_delay_ms = 20;
175 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22176 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:18177 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 11:00:54178 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
179 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22180
181 const int kPacketSize = 1000;
Erik Språng09708512018-03-14 14:16:50182 const int kPacketTimeMs =
183 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22184
185 // Send three packets and verify only 2 are delivered.
186 SendPackets(pipe.get(), 3, kPacketSize);
Peter Boströmd3c94472015-12-09 10:20:58187 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs +
188 config.queue_delay_ms);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22189
Sebastian Jansson09408112018-04-24 12:41:22190 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25191 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22192
mflodman@webrtc.org7acb65a2012-12-13 15:53:11193 // 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:22195 EXPECT_EQ(pipe->AverageDelay(), 170);
Erik Språng09708512018-03-14 14:16:50196 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:22199}
200
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52201// 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 10:28:56204 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06205 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52206 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22207 MockReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07208 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
209 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22210 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20211 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52212
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 12:41:22223 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52224 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 10:20:58228 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
Sebastian Jansson09408112018-04-24 12:41:22229 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52230 pipe->Process();
231 }
232
233 // Change the capacity.
234 config.link_capacity_kbps /= 2; // Reduce to 50%.
Artem Titove23b8a92018-08-16 13:51:07235 simulated_network->SetConfig(config);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52236
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 12:41:22245 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52246 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 10:20:58250 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
Sebastian Jansson09408112018-04-24 12:41:22251 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52252 pipe->Process();
253 }
254
255 // Check that all the packets were sent.
Erik Språng09708512018-03-14 14:16:50256 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->SentPackets());
Sebastian Jansson487c09b2019-02-21 15:21:51257 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value());
258 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 12:41:22259 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52260 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 10:28:56266 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06267 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52268 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22269 MockReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07270 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
271 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22272 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20273 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52274
Christoffer Rodbro813c79b2019-01-31 08:25:12275 // Add 20 packets of 1000 bytes, = 80 kb.
276 const int kNumPackets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52277 const int kPacketSize = 1000;
278 SendPackets(pipe.get(), kNumPackets, kPacketSize);
279
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52280 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 12:41:22281 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52282 pipe->Process();
283
Christoffer Rodbro813c79b2019-01-31 08:25:12284 // 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 12:41:22288 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52289 pipe->Process();
290 }
291
Christoffer Rodbro813c79b2019-01-31 08:25:12292 // 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 12:41:22300 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52301 pipe->Process();
302 }
303
304 // Check that all the packets were sent.
Christoffer Rodbro813c79b2019-01-31 08:25:12305 EXPECT_EQ(static_cast<size_t>(kNumPackets), pipe->SentPackets());
Sebastian Jansson487c09b2019-02-21 15:21:51306 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value());
307 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 12:41:22308 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52309 pipe->Process();
310}
philipela2c55232016-01-26 16:41:53311
312// At first disallow reordering and then allow reordering.
313TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) {
Artem Titov75e36472018-10-08 10:28:56314 BuiltInNetworkBehaviorConfig config;
philipela2c55232016-01-26 16:41:53315 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 12:41:22319 ReorderTestReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07320 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
321 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22322 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20323 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
philipela2c55232016-01-26 16:41:53324
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 12:41:22332 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 16:41:53333 int last_seq_num = -1;
Sebastian Jansson09408112018-04-24 12:41:22334 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 16:41:53335 EXPECT_GT(seq_num, last_seq_num);
336 last_seq_num = seq_num;
337 }
338
339 config.allow_reordering = true;
Artem Titove23b8a92018-08-16 13:51:07340 simulated_network->SetConfig(config);
philipela2c55232016-01-26 16:41:53341 SendPackets(pipe.get(), kNumPackets, kPacketSize);
342 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 12:41:22343 receiver.delivered_sequence_numbers_.clear();
philipela2c55232016-01-26 16:41:53344 pipe->Process();
345
346 // Confirm that all packets have been delivered
347 // and that reordering has occured.
Sebastian Jansson09408112018-04-24 12:41:22348 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 16:41:53349 bool reordering_has_occured = false;
350 last_seq_num = -1;
Sebastian Jansson09408112018-04-24 12:41:22351 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 16:41:53352 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 10:20:23360
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 10:28:56367 BuiltInNetworkBehaviorConfig config;
philipel536378b2016-05-31 10:20:23368 config.queue_length_packets = kNumPackets;
369 config.loss_percent = kLossPercent;
370 config.avg_burst_loss_length = kAvgBurstLength;
Sebastian Jansson09408112018-04-24 12:41:22371 ReorderTestReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:18372 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titovc7ea8522018-08-29 07:07:27373 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
374 &fake_clock_, std::move(simulated_network), &receiver));
philipel536378b2016-05-31 10:20:23375
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 12:41:22381 int lost_packets = kNumPackets - receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 10:20:23382 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 12:41:22387 size_t received_packets = receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 10:20:23388 int num_bursts = 0;
389 for (size_t i = 0; i < received_packets - 1; ++i) {
Sebastian Jansson09408112018-04-24 12:41:22390 int diff = receiver.delivered_sequence_numbers_[i + 1] -
391 receiver.delivered_sequence_numbers_[i];
philipel536378b2016-05-31 10:20:23392 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 23:57:57400
401TEST_F(FakeNetworkPipeTest, SetReceiver) {
Artem Titov75e36472018-10-08 10:28:56402 BuiltInNetworkBehaviorConfig config;
Sebastian Jansson09408112018-04-24 12:41:22403 config.link_capacity_kbps = 800;
404 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:18405 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 11:00:54406 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
407 &fake_clock_, std::move(simulated_network), &receiver));
minyue20c84cc2017-04-10 23:57:57408
Sebastian Jansson09408112018-04-24 12:41:22409 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 23:57:57416
Sebastian Jansson09408112018-04-24 12:41:22417 MockReceiver new_receiver;
418 pipe->SetReceiver(&new_receiver);
minyue20c84cc2017-04-10 23:57:57419
Sebastian Jansson09408112018-04-24 12:41:22420 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 23:57:57425}
426
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22427} // namespace webrtc