| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/test_tools/simulator/simulator.h" | 5 #include "net/quic/test_tools/simulator/simulator.h" |
| 6 | 6 |
| 7 #include "net/quic/platform/api/quic_logging.h" | 7 #include "net/quic/platform/api/quic_logging.h" |
| 8 #include "net/quic/platform/api/quic_ptr_util.h" | 8 #include "net/quic/platform/api/quic_ptr_util.h" |
| 9 #include "net/quic/platform/api/quic_test.h" |
| 9 #include "net/quic/test_tools/quic_test_utils.h" | 10 #include "net/quic/test_tools/quic_test_utils.h" |
| 10 #include "net/quic/test_tools/simulator/alarm_factory.h" | 11 #include "net/quic/test_tools/simulator/alarm_factory.h" |
| 11 #include "net/quic/test_tools/simulator/link.h" | 12 #include "net/quic/test_tools/simulator/link.h" |
| 12 #include "net/quic/test_tools/simulator/packet_filter.h" | 13 #include "net/quic/test_tools/simulator/packet_filter.h" |
| 13 #include "net/quic/test_tools/simulator/queue.h" | 14 #include "net/quic/test_tools/simulator/queue.h" |
| 14 #include "net/quic/test_tools/simulator/switch.h" | 15 #include "net/quic/test_tools/simulator/switch.h" |
| 15 #include "net/quic/test_tools/simulator/traffic_policer.h" | 16 #include "net/quic/test_tools/simulator/traffic_policer.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | 17 |
| 19 using std::string; | 18 using std::string; |
| 20 using testing::_; | 19 using testing::_; |
| 21 using testing::Return; | 20 using testing::Return; |
| 22 using testing::StrictMock; | 21 using testing::StrictMock; |
| 23 | 22 |
| 24 namespace net { | 23 namespace net { |
| 25 namespace simulator { | 24 namespace simulator { |
| 26 | 25 |
| 27 // A simple counter that increments its value by 1 every specified period. | 26 // A simple counter that increments its value by 1 every specified period. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 40 QUIC_DVLOG(1) << name_ << " has value " << value_ << " at time " | 39 QUIC_DVLOG(1) << name_ << " has value " << value_ << " at time " |
| 41 << clock_->Now().ToDebuggingValue(); | 40 << clock_->Now().ToDebuggingValue(); |
| 42 Schedule(clock_->Now() + period_); | 41 Schedule(clock_->Now() + period_); |
| 43 } | 42 } |
| 44 | 43 |
| 45 private: | 44 private: |
| 46 int value_; | 45 int value_; |
| 47 QuicTime::Delta period_; | 46 QuicTime::Delta period_; |
| 48 }; | 47 }; |
| 49 | 48 |
| 49 class SimulatorTest : public QuicTest {}; |
| 50 |
| 50 // Test that the basic event handling works. | 51 // Test that the basic event handling works. |
| 51 TEST(SimulatorTest, Counters) { | 52 TEST_F(SimulatorTest, Counters) { |
| 52 Simulator simulator; | 53 Simulator simulator; |
| 53 Counter fast_counter(&simulator, "fast_counter", | 54 Counter fast_counter(&simulator, "fast_counter", |
| 54 QuicTime::Delta::FromSeconds(3)); | 55 QuicTime::Delta::FromSeconds(3)); |
| 55 Counter slow_counter(&simulator, "slow_counter", | 56 Counter slow_counter(&simulator, "slow_counter", |
| 56 QuicTime::Delta::FromSeconds(10)); | 57 QuicTime::Delta::FromSeconds(10)); |
| 57 | 58 |
| 58 simulator.RunUntil( | 59 simulator.RunUntil( |
| 59 [&slow_counter]() { return slow_counter.get_value() >= 10; }); | 60 [&slow_counter]() { return slow_counter.get_value() >= 10; }); |
| 60 | 61 |
| 61 EXPECT_EQ(10, slow_counter.get_value()); | 62 EXPECT_EQ(10, slow_counter.get_value()); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 156 |
| 156 ConstrainedPortInterface* tx_port_; | 157 ConstrainedPortInterface* tx_port_; |
| 157 CounterPort rx_port_; | 158 CounterPort rx_port_; |
| 158 | 159 |
| 159 QuicByteCount bytes_transmitted_; | 160 QuicByteCount bytes_transmitted_; |
| 160 QuicPacketCount packets_transmitted_; | 161 QuicPacketCount packets_transmitted_; |
| 161 }; | 162 }; |
| 162 | 163 |
| 163 // Saturate a symmetric link and verify that the number of packets sent and | 164 // Saturate a symmetric link and verify that the number of packets sent and |
| 164 // received is correct. | 165 // received is correct. |
| 165 TEST(SimulatorTest, DirectLinkSaturation) { | 166 TEST_F(SimulatorTest, DirectLinkSaturation) { |
| 166 Simulator simulator; | 167 Simulator simulator; |
| 167 LinkSaturator saturator_a(&simulator, "Saturator A", 1000, "Saturator B"); | 168 LinkSaturator saturator_a(&simulator, "Saturator A", 1000, "Saturator B"); |
| 168 LinkSaturator saturator_b(&simulator, "Saturator B", 100, "Saturator A"); | 169 LinkSaturator saturator_b(&simulator, "Saturator B", 100, "Saturator A"); |
| 169 SymmetricLink link(&saturator_a, &saturator_b, | 170 SymmetricLink link(&saturator_a, &saturator_b, |
| 170 QuicBandwidth::FromKBytesPerSecond(1000), | 171 QuicBandwidth::FromKBytesPerSecond(1000), |
| 171 QuicTime::Delta::FromMilliseconds(100) + | 172 QuicTime::Delta::FromMilliseconds(100) + |
| 172 QuicTime::Delta::FromMicroseconds(1)); | 173 QuicTime::Delta::FromMicroseconds(1)); |
| 173 | 174 |
| 174 const QuicTime start_time = simulator.GetClock()->Now(); | 175 const QuicTime start_time = simulator.GetClock()->Now(); |
| 175 const QuicTime after_first_50_ms = | 176 const QuicTime after_first_50_ms = |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 return QuicTime::Delta::Zero(); | 234 return QuicTime::Delta::Zero(); |
| 234 } | 235 } |
| 235 | 236 |
| 236 std::vector<std::unique_ptr<Packet>>* packets() { return &packets_; } | 237 std::vector<std::unique_ptr<Packet>>* packets() { return &packets_; } |
| 237 | 238 |
| 238 private: | 239 private: |
| 239 std::vector<std::unique_ptr<Packet>> packets_; | 240 std::vector<std::unique_ptr<Packet>> packets_; |
| 240 }; | 241 }; |
| 241 | 242 |
| 242 // Ensure the queue behaves correctly with accepting packets. | 243 // Ensure the queue behaves correctly with accepting packets. |
| 243 TEST(SimulatorTest, Queue) { | 244 TEST_F(SimulatorTest, Queue) { |
| 244 Simulator simulator; | 245 Simulator simulator; |
| 245 Queue queue(&simulator, "Queue", 1000); | 246 Queue queue(&simulator, "Queue", 1000); |
| 246 PacketAcceptor acceptor; | 247 PacketAcceptor acceptor; |
| 247 queue.set_tx_port(&acceptor); | 248 queue.set_tx_port(&acceptor); |
| 248 | 249 |
| 249 EXPECT_EQ(0u, queue.bytes_queued()); | 250 EXPECT_EQ(0u, queue.bytes_queued()); |
| 250 EXPECT_EQ(0u, queue.packets_queued()); | 251 EXPECT_EQ(0u, queue.packets_queued()); |
| 251 EXPECT_EQ(0u, acceptor.packets()->size()); | 252 EXPECT_EQ(0u, acceptor.packets()->size()); |
| 252 | 253 |
| 253 auto first_packet = QuicMakeUnique<Packet>(); | 254 auto first_packet = QuicMakeUnique<Packet>(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 276 simulator.RunUntil([]() { return false; }); | 277 simulator.RunUntil([]() { return false; }); |
| 277 EXPECT_EQ(0u, queue.bytes_queued()); | 278 EXPECT_EQ(0u, queue.bytes_queued()); |
| 278 EXPECT_EQ(0u, queue.packets_queued()); | 279 EXPECT_EQ(0u, queue.packets_queued()); |
| 279 ASSERT_EQ(2u, acceptor.packets()->size()); | 280 ASSERT_EQ(2u, acceptor.packets()->size()); |
| 280 EXPECT_EQ(600u, acceptor.packets()->at(0)->size); | 281 EXPECT_EQ(600u, acceptor.packets()->at(0)->size); |
| 281 EXPECT_EQ(400u, acceptor.packets()->at(1)->size); | 282 EXPECT_EQ(400u, acceptor.packets()->at(1)->size); |
| 282 } | 283 } |
| 283 | 284 |
| 284 // Simulate a situation where the bottleneck link is 10 times slower than the | 285 // Simulate a situation where the bottleneck link is 10 times slower than the |
| 285 // uplink, and they are separated by a queue. | 286 // uplink, and they are separated by a queue. |
| 286 TEST(SimulatorTest, QueueBottleneck) { | 287 TEST_F(SimulatorTest, QueueBottleneck) { |
| 287 const QuicBandwidth local_bandwidth = | 288 const QuicBandwidth local_bandwidth = |
| 288 QuicBandwidth::FromKBytesPerSecond(1000); | 289 QuicBandwidth::FromKBytesPerSecond(1000); |
| 289 const QuicBandwidth bottleneck_bandwidth = 0.1f * local_bandwidth; | 290 const QuicBandwidth bottleneck_bandwidth = 0.1f * local_bandwidth; |
| 290 const QuicTime::Delta local_propagation_delay = | 291 const QuicTime::Delta local_propagation_delay = |
| 291 QuicTime::Delta::FromMilliseconds(1); | 292 QuicTime::Delta::FromMilliseconds(1); |
| 292 const QuicTime::Delta bottleneck_propagation_delay = | 293 const QuicTime::Delta bottleneck_propagation_delay = |
| 293 QuicTime::Delta::FromMilliseconds(20); | 294 QuicTime::Delta::FromMilliseconds(20); |
| 294 const QuicByteCount bdp = | 295 const QuicByteCount bdp = |
| 295 bottleneck_bandwidth * | 296 bottleneck_bandwidth * |
| 296 (local_propagation_delay + bottleneck_propagation_delay); | 297 (local_propagation_delay + bottleneck_propagation_delay); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 313 simulator.RunUntil( | 314 simulator.RunUntil( |
| 314 [&counter]() { return counter.packets() == packets_received; }); | 315 [&counter]() { return counter.packets() == packets_received; }); |
| 315 const double loss_ratio = | 316 const double loss_ratio = |
| 316 1 - | 317 1 - |
| 317 static_cast<double>(packets_received) / saturator.packets_transmitted(); | 318 static_cast<double>(packets_received) / saturator.packets_transmitted(); |
| 318 EXPECT_NEAR(loss_ratio, 0.9, 0.001); | 319 EXPECT_NEAR(loss_ratio, 0.9, 0.001); |
| 319 } | 320 } |
| 320 | 321 |
| 321 // Verify that the queue of exactly one packet allows the transmission to | 322 // Verify that the queue of exactly one packet allows the transmission to |
| 322 // actually go through. | 323 // actually go through. |
| 323 TEST(SimulatorTest, OnePacketQueue) { | 324 TEST_F(SimulatorTest, OnePacketQueue) { |
| 324 const QuicBandwidth local_bandwidth = | 325 const QuicBandwidth local_bandwidth = |
| 325 QuicBandwidth::FromKBytesPerSecond(1000); | 326 QuicBandwidth::FromKBytesPerSecond(1000); |
| 326 const QuicBandwidth bottleneck_bandwidth = 0.1f * local_bandwidth; | 327 const QuicBandwidth bottleneck_bandwidth = 0.1f * local_bandwidth; |
| 327 const QuicTime::Delta local_propagation_delay = | 328 const QuicTime::Delta local_propagation_delay = |
| 328 QuicTime::Delta::FromMilliseconds(1); | 329 QuicTime::Delta::FromMilliseconds(1); |
| 329 const QuicTime::Delta bottleneck_propagation_delay = | 330 const QuicTime::Delta bottleneck_propagation_delay = |
| 330 QuicTime::Delta::FromMilliseconds(20); | 331 QuicTime::Delta::FromMilliseconds(20); |
| 331 | 332 |
| 332 Simulator simulator; | 333 Simulator simulator; |
| 333 LinkSaturator saturator(&simulator, "Saturator", 1000, "Counter"); | 334 LinkSaturator saturator(&simulator, "Saturator", 1000, "Counter"); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 349 simulator.GetClock()->Now() + QuicTime::Delta::FromSeconds(10); | 350 simulator.GetClock()->Now() + QuicTime::Delta::FromSeconds(10); |
| 350 simulator.RunUntil([&simulator, &counter, deadline]() { | 351 simulator.RunUntil([&simulator, &counter, deadline]() { |
| 351 return counter.packets() == packets_received || | 352 return counter.packets() == packets_received || |
| 352 simulator.GetClock()->Now() > deadline; | 353 simulator.GetClock()->Now() > deadline; |
| 353 }); | 354 }); |
| 354 ASSERT_EQ(packets_received, counter.packets()); | 355 ASSERT_EQ(packets_received, counter.packets()); |
| 355 } | 356 } |
| 356 | 357 |
| 357 // Simulate a network where three endpoints are connected to a switch and they | 358 // Simulate a network where three endpoints are connected to a switch and they |
| 358 // are sending traffic in circle (1 -> 2, 2 -> 3, 3 -> 1). | 359 // are sending traffic in circle (1 -> 2, 2 -> 3, 3 -> 1). |
| 359 TEST(SimulatorTest, SwitchedNetwork) { | 360 TEST_F(SimulatorTest, SwitchedNetwork) { |
| 360 const QuicBandwidth bandwidth = QuicBandwidth::FromBytesPerSecond(10000); | 361 const QuicBandwidth bandwidth = QuicBandwidth::FromBytesPerSecond(10000); |
| 361 const QuicTime::Delta base_propagation_delay = | 362 const QuicTime::Delta base_propagation_delay = |
| 362 QuicTime::Delta::FromMilliseconds(50); | 363 QuicTime::Delta::FromMilliseconds(50); |
| 363 | 364 |
| 364 Simulator simulator; | 365 Simulator simulator; |
| 365 LinkSaturator saturator1(&simulator, "Saturator 1", 1000, "Saturator 2"); | 366 LinkSaturator saturator1(&simulator, "Saturator 1", 1000, "Saturator 2"); |
| 366 LinkSaturator saturator2(&simulator, "Saturator 2", 1000, "Saturator 3"); | 367 LinkSaturator saturator2(&simulator, "Saturator 2", 1000, "Saturator 3"); |
| 367 LinkSaturator saturator3(&simulator, "Saturator 3", 1000, "Saturator 1"); | 368 LinkSaturator saturator3(&simulator, "Saturator 3", 1000, "Saturator 1"); |
| 368 Switch network_switch(&simulator, "Switch", 8, | 369 Switch network_switch(&simulator, "Switch", 8, |
| 369 bandwidth * base_propagation_delay * 10); | 370 bandwidth * base_propagation_delay * 10); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 explicit CounterDelegate(size_t* counter) : counter_(counter) {} | 475 explicit CounterDelegate(size_t* counter) : counter_(counter) {} |
| 475 | 476 |
| 476 void OnAlarm() override { *counter_ += 1; } | 477 void OnAlarm() override { *counter_ += 1; } |
| 477 | 478 |
| 478 private: | 479 private: |
| 479 size_t* counter_; | 480 size_t* counter_; |
| 480 }; | 481 }; |
| 481 | 482 |
| 482 // Verifies that the alarms work correctly, even when they are repeatedly | 483 // Verifies that the alarms work correctly, even when they are repeatedly |
| 483 // toggled. | 484 // toggled. |
| 484 TEST(SimulatorTest, Alarms) { | 485 TEST_F(SimulatorTest, Alarms) { |
| 485 Simulator simulator; | 486 Simulator simulator; |
| 486 QuicAlarmFactory* alarm_factory = simulator.GetAlarmFactory(); | 487 QuicAlarmFactory* alarm_factory = simulator.GetAlarmFactory(); |
| 487 | 488 |
| 488 size_t fast_alarm_counter = 0; | 489 size_t fast_alarm_counter = 0; |
| 489 size_t slow_alarm_counter = 0; | 490 size_t slow_alarm_counter = 0; |
| 490 std::unique_ptr<QuicAlarm> alarm_fast( | 491 std::unique_ptr<QuicAlarm> alarm_fast( |
| 491 alarm_factory->CreateAlarm(new CounterDelegate(&fast_alarm_counter))); | 492 alarm_factory->CreateAlarm(new CounterDelegate(&fast_alarm_counter))); |
| 492 std::unique_ptr<QuicAlarm> alarm_slow( | 493 std::unique_ptr<QuicAlarm> alarm_slow( |
| 493 alarm_factory->CreateAlarm(new CounterDelegate(&slow_alarm_counter))); | 494 alarm_factory->CreateAlarm(new CounterDelegate(&slow_alarm_counter))); |
| 494 | 495 |
| 495 const QuicTime start_time = simulator.GetClock()->Now(); | 496 const QuicTime start_time = simulator.GetClock()->Now(); |
| 496 alarm_fast->Set(start_time + QuicTime::Delta::FromMilliseconds(100)); | 497 alarm_fast->Set(start_time + QuicTime::Delta::FromMilliseconds(100)); |
| 497 alarm_slow->Set(start_time + QuicTime::Delta::FromMilliseconds(750)); | 498 alarm_slow->Set(start_time + QuicTime::Delta::FromMilliseconds(750)); |
| 498 AlarmToggler toggler(&simulator, "Toggler", alarm_slow.get(), | 499 AlarmToggler toggler(&simulator, "Toggler", alarm_slow.get(), |
| 499 QuicTime::Delta::FromMilliseconds(100)); | 500 QuicTime::Delta::FromMilliseconds(100)); |
| 500 | 501 |
| 501 const QuicTime end_time = | 502 const QuicTime end_time = |
| 502 start_time + QuicTime::Delta::FromMilliseconds(1000); | 503 start_time + QuicTime::Delta::FromMilliseconds(1000); |
| 503 EXPECT_FALSE(simulator.RunUntil([&simulator, end_time]() { | 504 EXPECT_FALSE(simulator.RunUntil([&simulator, end_time]() { |
| 504 return simulator.GetClock()->Now() >= end_time; | 505 return simulator.GetClock()->Now() >= end_time; |
| 505 })); | 506 })); |
| 506 EXPECT_EQ(1u, slow_alarm_counter); | 507 EXPECT_EQ(1u, slow_alarm_counter); |
| 507 EXPECT_EQ(1u, fast_alarm_counter); | 508 EXPECT_EQ(1u, fast_alarm_counter); |
| 508 | 509 |
| 509 EXPECT_EQ(4, toggler.times_set()); | 510 EXPECT_EQ(4, toggler.times_set()); |
| 510 EXPECT_EQ(4, toggler.times_cancelled()); | 511 EXPECT_EQ(4, toggler.times_cancelled()); |
| 511 } | 512 } |
| 512 | 513 |
| 513 // Verifies that a cancelled alarm is never fired. | 514 // Verifies that a cancelled alarm is never fired. |
| 514 TEST(SimulatorTest, AlarmCancelling) { | 515 TEST_F(SimulatorTest, AlarmCancelling) { |
| 515 Simulator simulator; | 516 Simulator simulator; |
| 516 QuicAlarmFactory* alarm_factory = simulator.GetAlarmFactory(); | 517 QuicAlarmFactory* alarm_factory = simulator.GetAlarmFactory(); |
| 517 | 518 |
| 518 size_t alarm_counter = 0; | 519 size_t alarm_counter = 0; |
| 519 std::unique_ptr<QuicAlarm> alarm( | 520 std::unique_ptr<QuicAlarm> alarm( |
| 520 alarm_factory->CreateAlarm(new CounterDelegate(&alarm_counter))); | 521 alarm_factory->CreateAlarm(new CounterDelegate(&alarm_counter))); |
| 521 | 522 |
| 522 const QuicTime start_time = simulator.GetClock()->Now(); | 523 const QuicTime start_time = simulator.GetClock()->Now(); |
| 523 const QuicTime alarm_at = start_time + QuicTime::Delta::FromMilliseconds(300); | 524 const QuicTime alarm_at = start_time + QuicTime::Delta::FromMilliseconds(300); |
| 524 const QuicTime end_time = start_time + QuicTime::Delta::FromMilliseconds(400); | 525 const QuicTime end_time = start_time + QuicTime::Delta::FromMilliseconds(400); |
| 525 | 526 |
| 526 alarm->Set(alarm_at); | 527 alarm->Set(alarm_at); |
| 527 alarm->Cancel(); | 528 alarm->Cancel(); |
| 528 EXPECT_FALSE(alarm->IsSet()); | 529 EXPECT_FALSE(alarm->IsSet()); |
| 529 | 530 |
| 530 EXPECT_FALSE(simulator.RunUntil([&simulator, end_time]() { | 531 EXPECT_FALSE(simulator.RunUntil([&simulator, end_time]() { |
| 531 return simulator.GetClock()->Now() >= end_time; | 532 return simulator.GetClock()->Now() >= end_time; |
| 532 })); | 533 })); |
| 533 | 534 |
| 534 EXPECT_FALSE(alarm->IsSet()); | 535 EXPECT_FALSE(alarm->IsSet()); |
| 535 EXPECT_EQ(0u, alarm_counter); | 536 EXPECT_EQ(0u, alarm_counter); |
| 536 } | 537 } |
| 537 | 538 |
| 538 // Tests Simulator::RunUntilOrTimeout() interface. | 539 // Tests Simulator::RunUntilOrTimeout() interface. |
| 539 TEST(SimulatorTest, RunUntilOrTimeout) { | 540 TEST_F(SimulatorTest, RunUntilOrTimeout) { |
| 540 Simulator simulator; | 541 Simulator simulator; |
| 541 bool simulation_result; | 542 bool simulation_result; |
| 542 | 543 |
| 543 // Count the number of seconds since the beginning of the simulation. | 544 // Count the number of seconds since the beginning of the simulation. |
| 544 Counter counter(&simulator, "counter", QuicTime::Delta::FromSeconds(1)); | 545 Counter counter(&simulator, "counter", QuicTime::Delta::FromSeconds(1)); |
| 545 | 546 |
| 546 // Ensure that the counter reaches the value of 10 given a 20 second deadline. | 547 // Ensure that the counter reaches the value of 10 given a 20 second deadline. |
| 547 simulation_result = simulator.RunUntilOrTimeout( | 548 simulation_result = simulator.RunUntilOrTimeout( |
| 548 [&counter]() { return counter.get_value() == 10; }, | 549 [&counter]() { return counter.get_value() == 10; }, |
| 549 QuicTime::Delta::FromSeconds(20)); | 550 QuicTime::Delta::FromSeconds(20)); |
| 550 ASSERT_TRUE(simulation_result); | 551 ASSERT_TRUE(simulation_result); |
| 551 | 552 |
| 552 // Ensure that the counter will not reach the value of 100 given that the | 553 // Ensure that the counter will not reach the value of 100 given that the |
| 553 // starting value is 10 and the deadline is 20 seconds. | 554 // starting value is 10 and the deadline is 20 seconds. |
| 554 simulation_result = simulator.RunUntilOrTimeout( | 555 simulation_result = simulator.RunUntilOrTimeout( |
| 555 [&counter]() { return counter.get_value() == 100; }, | 556 [&counter]() { return counter.get_value() == 100; }, |
| 556 QuicTime::Delta::FromSeconds(20)); | 557 QuicTime::Delta::FromSeconds(20)); |
| 557 ASSERT_FALSE(simulation_result); | 558 ASSERT_FALSE(simulation_result); |
| 558 } | 559 } |
| 559 | 560 |
| 560 // Tests Simulator::RunFor() interface. | 561 // Tests Simulator::RunFor() interface. |
| 561 TEST(SimulatorTest, RunFor) { | 562 TEST_F(SimulatorTest, RunFor) { |
| 562 Simulator simulator; | 563 Simulator simulator; |
| 563 | 564 |
| 564 Counter counter(&simulator, "counter", QuicTime::Delta::FromSeconds(3)); | 565 Counter counter(&simulator, "counter", QuicTime::Delta::FromSeconds(3)); |
| 565 | 566 |
| 566 simulator.RunFor(QuicTime::Delta::FromSeconds(100)); | 567 simulator.RunFor(QuicTime::Delta::FromSeconds(100)); |
| 567 | 568 |
| 568 EXPECT_EQ(33, counter.get_value()); | 569 EXPECT_EQ(33, counter.get_value()); |
| 569 } | 570 } |
| 570 | 571 |
| 571 class MockPacketFilter : public PacketFilter { | 572 class MockPacketFilter : public PacketFilter { |
| 572 public: | 573 public: |
| 573 MockPacketFilter(Simulator* simulator, string name, Endpoint* endpoint) | 574 MockPacketFilter(Simulator* simulator, string name, Endpoint* endpoint) |
| 574 : PacketFilter(simulator, name, endpoint) {} | 575 : PacketFilter(simulator, name, endpoint) {} |
| 575 MOCK_METHOD1(FilterPacket, bool(const Packet&)); | 576 MOCK_METHOD1(FilterPacket, bool(const Packet&)); |
| 576 }; | 577 }; |
| 577 | 578 |
| 578 // Set up two trivial packet filters, one allowing any packets, and one dropping | 579 // Set up two trivial packet filters, one allowing any packets, and one dropping |
| 579 // all of them. | 580 // all of them. |
| 580 TEST(SimulatorTest, PacketFilter) { | 581 TEST_F(SimulatorTest, PacketFilter) { |
| 581 const QuicBandwidth bandwidth = | 582 const QuicBandwidth bandwidth = |
| 582 QuicBandwidth::FromBytesPerSecond(1024 * 1024); | 583 QuicBandwidth::FromBytesPerSecond(1024 * 1024); |
| 583 const QuicTime::Delta base_propagation_delay = | 584 const QuicTime::Delta base_propagation_delay = |
| 584 QuicTime::Delta::FromMilliseconds(5); | 585 QuicTime::Delta::FromMilliseconds(5); |
| 585 | 586 |
| 586 Simulator simulator; | 587 Simulator simulator; |
| 587 LinkSaturator saturator_a(&simulator, "Saturator A", 1000, "Saturator B"); | 588 LinkSaturator saturator_a(&simulator, "Saturator A", 1000, "Saturator B"); |
| 588 LinkSaturator saturator_b(&simulator, "Saturator B", 1000, "Saturator A"); | 589 LinkSaturator saturator_b(&simulator, "Saturator B", 1000, "Saturator A"); |
| 589 | 590 |
| 590 // Attach packets to the switch to create a delay between the point at which | 591 // Attach packets to the switch to create a delay between the point at which |
| (...skipping 18 matching lines...) Expand all Loading... |
| 609 | 610 |
| 610 // Run the simulation for a while, and expect that only B will receive any | 611 // Run the simulation for a while, and expect that only B will receive any |
| 611 // packets. | 612 // packets. |
| 612 simulator.RunFor(QuicTime::Delta::FromSeconds(10)); | 613 simulator.RunFor(QuicTime::Delta::FromSeconds(10)); |
| 613 EXPECT_GE(saturator_b.counter()->packets(), 1u); | 614 EXPECT_GE(saturator_b.counter()->packets(), 1u); |
| 614 EXPECT_EQ(saturator_a.counter()->packets(), 0u); | 615 EXPECT_EQ(saturator_a.counter()->packets(), 0u); |
| 615 } | 616 } |
| 616 | 617 |
| 617 // Set up a traffic policer in one direction that throttles at 25% of link | 618 // Set up a traffic policer in one direction that throttles at 25% of link |
| 618 // bandwidth, and put two link saturators at each endpoint. | 619 // bandwidth, and put two link saturators at each endpoint. |
| 619 TEST(SimulatorTest, TrafficPolicer) { | 620 TEST_F(SimulatorTest, TrafficPolicer) { |
| 620 const QuicBandwidth bandwidth = | 621 const QuicBandwidth bandwidth = |
| 621 QuicBandwidth::FromBytesPerSecond(1024 * 1024); | 622 QuicBandwidth::FromBytesPerSecond(1024 * 1024); |
| 622 const QuicTime::Delta base_propagation_delay = | 623 const QuicTime::Delta base_propagation_delay = |
| 623 QuicTime::Delta::FromMilliseconds(5); | 624 QuicTime::Delta::FromMilliseconds(5); |
| 624 const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(10); | 625 const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(10); |
| 625 | 626 |
| 626 Simulator simulator; | 627 Simulator simulator; |
| 627 LinkSaturator saturator1(&simulator, "Saturator 1", 1000, "Saturator 2"); | 628 LinkSaturator saturator1(&simulator, "Saturator 1", 1000, "Saturator 2"); |
| 628 LinkSaturator saturator2(&simulator, "Saturator 2", 1000, "Saturator 1"); | 629 LinkSaturator saturator2(&simulator, "Saturator 2", 1000, "Saturator 1"); |
| 629 Switch network_switch(&simulator, "Switch", 8, | 630 Switch network_switch(&simulator, "Switch", 8, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 | 668 |
| 668 // Check that only one direction is throttled. | 669 // Check that only one direction is throttled. |
| 669 test::ExpectApproxEq(saturator1.bytes_transmitted() / 4, | 670 test::ExpectApproxEq(saturator1.bytes_transmitted() / 4, |
| 670 saturator2.counter()->bytes(), 0.1f); | 671 saturator2.counter()->bytes(), 0.1f); |
| 671 test::ExpectApproxEq(saturator2.bytes_transmitted(), | 672 test::ExpectApproxEq(saturator2.bytes_transmitted(), |
| 672 saturator1.counter()->bytes(), 0.1f); | 673 saturator1.counter()->bytes(), 0.1f); |
| 673 } | 674 } |
| 674 | 675 |
| 675 // Ensure that a larger burst is allowed when the policed saturator exits | 676 // Ensure that a larger burst is allowed when the policed saturator exits |
| 676 // quiescence. | 677 // quiescence. |
| 677 TEST(SimulatorTest, TrafficPolicerBurst) { | 678 TEST_F(SimulatorTest, TrafficPolicerBurst) { |
| 678 const QuicBandwidth bandwidth = | 679 const QuicBandwidth bandwidth = |
| 679 QuicBandwidth::FromBytesPerSecond(1024 * 1024); | 680 QuicBandwidth::FromBytesPerSecond(1024 * 1024); |
| 680 const QuicTime::Delta base_propagation_delay = | 681 const QuicTime::Delta base_propagation_delay = |
| 681 QuicTime::Delta::FromMilliseconds(5); | 682 QuicTime::Delta::FromMilliseconds(5); |
| 682 const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(10); | 683 const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(10); |
| 683 | 684 |
| 684 Simulator simulator; | 685 Simulator simulator; |
| 685 LinkSaturator saturator1(&simulator, "Saturator 1", 1000, "Saturator 2"); | 686 LinkSaturator saturator1(&simulator, "Saturator 1", 1000, "Saturator 2"); |
| 686 LinkSaturator saturator2(&simulator, "Saturator 2", 1000, "Saturator 1"); | 687 LinkSaturator saturator2(&simulator, "Saturator 2", 1000, "Saturator 1"); |
| 687 Switch network_switch(&simulator, "Switch", 8, | 688 Switch network_switch(&simulator, "Switch", 8, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 saturator2.counter()->bytes(), 0.1f); | 723 saturator2.counter()->bytes(), 0.1f); |
| 723 | 724 |
| 724 // Expect subsequent traffic to be policed. | 725 // Expect subsequent traffic to be policed. |
| 725 saturator1.Resume(); | 726 saturator1.Resume(); |
| 726 simulator.RunFor(QuicTime::Delta::FromSeconds(10)); | 727 simulator.RunFor(QuicTime::Delta::FromSeconds(10)); |
| 727 test::ExpectApproxEq(saturator1.bytes_transmitted() / 4, | 728 test::ExpectApproxEq(saturator1.bytes_transmitted() / 4, |
| 728 saturator2.counter()->bytes(), 0.1f); | 729 saturator2.counter()->bytes(), 0.1f); |
| 729 } | 730 } |
| 730 | 731 |
| 731 // Test that the packet aggregation support in queues work. | 732 // Test that the packet aggregation support in queues work. |
| 732 TEST(SimulatorTest, PacketAggregation) { | 733 TEST_F(SimulatorTest, PacketAggregation) { |
| 733 // Model network where the delays are dominated by transfer delay. | 734 // Model network where the delays are dominated by transfer delay. |
| 734 const QuicBandwidth bandwidth = QuicBandwidth::FromBytesPerSecond(1000); | 735 const QuicBandwidth bandwidth = QuicBandwidth::FromBytesPerSecond(1000); |
| 735 const QuicTime::Delta base_propagation_delay = | 736 const QuicTime::Delta base_propagation_delay = |
| 736 QuicTime::Delta::FromMicroseconds(1); | 737 QuicTime::Delta::FromMicroseconds(1); |
| 737 const QuicByteCount aggregation_threshold = 1000; | 738 const QuicByteCount aggregation_threshold = 1000; |
| 738 const QuicTime::Delta aggregation_timeout = QuicTime::Delta::FromSeconds(30); | 739 const QuicTime::Delta aggregation_timeout = QuicTime::Delta::FromSeconds(30); |
| 739 | 740 |
| 740 Simulator simulator; | 741 Simulator simulator; |
| 741 LinkSaturator saturator1(&simulator, "Saturator 1", 10, "Saturator 2"); | 742 LinkSaturator saturator1(&simulator, "Saturator 1", 10, "Saturator 2"); |
| 742 LinkSaturator saturator2(&simulator, "Saturator 2", 10, "Saturator 1"); | 743 LinkSaturator saturator2(&simulator, "Saturator 2", 10, "Saturator 1"); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 799 EXPECT_EQ(500u, queue->bytes_queued()); | 800 EXPECT_EQ(500u, queue->bytes_queued()); |
| 800 | 801 |
| 801 // Time out again. | 802 // Time out again. |
| 802 simulator.RunFor(aggregation_timeout); | 803 simulator.RunFor(aggregation_timeout); |
| 803 EXPECT_EQ(6900u, saturator2.counter()->bytes()); | 804 EXPECT_EQ(6900u, saturator2.counter()->bytes()); |
| 804 EXPECT_EQ(0u, queue->bytes_queued()); | 805 EXPECT_EQ(0u, queue->bytes_queued()); |
| 805 } | 806 } |
| 806 | 807 |
| 807 } // namespace simulator | 808 } // namespace simulator |
| 808 } // namespace net | 809 } // namespace net |
| OLD | NEW |