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

Side by Side Diff: net/quic/quartc/quartc_alarm_factory_test.cc

Issue 2324833004: Define Stable API for WebRTC/Quartc (Closed)
Patch Set: Fix the IOS simulator and merge. Created 4 years, 1 month 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/quartc/quartc_alarm_factory.cc ('k') | net/quic/quartc/quartc_factory.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2016 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/quic/chromium/quic_chromium_alarm_factory.h" 5 #include "net/quic/quartc/quartc_alarm_factory.h"
6 6
7 #include "net/quic/test_tools/mock_clock.h" 7 #include "net/quic/test_tools/mock_clock.h"
8 #include "net/quic/test_tools/test_task_runner.h" 8 #include "net/quic/test_tools/test_task_runner.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace net { 11 namespace net {
12 namespace test { 12 namespace test {
13 namespace { 13 namespace {
14 14
15 class TestDelegate : public QuicAlarm::Delegate { 15 class TestDelegate : public QuicAlarm::Delegate {
16 public: 16 public:
17 TestDelegate() : fired_(false) {} 17 TestDelegate() : fired_(false) {}
18 18
19 void OnAlarm() override { fired_ = true; } 19 void OnAlarm() override { fired_ = true; }
20 20
21 bool fired() const { return fired_; } 21 bool fired() const { return fired_; }
22 void Clear() { fired_ = false; } 22 void Clear() { fired_ = false; }
23 23
24 private: 24 private:
25 bool fired_; 25 bool fired_;
26 }; 26 };
27 27
28 class QuicChromiumAlarmFactoryTest : public ::testing::Test { 28 class QuartcAlarmFactoryTest : public ::testing::Test {
29 protected: 29 protected:
30 QuicChromiumAlarmFactoryTest() 30 QuartcAlarmFactoryTest()
31 : runner_(new TestTaskRunner(&clock_)), 31 : runner_(new TestTaskRunner(&clock_)),
32 alarm_factory_(runner_.get(), &clock_) {} 32 alarm_factory_(runner_.get(), &clock_) {}
33 33
34 scoped_refptr<TestTaskRunner> runner_; 34 scoped_refptr<TestTaskRunner> runner_;
35 QuicChromiumAlarmFactory alarm_factory_; 35 QuartcAlarmFactory alarm_factory_;
36 MockClock clock_; 36 MockClock clock_;
37 }; 37 };
38 38
39 TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarm) { 39 TEST_F(QuartcAlarmFactoryTest, CreateAlarm) {
40 TestDelegate* delegate = new TestDelegate(); 40 TestDelegate* delegate = new TestDelegate();
41 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); 41 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
42 42
43 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); 43 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
44 alarm->Set(clock_.Now() + delta); 44 alarm->Set(clock_.Now() + delta);
45 45
46 // Verify that the alarm task has been posted. 46 // Verify that the alarm task has been posted.
47 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); 47 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
48 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), 48 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()),
49 runner_->GetPostedTasks()[0].delay); 49 runner_->GetPostedTasks()[0].delay);
50 50
51 runner_->RunNextTask(); 51 runner_->RunNextTask();
52 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); 52 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now());
53 EXPECT_TRUE(delegate->fired()); 53 EXPECT_TRUE(delegate->fired());
54 } 54 }
55 55
56 TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndCancel) { 56 TEST_F(QuartcAlarmFactoryTest, CreateAlarmAndCancel) {
57 TestDelegate* delegate = new TestDelegate(); 57 TestDelegate* delegate = new TestDelegate();
58 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); 58 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
59 59
60 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); 60 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
61 alarm->Set(clock_.Now() + delta); 61 alarm->Set(clock_.Now() + delta);
62 alarm->Cancel(); 62 alarm->Cancel();
63 63
64 // The alarm task should still be posted. 64 // The alarm task should still be posted.
65 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); 65 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
66 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), 66 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()),
67 runner_->GetPostedTasks()[0].delay); 67 runner_->GetPostedTasks()[0].delay);
68 68
69 runner_->RunNextTask(); 69 runner_->RunNextTask();
70 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); 70 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now());
71 EXPECT_FALSE(delegate->fired()); 71 EXPECT_FALSE(delegate->fired());
72 } 72 }
73 73
74 TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndReset) { 74 TEST_F(QuartcAlarmFactoryTest, CreateAlarmAndReset) {
75 TestDelegate* delegate = new TestDelegate(); 75 TestDelegate* delegate = new TestDelegate();
76 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); 76 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
77 77
78 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); 78 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
79 alarm->Set(clock_.Now() + delta); 79 alarm->Set(clock_.Now() + delta);
80 alarm->Cancel(); 80 alarm->Cancel();
81 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); 81 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3);
82 alarm->Set(clock_.Now() + new_delta); 82 alarm->Set(clock_.Now() + new_delta);
83 83
84 // The alarm task should still be posted. 84 // The alarm task should still be posted.
85 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); 85 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
86 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()), 86 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()),
87 runner_->GetPostedTasks()[0].delay); 87 runner_->GetPostedTasks()[0].delay);
88 88
89 runner_->RunNextTask(); 89 runner_->RunNextTask();
90 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); 90 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now());
91 EXPECT_FALSE(delegate->fired()); 91 EXPECT_FALSE(delegate->fired());
92 92
93 // The alarm task should be posted again. 93 // The alarm task should be posted again.
94 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); 94 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
95 95
96 runner_->RunNextTask(); 96 runner_->RunNextTask();
97 EXPECT_EQ(QuicTime::Zero() + new_delta, clock_.Now()); 97 EXPECT_EQ(QuicTime::Zero() + new_delta, clock_.Now());
98 EXPECT_TRUE(delegate->fired()); 98 EXPECT_TRUE(delegate->fired());
99 } 99 }
100 100
101 TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndResetEarlier) { 101 TEST_F(QuartcAlarmFactoryTest, CreateAlarmAndResetEarlier) {
102 TestDelegate* delegate = new TestDelegate(); 102 TestDelegate* delegate = new TestDelegate();
103 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); 103 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
104 104
105 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(3); 105 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(3);
106 alarm->Set(clock_.Now() + delta); 106 alarm->Set(clock_.Now() + delta);
107 alarm->Cancel(); 107 alarm->Cancel();
108 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(1); 108 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(1);
109 alarm->Set(clock_.Now() + new_delta); 109 alarm->Set(clock_.Now() + new_delta);
110 110
111 // Both alarm tasks will be posted. 111 // Both alarm tasks will be posted.
112 ASSERT_EQ(2u, runner_->GetPostedTasks().size()); 112 ASSERT_EQ(2u, runner_->GetPostedTasks().size());
113 113
114 // The earlier task will execute and will fire the alarm-> 114 // The earlier task will execute and will fire the alarm->
115 runner_->RunNextTask(); 115 runner_->RunNextTask();
116 EXPECT_EQ(QuicTime::Zero() + new_delta, clock_.Now()); 116 EXPECT_EQ(QuicTime::Zero() + new_delta, clock_.Now());
117 EXPECT_TRUE(delegate->fired()); 117 EXPECT_TRUE(delegate->fired());
118 delegate->Clear(); 118 delegate->Clear();
119 119
120 // The latter task is still posted. 120 // The latter task is still posted.
121 ASSERT_EQ(1u, runner_->GetPostedTasks().size()); 121 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
122 122
123 // When the latter task is executed, the weak ptr will be invalid and 123 // When the latter task is executed, the weak ptr will be invalid and
124 // the alarm will not fire. 124 // the alarm will not fire.
125 runner_->RunNextTask(); 125 runner_->RunNextTask();
126 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now()); 126 EXPECT_EQ(QuicTime::Zero() + delta, clock_.Now());
127 EXPECT_FALSE(delegate->fired()); 127 EXPECT_FALSE(delegate->fired());
128 } 128 }
129 129
130 TEST_F(QuicChromiumAlarmFactoryTest, CreateAlarmAndUpdate) { 130 TEST_F(QuartcAlarmFactoryTest, CreateAlarmAndUpdate) {
131 TestDelegate* delegate = new TestDelegate(); 131 TestDelegate* delegate = new TestDelegate();
132 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate)); 132 std::unique_ptr<QuicAlarm> alarm(alarm_factory_.CreateAlarm(delegate));
133 133
134 QuicTime start = clock_.Now(); 134 QuicTime start = clock_.Now();
135 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1); 135 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
136 alarm->Set(clock_.Now() + delta); 136 alarm->Set(clock_.Now() + delta);
137 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3); 137 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3);
138 alarm->Update(clock_.Now() + new_delta, QuicTime::Delta::FromMicroseconds(1)); 138 alarm->Update(clock_.Now() + new_delta, QuicTime::Delta::FromMicroseconds(1));
139 139
140 // The alarm task should still be posted. 140 // The alarm task should still be posted.
(...skipping 22 matching lines...) Expand all
163 EXPECT_TRUE(alarm->IsSet()); 163 EXPECT_TRUE(alarm->IsSet());
164 164
165 // Update it with an uninitialized time and ensure it's cancelled. 165 // Update it with an uninitialized time and ensure it's cancelled.
166 alarm->Update(QuicTime::Zero(), QuicTime::Delta::FromMicroseconds(1)); 166 alarm->Update(QuicTime::Zero(), QuicTime::Delta::FromMicroseconds(1));
167 EXPECT_FALSE(alarm->IsSet()); 167 EXPECT_FALSE(alarm->IsSet());
168 } 168 }
169 169
170 } // namespace 170 } // namespace
171 } // namespace test 171 } // namespace test
172 } // namespace net 172 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/quartc/quartc_alarm_factory.cc ('k') | net/quic/quartc/quartc_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698