| 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/tools/quic/quic_epoll_connection_helper.h" | 5 #include "net/tools/quic/quic_epoll_connection_helper.h" |
| 6 | 6 |
| 7 #include "net/quic/crypto/quic_random.h" | 7 #include "net/quic/crypto/quic_random.h" |
| 8 #include "net/tools/quic/test_tools/mock_epoll_server.h" | 8 #include "net/tools/quic/test_tools/mock_epoll_server.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 epoll_server_.AdvanceBy(delta.ToMicroseconds()); | 43 epoll_server_.AdvanceBy(delta.ToMicroseconds()); |
| 44 | 44 |
| 45 EXPECT_EQ(start.Add(delta), clock->Now()); | 45 EXPECT_EQ(start.Add(delta), clock->Now()); |
| 46 } | 46 } |
| 47 | 47 |
| 48 TEST_F(QuicEpollConnectionHelperTest, GetRandomGenerator) { | 48 TEST_F(QuicEpollConnectionHelperTest, GetRandomGenerator) { |
| 49 QuicRandom* random = helper_.GetRandomGenerator(); | 49 QuicRandom* random = helper_.GetRandomGenerator(); |
| 50 EXPECT_EQ(QuicRandom::GetInstance(), random); | 50 EXPECT_EQ(QuicRandom::GetInstance(), random); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // The boolean parameter denotes whether or not to use an arena. | |
| 54 class QuicEpollConnectionHelperAlarmTest | |
| 55 : public QuicEpollConnectionHelperTest, | |
| 56 public ::testing::WithParamInterface<bool> { | |
| 57 protected: | |
| 58 QuicConnectionArena* GetArenaParam() { | |
| 59 return GetParam() ? &arena_ : nullptr; | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 QuicConnectionArena arena_; | |
| 64 }; | |
| 65 | |
| 66 INSTANTIATE_TEST_CASE_P(QuicEpollConnectionHelperAlarmTest, | |
| 67 QuicEpollConnectionHelperAlarmTest, | |
| 68 ::testing::Bool()); | |
| 69 | |
| 70 TEST_P(QuicEpollConnectionHelperAlarmTest, CreateAlarm) { | |
| 71 QuicArenaScopedPtr<TestDelegate> delegate = | |
| 72 QuicArenaScopedPtr<TestDelegate>(new TestDelegate()); | |
| 73 QuicArenaScopedPtr<QuicAlarm> alarm( | |
| 74 helper_.CreateAlarm(std::move(delegate), GetArenaParam())); | |
| 75 | |
| 76 const QuicClock* clock = helper_.GetClock(); | |
| 77 QuicTime start = clock->Now(); | |
| 78 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | |
| 79 alarm->Set(start.Add(delta)); | |
| 80 | |
| 81 epoll_server_.AdvanceByAndWaitForEventsAndExecuteCallbacks( | |
| 82 delta.ToMicroseconds()); | |
| 83 EXPECT_EQ(start.Add(delta), clock->Now()); | |
| 84 } | |
| 85 | |
| 86 TEST_P(QuicEpollConnectionHelperAlarmTest, CreateAlarmAndCancel) { | |
| 87 QuicArenaScopedPtr<TestDelegate> delegate = | |
| 88 QuicArenaScopedPtr<TestDelegate>(new TestDelegate()); | |
| 89 TestDelegate* unowned_delegate = delegate.get(); | |
| 90 QuicArenaScopedPtr<QuicAlarm> alarm( | |
| 91 helper_.CreateAlarm(std::move(delegate), GetArenaParam())); | |
| 92 | |
| 93 const QuicClock* clock = helper_.GetClock(); | |
| 94 QuicTime start = clock->Now(); | |
| 95 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | |
| 96 alarm->Set(start.Add(delta)); | |
| 97 alarm->Cancel(); | |
| 98 | |
| 99 epoll_server_.AdvanceByExactlyAndCallCallbacks(delta.ToMicroseconds()); | |
| 100 EXPECT_EQ(start.Add(delta), clock->Now()); | |
| 101 EXPECT_FALSE(unowned_delegate->fired()); | |
| 102 } | |
| 103 | |
| 104 TEST_P(QuicEpollConnectionHelperAlarmTest, CreateAlarmAndReset) { | |
| 105 QuicArenaScopedPtr<TestDelegate> delegate = | |
| 106 QuicArenaScopedPtr<TestDelegate>(new TestDelegate()); | |
| 107 TestDelegate* unowned_delegate = delegate.get(); | |
| 108 QuicArenaScopedPtr<QuicAlarm> alarm( | |
| 109 helper_.CreateAlarm(std::move(delegate), GetArenaParam())); | |
| 110 | |
| 111 const QuicClock* clock = helper_.GetClock(); | |
| 112 QuicTime start = clock->Now(); | |
| 113 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | |
| 114 alarm->Set(clock->Now().Add(delta)); | |
| 115 alarm->Cancel(); | |
| 116 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); | |
| 117 alarm->Set(clock->Now().Add(new_delta)); | |
| 118 | |
| 119 epoll_server_.AdvanceByExactlyAndCallCallbacks(delta.ToMicroseconds()); | |
| 120 EXPECT_EQ(start.Add(delta), clock->Now()); | |
| 121 EXPECT_FALSE(unowned_delegate->fired()); | |
| 122 | |
| 123 epoll_server_.AdvanceByExactlyAndCallCallbacks( | |
| 124 new_delta.Subtract(delta).ToMicroseconds()); | |
| 125 EXPECT_EQ(start.Add(new_delta), clock->Now()); | |
| 126 EXPECT_TRUE(unowned_delegate->fired()); | |
| 127 } | |
| 128 | |
| 129 TEST_P(QuicEpollConnectionHelperAlarmTest, CreateAlarmAndUpdate) { | |
| 130 QuicArenaScopedPtr<TestDelegate> delegate = | |
| 131 QuicArenaScopedPtr<TestDelegate>(new TestDelegate()); | |
| 132 TestDelegate* unowned_delegate = delegate.get(); | |
| 133 QuicArenaScopedPtr<QuicAlarm> alarm( | |
| 134 helper_.CreateAlarm(std::move(delegate), GetArenaParam())); | |
| 135 | |
| 136 const QuicClock* clock = helper_.GetClock(); | |
| 137 QuicTime start = clock->Now(); | |
| 138 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); | |
| 139 alarm->Set(clock->Now().Add(delta)); | |
| 140 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); | |
| 141 alarm->Update(clock->Now().Add(new_delta), | |
| 142 QuicTime::Delta::FromMicroseconds(1)); | |
| 143 | |
| 144 epoll_server_.AdvanceByExactlyAndCallCallbacks(delta.ToMicroseconds()); | |
| 145 EXPECT_EQ(start.Add(delta), clock->Now()); | |
| 146 EXPECT_FALSE(unowned_delegate->fired()); | |
| 147 | |
| 148 // Move the alarm forward 1us and ensure it doesn't move forward. | |
| 149 alarm->Update(clock->Now().Add(new_delta), | |
| 150 QuicTime::Delta::FromMicroseconds(2)); | |
| 151 | |
| 152 epoll_server_.AdvanceByExactlyAndCallCallbacks( | |
| 153 new_delta.Subtract(delta).ToMicroseconds()); | |
| 154 EXPECT_EQ(start.Add(new_delta), clock->Now()); | |
| 155 EXPECT_TRUE(unowned_delegate->fired()); | |
| 156 | |
| 157 // Set the alarm via an update call. | |
| 158 new_delta = QuicTime::Delta::FromMicroseconds(5); | |
| 159 alarm->Update(clock->Now().Add(new_delta), | |
| 160 QuicTime::Delta::FromMicroseconds(1)); | |
| 161 EXPECT_TRUE(alarm->IsSet()); | |
| 162 | |
| 163 // Update it with an uninitialized time and ensure it's cancelled. | |
| 164 alarm->Update(QuicTime::Zero(), QuicTime::Delta::FromMicroseconds(1)); | |
| 165 EXPECT_FALSE(alarm->IsSet()); | |
| 166 } | |
| 167 | |
| 168 } // namespace | 53 } // namespace |
| 169 } // namespace test | 54 } // namespace test |
| 170 } // namespace net | 55 } // namespace net |
| OLD | NEW |