1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
|
/*
* Copyright 2022 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "test/network/simulated_network.h"
#include <cstddef>
#include <cstdint>
#include <optional>
#include <vector>
#include "api/test/simulated_network.h"
#include "api/units/data_rate.h"
#include "api/units/time_delta.h"
#include "api/units/timestamp.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace webrtc {
namespace {
using ::testing::ElementsAre;
using ::testing::MockFunction;
using ::testing::SizeIs;
PacketInFlightInfo PacketWithSize(size_t size) {
return PacketInFlightInfo(/*size=*/size, /*send_time_us=*/0, /*packet_id=*/1);
}
TEST(SimulatedNetworkTest, NextDeliveryTimeIsUnknownOnEmptyNetwork) {
SimulatedNetwork network = SimulatedNetwork({});
EXPECT_EQ(network.NextDeliveryTimeUs(), std::nullopt);
}
TEST(SimulatedNetworkTest, EnqueueFirstPacketOnNetworkWithInfiniteCapacity) {
// A packet of 1 kB that gets enqueued on a network with infinite capacity
// should be ready to exit the network immediately.
SimulatedNetwork network = SimulatedNetwork({});
ASSERT_TRUE(network.EnqueuePacket(PacketWithSize(1'000)));
EXPECT_EQ(network.NextDeliveryTimeUs(), 0);
}
TEST(SimulatedNetworkTest, EnqueueFirstPacketOnNetworkWithLimitedCapacity) {
// A packet of 125 bytes that gets enqueued on a network with 1 kbps capacity
// should be ready to exit the network in 1 second.
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
ASSERT_TRUE(network.EnqueuePacket(PacketWithSize(125)));
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
}
TEST(SimulatedNetworkTest,
EnqueuePacketsButNextDeliveryIsBasedOnFirstEnqueuedPacket) {
// A packet of 125 bytes that gets enqueued on a network with 1 kbps capacity
// should be ready to exit the network in 1 second.
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1)));
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
// Enqueuing another packet after 100 us doesn't change the next delivery
// time.
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/100, /*packet_id=*/2)));
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
// Enqueuing another packet after 2 seconds doesn't change the next delivery
// time since the first packet has not left the network yet.
ASSERT_TRUE(network.EnqueuePacket(PacketInFlightInfo(
/*size=*/125, /*send_time_us=*/TimeDelta::Seconds(2).us(),
/*packet_id=*/3)));
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
}
TEST(SimulatedNetworkTest, EnqueueFailsWhenQueueLengthIsReached) {
SimulatedNetwork network =
SimulatedNetwork({.queue_length_packets = 1,
.link_capacity = DataRate::KilobitsPerSec(1)});
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1)));
// Until there is 1 packet in the queue, no other packets can be enqueued,
// the only way to make space for new packets is calling
// DequeueDeliverablePackets at a time greater than or equal to
// NextDeliveryTimeUs.
EXPECT_FALSE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125,
/*send_time_us=*/TimeDelta::Seconds(0.5).us(),
/*packet_id=*/2)));
// Even if the send_time_us is after NextDeliveryTimeUs, it is still not
// possible to enqueue a new packet since the client didn't deque any packet
// from the queue (in this case the client is introducing unbounded delay but
// the network cannot do anything about it).
EXPECT_FALSE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125,
/*send_time_us=*/TimeDelta::Seconds(2).us(),
/*packet_id=*/3)));
}
TEST(SimulatedNetworkTest, PacketOverhead) {
// A packet of 125 bytes that gets enqueued on a network with 1 kbps capacity
// should be ready to exit the network in 1 second, but since there is an
// overhead per packet of 125 bytes, it will exit the network after 2 seconds.
SimulatedNetwork network = SimulatedNetwork(
{.link_capacity = DataRate::KilobitsPerSec(1), .packet_overhead = 125});
ASSERT_TRUE(network.EnqueuePacket(PacketWithSize(125)));
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(2).us());
}
TEST(SimulatedNetworkTest,
DequeueDeliverablePacketsLeavesPacketsInCapacityLink) {
// A packet of 125 bytes that gets enqueued on a network with 1 kbps capacity
// should be ready to exit the network in 1 second.
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1)));
// Enqueue another packet of 125 bytes (this one should exit after 2 seconds).
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125,
/*send_time_us=*/TimeDelta::Seconds(1).us(),
/*packet_id=*/2)));
// The first packet will exit after 1 second, so that is the next delivery
// time.
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
// After 1 seconds, we collect the delivered packets...
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(1).us());
ASSERT_EQ(delivered_packets.size(), 1ul);
EXPECT_EQ(delivered_packets[0].packet_id, 1ul);
EXPECT_EQ(delivered_packets[0].receive_time_us, TimeDelta::Seconds(1).us());
// ... And after the first enqueued packet has left the network, the next
// delivery time reflects the delivery time of the next packet.
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(2).us());
}
TEST(SimulatedNetworkTest,
DequeueDeliverablePacketsAppliesConfigChangesToCapacityLink) {
// A packet of 125 bytes that gets enqueued on a network with 1 kbps capacity
// should be ready to exit the network in 1 second.
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
const PacketInFlightInfo packet_1 =
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1);
ASSERT_TRUE(network.EnqueuePacket(packet_1));
// Enqueue another packet of 125 bytes with send time 1 second so this should
// exit after 2 seconds.
PacketInFlightInfo packet_2 =
PacketInFlightInfo(/*size=*/125,
/*send_time_us=*/TimeDelta::Seconds(1).us(),
/*packet_id=*/2);
ASSERT_TRUE(network.EnqueuePacket(packet_2));
// The first packet will exit after 1 second, so that is the next delivery
// time.
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
// Since the link capacity changes from 1 kbps to 10 kbps, packets will take
// 100 ms each to leave the network.
network.SetConfig({.link_capacity = DataRate::KilobitsPerSec(10)});
// The next delivery time doesn't change (it will be updated, if needed at
// DequeueDeliverablePackets time).
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
// Getting the first enqueued packet after 100 ms.
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Millis(100).us());
ASSERT_EQ(delivered_packets.size(), 1ul);
EXPECT_THAT(delivered_packets,
ElementsAre(PacketDeliveryInfo(
/*source=*/packet_1,
/*receive_time_us=*/TimeDelta::Millis(100).us())));
// Getting the second enqueued packet that cannot be delivered before its send
// time, hence it will be delivered after 1.1 seconds.
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Millis(1100).us());
delivered_packets = network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Millis(1100).us());
ASSERT_EQ(delivered_packets.size(), 1ul);
EXPECT_THAT(delivered_packets,
ElementsAre(PacketDeliveryInfo(
/*source=*/packet_2,
/*receive_time_us=*/TimeDelta::Millis(1100).us())));
}
TEST(SimulatedNetworkTest,
SetConfigUpdateNextDeliveryTimeIfLinkCapacityChange) {
// A packet of 125 bytes that gets enqueued on a network with 1 kbps capacity
// should be ready to exit the network in 1 second.
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
MockFunction<void()> delivery_time_changed_callback;
network.RegisterDeliveryTimeChangedCallback(
delivery_time_changed_callback.AsStdFunction());
const PacketInFlightInfo packet_1 =
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1);
ASSERT_TRUE(network.EnqueuePacket(packet_1));
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
// Since the link capacity changes from 1 kbps to 10 kbps, packets will take
// 100 ms each to leave the network. After 500ms, half the packet should have
// gone through.
EXPECT_CALL(delivery_time_changed_callback, Call).WillOnce([&]() {
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Millis(500 + 50).us());
});
network.SetConfig({.link_capacity = DataRate::KilobitsPerSec(10)},
/*config_update_time*/ Timestamp::Millis(500));
}
TEST(SimulatedNetworkTest,
SetConfigUpdateNextDeliveryTimeIfLinkCapacityChangeFromZero) {
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::Zero()});
MockFunction<void()> delivery_time_changed_callback;
network.RegisterDeliveryTimeChangedCallback(
delivery_time_changed_callback.AsStdFunction());
const PacketInFlightInfo packet_1 =
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1);
const PacketInFlightInfo packet_2 =
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/2);
ASSERT_TRUE(network.EnqueuePacket(packet_1));
ASSERT_TRUE(network.EnqueuePacket(packet_2));
EXPECT_FALSE(network.NextDeliveryTimeUs().has_value());
// The link capacity changes from 0 kbps to 10 kbps during 10ms 1/10th of the
// packet will be transmitted. (The packet would take 100ms to go through the
// network at 10kbps.)
::testing::Sequence s;
EXPECT_CALL(delivery_time_changed_callback, Call)
.InSequence(s)
.WillOnce([&]() {
EXPECT_EQ(network.NextDeliveryTimeUs(),
TimeDelta::Millis(500 + 100).us());
});
EXPECT_CALL(delivery_time_changed_callback, Call)
.InSequence(s)
.WillOnce(
[&]() { EXPECT_FALSE(network.NextDeliveryTimeUs().has_value()); });
EXPECT_CALL(delivery_time_changed_callback, Call)
.InSequence(s)
.WillOnce([&]() {
EXPECT_EQ(network.NextDeliveryTimeUs(),
TimeDelta::Millis(610 + 90).us());
});
network.SetConfig({.link_capacity = DataRate::KilobitsPerSec(10)},
/*config_update_time*/ Timestamp::Millis(500));
network.SetConfig({.link_capacity = DataRate::Zero()},
/*config_update_time*/ Timestamp::Millis(510));
network.SetConfig({.link_capacity = DataRate::KilobitsPerSec(10)},
/*config_update_time*/ Timestamp::Millis(610));
}
TEST(SimulatedNetworkTest, SetConfigUpdateQueueDelayAfterDelivery) {
// A packet of 125 bytes that gets enqueued on a network with 1000 kbps
// capacity should be ready to exit the narrow section in 1 ms.
SimulatedNetwork network =
SimulatedNetwork({.queue_delay_ms = 1000,
.link_capacity = DataRate::KilobitsPerSec(1000)});
MockFunction<void()> delivery_time_changed_callback;
network.RegisterDeliveryTimeChangedCallback(
delivery_time_changed_callback.AsStdFunction());
EXPECT_CALL(delivery_time_changed_callback, Call).Times(0);
const PacketInFlightInfo packet_1 =
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1);
ASSERT_TRUE(network.EnqueuePacket(packet_1));
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Millis(1).us());
// But no packets is actually delivered. Only moved to the delay link.
EXPECT_TRUE(network
.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Millis(1).us())
.empty());
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Millis(1000 + 1).us());
// Changing the queue time does not change the next delivery time.
network.SetConfig(
{.queue_delay_ms = 1, .link_capacity = DataRate::KilobitsPerSec(100)},
/*config_update_time*/ Timestamp::Millis(500));
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Millis(1000 + 1).us());
// A new packet require NextDeliveryTimeUs to change since the capacity
// change. But does not affect the delivery time of packet_1.
const PacketInFlightInfo packet_2 = PacketInFlightInfo(
/*size=*/125, /*send_time_us=*/TimeDelta::Millis(500).us(),
/*packet_id=*/2);
ASSERT_TRUE(network.EnqueuePacket(packet_2));
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Millis(1000 + 1).us());
// At 100kbps, it will take packet 2 10ms to pass through the narrow section.
// Since delay is lower for packet_2, but reordering is not allowed, both
// packets are delivered at the same time.
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Millis(1000 + 1).us());
ASSERT_THAT(delivered_packets, SizeIs(2));
EXPECT_EQ(delivered_packets[0].receive_time_us,
delivered_packets[1].receive_time_us);
}
TEST(SimulatedNetworkTest, NetworkEmptyAfterLastPacketDequeued) {
// A packet of 125 bytes that gets enqueued on a network with 1 kbps
// capacity should be ready to exit the network in 1 second.
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
ASSERT_TRUE(network.EnqueuePacket(PacketWithSize(125)));
// Collecting all the delivered packets ...
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(1).us());
EXPECT_EQ(delivered_packets.size(), 1ul);
// ... leaves the network empty.
EXPECT_EQ(network.NextDeliveryTimeUs(), std::nullopt);
}
TEST(SimulatedNetworkTest, DequeueDeliverablePacketsOnLateCall) {
// A packet of 125 bytes that gets enqueued on a network with 1 kbps
// capacity should be ready to exit the network in 1 second.
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1)));
// Enqueue another packet of 125 bytes with send time 1 second so this
// should exit after 2 seconds.
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125,
/*send_time_us=*/TimeDelta::Seconds(1).us(),
/*packet_id=*/2)));
// Collecting delivered packets after 3 seconds will result in the delivery
// of both the enqueued packets.
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(3).us());
EXPECT_EQ(delivered_packets.size(), 2ul);
}
TEST(SimulatedNetworkTest,
DequeueDeliverablePacketsOnEarlyCallReturnsNoPackets) {
// A packet of 125 bytes that gets enqueued on a network with 1 kbps
// capacity should be ready to exit the network in 1 second.
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
ASSERT_TRUE(network.EnqueuePacket(PacketWithSize(125)));
// Collecting delivered packets after 0.5 seconds will result in the
// delivery of 0 packets.
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(0.5).us());
EXPECT_EQ(delivered_packets.size(), 0ul);
// Since the first enqueued packet was supposed to exit after 1 second.
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
}
TEST(SimulatedNetworkTest, QueueDelayMsWithoutStandardDeviation) {
// A packet of 125 bytes that gets enqueued on a network with 1 kbps
// capacity should be ready to exit the network in 1 second.
SimulatedNetwork network = SimulatedNetwork(
{.queue_delay_ms = 100, .link_capacity = DataRate::KilobitsPerSec(1)});
ASSERT_TRUE(network.EnqueuePacket(PacketWithSize(125)));
// The next delivery time is still 1 second even if there are 100 ms of
// extra delay but this will be applied at DequeueDeliverablePackets time.
ASSERT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
// Since all packets are delayed by 100 ms, after 1 second, no packets will
// exit the network.
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(1).us());
EXPECT_EQ(delivered_packets.size(), 0ul);
// And the updated next delivery time takes into account the extra delay of
// 100 ms so the first packet in the network will be delivered after 1.1
// seconds.
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Millis(1100).us());
delivered_packets = network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Millis(1100).us());
EXPECT_EQ(delivered_packets.size(), 1ul);
}
TEST(SimulatedNetworkTest,
QueueDelayMsWithStandardDeviationAndReorderNotAllowed) {
SimulatedNetwork network =
SimulatedNetwork({.queue_delay_ms = 100,
.delay_standard_deviation_ms = 90,
.link_capacity = DataRate::KilobitsPerSec(1),
.allow_reordering = false});
// A packet of 125 bytes that gets enqueued on a network with 1 kbps
// capacity should be ready to exit the network in 1 second.
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1)));
// But 3 more packets of size 1 byte are enqueued at the same time.
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/1, /*send_time_us=*/0, /*packet_id=*/2)));
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/1, /*send_time_us=*/0, /*packet_id=*/3)));
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/1, /*send_time_us=*/0, /*packet_id=*/4)));
// After 5 seconds all of them exit the network.
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(5).us());
ASSERT_EQ(delivered_packets.size(), 4ul);
// And they are still in order even if the delay was applied.
EXPECT_EQ(delivered_packets[0].packet_id, 1ul);
EXPECT_EQ(delivered_packets[1].packet_id, 2ul);
EXPECT_GE(delivered_packets[1].receive_time_us,
delivered_packets[0].receive_time_us);
EXPECT_EQ(delivered_packets[2].packet_id, 3ul);
EXPECT_GE(delivered_packets[2].receive_time_us,
delivered_packets[1].receive_time_us);
EXPECT_EQ(delivered_packets[3].packet_id, 4ul);
EXPECT_GE(delivered_packets[3].receive_time_us,
delivered_packets[2].receive_time_us);
}
TEST(SimulatedNetworkTest, QueueDelayMsWithStandardDeviationAndReorderAllowed) {
SimulatedNetwork network =
SimulatedNetwork({.queue_delay_ms = 100,
.delay_standard_deviation_ms = 90,
.link_capacity = DataRate::KilobitsPerSec(1),
.allow_reordering = true},
/*random_seed=*/1);
// A packet of 125 bytes that gets enqueued on a network with 1 kbps
// capacity should be ready to exit the network in 1 second.
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1)));
// But 3 more packets of size 1 byte are enqueued at the same time.
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/1, /*send_time_us=*/0, /*packet_id=*/2)));
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/1, /*send_time_us=*/0, /*packet_id=*/3)));
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/1, /*send_time_us=*/0, /*packet_id=*/4)));
// After 5 seconds all of them exit the network.
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(5).us());
ASSERT_EQ(delivered_packets.size(), 4ul);
// And they have been reordered accorting to the applied extra delay.
EXPECT_EQ(delivered_packets[0].packet_id, 3ul);
EXPECT_EQ(delivered_packets[1].packet_id, 1ul);
EXPECT_GE(delivered_packets[1].receive_time_us,
delivered_packets[0].receive_time_us);
EXPECT_EQ(delivered_packets[2].packet_id, 2ul);
EXPECT_GE(delivered_packets[2].receive_time_us,
delivered_packets[1].receive_time_us);
EXPECT_EQ(delivered_packets[3].packet_id, 4ul);
EXPECT_GE(delivered_packets[3].receive_time_us,
delivered_packets[2].receive_time_us);
}
TEST(SimulatedNetworkTest, PacketLoss) {
// On a network with 50% probability of packet loss ...
SimulatedNetwork network =
SimulatedNetwork({.loss_percent = 50}, /*random_seed =*/1);
// Enqueueing 8 packets ...
for (int i = 0; i < 8; i++) {
ASSERT_TRUE(network.EnqueuePacket(PacketInFlightInfo(
/*size=*/1, /*send_time_us=*/0, /*packet_id=*/i + 1)));
}
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(5).us());
EXPECT_EQ(delivered_packets.size(), 8ul);
// Results in the loss of 4 of them.
int lost_packets = 0;
for (const auto& packet : delivered_packets) {
if (packet.receive_time_us == PacketDeliveryInfo::kNotReceived) {
lost_packets++;
}
}
EXPECT_EQ(lost_packets, 4);
}
TEST(SimulatedNetworkTest, NextDeliveryTimeSetAfterLostPackets) {
// On a network with 50% probablility of packet loss ...
SimulatedNetwork network =
SimulatedNetwork({.queue_delay_ms = 10,
.link_capacity = DataRate::KilobitsPerSec(1000),
.loss_percent = 50},
/*random_seed =*/1);
// Enqueueing 8 packets at the same time. It should take 1ms to pass through
// the capacity limited section per packet, it total adding 8ms delay to the
// last packet. Since queue delay is 10ms, multiple packets will be in the
// delay queue at the same time.
for (int i = 0; i < 8; i++) {
ASSERT_TRUE(network.EnqueuePacket(PacketInFlightInfo(
/*size=*/125, /*send_time_us=*/0, /*packet_id=*/i + 1)));
}
int64_t time_us = 0;
std::vector<PacketDeliveryInfo> delivered_packets;
// This assumes first packet is lost and last packet is delivered....
while (delivered_packets.size() != 8) {
ASSERT_TRUE(network.NextDeliveryTimeUs().has_value());
time_us = *network.NextDeliveryTimeUs();
std::vector<PacketDeliveryInfo> packets =
network.DequeueDeliverablePackets(time_us);
delivered_packets.insert(delivered_packets.end(), packets.begin(),
packets.end());
}
// Results in the loss of 4 of them.
int lost_packets = 0;
int received_packets = 0;
for (const auto& packet : delivered_packets) {
if (packet.receive_time_us == PacketDeliveryInfo::kNotReceived) {
lost_packets++;
} else {
received_packets++;
}
}
EXPECT_EQ(delivered_packets.back().receive_time_us,
Timestamp::Millis(10 + 8).us());
EXPECT_EQ(lost_packets, 4);
EXPECT_EQ(received_packets, 4);
}
TEST(SimulatedNetworkTest, PacketLossBurst) {
// On a network with 50% probability of packet loss and an average burst
// loss length of 100 ...
SimulatedNetwork network = SimulatedNetwork(
{.loss_percent = 50, .avg_burst_loss_length = 100}, /*random_seed=*/1);
// Enqueueing 20 packets ...
for (int i = 0; i < 20; i++) {
ASSERT_TRUE(network.EnqueuePacket(PacketInFlightInfo(
/*size=*/1, /*send_time_us=*/0, /*packet_id=*/i + 1)));
}
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(5).us());
EXPECT_EQ(delivered_packets.size(), 20ul);
// Results in a burst of lost packets after the first packet lost.
// With the current random seed, the first 12 are not lost, while the
// last 8 are.
int current_packet = 0;
for (const auto& packet : delivered_packets) {
if (current_packet < 12) {
EXPECT_NE(packet.receive_time_us, PacketDeliveryInfo::kNotReceived);
current_packet++;
} else {
EXPECT_EQ(packet.receive_time_us, PacketDeliveryInfo::kNotReceived);
current_packet++;
}
}
}
TEST(SimulatedNetworkTest, PauseTransmissionUntil) {
// 3 packets of 125 bytes that gets enqueued on a network with 1 kbps
// capacity should be ready to exit the network after 1, 2 and 3 seconds
// respectively.
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/1)));
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/2)));
ASSERT_TRUE(network.EnqueuePacket(
PacketInFlightInfo(/*size=*/125, /*send_time_us=*/0, /*packet_id=*/3)));
ASSERT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(1).us());
// The network gets paused for 5 seconds, which means that the first packet
// can exit after 5 seconds instead of 1 second.
network.PauseTransmissionUntil(TimeDelta::Seconds(5).us());
// No packets after 1 second.
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(1).us());
EXPECT_EQ(delivered_packets.size(), 0ul);
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(5).us());
// The first packet exits after 5 seconds.
delivered_packets = network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(5).us());
EXPECT_EQ(delivered_packets.size(), 1ul);
// After the first packet is exited, the next delivery time reflects the
// delivery time of the next packet which accounts for the network pause.
EXPECT_EQ(network.NextDeliveryTimeUs(), TimeDelta::Seconds(6).us());
// And 2 seconds after the exit of the first enqueued packet, the following
// 2 packets are also delivered.
delivered_packets = network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(7).us());
EXPECT_EQ(delivered_packets.size(), 2ul);
}
TEST(SimulatedNetworkTest, CongestedNetworkRespectsLinkCapacity) {
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
for (size_t i = 0; i < 1'000; ++i) {
ASSERT_TRUE(network.EnqueuePacket(PacketInFlightInfo(
/*size=*/125, /*send_time_us=*/0, /*packet_id=*/i)));
}
PacketDeliveryInfo last_delivered_packet{
PacketInFlightInfo(/*size=*/0, /*send_time_us=*/0, /*packet_id=*/0), 0};
while (network.NextDeliveryTimeUs().has_value()) {
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/network.NextDeliveryTimeUs().value());
if (!delivered_packets.empty()) {
last_delivered_packet = delivered_packets.back();
}
}
// 1000 packets of 1000 bits each will take 1000 seconds to exit a 1 kpbs
// network.
EXPECT_EQ(last_delivered_packet.receive_time_us,
TimeDelta::Seconds(1000).us());
EXPECT_EQ(last_delivered_packet.packet_id, 999ul);
}
TEST(SimulatedNetworkTest, EnqueuePacketWithSubSecondNonMonotonicBehaviour) {
// On multi-core systems, different threads can experience sub-millisecond
// non monothonic behaviour when running on different cores. This test
// checks that when a non monotonic packet enqueue, the network continues to
// work and the out of order packet is sent anyway.
SimulatedNetwork network =
SimulatedNetwork({.link_capacity = DataRate::KilobitsPerSec(1)});
ASSERT_TRUE(network.EnqueuePacket(PacketInFlightInfo(
/*size=*/125, /*send_time_us=*/TimeDelta::Seconds(1).us(),
/*packet_id=*/0)));
ASSERT_TRUE(network.EnqueuePacket(PacketInFlightInfo(
/*size=*/125, /*send_time_us=*/TimeDelta::Seconds(1).us() - 1,
/*packet_id=*/1)));
std::vector<PacketDeliveryInfo> delivered_packets =
network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(2).us());
ASSERT_EQ(delivered_packets.size(), 1ul);
EXPECT_EQ(delivered_packets[0].packet_id, 0ul);
EXPECT_EQ(delivered_packets[0].receive_time_us, TimeDelta::Seconds(2).us());
delivered_packets = network.DequeueDeliverablePackets(
/*receive_time_us=*/TimeDelta::Seconds(3).us());
ASSERT_EQ(delivered_packets.size(), 1ul);
EXPECT_EQ(delivered_packets[0].packet_id, 1ul);
EXPECT_EQ(delivered_packets[0].receive_time_us, TimeDelta::Seconds(3).us());
}
// TODO(bugs.webrtc.org/14525): Re-enable when the DCHECK will be uncommented
// and the non-monotonic events on real time clock tests is solved/understood.
// TEST(SimulatedNetworkDeathTest, EnqueuePacketExpectMonotonicSendTime) {
// SimulatedNetwork network = SimulatedNetwork({.link_capacity =
// DataRate::KilobitsPerSec(1)});
// ASSERT_TRUE(network.EnqueuePacket(PacketInFlightInfo(
// /*size=*/125, /*send_time_us=*/2'000'000, /*packet_id=*/0)));
// EXPECT_DEATH_IF_SUPPORTED(network.EnqueuePacket(PacketInFlightInfo(
// /*size=*/125, /*send_time_us=*/900'000, /*packet_id=*/1)), "");
// }
} // namespace
} // namespace webrtc
|