blob: 852a427222044d10fcc8feb322d7b4aa94be59f8 [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 Chapovalov9a5efe92020-05-14 22:06:18 +020027 MOCK_METHOD(DeliveryStatus,
28 DeliverPacket,
29 (MediaType, rtc::CopyOnWriteBuffer, int64_t),
30 (override));
Sebastian Jansson09408112018-04-24 14:41:22 +020031 virtual ~MockReceiver() = default;
32};
33
34class ReorderTestReceiver : public MockReceiver {
35 public:
36 DeliveryStatus DeliverPacket(MediaType media_type,
37 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +020038 int64_t /* packet_time_us */) override {
Sebastian Jansson09408112018-04-24 14:41:22 +020039 RTC_DCHECK_GE(packet.size(), sizeof(int));
40 int seq_num;
41 memcpy(&seq_num, packet.data<uint8_t>(), sizeof(int));
42 delivered_sequence_numbers_.push_back(seq_num);
43 return DeliveryStatus::DELIVERY_OK;
44 }
45 std::vector<int> delivered_sequence_numbers_;
minyue20c84cc2017-04-10 16:57:57 -070046};
47
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000048class FakeNetworkPipeTest : public ::testing::Test {
Peter Boströmd3c94472015-12-09 11:20:58 +010049 public:
50 FakeNetworkPipeTest() : fake_clock_(12345) {}
51
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000052 protected:
philipela2c55232016-01-26 08:41:53 -080053 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) {
kwiberg352444f2016-11-28 15:58:53 -080054 RTC_DCHECK_GE(packet_size, sizeof(int));
kwibergbfefb032016-05-01 14:53:46 -070055 std::unique_ptr<uint8_t[]> packet(new uint8_t[packet_size]);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000056 for (int i = 0; i < number_packets; ++i) {
philipela2c55232016-01-26 08:41:53 -080057 // Set a sequence number for the packets by
58 // using the first bytes in the packet.
59 memcpy(packet.get(), &i, sizeof(int));
Sebastian Jansson09408112018-04-24 14:41:22 +020060 rtc::CopyOnWriteBuffer buffer(packet.get(), packet_size);
Niels Möller70082872018-08-07 11:03:12 +020061 pipe->DeliverPacket(MediaType::ANY, buffer, /* packet_time_us */ -1);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000062 }
63 }
64
philipela2c55232016-01-26 08:41:53 -080065 int PacketTimeMs(int capacity_kbps, int packet_size) const {
66 return 8 * packet_size / capacity_kbps;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000067 }
68
Peter Boströmd3c94472015-12-09 11:20:58 +010069 SimulatedClock fake_clock_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000070};
71
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000072// Test the capacity link and verify we get as many packets as we expect.
73TEST_F(FakeNetworkPipeTest, CapacityTest) {
Artem Titov75e36472018-10-08 12:28:56 +020074 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +000075 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000076 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +020077 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +020078 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 13:00:54 +020079 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
80 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000081
82 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
83 // get through the pipe.
84 const int kNumPackets = 10;
85 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 16:57:57 -070086 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000087
88 // Time to get one packet through the link.
Erik Språng09708512018-03-14 15:16:50 +010089 const int kPacketTimeMs =
90 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000091
92 // Time haven't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 14:41:22 +020093 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000094 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000095
96 // Advance enough time to release one packet.
Peter Boströmd3c94472015-12-09 11:20:58 +010097 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 14:41:22 +020098 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000099 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000100
101 // Release all but one packet
Peter Boströmd3c94472015-12-09 11:20:58 +0100102 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1);
Sebastian Jansson09408112018-04-24 14:41:22 +0200103 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(8);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000104 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000105
106 // And the last one.
Peter Boströmd3c94472015-12-09 11:20:58 +0100107 fake_clock_.AdvanceTimeMilliseconds(1);
Sebastian Jansson09408112018-04-24 14:41:22 +0200108 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000109 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000110}
111
112// Test the extra network delay.
113TEST_F(FakeNetworkPipeTest, ExtraDelayTest) {
Artem Titov75e36472018-10-08 12:28:56 +0200114 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000115 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000116 config.queue_delay_ms = 100;
117 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +0200118 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200119 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 13:00:54 +0200120 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
121 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000122
123 const int kNumPackets = 2;
124 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 16:57:57 -0700125 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000126
127 // Time to get one packet through the link.
Erik Språng09708512018-03-14 15:16:50 +0100128 const int kPacketTimeMs =
129 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000130
131 // Increase more than kPacketTimeMs, but not more than the extra delay.
Peter Boströmd3c94472015-12-09 11:20:58 +0100132 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 14:41:22 +0200133 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000134 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000135
136 // Advance the network delay to get the first packet.
Peter Boströmd3c94472015-12-09 11:20:58 +0100137 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
Sebastian Jansson09408112018-04-24 14:41:22 +0200138 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000139 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000140
141 // Advance one more kPacketTimeMs to get the last packet.
Peter Boströmd3c94472015-12-09 11:20:58 +0100142 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 14:41:22 +0200143 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000144 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000145}
146
147// Test the number of buffers and packets are dropped when sending too many
148// packets too quickly.
149TEST_F(FakeNetworkPipeTest, QueueLengthTest) {
Artem Titov75e36472018-10-08 12:28:56 +0200150 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000151 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000152 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +0200153 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200154 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 13:00:54 +0200155 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
156 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000157
158 const int kPacketSize = 1000;
Erik Språng09708512018-03-14 15:16:50 +0100159 const int kPacketTimeMs =
160 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000161
162 // Send three packets and verify only 2 are delivered.
163 SendPackets(pipe.get(), 3, kPacketSize);
164
165 // Increase time enough to deliver all three packets, verify only two are
166 // delivered.
Peter Boströmd3c94472015-12-09 11:20:58 +0100167 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs);
Sebastian Jansson09408112018-04-24 14:41:22 +0200168 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000169 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000170}
171
172// Test we get statistics as expected.
173TEST_F(FakeNetworkPipeTest, StatisticsTest) {
Artem Titov75e36472018-10-08 12:28:56 +0200174 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000175 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000176 config.queue_delay_ms = 20;
177 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +0200178 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200179 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 13:00:54 +0200180 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
181 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000182
183 const int kPacketSize = 1000;
Erik Språng09708512018-03-14 15:16:50 +0100184 const int kPacketTimeMs =
185 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000186
187 // Send three packets and verify only 2 are delivered.
188 SendPackets(pipe.get(), 3, kPacketSize);
Peter Boströmd3c94472015-12-09 11:20:58 +0100189 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs +
190 config.queue_delay_ms);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000191
Sebastian Jansson09408112018-04-24 14:41:22 +0200192 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000193 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000194
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000195 // Packet 1: kPacketTimeMs + config.queue_delay_ms,
196 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average.
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000197 EXPECT_EQ(pipe->AverageDelay(), 170);
Erik Språng09708512018-03-14 15:16:50 +0100198 EXPECT_EQ(pipe->SentPackets(), 2u);
199 EXPECT_EQ(pipe->DroppedPackets(), 1u);
200 EXPECT_EQ(pipe->PercentageLoss(), 1 / 3.f);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000201}
202
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000203// Change the link capacity half-way through the test and verify that the
204// delivery times change accordingly.
205TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) {
Artem Titov75e36472018-10-08 12:28:56 +0200206 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000207 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000208 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +0200209 MockReceiver receiver;
Artem Titove23b8a92018-08-16 15:51:07 +0200210 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
211 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 14:41:22 +0200212 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 17:02:20 +0200213 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000214
215 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
216 // get through the pipe.
217 const int kNumPackets = 10;
218 const int kPacketSize = 1000;
219 SendPackets(pipe.get(), kNumPackets, kPacketSize);
220
221 // Time to get one packet through the link.
222 int packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
223
224 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 14:41:22 +0200225 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000226 pipe->Process();
227
228 // Advance time in steps to release one packet at a time.
229 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 11:20:58 +0100230 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
Sebastian Jansson09408112018-04-24 14:41:22 +0200231 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000232 pipe->Process();
233 }
234
235 // Change the capacity.
236 config.link_capacity_kbps /= 2; // Reduce to 50%.
Artem Titove23b8a92018-08-16 15:51:07 +0200237 simulated_network->SetConfig(config);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000238
239 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
240 // seconds to get them through the pipe.
241 SendPackets(pipe.get(), kNumPackets, kPacketSize);
242
243 // Time to get one packet through the link.
244 packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
245
246 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 14:41:22 +0200247 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000248 pipe->Process();
249
250 // Advance time in steps to release one packet at a time.
251 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 11:20:58 +0100252 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
Sebastian Jansson09408112018-04-24 14:41:22 +0200253 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000254 pipe->Process();
255 }
256
257 // Check that all the packets were sent.
Erik Språng09708512018-03-14 15:16:50 +0100258 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->SentPackets());
Sebastian Jansson487c09b2019-02-21 16:21:51 +0100259 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value());
260 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 14:41:22 +0200261 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000262 pipe->Process();
263}
264
265// Change the link capacity half-way through the test and verify that the
266// delivery times change accordingly.
267TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) {
Artem Titov75e36472018-10-08 12:28:56 +0200268 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000269 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000270 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 14:41:22 +0200271 MockReceiver receiver;
Artem Titove23b8a92018-08-16 15:51:07 +0200272 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
273 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 14:41:22 +0200274 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 17:02:20 +0200275 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000276
Christoffer Rodbro813c79b2019-01-31 09:25:12 +0100277 // Add 20 packets of 1000 bytes, = 80 kb.
278 const int kNumPackets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000279 const int kPacketSize = 1000;
280 SendPackets(pipe.get(), kNumPackets, kPacketSize);
281
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000282 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 14:41:22 +0200283 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000284 pipe->Process();
285
Christoffer Rodbro813c79b2019-01-31 09:25:12 +0100286 // Advance time in steps to release half of the packets one at a time.
287 int step_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
288 for (int i = 0; i < kNumPackets / 2; ++i) {
289 fake_clock_.AdvanceTimeMilliseconds(step_ms);
Sebastian Jansson09408112018-04-24 14:41:22 +0200290 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000291 pipe->Process();
292 }
293
Christoffer Rodbro813c79b2019-01-31 09:25:12 +0100294 // Change the capacity.
295 config.link_capacity_kbps *= 2; // Double the capacity.
296 simulated_network->SetConfig(config);
297
298 // Advance time in steps to release remaining packets one at a time.
299 step_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
300 for (int i = 0; i < kNumPackets / 2; ++i) {
301 fake_clock_.AdvanceTimeMilliseconds(step_ms);
Sebastian Jansson09408112018-04-24 14:41:22 +0200302 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000303 pipe->Process();
304 }
305
306 // Check that all the packets were sent.
Christoffer Rodbro813c79b2019-01-31 09:25:12 +0100307 EXPECT_EQ(static_cast<size_t>(kNumPackets), pipe->SentPackets());
Sebastian Jansson487c09b2019-02-21 16:21:51 +0100308 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value());
309 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 14:41:22 +0200310 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000311 pipe->Process();
312}
philipela2c55232016-01-26 08:41:53 -0800313
314// At first disallow reordering and then allow reordering.
315TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) {
Artem Titov75e36472018-10-08 12:28:56 +0200316 BuiltInNetworkBehaviorConfig config;
philipela2c55232016-01-26 08:41:53 -0800317 config.queue_length_packets = 1000;
318 config.link_capacity_kbps = 800;
319 config.queue_delay_ms = 100;
320 config.delay_standard_deviation_ms = 10;
Sebastian Jansson09408112018-04-24 14:41:22 +0200321 ReorderTestReceiver receiver;
Artem Titove23b8a92018-08-16 15:51:07 +0200322 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
323 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 14:41:22 +0200324 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 17:02:20 +0200325 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
philipela2c55232016-01-26 08:41:53 -0800326
327 const uint32_t kNumPackets = 100;
328 const int kPacketSize = 10;
329 SendPackets(pipe.get(), kNumPackets, kPacketSize);
330 fake_clock_.AdvanceTimeMilliseconds(1000);
331 pipe->Process();
332
333 // Confirm that all packets have been delivered in order.
Sebastian Jansson09408112018-04-24 14:41:22 +0200334 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 08:41:53 -0800335 int last_seq_num = -1;
Sebastian Jansson09408112018-04-24 14:41:22 +0200336 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 08:41:53 -0800337 EXPECT_GT(seq_num, last_seq_num);
338 last_seq_num = seq_num;
339 }
340
341 config.allow_reordering = true;
Artem Titove23b8a92018-08-16 15:51:07 +0200342 simulated_network->SetConfig(config);
philipela2c55232016-01-26 08:41:53 -0800343 SendPackets(pipe.get(), kNumPackets, kPacketSize);
344 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 14:41:22 +0200345 receiver.delivered_sequence_numbers_.clear();
philipela2c55232016-01-26 08:41:53 -0800346 pipe->Process();
347
348 // Confirm that all packets have been delivered
349 // and that reordering has occured.
Sebastian Jansson09408112018-04-24 14:41:22 +0200350 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 08:41:53 -0800351 bool reordering_has_occured = false;
352 last_seq_num = -1;
Sebastian Jansson09408112018-04-24 14:41:22 +0200353 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 08:41:53 -0800354 if (last_seq_num > seq_num) {
355 reordering_has_occured = true;
356 break;
357 }
358 last_seq_num = seq_num;
359 }
360 EXPECT_TRUE(reordering_has_occured);
361}
philipel536378b2016-05-31 03:20:23 -0700362
363TEST_F(FakeNetworkPipeTest, BurstLoss) {
364 const int kLossPercent = 5;
365 const int kAvgBurstLength = 3;
366 const int kNumPackets = 10000;
367 const int kPacketSize = 10;
368
Artem Titov75e36472018-10-08 12:28:56 +0200369 BuiltInNetworkBehaviorConfig config;
philipel536378b2016-05-31 03:20:23 -0700370 config.queue_length_packets = kNumPackets;
371 config.loss_percent = kLossPercent;
372 config.avg_burst_loss_length = kAvgBurstLength;
Sebastian Jansson09408112018-04-24 14:41:22 +0200373 ReorderTestReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200374 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titovc7ea8522018-08-29 09:07:27 +0200375 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
376 &fake_clock_, std::move(simulated_network), &receiver));
philipel536378b2016-05-31 03:20:23 -0700377
378 SendPackets(pipe.get(), kNumPackets, kPacketSize);
379 fake_clock_.AdvanceTimeMilliseconds(1000);
380 pipe->Process();
381
382 // Check that the average loss is |kLossPercent| percent.
Sebastian Jansson09408112018-04-24 14:41:22 +0200383 int lost_packets = kNumPackets - receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 03:20:23 -0700384 double loss_fraction = lost_packets / static_cast<double>(kNumPackets);
385
386 EXPECT_NEAR(kLossPercent / 100.0, loss_fraction, 0.05);
387
388 // Find the number of bursts that has occurred.
Sebastian Jansson09408112018-04-24 14:41:22 +0200389 size_t received_packets = receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 03:20:23 -0700390 int num_bursts = 0;
391 for (size_t i = 0; i < received_packets - 1; ++i) {
Sebastian Jansson09408112018-04-24 14:41:22 +0200392 int diff = receiver.delivered_sequence_numbers_[i + 1] -
393 receiver.delivered_sequence_numbers_[i];
philipel536378b2016-05-31 03:20:23 -0700394 if (diff > 1)
395 ++num_bursts;
396 }
397
398 double average_burst_length = static_cast<double>(lost_packets) / num_bursts;
399
400 EXPECT_NEAR(kAvgBurstLength, average_burst_length, 0.3);
401}
minyue20c84cc2017-04-10 16:57:57 -0700402
403TEST_F(FakeNetworkPipeTest, SetReceiver) {
Artem Titov75e36472018-10-08 12:28:56 +0200404 BuiltInNetworkBehaviorConfig config;
Sebastian Jansson09408112018-04-24 14:41:22 +0200405 config.link_capacity_kbps = 800;
406 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200407 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 13:00:54 +0200408 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
409 &fake_clock_, std::move(simulated_network), &receiver));
minyue20c84cc2017-04-10 16:57:57 -0700410
Sebastian Jansson09408112018-04-24 14:41:22 +0200411 const int kPacketSize = 1000;
412 const int kPacketTimeMs =
413 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
414 SendPackets(pipe.get(), 1, kPacketSize);
415 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
416 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
417 pipe->Process();
minyue20c84cc2017-04-10 16:57:57 -0700418
Sebastian Jansson09408112018-04-24 14:41:22 +0200419 MockReceiver new_receiver;
420 pipe->SetReceiver(&new_receiver);
minyue20c84cc2017-04-10 16:57:57 -0700421
Sebastian Jansson09408112018-04-24 14:41:22 +0200422 SendPackets(pipe.get(), 1, kPacketSize);
423 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
424 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
425 EXPECT_CALL(new_receiver, DeliverPacket(_, _, _)).Times(1);
426 pipe->Process();
minyue20c84cc2017-04-10 16:57:57 -0700427}
428
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000429} // namespace webrtc