| 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 "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "net/quic/test_tools/quic_test_utils.h" | 8 #include "net/quic/test_tools/quic_test_utils.h" |
| 9 #include "net/quic/test_tools/simulator/alarm_factory.h" | 9 #include "net/quic/test_tools/simulator/alarm_factory.h" |
| 10 #include "net/quic/test_tools/simulator/link.h" | 10 #include "net/quic/test_tools/simulator/link.h" |
| 11 #include "net/quic/test_tools/simulator/packet_filter.h" |
| 11 #include "net/quic/test_tools/simulator/queue.h" | 12 #include "net/quic/test_tools/simulator/queue.h" |
| 12 #include "net/quic/test_tools/simulator/switch.h" | 13 #include "net/quic/test_tools/simulator/switch.h" |
| 13 #include "net/quic/test_tools/simulator/traffic_policer.h" | 14 #include "net/quic/test_tools/simulator/traffic_policer.h" |
| 14 | 15 |
| 15 #include "net/test/gtest_util.h" | 16 #include "net/test/gtest_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 19 using testing::_; |
| 20 using testing::Return; |
| 21 using testing::StrictMock; |
| 22 |
| 18 namespace net { | 23 namespace net { |
| 19 namespace simulator { | 24 namespace simulator { |
| 20 | 25 |
| 21 // 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. |
| 22 class Counter : public Actor { | 27 class Counter : public Actor { |
| 23 public: | 28 public: |
| 24 Counter(Simulator* simulator, std::string name, QuicTime::Delta period) | 29 Counter(Simulator* simulator, std::string name, QuicTime::Delta period) |
| 25 : Actor(simulator, name), value_(-1), period_(period) { | 30 : Actor(simulator, name), value_(-1), period_(period) { |
| 26 Schedule(clock_->Now()); | 31 Schedule(clock_->Now()); |
| 27 } | 32 } |
| (...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 TEST(SimulatorTest, RunFor) { | 562 TEST(SimulatorTest, RunFor) { |
| 558 Simulator simulator; | 563 Simulator simulator; |
| 559 | 564 |
| 560 Counter counter(&simulator, "counter", QuicTime::Delta::FromSeconds(3)); | 565 Counter counter(&simulator, "counter", QuicTime::Delta::FromSeconds(3)); |
| 561 | 566 |
| 562 simulator.RunFor(QuicTime::Delta::FromSeconds(100)); | 567 simulator.RunFor(QuicTime::Delta::FromSeconds(100)); |
| 563 | 568 |
| 564 EXPECT_EQ(33, counter.get_value()); | 569 EXPECT_EQ(33, counter.get_value()); |
| 565 } | 570 } |
| 566 | 571 |
| 572 class MockPacketFilter : public PacketFilter { |
| 573 public: |
| 574 MockPacketFilter(Simulator* simulator, std::string name, Endpoint* endpoint) |
| 575 : PacketFilter(simulator, name, endpoint) {} |
| 576 MOCK_METHOD1(FilterPacket, bool(const Packet&)); |
| 577 }; |
| 578 |
| 579 // Set up two trivial packet filters, one allowing any packets, and one dropping |
| 580 // all of them. |
| 581 TEST(SimulatorTest, PacketFilter) { |
| 582 const QuicBandwidth bandwidth = |
| 583 QuicBandwidth::FromBytesPerSecond(1024 * 1024); |
| 584 const QuicTime::Delta base_propagation_delay = |
| 585 QuicTime::Delta::FromMilliseconds(5); |
| 586 |
| 587 Simulator simulator; |
| 588 LinkSaturator saturator_a(&simulator, "Saturator A", 1000, "Saturator B"); |
| 589 LinkSaturator saturator_b(&simulator, "Saturator B", 1000, "Saturator A"); |
| 590 |
| 591 // Attach packets to the switch to create a delay between the point at which |
| 592 // the packet is generated and the point at which it is filtered. Note that |
| 593 // if the saturators were connected directly, the link would be always |
| 594 // available for the endpoint which has all of its packets dropped, resulting |
| 595 // in saturator looping infinitely. |
| 596 Switch network_switch(&simulator, "Switch", 8, |
| 597 bandwidth * base_propagation_delay * 10); |
| 598 StrictMock<MockPacketFilter> a_to_b_filter(&simulator, "A -> B filter", |
| 599 network_switch.port(1)); |
| 600 StrictMock<MockPacketFilter> b_to_a_filter(&simulator, "B -> A filter", |
| 601 network_switch.port(2)); |
| 602 SymmetricLink link_a(&a_to_b_filter, &saturator_b, bandwidth, |
| 603 base_propagation_delay); |
| 604 SymmetricLink link_b(&b_to_a_filter, &saturator_a, bandwidth, |
| 605 base_propagation_delay); |
| 606 |
| 607 // Allow packets from A to B, but not from B to A. |
| 608 EXPECT_CALL(a_to_b_filter, FilterPacket(_)).WillRepeatedly(Return(true)); |
| 609 EXPECT_CALL(b_to_a_filter, FilterPacket(_)).WillRepeatedly(Return(false)); |
| 610 |
| 611 // Run the simulation for a while, and expect that only B will receive any |
| 612 // packets. |
| 613 simulator.RunFor(QuicTime::Delta::FromSeconds(10)); |
| 614 EXPECT_GE(saturator_b.counter()->packets(), 1u); |
| 615 EXPECT_EQ(saturator_a.counter()->packets(), 0u); |
| 616 } |
| 617 |
| 567 // 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 |
| 568 // bandwidth, and put two link saturators at each endpoint. | 619 // bandwidth, and put two link saturators at each endpoint. |
| 569 TEST(SimulatorTest, TrafficPolicer) { | 620 TEST(SimulatorTest, TrafficPolicer) { |
| 570 const QuicBandwidth bandwidth = | 621 const QuicBandwidth bandwidth = |
| 571 QuicBandwidth::FromBytesPerSecond(1024 * 1024); | 622 QuicBandwidth::FromBytesPerSecond(1024 * 1024); |
| 572 const QuicTime::Delta base_propagation_delay = | 623 const QuicTime::Delta base_propagation_delay = |
| 573 QuicTime::Delta::FromMilliseconds(5); | 624 QuicTime::Delta::FromMilliseconds(5); |
| 574 const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(10); | 625 const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(10); |
| 575 | 626 |
| 576 Simulator simulator; | 627 Simulator simulator; |
| 577 LinkSaturator saturator1(&simulator, "Saturator 1", 1000, "Saturator 2"); | 628 LinkSaturator saturator1(&simulator, "Saturator 1", 1000, "Saturator 2"); |
| 578 LinkSaturator saturator2(&simulator, "Saturator 2", 1000, "Saturator 1"); | 629 LinkSaturator saturator2(&simulator, "Saturator 2", 1000, "Saturator 1"); |
| 579 Switch network_switch(&simulator, "Switch", 8, | 630 Switch network_switch(&simulator, "Switch", 8, |
| 580 bandwidth * base_propagation_delay * 10); | 631 bandwidth * base_propagation_delay * 10); |
| 581 | 632 |
| 582 const QuicByteCount initial_burst = 1000 * 10; | 633 const QuicByteCount initial_burst = 1000 * 10; |
| 583 const QuicByteCount max_bucket_size = 1000 * 100; | 634 const QuicByteCount max_bucket_size = 1000 * 100; |
| 584 const QuicBandwidth target_bandwidth = bandwidth * 0.25; | 635 const QuicBandwidth target_bandwidth = bandwidth * 0.25; |
| 585 TrafficPolicer policer(&simulator, "Policer", initial_burst, max_bucket_size, | 636 TrafficPolicer policer(&simulator, "Policer", initial_burst, max_bucket_size, |
| 586 target_bandwidth, network_switch.port(2)); | 637 target_bandwidth, network_switch.port(2)); |
| 587 | 638 |
| 588 SymmetricLink link1(&saturator1, network_switch.port(1), bandwidth, | 639 SymmetricLink link1(&saturator1, network_switch.port(1), bandwidth, |
| 589 base_propagation_delay); | 640 base_propagation_delay); |
| 590 SymmetricLink link2(&saturator2, policer.output(), bandwidth, | 641 SymmetricLink link2(&saturator2, &policer, bandwidth, base_propagation_delay); |
| 591 base_propagation_delay); | |
| 592 | 642 |
| 593 // Ensure the initial burst passes without being dropped at all. | 643 // Ensure the initial burst passes without being dropped at all. |
| 594 bool simulator_result = simulator.RunUntilOrTimeout( | 644 bool simulator_result = simulator.RunUntilOrTimeout( |
| 595 [&saturator1, initial_burst]() { | 645 [&saturator1, initial_burst]() { |
| 596 return saturator1.bytes_transmitted() == initial_burst; | 646 return saturator1.bytes_transmitted() == initial_burst; |
| 597 }, | 647 }, |
| 598 timeout); | 648 timeout); |
| 599 ASSERT_TRUE(simulator_result); | 649 ASSERT_TRUE(simulator_result); |
| 600 saturator1.Pause(); | 650 saturator1.Pause(); |
| 601 simulator_result = simulator.RunUntilOrTimeout( | 651 simulator_result = simulator.RunUntilOrTimeout( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 bandwidth * base_propagation_delay * 10); | 689 bandwidth * base_propagation_delay * 10); |
| 640 | 690 |
| 641 const QuicByteCount initial_burst = 1000 * 10; | 691 const QuicByteCount initial_burst = 1000 * 10; |
| 642 const QuicByteCount max_bucket_size = 1000 * 100; | 692 const QuicByteCount max_bucket_size = 1000 * 100; |
| 643 const QuicBandwidth target_bandwidth = bandwidth * 0.25; | 693 const QuicBandwidth target_bandwidth = bandwidth * 0.25; |
| 644 TrafficPolicer policer(&simulator, "Policer", initial_burst, max_bucket_size, | 694 TrafficPolicer policer(&simulator, "Policer", initial_burst, max_bucket_size, |
| 645 target_bandwidth, network_switch.port(2)); | 695 target_bandwidth, network_switch.port(2)); |
| 646 | 696 |
| 647 SymmetricLink link1(&saturator1, network_switch.port(1), bandwidth, | 697 SymmetricLink link1(&saturator1, network_switch.port(1), bandwidth, |
| 648 base_propagation_delay); | 698 base_propagation_delay); |
| 649 SymmetricLink link2(&saturator2, policer.output(), bandwidth, | 699 SymmetricLink link2(&saturator2, &policer, bandwidth, base_propagation_delay); |
| 650 base_propagation_delay); | |
| 651 | 700 |
| 652 // Ensure at least one packet is sent on each side. | 701 // Ensure at least one packet is sent on each side. |
| 653 bool simulator_result = simulator.RunUntilOrTimeout( | 702 bool simulator_result = simulator.RunUntilOrTimeout( |
| 654 [&saturator1, &saturator2]() { | 703 [&saturator1, &saturator2]() { |
| 655 return saturator1.packets_transmitted() > 0 && | 704 return saturator1.packets_transmitted() > 0 && |
| 656 saturator2.packets_transmitted() > 0; | 705 saturator2.packets_transmitted() > 0; |
| 657 }, | 706 }, |
| 658 timeout); | 707 timeout); |
| 659 ASSERT_TRUE(simulator_result); | 708 ASSERT_TRUE(simulator_result); |
| 660 | 709 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 675 | 724 |
| 676 // Expect subsequent traffic to be policed. | 725 // Expect subsequent traffic to be policed. |
| 677 saturator1.Resume(); | 726 saturator1.Resume(); |
| 678 simulator.RunFor(QuicTime::Delta::FromSeconds(10)); | 727 simulator.RunFor(QuicTime::Delta::FromSeconds(10)); |
| 679 test::ExpectApproxEq(saturator1.bytes_transmitted() / 4, | 728 test::ExpectApproxEq(saturator1.bytes_transmitted() / 4, |
| 680 saturator2.counter()->bytes(), 0.1f); | 729 saturator2.counter()->bytes(), 0.1f); |
| 681 } | 730 } |
| 682 | 731 |
| 683 } // namespace simulator | 732 } // namespace simulator |
| 684 } // namespace net | 733 } // namespace net |
| OLD | NEW |