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.
5 #include "net/tools/quic/quic_epoll_connection_helper.h"
7 #include "net/quic/crypto/quic_random.h"
8 #include "net/tools/quic/test_tools/mock_epoll_server.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using net::tools::test::MockEpollServer
;
18 class TestDelegate
: public QuicAlarm::Delegate
{
20 TestDelegate() : fired_(false) {}
22 QuicTime
OnAlarm() override
{
24 return QuicTime::Zero();
27 bool fired() const { return fired_
; }
33 class QuicEpollConnectionHelperTest
: public ::testing::Test
{
35 QuicEpollConnectionHelperTest() : helper_(&epoll_server_
) {}
37 MockEpollServer epoll_server_
;
38 QuicEpollConnectionHelper helper_
;
41 TEST_F(QuicEpollConnectionHelperTest
, GetClock
) {
42 const QuicClock
* clock
= helper_
.GetClock();
43 QuicTime start
= clock
->Now();
45 QuicTime::Delta delta
= QuicTime::Delta::FromMilliseconds(5);
46 epoll_server_
.AdvanceBy(delta
.ToMicroseconds());
48 EXPECT_EQ(start
.Add(delta
), clock
->Now());
51 TEST_F(QuicEpollConnectionHelperTest
, GetRandomGenerator
) {
52 QuicRandom
* random
= helper_
.GetRandomGenerator();
53 EXPECT_EQ(QuicRandom::GetInstance(), random
);
56 TEST_F(QuicEpollConnectionHelperTest
, CreateAlarm
) {
57 TestDelegate
* delegate
= new TestDelegate();
58 scoped_ptr
<QuicAlarm
> alarm(helper_
.CreateAlarm(delegate
));
60 const QuicClock
* clock
= helper_
.GetClock();
61 QuicTime start
= clock
->Now();
62 QuicTime::Delta delta
= QuicTime::Delta::FromMicroseconds(1);
63 alarm
->Set(start
.Add(delta
));
65 epoll_server_
.AdvanceByAndWaitForEventsAndExecuteCallbacks(
66 delta
.ToMicroseconds());
67 EXPECT_EQ(start
.Add(delta
), clock
->Now());
70 TEST_F(QuicEpollConnectionHelperTest
, CreateAlarmAndCancel
) {
71 TestDelegate
* delegate
= new TestDelegate();
72 scoped_ptr
<QuicAlarm
> alarm(helper_
.CreateAlarm(delegate
));
74 const QuicClock
* clock
= helper_
.GetClock();
75 QuicTime start
= clock
->Now();
76 QuicTime::Delta delta
= QuicTime::Delta::FromMicroseconds(1);
77 alarm
->Set(start
.Add(delta
));
80 epoll_server_
.AdvanceByExactlyAndCallCallbacks(delta
.ToMicroseconds());
81 EXPECT_EQ(start
.Add(delta
), clock
->Now());
82 EXPECT_FALSE(delegate
->fired());
85 TEST_F(QuicEpollConnectionHelperTest
, CreateAlarmAndReset
) {
86 TestDelegate
* delegate
= new TestDelegate();
87 scoped_ptr
<QuicAlarm
> alarm(helper_
.CreateAlarm(delegate
));
89 const QuicClock
* clock
= helper_
.GetClock();
90 QuicTime start
= clock
->Now();
91 QuicTime::Delta delta
= QuicTime::Delta::FromMicroseconds(1);
92 alarm
->Set(clock
->Now().Add(delta
));
94 QuicTime::Delta new_delta
= QuicTime::Delta::FromMicroseconds(3);
95 alarm
->Set(clock
->Now().Add(new_delta
));
97 epoll_server_
.AdvanceByExactlyAndCallCallbacks(delta
.ToMicroseconds());
98 EXPECT_EQ(start
.Add(delta
), clock
->Now());
99 EXPECT_FALSE(delegate
->fired());
101 epoll_server_
.AdvanceByExactlyAndCallCallbacks(
102 new_delta
.Subtract(delta
).ToMicroseconds());
103 EXPECT_EQ(start
.Add(new_delta
), clock
->Now());
104 EXPECT_TRUE(delegate
->fired());
107 TEST_F(QuicEpollConnectionHelperTest
, CreateAlarmAndUpdate
) {
108 TestDelegate
* delegate
= new TestDelegate();
109 scoped_ptr
<QuicAlarm
> alarm(helper_
.CreateAlarm(delegate
));
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 QuicTime::Delta new_delta
= QuicTime::Delta::FromMicroseconds(3);
116 alarm
->Update(clock
->Now().Add(new_delta
),
117 QuicTime::Delta::FromMicroseconds(1));
119 epoll_server_
.AdvanceByExactlyAndCallCallbacks(delta
.ToMicroseconds());
120 EXPECT_EQ(start
.Add(delta
), clock
->Now());
121 EXPECT_FALSE(delegate
->fired());
123 // Move the alarm forward 1us and ensure it doesn't move forward.
124 alarm
->Update(clock
->Now().Add(new_delta
),
125 QuicTime::Delta::FromMicroseconds(2));
127 epoll_server_
.AdvanceByExactlyAndCallCallbacks(
128 new_delta
.Subtract(delta
).ToMicroseconds());
129 EXPECT_EQ(start
.Add(new_delta
), clock
->Now());
130 EXPECT_TRUE(delegate
->fired());
132 // Set the alarm via an update call.
133 new_delta
= QuicTime::Delta::FromMicroseconds(5);
134 alarm
->Update(clock
->Now().Add(new_delta
),
135 QuicTime::Delta::FromMicroseconds(1));
136 EXPECT_TRUE(alarm
->IsSet());
138 // Update it with an uninitialized time and ensure it's cancelled.
139 alarm
->Update(QuicTime::Zero(), QuicTime::Delta::FromMicroseconds(1));
140 EXPECT_FALSE(alarm
->IsSet());