blob: df5ba1b7bddfcda4f82a83e995927c11e0b0732e [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
Steve Anton40d55332019-01-07 18:21:4716#include "absl/memory/memory.h"
Artem Titove23b8a92018-08-16 13:51:0717#include "call/simulated_network.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "system_wrappers/include/clock.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "test/gmock.h"
20#include "test/gtest.h"
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2221
22using ::testing::_;
23using ::testing::AnyNumber;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2224using ::testing::Invoke;
Sebastian Jansson487c09b2019-02-21 15:21:5125using ::testing::Return;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2226
27namespace webrtc {
28
minyue20c84cc2017-04-10 23:57:5729class MockReceiver : public PacketReceiver {
30 public:
Danil Chapovalov292a73e2017-12-07 16:00:4031 MOCK_METHOD3(DeliverPacket,
Niels Möller70082872018-08-07 09:03:1232 DeliveryStatus(MediaType, rtc::CopyOnWriteBuffer, int64_t));
Sebastian Jansson09408112018-04-24 12:41:2233 virtual ~MockReceiver() = default;
34};
35
36class ReorderTestReceiver : public MockReceiver {
37 public:
38 DeliveryStatus DeliverPacket(MediaType media_type,
39 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 09:03:1240 int64_t /* packet_time_us */) override {
Sebastian Jansson09408112018-04-24 12:41:2241 RTC_DCHECK_GE(packet.size(), sizeof(int));
42 int seq_num;
43 memcpy(&seq_num, packet.data<uint8_t>(), sizeof(int));
44 delivered_sequence_numbers_.push_back(seq_num);
45 return DeliveryStatus::DELIVERY_OK;
46 }
47 std::vector<int> delivered_sequence_numbers_;
minyue20c84cc2017-04-10 23:57:5748};
49
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2250class FakeNetworkPipeTest : public ::testing::Test {
Peter Boströmd3c94472015-12-09 10:20:5851 public:
52 FakeNetworkPipeTest() : fake_clock_(12345) {}
53
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2254 protected:
philipela2c55232016-01-26 16:41:5355 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) {
kwiberg352444f2016-11-28 23:58:5356 RTC_DCHECK_GE(packet_size, sizeof(int));
kwibergbfefb032016-05-01 21:53:4657 std::unique_ptr<uint8_t[]> packet(new uint8_t[packet_size]);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2258 for (int i = 0; i < number_packets; ++i) {
philipela2c55232016-01-26 16:41:5359 // Set a sequence number for the packets by
60 // using the first bytes in the packet.
61 memcpy(packet.get(), &i, sizeof(int));
Sebastian Jansson09408112018-04-24 12:41:2262 rtc::CopyOnWriteBuffer buffer(packet.get(), packet_size);
Niels Möller70082872018-08-07 09:03:1263 pipe->DeliverPacket(MediaType::ANY, buffer, /* packet_time_us */ -1);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2264 }
65 }
66
philipela2c55232016-01-26 16:41:5367 int PacketTimeMs(int capacity_kbps, int packet_size) const {
68 return 8 * packet_size / capacity_kbps;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2269 }
70
Peter Boströmd3c94472015-12-09 10:20:5871 SimulatedClock fake_clock_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2272};
73
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2274// Test the capacity link and verify we get as many packets as we expect.
75TEST_F(FakeNetworkPipeTest, CapacityTest) {
Artem Titov75e36472018-10-08 10:28:5676 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:0677 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:1178 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:2279 MockReceiver receiver;
Artem Titov3229d652018-08-17 11:00:5480 auto simulated_network = absl::make_unique<SimulatedNetwork>(config);
81 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
82 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2283
84 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
85 // get through the pipe.
86 const int kNumPackets = 10;
87 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 23:57:5788 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2289
90 // Time to get one packet through the link.
Erik Språng09708512018-03-14 14:16:5091 const int kPacketTimeMs =
92 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2293
94 // Time haven't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 12:41:2295 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:2596 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2297
98 // Advance enough time to release one packet.
Peter Boströmd3c94472015-12-09 10:20:5899 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 12:41:22100 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25101 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22102
103 // Release all but one packet
Peter Boströmd3c94472015-12-09 10:20:58104 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1);
Sebastian Jansson09408112018-04-24 12:41:22105 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(8);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25106 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22107
108 // And the last one.
Peter Boströmd3c94472015-12-09 10:20:58109 fake_clock_.AdvanceTimeMilliseconds(1);
Sebastian Jansson09408112018-04-24 12:41:22110 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25111 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22112}
113
114// Test the extra network delay.
115TEST_F(FakeNetworkPipeTest, ExtraDelayTest) {
Artem Titov75e36472018-10-08 10:28:56116 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06117 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11118 config.queue_delay_ms = 100;
119 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22120 MockReceiver receiver;
Artem Titov3229d652018-08-17 11:00:54121 auto simulated_network = absl::make_unique<SimulatedNetwork>(config);
122 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
123 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22124
125 const int kNumPackets = 2;
126 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 23:57:57127 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22128
129 // Time to get one packet through the link.
Erik Språng09708512018-03-14 14:16:50130 const int kPacketTimeMs =
131 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22132
133 // Increase more than kPacketTimeMs, but not more than the extra delay.
Peter Boströmd3c94472015-12-09 10:20:58134 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 12:41:22135 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25136 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22137
138 // Advance the network delay to get the first packet.
Peter Boströmd3c94472015-12-09 10:20:58139 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
Sebastian Jansson09408112018-04-24 12:41:22140 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25141 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22142
143 // Advance one more kPacketTimeMs to get the last packet.
Peter Boströmd3c94472015-12-09 10:20:58144 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Sebastian Jansson09408112018-04-24 12:41:22145 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25146 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22147}
148
149// Test the number of buffers and packets are dropped when sending too many
150// packets too quickly.
151TEST_F(FakeNetworkPipeTest, QueueLengthTest) {
Artem Titov75e36472018-10-08 10:28:56152 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06153 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11154 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22155 MockReceiver receiver;
Artem Titov3229d652018-08-17 11:00:54156 auto simulated_network = absl::make_unique<SimulatedNetwork>(config);
157 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
158 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22159
160 const int kPacketSize = 1000;
Erik Språng09708512018-03-14 14:16:50161 const int kPacketTimeMs =
162 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22163
164 // Send three packets and verify only 2 are delivered.
165 SendPackets(pipe.get(), 3, kPacketSize);
166
167 // Increase time enough to deliver all three packets, verify only two are
168 // delivered.
Peter Boströmd3c94472015-12-09 10:20:58169 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs);
Sebastian Jansson09408112018-04-24 12:41:22170 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25171 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22172}
173
174// Test we get statistics as expected.
175TEST_F(FakeNetworkPipeTest, StatisticsTest) {
Artem Titov75e36472018-10-08 10:28:56176 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06177 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11178 config.queue_delay_ms = 20;
179 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22180 MockReceiver receiver;
Artem Titov3229d652018-08-17 11:00:54181 auto simulated_network = absl::make_unique<SimulatedNetwork>(config);
182 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
183 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22184
185 const int kPacketSize = 1000;
Erik Språng09708512018-03-14 14:16:50186 const int kPacketTimeMs =
187 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22188
189 // Send three packets and verify only 2 are delivered.
190 SendPackets(pipe.get(), 3, kPacketSize);
Peter Boströmd3c94472015-12-09 10:20:58191 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs +
192 config.queue_delay_ms);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22193
Sebastian Jansson09408112018-04-24 12:41:22194 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25195 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22196
mflodman@webrtc.org7acb65a2012-12-13 15:53:11197 // Packet 1: kPacketTimeMs + config.queue_delay_ms,
198 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average.
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22199 EXPECT_EQ(pipe->AverageDelay(), 170);
Erik Språng09708512018-03-14 14:16:50200 EXPECT_EQ(pipe->SentPackets(), 2u);
201 EXPECT_EQ(pipe->DroppedPackets(), 1u);
202 EXPECT_EQ(pipe->PercentageLoss(), 1 / 3.f);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22203}
204
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52205// Change the link capacity half-way through the test and verify that the
206// delivery times change accordingly.
207TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) {
Artem Titov75e36472018-10-08 10:28:56208 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06209 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52210 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22211 MockReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07212 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
213 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22214 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20215 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52216
217 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
218 // get through the pipe.
219 const int kNumPackets = 10;
220 const int kPacketSize = 1000;
221 SendPackets(pipe.get(), kNumPackets, kPacketSize);
222
223 // Time to get one packet through the link.
224 int packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
225
226 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 12:41:22227 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52228 pipe->Process();
229
230 // Advance time in steps to release one packet at a time.
231 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 10:20:58232 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
Sebastian Jansson09408112018-04-24 12:41:22233 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52234 pipe->Process();
235 }
236
237 // Change the capacity.
238 config.link_capacity_kbps /= 2; // Reduce to 50%.
Artem Titove23b8a92018-08-16 13:51:07239 simulated_network->SetConfig(config);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52240
241 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
242 // seconds to get them through the pipe.
243 SendPackets(pipe.get(), kNumPackets, kPacketSize);
244
245 // Time to get one packet through the link.
246 packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
247
248 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 12:41:22249 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52250 pipe->Process();
251
252 // Advance time in steps to release one packet at a time.
253 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 10:20:58254 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
Sebastian Jansson09408112018-04-24 12:41:22255 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52256 pipe->Process();
257 }
258
259 // Check that all the packets were sent.
Erik Språng09708512018-03-14 14:16:50260 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->SentPackets());
Sebastian Jansson487c09b2019-02-21 15:21:51261 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value());
262 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 12:41:22263 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52264 pipe->Process();
265}
266
267// Change the link capacity half-way through the test and verify that the
268// delivery times change accordingly.
269TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) {
Artem Titov75e36472018-10-08 10:28:56270 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06271 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52272 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22273 MockReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07274 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
275 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22276 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20277 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52278
Christoffer Rodbro813c79b2019-01-31 08:25:12279 // Add 20 packets of 1000 bytes, = 80 kb.
280 const int kNumPackets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52281 const int kPacketSize = 1000;
282 SendPackets(pipe.get(), kNumPackets, kPacketSize);
283
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52284 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 12:41:22285 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52286 pipe->Process();
287
Christoffer Rodbro813c79b2019-01-31 08:25:12288 // Advance time in steps to release half of the packets one at a time.
289 int step_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
290 for (int i = 0; i < kNumPackets / 2; ++i) {
291 fake_clock_.AdvanceTimeMilliseconds(step_ms);
Sebastian Jansson09408112018-04-24 12:41:22292 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52293 pipe->Process();
294 }
295
Christoffer Rodbro813c79b2019-01-31 08:25:12296 // Change the capacity.
297 config.link_capacity_kbps *= 2; // Double the capacity.
298 simulated_network->SetConfig(config);
299
300 // Advance time in steps to release remaining packets one at a time.
301 step_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
302 for (int i = 0; i < kNumPackets / 2; ++i) {
303 fake_clock_.AdvanceTimeMilliseconds(step_ms);
Sebastian Jansson09408112018-04-24 12:41:22304 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52305 pipe->Process();
306 }
307
308 // Check that all the packets were sent.
Christoffer Rodbro813c79b2019-01-31 08:25:12309 EXPECT_EQ(static_cast<size_t>(kNumPackets), pipe->SentPackets());
Sebastian Jansson487c09b2019-02-21 15:21:51310 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value());
311 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 12:41:22312 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52313 pipe->Process();
314}
philipela2c55232016-01-26 16:41:53315
316// At first disallow reordering and then allow reordering.
317TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) {
Artem Titov75e36472018-10-08 10:28:56318 BuiltInNetworkBehaviorConfig config;
philipela2c55232016-01-26 16:41:53319 config.queue_length_packets = 1000;
320 config.link_capacity_kbps = 800;
321 config.queue_delay_ms = 100;
322 config.delay_standard_deviation_ms = 10;
Sebastian Jansson09408112018-04-24 12:41:22323 ReorderTestReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07324 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
325 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22326 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20327 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
philipela2c55232016-01-26 16:41:53328
329 const uint32_t kNumPackets = 100;
330 const int kPacketSize = 10;
331 SendPackets(pipe.get(), kNumPackets, kPacketSize);
332 fake_clock_.AdvanceTimeMilliseconds(1000);
333 pipe->Process();
334
335 // Confirm that all packets have been delivered in order.
Sebastian Jansson09408112018-04-24 12:41:22336 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 16:41:53337 int last_seq_num = -1;
Sebastian Jansson09408112018-04-24 12:41:22338 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 16:41:53339 EXPECT_GT(seq_num, last_seq_num);
340 last_seq_num = seq_num;
341 }
342
343 config.allow_reordering = true;
Artem Titove23b8a92018-08-16 13:51:07344 simulated_network->SetConfig(config);
philipela2c55232016-01-26 16:41:53345 SendPackets(pipe.get(), kNumPackets, kPacketSize);
346 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 12:41:22347 receiver.delivered_sequence_numbers_.clear();
philipela2c55232016-01-26 16:41:53348 pipe->Process();
349
350 // Confirm that all packets have been delivered
351 // and that reordering has occured.
Sebastian Jansson09408112018-04-24 12:41:22352 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 16:41:53353 bool reordering_has_occured = false;
354 last_seq_num = -1;
Sebastian Jansson09408112018-04-24 12:41:22355 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 16:41:53356 if (last_seq_num > seq_num) {
357 reordering_has_occured = true;
358 break;
359 }
360 last_seq_num = seq_num;
361 }
362 EXPECT_TRUE(reordering_has_occured);
363}
philipel536378b2016-05-31 10:20:23364
365TEST_F(FakeNetworkPipeTest, BurstLoss) {
366 const int kLossPercent = 5;
367 const int kAvgBurstLength = 3;
368 const int kNumPackets = 10000;
369 const int kPacketSize = 10;
370
Artem Titov75e36472018-10-08 10:28:56371 BuiltInNetworkBehaviorConfig config;
philipel536378b2016-05-31 10:20:23372 config.queue_length_packets = kNumPackets;
373 config.loss_percent = kLossPercent;
374 config.avg_burst_loss_length = kAvgBurstLength;
Sebastian Jansson09408112018-04-24 12:41:22375 ReorderTestReceiver receiver;
Artem Titov3229d652018-08-17 11:00:54376 auto simulated_network = absl::make_unique<SimulatedNetwork>(config);
Artem Titovc7ea8522018-08-29 07:07:27377 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
378 &fake_clock_, std::move(simulated_network), &receiver));
philipel536378b2016-05-31 10:20:23379
380 SendPackets(pipe.get(), kNumPackets, kPacketSize);
381 fake_clock_.AdvanceTimeMilliseconds(1000);
382 pipe->Process();
383
384 // Check that the average loss is |kLossPercent| percent.
Sebastian Jansson09408112018-04-24 12:41:22385 int lost_packets = kNumPackets - receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 10:20:23386 double loss_fraction = lost_packets / static_cast<double>(kNumPackets);
387
388 EXPECT_NEAR(kLossPercent / 100.0, loss_fraction, 0.05);
389
390 // Find the number of bursts that has occurred.
Sebastian Jansson09408112018-04-24 12:41:22391 size_t received_packets = receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 10:20:23392 int num_bursts = 0;
393 for (size_t i = 0; i < received_packets - 1; ++i) {
Sebastian Jansson09408112018-04-24 12:41:22394 int diff = receiver.delivered_sequence_numbers_[i + 1] -
395 receiver.delivered_sequence_numbers_[i];
philipel536378b2016-05-31 10:20:23396 if (diff > 1)
397 ++num_bursts;
398 }
399
400 double average_burst_length = static_cast<double>(lost_packets) / num_bursts;
401
402 EXPECT_NEAR(kAvgBurstLength, average_burst_length, 0.3);
403}
minyue20c84cc2017-04-10 23:57:57404
405TEST_F(FakeNetworkPipeTest, SetReceiver) {
Artem Titov75e36472018-10-08 10:28:56406 BuiltInNetworkBehaviorConfig config;
Sebastian Jansson09408112018-04-24 12:41:22407 config.link_capacity_kbps = 800;
408 MockReceiver receiver;
Artem Titov3229d652018-08-17 11:00:54409 auto simulated_network = absl::make_unique<SimulatedNetwork>(config);
410 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
411 &fake_clock_, std::move(simulated_network), &receiver));
minyue20c84cc2017-04-10 23:57:57412
Sebastian Jansson09408112018-04-24 12:41:22413 const int kPacketSize = 1000;
414 const int kPacketTimeMs =
415 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
416 SendPackets(pipe.get(), 1, kPacketSize);
417 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
418 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
419 pipe->Process();
minyue20c84cc2017-04-10 23:57:57420
Sebastian Jansson09408112018-04-24 12:41:22421 MockReceiver new_receiver;
422 pipe->SetReceiver(&new_receiver);
minyue20c84cc2017-04-10 23:57:57423
Sebastian Jansson09408112018-04-24 12:41:22424 SendPackets(pipe.get(), 1, kPacketSize);
425 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
426 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
427 EXPECT_CALL(new_receiver, DeliverPacket(_, _, _)).Times(1);
428 pipe->Process();
minyue20c84cc2017-04-10 23:57:57429}
430
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22431} // namespace webrtc