| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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/quartc/quartc_alarm_factory.h" | 5 #include "net/quic/quartc/quartc_alarm_factory.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/test_task_runner.h" | 8 #include "net/quic/test_tools/test_task_runner.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 TEST_F(QuartcAlarmFactoryTest, CreateAlarm) { | 39 TEST_F(QuartcAlarmFactoryTest, CreateAlarm) { |
| 40 TestDelegate* delegate = new TestDelegate(); | 40 TestDelegate* delegate = new TestDelegate(); |
| 41 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); | 41 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); |
| 42 | 42 |
| 43 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | 43 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); |
| 44 alarm->Set(clock_.Now() + delta); | 44 alarm->Set(clock_.Now() + delta); |
| 45 | 45 |
| 46 // Verify that the alarm task has been posted. | 46 // Verify that the alarm task has been posted. |
| 47 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | 47 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); |
| 48 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), | 48 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), |
| 49 runner_->GetPostedTasks()[0].delay); | 49 runner_->GetPostedTasks()[0].first.delay); |
| 50 | 50 |
| 51 runner_->RunNextTask(); | 51 runner_->RunNextTask(); |
| 52 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); | 52 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); |
| 53 EXPECT_TRUE(delegate->fired()); | 53 EXPECT_TRUE(delegate->fired()); |
| 54 } | 54 } |
| 55 | 55 |
| 56 TEST_F(QuartcAlarmFactoryTest, CreateAlarmAndCancel) { | 56 TEST_F(QuartcAlarmFactoryTest, CreateAlarmAndCancel) { |
| 57 TestDelegate* delegate = new TestDelegate(); | 57 TestDelegate* delegate = new TestDelegate(); |
| 58 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); | 58 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); |
| 59 | 59 |
| 60 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | 60 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); |
| 61 alarm->Set(clock_.Now() + delta); | 61 alarm->Set(clock_.Now() + delta); |
| 62 alarm->Cancel(); | 62 alarm->Cancel(); |
| 63 | 63 |
| 64 // The alarm task should still be posted. | 64 // The alarm task should still be posted. |
| 65 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | 65 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); |
| 66 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), | 66 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), |
| 67 runner_->GetPostedTasks()[0].delay); | 67 runner_->GetPostedTasks()[0].first.delay); |
| 68 | 68 |
| 69 runner_->RunNextTask(); | 69 runner_->RunNextTask(); |
| 70 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); | 70 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); |
| 71 EXPECT_FALSE(delegate->fired()); | 71 EXPECT_FALSE(delegate->fired()); |
| 72 } | 72 } |
| 73 | 73 |
| 74 TEST_F(QuartcAlarmFactoryTest, CreateAlarmAndReset) { | 74 TEST_F(QuartcAlarmFactoryTest, CreateAlarmAndReset) { |
| 75 TestDelegate* delegate = new TestDelegate(); | 75 TestDelegate* delegate = new TestDelegate(); |
| 76 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); | 76 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); |
| 77 | 77 |
| 78 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | 78 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); |
| 79 alarm->Set(clock_.Now() + delta); | 79 alarm->Set(clock_.Now() + delta); |
| 80 alarm->Cancel(); | 80 alarm->Cancel(); |
| 81 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); | 81 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); |
| 82 alarm->Set(clock_.Now() + new_delta); | 82 alarm->Set(clock_.Now() + new_delta); |
| 83 | 83 |
| 84 // The alarm task should still be posted. | 84 // The alarm task should still be posted. |
| 85 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | 85 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); |
| 86 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), | 86 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), |
| 87 runner_->GetPostedTasks()[0].delay); | 87 runner_->GetPostedTasks()[0].first.delay); |
| 88 | 88 |
| 89 runner_->RunNextTask(); | 89 runner_->RunNextTask(); |
| 90 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); | 90 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); |
| 91 EXPECT_FALSE(delegate->fired()); | 91 EXPECT_FALSE(delegate->fired()); |
| 92 | 92 |
| 93 // The alarm task should be posted again. | 93 // The alarm task should be posted again. |
| 94 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | 94 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); |
| 95 | 95 |
| 96 runner_->RunNextTask(); | 96 runner_->RunNextTask(); |
| 97 EXPECT_EQ(QuicTime::Zero() + new_delta, clock_.Now()); | 97 EXPECT_EQ(QuicTime::Zero() + new_delta, clock_.Now()); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 | 133 |
| 134 QuicTime start = clock_.Now(); | 134 QuicTime start = clock_.Now(); |
| 135 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | 135 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); |
| 136 alarm->Set(clock_.Now() + delta); | 136 alarm->Set(clock_.Now() + delta); |
| 137 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); | 137 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); |
| 138 alarm->Update(clock_.Now() + new_delta, QuicTime::Delta::FromMicroseconds(1)); | 138 alarm->Update(clock_.Now() + new_delta, QuicTime::Delta::FromMicroseconds(1)); |
| 139 | 139 |
| 140 // The alarm task should still be posted. | 140 // The alarm task should still be posted. |
| 141 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | 141 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); |
| 142 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), | 142 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), |
| 143 runner_->GetPostedTasks()[0].delay); | 143 runner_->GetPostedTasks()[0].first.delay); |
| 144 | 144 |
| 145 runner_->RunNextTask(); | 145 runner_->RunNextTask(); |
| 146 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); | 146 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); |
| 147 EXPECT_FALSE(delegate->fired()); | 147 EXPECT_FALSE(delegate->fired()); |
| 148 | 148 |
| 149 // Move the alarm forward 1us and ensure it doesn't move forward. | 149 // Move the alarm forward 1us and ensure it doesn't move forward. |
| 150 alarm->Update(clock_.Now() + new_delta, QuicTime::Delta::FromMicroseconds(2)); | 150 alarm->Update(clock_.Now() + new_delta, QuicTime::Delta::FromMicroseconds(2)); |
| 151 | 151 |
| 152 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); | 152 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); |
| 153 EXPECT_EQ( | 153 EXPECT_EQ( |
| 154 base::TimeDelta::FromMicroseconds((new_delta - delta).ToMicroseconds()), | 154 base::TimeDelta::FromMicroseconds((new_delta - delta).ToMicroseconds()), |
| 155 runner_->GetPostedTasks()[0].delay); | 155 runner_->GetPostedTasks()[0].first.delay); |
| 156 runner_->RunNextTask(); | 156 runner_->RunNextTask(); |
| 157 EXPECT_EQ(start + new_delta, clock_.Now()); | 157 EXPECT_EQ(start + new_delta, clock_.Now()); |
| 158 EXPECT_TRUE(delegate->fired()); | 158 EXPECT_TRUE(delegate->fired()); |
| 159 | 159 |
| 160 // Set the alarm via an update call. | 160 // Set the alarm via an update call. |
| 161 new_delta = QuicTime::Delta::FromMicroseconds(5); | 161 new_delta = QuicTime::Delta::FromMicroseconds(5); |
| 162 alarm->Update(clock_.Now() + new_delta, QuicTime::Delta::FromMicroseconds(1)); | 162 alarm->Update(clock_.Now() + new_delta, QuicTime::Delta::FromMicroseconds(1)); |
| 163 EXPECT_TRUE(alarm->IsSet()); | 163 EXPECT_TRUE(alarm->IsSet()); |
| 164 | 164 |
| 165 // Update it with an uninitialized time and ensure it's cancelled. | 165 // Update it with an uninitialized time and ensure it's cancelled. |
| 166 alarm->Update(QuicTime::Zero(), QuicTime::Delta::FromMicroseconds(1)); | 166 alarm->Update(QuicTime::Zero(), QuicTime::Delta::FromMicroseconds(1)); |
| 167 EXPECT_FALSE(alarm->IsSet()); | 167 EXPECT_FALSE(alarm->IsSet()); |
| 168 } | 168 } |
| 169 | 169 |
| 170 } // namespace | 170 } // namespace |
| 171 } // namespace test | 171 } // namespace test |
| 172 } // namespace net | 172 } // namespace net |
| OLD | NEW |