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/quic/quic_connection_helper.h"
7 #include "net/quic/test_tools/mock_clock.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"
16 class TestDelegate
: public QuicAlarm::Delegate
{
18 TestDelegate() : fired_(false) {}
20 QuicTime
OnAlarm() override
{
22 return QuicTime::Zero();
25 bool fired() const { return fired_
; }
26 void Clear() { fired_
= false; }
32 class QuicConnectionHelperTest
: public ::testing::Test
{
34 QuicConnectionHelperTest()
35 : runner_(new TestTaskRunner(&clock_
)),
36 helper_(runner_
.get(), &clock_
, &random_generator_
) {
39 scoped_refptr
<TestTaskRunner
> runner_
;
40 QuicConnectionHelper helper_
;
42 MockRandom random_generator_
;
45 TEST_F(QuicConnectionHelperTest
, GetClock
) {
46 EXPECT_EQ(&clock_
, helper_
.GetClock());
49 TEST_F(QuicConnectionHelperTest
, GetRandomGenerator
) {
50 EXPECT_EQ(&random_generator_
, helper_
.GetRandomGenerator());
53 TEST_F(QuicConnectionHelperTest
, CreateAlarm
) {
54 TestDelegate
* delegate
= new TestDelegate();
55 scoped_ptr
<QuicAlarm
> alarm(helper_
.CreateAlarm(delegate
));
57 QuicTime::Delta delta
= QuicTime::Delta::FromMicroseconds(1);
58 alarm
->Set(clock_
.Now().Add(delta
));
60 // Verify that the alarm task has been posted.
61 ASSERT_EQ(1u, runner_
->GetPostedTasks().size());
62 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta
.ToMicroseconds()),
63 runner_
->GetPostedTasks()[0].delay
);
65 runner_
->RunNextTask();
66 EXPECT_EQ(QuicTime::Zero().Add(delta
), clock_
.Now());
67 EXPECT_TRUE(delegate
->fired());
70 TEST_F(QuicConnectionHelperTest
, CreateAlarmAndCancel
) {
71 TestDelegate
* delegate
= new TestDelegate();
72 scoped_ptr
<QuicAlarm
> alarm(helper_
.CreateAlarm(delegate
));
74 QuicTime::Delta delta
= QuicTime::Delta::FromMicroseconds(1);
75 alarm
->Set(clock_
.Now().Add(delta
));
78 // The alarm task should still be posted.
79 ASSERT_EQ(1u, runner_
->GetPostedTasks().size());
80 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta
.ToMicroseconds()),
81 runner_
->GetPostedTasks()[0].delay
);
83 runner_
->RunNextTask();
84 EXPECT_EQ(QuicTime::Zero().Add(delta
), clock_
.Now());
85 EXPECT_FALSE(delegate
->fired());
88 TEST_F(QuicConnectionHelperTest
, CreateAlarmAndReset
) {
89 TestDelegate
* delegate
= new TestDelegate();
90 scoped_ptr
<QuicAlarm
> alarm(helper_
.CreateAlarm(delegate
));
92 QuicTime::Delta delta
= QuicTime::Delta::FromMicroseconds(1);
93 alarm
->Set(clock_
.Now().Add(delta
));
95 QuicTime::Delta new_delta
= QuicTime::Delta::FromMicroseconds(3);
96 alarm
->Set(clock_
.Now().Add(new_delta
));
98 // The alarm task should still be posted.
99 ASSERT_EQ(1u, runner_
->GetPostedTasks().size());
100 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta
.ToMicroseconds()),
101 runner_
->GetPostedTasks()[0].delay
);
103 runner_
->RunNextTask();
104 EXPECT_EQ(QuicTime::Zero().Add(delta
), clock_
.Now());
105 EXPECT_FALSE(delegate
->fired());
107 // The alarm task should be posted again.
108 ASSERT_EQ(1u, runner_
->GetPostedTasks().size());
110 runner_
->RunNextTask();
111 EXPECT_EQ(QuicTime::Zero().Add(new_delta
), clock_
.Now());
112 EXPECT_TRUE(delegate
->fired());
115 TEST_F(QuicConnectionHelperTest
, CreateAlarmAndResetEarlier
) {
116 TestDelegate
* delegate
= new TestDelegate();
117 scoped_ptr
<QuicAlarm
> alarm(helper_
.CreateAlarm(delegate
));
119 QuicTime::Delta delta
= QuicTime::Delta::FromMicroseconds(3);
120 alarm
->Set(clock_
.Now().Add(delta
));
122 QuicTime::Delta new_delta
= QuicTime::Delta::FromMicroseconds(1);
123 alarm
->Set(clock_
.Now().Add(new_delta
));
125 // Both alarm tasks will be posted.
126 ASSERT_EQ(2u, runner_
->GetPostedTasks().size());
128 // The earlier task will execute and will fire the alarm->
129 runner_
->RunNextTask();
130 EXPECT_EQ(QuicTime::Zero().Add(new_delta
), clock_
.Now());
131 EXPECT_TRUE(delegate
->fired());
134 // The latter task is still posted.
135 ASSERT_EQ(1u, runner_
->GetPostedTasks().size());
137 // When the latter task is executed, the weak ptr will be invalid and
138 // the alarm will not fire.
139 runner_
->RunNextTask();
140 EXPECT_EQ(QuicTime::Zero().Add(delta
), clock_
.Now());
141 EXPECT_FALSE(delegate
->fired());
144 TEST_F(QuicConnectionHelperTest
, CreateAlarmAndUpdate
) {
145 TestDelegate
* delegate
= new TestDelegate();
146 scoped_ptr
<QuicAlarm
> alarm(helper_
.CreateAlarm(delegate
));
148 const QuicClock
* clock
= helper_
.GetClock();
149 QuicTime start
= clock
->Now();
150 QuicTime::Delta delta
= QuicTime::Delta::FromMicroseconds(1);
151 alarm
->Set(clock
->Now().Add(delta
));
152 QuicTime::Delta new_delta
= QuicTime::Delta::FromMicroseconds(3);
153 alarm
->Update(clock
->Now().Add(new_delta
),
154 QuicTime::Delta::FromMicroseconds(1));
156 // The alarm task should still be posted.
157 ASSERT_EQ(1u, runner_
->GetPostedTasks().size());
158 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta
.ToMicroseconds()),
159 runner_
->GetPostedTasks()[0].delay
);
161 runner_
->RunNextTask();
162 EXPECT_EQ(QuicTime::Zero().Add(delta
), clock
->Now());
163 EXPECT_FALSE(delegate
->fired());
165 // Move the alarm forward 1us and ensure it doesn't move forward.
166 alarm
->Update(clock
->Now().Add(new_delta
),
167 QuicTime::Delta::FromMicroseconds(2));
169 ASSERT_EQ(1u, runner_
->GetPostedTasks().size());
171 base::TimeDelta::FromMicroseconds(
172 new_delta
.Subtract(delta
).ToMicroseconds()),
173 runner_
->GetPostedTasks()[0].delay
);
174 runner_
->RunNextTask();
175 EXPECT_EQ(start
.Add(new_delta
), clock
->Now());
176 EXPECT_TRUE(delegate
->fired());
178 // Set the alarm via an update call.
179 new_delta
= QuicTime::Delta::FromMicroseconds(5);
180 alarm
->Update(clock
->Now().Add(new_delta
),
181 QuicTime::Delta::FromMicroseconds(1));
182 EXPECT_TRUE(alarm
->IsSet());
184 // Update it with an uninitialized time and ensure it's cancelled.
185 alarm
->Update(QuicTime::Zero(), QuicTime::Delta::FromMicroseconds(1));
186 EXPECT_FALSE(alarm
->IsSet());