| 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/quic_chromium_connection_helper.h" | 5 #include "net/quic/quic_chromium_connection_helper.h" |
| 6 | 6 |
| 7 #include "net/quic/test_tools/mock_clock.h" | 7 #include "net/quic/test_tools/mock_clock.h" |
| 8 #include "net/quic/test_tools/mock_random.h" | 8 #include "net/quic/test_tools/mock_random.h" |
| 9 #include "net/quic/test_tools/test_task_runner.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 10 |
| 12 namespace net { | 11 namespace net { |
| 13 namespace test { | 12 namespace test { |
| 14 namespace { | 13 namespace { |
| 15 | 14 |
| 16 class TestDelegate : public QuicAlarm::Delegate { | |
| 17 public: | |
| 18 TestDelegate() : fired_(false) {} | |
| 19 | |
| 20 void OnAlarm() override { fired_ = true; } | |
| 21 | |
| 22 bool fired() const { return fired_; } | |
| 23 void Clear() { fired_ = false; } | |
| 24 | |
| 25 private: | |
| 26 bool fired_; | |
| 27 }; | |
| 28 | |
| 29 class QuicChromiumConnectionHelperTest : public ::testing::Test { | 15 class QuicChromiumConnectionHelperTest : public ::testing::Test { |
| 30 protected: | 16 protected: |
| 31 QuicChromiumConnectionHelperTest() | 17 QuicChromiumConnectionHelperTest() : helper_(&clock_, &random_generator_) {} |
| 32 : runner_(new TestTaskRunner(&clock_)), | |
| 33 helper_(runner_.get(), &clock_, &random_generator_) {} | |
| 34 | 18 |
| 35 scoped_refptr<TestTaskRunner> runner_; | |
| 36 QuicChromiumConnectionHelper helper_; | 19 QuicChromiumConnectionHelper helper_; |
| 37 MockClock clock_; | 20 MockClock clock_; |
| 38 MockRandom random_generator_; | 21 MockRandom random_generator_; |
| 39 }; | 22 }; |
| 40 | 23 |
| 41 TEST_F(QuicChromiumConnectionHelperTest, GetClock) { | 24 TEST_F(QuicChromiumConnectionHelperTest, GetClock) { |
| 42 EXPECT_EQ(&clock_, helper_.GetClock()); | 25 EXPECT_EQ(&clock_, helper_.GetClock()); |
| 43 } | 26 } |
| 44 | 27 |
| 45 TEST_F(QuicChromiumConnectionHelperTest, GetRandomGenerator) { | 28 TEST_F(QuicChromiumConnectionHelperTest, GetRandomGenerator) { |
| 46 EXPECT_EQ(&random_generator_, helper_.GetRandomGenerator()); | 29 EXPECT_EQ(&random_generator_, helper_.GetRandomGenerator()); |
| 47 } | 30 } |
| 48 | 31 |
| 49 TEST_F(QuicChromiumConnectionHelperTest, CreateAlarm) { | |
| 50 TestDelegate* delegate = new TestDelegate(); | |
| 51 std::unique_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate)); | |
| 52 | |
| 53 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | |
| 54 alarm->Set(clock_.Now().Add(delta)); | |
| 55 | |
| 56 // Verify that the alarm task has been posted. | |
| 57 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | |
| 58 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), | |
| 59 runner_->GetPostedTasks()[0].delay); | |
| 60 | |
| 61 runner_->RunNextTask(); | |
| 62 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now()); | |
| 63 EXPECT_TRUE(delegate->fired()); | |
| 64 } | |
| 65 | |
| 66 TEST_F(QuicChromiumConnectionHelperTest, CreateAlarmAndCancel) { | |
| 67 TestDelegate* delegate = new TestDelegate(); | |
| 68 std::unique_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate)); | |
| 69 | |
| 70 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | |
| 71 alarm->Set(clock_.Now().Add(delta)); | |
| 72 alarm->Cancel(); | |
| 73 | |
| 74 // The alarm task should still be posted. | |
| 75 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | |
| 76 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), | |
| 77 runner_->GetPostedTasks()[0].delay); | |
| 78 | |
| 79 runner_->RunNextTask(); | |
| 80 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now()); | |
| 81 EXPECT_FALSE(delegate->fired()); | |
| 82 } | |
| 83 | |
| 84 TEST_F(QuicChromiumConnectionHelperTest, CreateAlarmAndReset) { | |
| 85 TestDelegate* delegate = new TestDelegate(); | |
| 86 std::unique_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate)); | |
| 87 | |
| 88 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | |
| 89 alarm->Set(clock_.Now().Add(delta)); | |
| 90 alarm->Cancel(); | |
| 91 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); | |
| 92 alarm->Set(clock_.Now().Add(new_delta)); | |
| 93 | |
| 94 // The alarm task should still be posted. | |
| 95 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | |
| 96 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), | |
| 97 runner_->GetPostedTasks()[0].delay); | |
| 98 | |
| 99 runner_->RunNextTask(); | |
| 100 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now()); | |
| 101 EXPECT_FALSE(delegate->fired()); | |
| 102 | |
| 103 // The alarm task should be posted again. | |
| 104 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | |
| 105 | |
| 106 runner_->RunNextTask(); | |
| 107 EXPECT_EQ(QuicTime::Zero().Add(new_delta), clock_.Now()); | |
| 108 EXPECT_TRUE(delegate->fired()); | |
| 109 } | |
| 110 | |
| 111 TEST_F(QuicChromiumConnectionHelperTest, CreateAlarmAndResetEarlier) { | |
| 112 TestDelegate* delegate = new TestDelegate(); | |
| 113 std::unique_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate)); | |
| 114 | |
| 115 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(3); | |
| 116 alarm->Set(clock_.Now().Add(delta)); | |
| 117 alarm->Cancel(); | |
| 118 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(1); | |
| 119 alarm->Set(clock_.Now().Add(new_delta)); | |
| 120 | |
| 121 // Both alarm tasks will be posted. | |
| 122 ASSERT_EQ(2u, runner_->GetPostedTasks().size()); | |
| 123 | |
| 124 // The earlier task will execute and will fire the alarm-> | |
| 125 runner_->RunNextTask(); | |
| 126 EXPECT_EQ(QuicTime::Zero().Add(new_delta), clock_.Now()); | |
| 127 EXPECT_TRUE(delegate->fired()); | |
| 128 delegate->Clear(); | |
| 129 | |
| 130 // The latter task is still posted. | |
| 131 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | |
| 132 | |
| 133 // When the latter task is executed, the weak ptr will be invalid and | |
| 134 // the alarm will not fire. | |
| 135 runner_->RunNextTask(); | |
| 136 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now()); | |
| 137 EXPECT_FALSE(delegate->fired()); | |
| 138 } | |
| 139 | |
| 140 TEST_F(QuicChromiumConnectionHelperTest, CreateAlarmAndUpdate) { | |
| 141 TestDelegate* delegate = new TestDelegate(); | |
| 142 std::unique_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate)); | |
| 143 | |
| 144 const QuicClock* clock = helper_.GetClock(); | |
| 145 QuicTime start = clock->Now(); | |
| 146 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | |
| 147 alarm->Set(clock->Now().Add(delta)); | |
| 148 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); | |
| 149 alarm->Update(clock->Now().Add(new_delta), | |
| 150 QuicTime::Delta::FromMicroseconds(1)); | |
| 151 | |
| 152 // The alarm task should still be posted. | |
| 153 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | |
| 154 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), | |
| 155 runner_->GetPostedTasks()[0].delay); | |
| 156 | |
| 157 runner_->RunNextTask(); | |
| 158 EXPECT_EQ(QuicTime::Zero().Add(delta), clock->Now()); | |
| 159 EXPECT_FALSE(delegate->fired()); | |
| 160 | |
| 161 // Move the alarm forward 1us and ensure it doesn't move forward. | |
| 162 alarm->Update(clock->Now().Add(new_delta), | |
| 163 QuicTime::Delta::FromMicroseconds(2)); | |
| 164 | |
| 165 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | |
| 166 EXPECT_EQ(base::TimeDelta::FromMicroseconds( | |
| 167 new_delta.Subtract(delta).ToMicroseconds()), | |
| 168 runner_->GetPostedTasks()[0].delay); | |
| 169 runner_->RunNextTask(); | |
| 170 EXPECT_EQ(start.Add(new_delta), clock->Now()); | |
| 171 EXPECT_TRUE(delegate->fired()); | |
| 172 | |
| 173 // Set the alarm via an update call. | |
| 174 new_delta = QuicTime::Delta::FromMicroseconds(5); | |
| 175 alarm->Update(clock->Now().Add(new_delta), | |
| 176 QuicTime::Delta::FromMicroseconds(1)); | |
| 177 EXPECT_TRUE(alarm->IsSet()); | |
| 178 | |
| 179 // Update it with an uninitialized time and ensure it's cancelled. | |
| 180 alarm->Update(QuicTime::Zero(), QuicTime::Delta::FromMicroseconds(1)); | |
| 181 EXPECT_FALSE(alarm->IsSet()); | |
| 182 } | |
| 183 | |
| 184 } // namespace | 32 } // namespace |
| 185 } // namespace test | 33 } // namespace test |
| 186 } // namespace net | 34 } // namespace net |
| OLD | NEW |