quic_chromium_alarm_factory_test.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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
  3. // found in the LICENSE file.
  4. #include "net/quic/quic_chromium_alarm_factory.h"
  5. #include "net/quic/test_task_runner.h"
  6. #include "net/third_party/quiche/src/quiche/quic/test_tools/mock_clock.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace net::test {
  9. namespace {
  10. class TestDelegate : public quic::QuicAlarm::DelegateWithoutContext {
  11. public:
  12. TestDelegate() = default;
  13. void OnAlarm() override { fired_ = true; }
  14. bool fired() const { return fired_; }
  15. void Clear() { fired_ = false; }
  16. private:
  17. bool fired_ = false;
  18. };
  19. class QuicChromiumAlarmFactoryTest : public ::testing::Test {
  20. protected:
  21. QuicChromiumAlarmFactoryTest()
  22. : runner_(base::MakeRefCounted<TestTaskRunner>(&clock_)),
  23. alarm_factory_(runner_.get(), &clock_) {}
  24. scoped_refptr<TestTaskRunner> runner_;
  25. QuicChromiumAlarmFactory alarm_factory_;
  26. quic::MockClock clock_;
  27. };
  28. TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarm) {
  29. TestDelegate* delegate = new TestDelegate();
  30. std::unique_ptr<quic::QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
  31. // Set the deadline 1µs in the future.
  32. constexpr quic::QuicTime::Delta kDelta =
  33. quic::QuicTime::Delta::FromMicroseconds(1);
  34. quic::QuicTime deadline = clock_.Now() + kDelta;
  35. alarm->Set(deadline);
  36. EXPECT_TRUE(alarm->IsSet());
  37. EXPECT_EQ(alarm->deadline(), deadline);
  38. EXPECT_FALSE(delegate->fired());
  39. runner_->FastForwardBy(kDelta);
  40. EXPECT_EQ(quic::QuicTime::Zero() + kDelta, clock_.Now());
  41. EXPECT_FALSE(alarm->IsSet());
  42. EXPECT_TRUE(delegate->fired());
  43. }
  44. TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndCancel) {
  45. TestDelegate* delegate = new TestDelegate();
  46. std::unique_ptr<quic::QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
  47. constexpr quic::QuicTime::Delta kDelta =
  48. quic::QuicTime::Delta::FromMicroseconds(1);
  49. quic::QuicTime deadline = clock_.Now() + kDelta;
  50. alarm->Set(deadline);
  51. EXPECT_TRUE(alarm->IsSet());
  52. EXPECT_EQ(alarm->deadline(), deadline);
  53. EXPECT_FALSE(delegate->fired());
  54. alarm->Cancel();
  55. EXPECT_FALSE(alarm->IsSet());
  56. EXPECT_FALSE(delegate->fired());
  57. // Advancing time should not cause the alarm to fire.
  58. runner_->FastForwardBy(kDelta);
  59. EXPECT_EQ(quic::QuicTime::Zero() + kDelta, clock_.Now());
  60. EXPECT_FALSE(alarm->IsSet());
  61. EXPECT_FALSE(delegate->fired());
  62. }
  63. TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndReset) {
  64. TestDelegate* delegate = new TestDelegate();
  65. std::unique_ptr<quic::QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
  66. // Set the deadline 1µs in the future.
  67. constexpr quic::QuicTime::Delta kDelta =
  68. quic::QuicTime::Delta::FromMicroseconds(1);
  69. quic::QuicTime deadline = clock_.Now() + kDelta;
  70. alarm->Set(deadline);
  71. EXPECT_TRUE(alarm->IsSet());
  72. EXPECT_EQ(alarm->deadline(), deadline);
  73. EXPECT_FALSE(delegate->fired());
  74. alarm->Cancel();
  75. EXPECT_FALSE(alarm->IsSet());
  76. EXPECT_FALSE(delegate->fired());
  77. // Set the timer with a longer delta.
  78. constexpr quic::QuicTime::Delta kNewDelta =
  79. quic::QuicTime::Delta::FromMicroseconds(3);
  80. quic::QuicTime new_deadline = clock_.Now() + kNewDelta;
  81. alarm->Set(new_deadline);
  82. EXPECT_TRUE(alarm->IsSet());
  83. EXPECT_EQ(alarm->deadline(), new_deadline);
  84. EXPECT_FALSE(delegate->fired());
  85. // Advancing time for the first delay should not cause the alarm to fire.
  86. runner_->FastForwardBy(kDelta);
  87. EXPECT_EQ(quic::QuicTime::Zero() + kDelta, clock_.Now());
  88. EXPECT_TRUE(alarm->IsSet());
  89. EXPECT_FALSE(delegate->fired());
  90. // Advancing time for the remaining of the new delay will fire the alarm.
  91. runner_->FastForwardBy(kNewDelta - kDelta);
  92. EXPECT_EQ(quic::QuicTime::Zero() + kNewDelta, clock_.Now());
  93. EXPECT_FALSE(alarm->IsSet());
  94. EXPECT_TRUE(delegate->fired());
  95. }
  96. TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndResetEarlier) {
  97. TestDelegate* delegate = new TestDelegate();
  98. std::unique_ptr<quic::QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
  99. // Set the deadline 3µs in the future.
  100. constexpr quic::QuicTime::Delta kDelta =
  101. quic::QuicTime::Delta::FromMicroseconds(3);
  102. quic::QuicTime deadline = clock_.Now() + kDelta;
  103. alarm->Set(deadline);
  104. EXPECT_TRUE(alarm->IsSet());
  105. EXPECT_EQ(alarm->deadline(), deadline);
  106. EXPECT_FALSE(delegate->fired());
  107. alarm->Cancel();
  108. EXPECT_FALSE(alarm->IsSet());
  109. EXPECT_FALSE(delegate->fired());
  110. // Set the timer with a shorter delta.
  111. constexpr quic::QuicTime::Delta kNewDelta =
  112. quic::QuicTime::Delta::FromMicroseconds(1);
  113. quic::QuicTime new_deadline = clock_.Now() + kNewDelta;
  114. alarm->Set(new_deadline);
  115. EXPECT_TRUE(alarm->IsSet());
  116. EXPECT_EQ(alarm->deadline(), new_deadline);
  117. EXPECT_FALSE(delegate->fired());
  118. // Advancing time for the shorter delay will fire the alarm.
  119. runner_->FastForwardBy(kNewDelta);
  120. EXPECT_EQ(quic::QuicTime::Zero() + kNewDelta, clock_.Now());
  121. EXPECT_FALSE(alarm->IsSet());
  122. EXPECT_TRUE(delegate->fired());
  123. delegate->Clear();
  124. EXPECT_FALSE(delegate->fired());
  125. // Advancing time for the remaining of the new original delay should not cause
  126. // the alarm to fire again.
  127. runner_->FastForwardBy(kDelta - kNewDelta);
  128. EXPECT_EQ(quic::QuicTime::Zero() + kDelta, clock_.Now());
  129. EXPECT_FALSE(alarm->IsSet());
  130. EXPECT_FALSE(delegate->fired());
  131. }
  132. TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndUpdate) {
  133. TestDelegate* delegate = new TestDelegate();
  134. std::unique_ptr<quic::QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
  135. // Set the deadline 1µs in the future.
  136. constexpr quic::QuicTime::Delta kDelta =
  137. quic::QuicTime::Delta::FromMicroseconds(1);
  138. quic::QuicTime deadline = clock_.Now() + kDelta;
  139. alarm->Set(deadline);
  140. EXPECT_TRUE(alarm->IsSet());
  141. EXPECT_EQ(alarm->deadline(), deadline);
  142. EXPECT_FALSE(delegate->fired());
  143. // Update the deadline.
  144. constexpr quic::QuicTime::Delta kNewDelta =
  145. quic::QuicTime::Delta::FromMicroseconds(3);
  146. quic::QuicTime new_deadline = clock_.Now() + kNewDelta;
  147. alarm->Update(new_deadline, quic::QuicTime::Delta::FromMicroseconds(1));
  148. EXPECT_TRUE(alarm->IsSet());
  149. EXPECT_EQ(alarm->deadline(), new_deadline);
  150. EXPECT_FALSE(delegate->fired());
  151. // Update the alarm with another delta that is not further away from the
  152. // current deadline than the granularity. The deadline should not change.
  153. alarm->Update(new_deadline + quic::QuicTime::Delta::FromMicroseconds(1),
  154. quic::QuicTime::Delta::FromMicroseconds(2));
  155. EXPECT_TRUE(alarm->IsSet());
  156. EXPECT_EQ(alarm->deadline(), new_deadline);
  157. EXPECT_FALSE(delegate->fired());
  158. // Advancing time for the first delay should not cause the alarm to fire.
  159. runner_->FastForwardBy(kDelta);
  160. EXPECT_EQ(quic::QuicTime::Zero() + kDelta, clock_.Now());
  161. EXPECT_TRUE(alarm->IsSet());
  162. EXPECT_FALSE(delegate->fired());
  163. // Advancing time for the remaining of the new delay will fire the alarm.
  164. runner_->FastForwardBy(kNewDelta - kDelta);
  165. EXPECT_EQ(quic::QuicTime::Zero() + kNewDelta, clock_.Now());
  166. EXPECT_FALSE(alarm->IsSet());
  167. EXPECT_TRUE(delegate->fired());
  168. // Set the alarm via an update call.
  169. new_deadline = clock_.Now() + quic::QuicTime::Delta::FromMicroseconds(5);
  170. alarm->Update(new_deadline, quic::QuicTime::Delta::FromMicroseconds(1));
  171. EXPECT_TRUE(alarm->IsSet());
  172. EXPECT_EQ(alarm->deadline(), new_deadline);
  173. // Update it with an uninitialized time and ensure it's cancelled.
  174. alarm->Update(quic::QuicTime::Zero(),
  175. quic::QuicTime::Delta::FromMicroseconds(1));
  176. EXPECT_FALSE(alarm->IsSet());
  177. }
  178. } // namespace
  179. } // namespace net::test