Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: net/quic/quic_chromium_alarm_factory_test.cc

Issue 1908103002: Landing Recent QUIC changes until 4/15/2016 17:20 UTC (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/quic/quic_chromium_alarm_factory.cc ('k') | net/quic/quic_chromium_client_session_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
5 #include "net/quic/quic_chromium_alarm_factory.h"
6
7 #include "net/quic/test_tools/mock_clock.h"
8 #include "net/quic/test_tools/test_task_runner.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace net {
12 namespace test {
13 namespace {
14
15 class TestDelegate : public QuicAlarm::Delegate {
16 public:
17 TestDelegate() : fired_(false) {}
18
19 void OnAlarm() override { fired_ = true; }
20
21 bool fired() const { return fired_; }
22 void Clear() { fired_ = false; }
23
24 private:
25 bool fired_;
26 };
27
28 class QuicChromiumAlarmFactoryTest : public ::testing::Test {
29 protected:
30 QuicChromiumAlarmFactoryTest()
31 : runner_(new TestTaskRunner(&clock_)),
32 alarm_factory_(runner_.get(), &clock_) {}
33
34 scoped_refptr<TestTaskRunner> runner_;
35 QuicChromiumAlarmFactory alarm_factory_;
36 MockClock clock_;
37 };
38
39 TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarm) {
40 TestDelegate* delegate = new TestDelegate();
41 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
42
43 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
44 alarm->Set(clock_.Now().Add(delta));
45
46 // Verify that the alarm task has been posted.
47 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
48 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()),
49 runner_->GetPostedTasks()[0].delay);
50
51 runner_->RunNextTask();
52 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now());
53 EXPECT_TRUE(delegate->fired());
54 }
55
56 TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndCancel) {
57 TestDelegate* delegate = new TestDelegate();
58 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
59
60 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
61 alarm->Set(clock_.Now().Add(delta));
62 alarm->Cancel();
63
64 // The alarm task should still be posted.
65 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
66 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()),
67 runner_->GetPostedTasks()[0].delay);
68
69 runner_->RunNextTask();
70 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now());
71 EXPECT_FALSE(delegate->fired());
72 }
73
74 TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndReset) {
75 TestDelegate* delegate = new TestDelegate();
76 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
77
78 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
79 alarm->Set(clock_.Now().Add(delta));
80 alarm->Cancel();
81 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3);
82 alarm->Set(clock_.Now().Add(new_delta));
83
84 // The alarm task should still be posted.
85 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
86 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()),
87 runner_->GetPostedTasks()[0].delay);
88
89 runner_->RunNextTask();
90 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now());
91 EXPECT_FALSE(delegate->fired());
92
93 // The alarm task should be posted again.
94 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
95
96 runner_->RunNextTask();
97 EXPECT_EQ(QuicTime::Zero().Add(new_delta), clock_.Now());
98 EXPECT_TRUE(delegate->fired());
99 }
100
101 TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndResetEarlier) {
102 TestDelegate* delegate = new TestDelegate();
103 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
104
105 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(3);
106 alarm->Set(clock_.Now().Add(delta));
107 alarm->Cancel();
108 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(1);
109 alarm->Set(clock_.Now().Add(new_delta));
110
111 // Both alarm tasks will be posted.
112 ASSERT_EQ(2u, runner_->GetPostedTasks().size());
113
114 // The earlier task will execute and will fire the alarm->
115 runner_->RunNextTask();
116 EXPECT_EQ(QuicTime::Zero().Add(new_delta), clock_.Now());
117 EXPECT_TRUE(delegate->fired());
118 delegate->Clear();
119
120 // The latter task is still posted.
121 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
122
123 // When the latter task is executed, the weak ptr will be invalid and
124 // the alarm will not fire.
125 runner_->RunNextTask();
126 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now());
127 EXPECT_FALSE(delegate->fired());
128 }
129
130 TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndUpdate) {
131 TestDelegate* delegate = new TestDelegate();
132 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
133
134 QuicTime start = clock_.Now();
135 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
136 alarm->Set(clock_.Now().Add(delta));
137 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3);
138 alarm->Update(clock_.Now().Add(new_delta),
139 QuicTime::Delta::FromMicroseconds(1));
140
141 // The alarm task should still be posted.
142 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
143 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()),
144 runner_->GetPostedTasks()[0].delay);
145
146 runner_->RunNextTask();
147 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now());
148 EXPECT_FALSE(delegate->fired());
149
150 // Move the alarm forward 1us and ensure it doesn't move forward.
151 alarm->Update(clock_.Now().Add(new_delta),
152 QuicTime::Delta::FromMicroseconds(2));
153
154 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
155 EXPECT_EQ(base::TimeDelta::FromMicroseconds(
156 new_delta.Subtract(delta).ToMicroseconds()),
157 runner_->GetPostedTasks()[0].delay);
158 runner_->RunNextTask();
159 EXPECT_EQ(start.Add(new_delta), clock_.Now());
160 EXPECT_TRUE(delegate->fired());
161
162 // Set the alarm via an update call.
163 new_delta = QuicTime::Delta::FromMicroseconds(5);
164 alarm->Update(clock_.Now().Add(new_delta),
165 QuicTime::Delta::FromMicroseconds(1));
166 EXPECT_TRUE(alarm->IsSet());
167
168 // Update it with an uninitialized time and ensure it's cancelled.
169 alarm->Update(QuicTime::Zero(), QuicTime::Delta::FromMicroseconds(1));
170 EXPECT_FALSE(alarm->IsSet());
171 }
172
173 } // namespace
174 } // namespace test
175 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/quic_chromium_alarm_factory.cc ('k') | net/quic/quic_chromium_client_session_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698