blob: 09e585241a407677af37e9a7c266fdaeb1624074 [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>
14
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "call/call.h"
Artem Titove23b8a92018-08-16 13:51:0716#include "call/simulated_network.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "modules/rtp_rtcp/include/rtp_header_parser.h"
18#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;
24using ::testing::Return;
25using ::testing::Invoke;
26
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 Titov3229d652018-08-17 11:00:5476 DefaultNetworkSimulationConfig 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 Titov3229d652018-08-17 11:00:54116 DefaultNetworkSimulationConfig 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 Titov3229d652018-08-17 11:00:54152 DefaultNetworkSimulationConfig 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 Titov3229d652018-08-17 11:00:54176 DefaultNetworkSimulationConfig 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 Titov3229d652018-08-17 11:00:54208 DefaultNetworkSimulationConfig 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());
Peter Boströmd3c94472015-12-09 10:20:58261 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess());
Sebastian Jansson09408112018-04-24 12:41:22262 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52263 pipe->Process();
264}
265
266// Change the link capacity half-way through the test and verify that the
267// delivery times change accordingly.
268TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) {
Artem Titov3229d652018-08-17 11:00:54269 DefaultNetworkSimulationConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06270 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52271 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22272 MockReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07273 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
274 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22275 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20276 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52277
278 // Add 10 packets of 1000 bytes, = 80 kb.
279 const int kNumPackets = 10;
280 const int kPacketSize = 1000;
281 SendPackets(pipe.get(), kNumPackets, kPacketSize);
282
283 // Time to get one packet through the link at the initial speed.
284 int packet_time_1_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
285
286 // Change the capacity.
287 config.link_capacity_kbps *= 2; // Double the capacity.
Artem Titove23b8a92018-08-16 13:51:07288 simulated_network->SetConfig(config);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52289
290 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
291 // seconds to get them through the pipe.
292 SendPackets(pipe.get(), kNumPackets, kPacketSize);
293
294 // Time to get one packet through the link at the new capacity.
295 int packet_time_2_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
296
297 // Time hasn't increased yet, so we souldn't get any packets.
Sebastian Jansson09408112018-04-24 12:41:22298 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52299 pipe->Process();
300
301 // Advance time in steps to release one packet at a time.
302 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 10:20:58303 fake_clock_.AdvanceTimeMilliseconds(packet_time_1_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 // Advance time in steps to release one packet at a time.
309 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 10:20:58310 fake_clock_.AdvanceTimeMilliseconds(packet_time_2_ms);
Sebastian Jansson09408112018-04-24 12:41:22311 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52312 pipe->Process();
313 }
314
315 // Check that all the packets were sent.
Erik Språng09708512018-03-14 14:16:50316 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->SentPackets());
Peter Boströmd3c94472015-12-09 10:20:58317 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess());
Sebastian Jansson09408112018-04-24 12:41:22318 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52319 pipe->Process();
320}
philipela2c55232016-01-26 16:41:53321
322// At first disallow reordering and then allow reordering.
323TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) {
Artem Titov3229d652018-08-17 11:00:54324 DefaultNetworkSimulationConfig config;
philipela2c55232016-01-26 16:41:53325 config.queue_length_packets = 1000;
326 config.link_capacity_kbps = 800;
327 config.queue_delay_ms = 100;
328 config.delay_standard_deviation_ms = 10;
Sebastian Jansson09408112018-04-24 12:41:22329 ReorderTestReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07330 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
331 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22332 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20333 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
philipela2c55232016-01-26 16:41:53334
335 const uint32_t kNumPackets = 100;
336 const int kPacketSize = 10;
337 SendPackets(pipe.get(), kNumPackets, kPacketSize);
338 fake_clock_.AdvanceTimeMilliseconds(1000);
339 pipe->Process();
340
341 // Confirm that all packets have been delivered in order.
Sebastian Jansson09408112018-04-24 12:41:22342 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 16:41:53343 int last_seq_num = -1;
Sebastian Jansson09408112018-04-24 12:41:22344 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 16:41:53345 EXPECT_GT(seq_num, last_seq_num);
346 last_seq_num = seq_num;
347 }
348
349 config.allow_reordering = true;
Artem Titove23b8a92018-08-16 13:51:07350 simulated_network->SetConfig(config);
philipela2c55232016-01-26 16:41:53351 SendPackets(pipe.get(), kNumPackets, kPacketSize);
352 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 12:41:22353 receiver.delivered_sequence_numbers_.clear();
philipela2c55232016-01-26 16:41:53354 pipe->Process();
355
356 // Confirm that all packets have been delivered
357 // and that reordering has occured.
Sebastian Jansson09408112018-04-24 12:41:22358 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 16:41:53359 bool reordering_has_occured = false;
360 last_seq_num = -1;
Sebastian Jansson09408112018-04-24 12:41:22361 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 16:41:53362 if (last_seq_num > seq_num) {
363 reordering_has_occured = true;
364 break;
365 }
366 last_seq_num = seq_num;
367 }
368 EXPECT_TRUE(reordering_has_occured);
369}
philipel536378b2016-05-31 10:20:23370
371TEST_F(FakeNetworkPipeTest, BurstLoss) {
372 const int kLossPercent = 5;
373 const int kAvgBurstLength = 3;
374 const int kNumPackets = 10000;
375 const int kPacketSize = 10;
376
Artem Titov3229d652018-08-17 11:00:54377 DefaultNetworkSimulationConfig config;
philipel536378b2016-05-31 10:20:23378 config.queue_length_packets = kNumPackets;
379 config.loss_percent = kLossPercent;
380 config.avg_burst_loss_length = kAvgBurstLength;
Sebastian Jansson09408112018-04-24 12:41:22381 ReorderTestReceiver receiver;
Artem Titov3229d652018-08-17 11:00:54382 auto simulated_network = absl::make_unique<SimulatedNetwork>(config);
Artem Titovc7ea8522018-08-29 07:07:27383 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
384 &fake_clock_, std::move(simulated_network), &receiver));
philipel536378b2016-05-31 10:20:23385
386 SendPackets(pipe.get(), kNumPackets, kPacketSize);
387 fake_clock_.AdvanceTimeMilliseconds(1000);
388 pipe->Process();
389
390 // Check that the average loss is |kLossPercent| percent.
Sebastian Jansson09408112018-04-24 12:41:22391 int lost_packets = kNumPackets - receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 10:20:23392 double loss_fraction = lost_packets / static_cast<double>(kNumPackets);
393
394 EXPECT_NEAR(kLossPercent / 100.0, loss_fraction, 0.05);
395
396 // Find the number of bursts that has occurred.
Sebastian Jansson09408112018-04-24 12:41:22397 size_t received_packets = receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 10:20:23398 int num_bursts = 0;
399 for (size_t i = 0; i < received_packets - 1; ++i) {
Sebastian Jansson09408112018-04-24 12:41:22400 int diff = receiver.delivered_sequence_numbers_[i + 1] -
401 receiver.delivered_sequence_numbers_[i];
philipel536378b2016-05-31 10:20:23402 if (diff > 1)
403 ++num_bursts;
404 }
405
406 double average_burst_length = static_cast<double>(lost_packets) / num_bursts;
407
408 EXPECT_NEAR(kAvgBurstLength, average_burst_length, 0.3);
409}
minyue20c84cc2017-04-10 23:57:57410
411TEST_F(FakeNetworkPipeTest, SetReceiver) {
Artem Titov3229d652018-08-17 11:00:54412 DefaultNetworkSimulationConfig config;
Sebastian Jansson09408112018-04-24 12:41:22413 config.link_capacity_kbps = 800;
414 MockReceiver receiver;
Artem Titov3229d652018-08-17 11:00:54415 auto simulated_network = absl::make_unique<SimulatedNetwork>(config);
416 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
417 &fake_clock_, std::move(simulated_network), &receiver));
minyue20c84cc2017-04-10 23:57:57418
Sebastian Jansson09408112018-04-24 12:41:22419 const int kPacketSize = 1000;
420 const int kPacketTimeMs =
421 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
422 SendPackets(pipe.get(), 1, kPacketSize);
423 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
424 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(1);
425 pipe->Process();
minyue20c84cc2017-04-10 23:57:57426
Sebastian Jansson09408112018-04-24 12:41:22427 MockReceiver new_receiver;
428 pipe->SetReceiver(&new_receiver);
minyue20c84cc2017-04-10 23:57:57429
Sebastian Jansson09408112018-04-24 12:41:22430 SendPackets(pipe.get(), 1, kPacketSize);
431 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
432 EXPECT_CALL(receiver, DeliverPacket(_, _, _)).Times(0);
433 EXPECT_CALL(new_receiver, DeliverPacket(_, _, _)).Times(1);
434 pipe->Process();
minyue20c84cc2017-04-10 23:57:57435}
436
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22437} // namespace webrtc