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

Side by Side Diff: components/scheduler/renderer/throttling_helper_unittest.cc

Issue 1441073006: Move throttling of background timers into the renderer scheduler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Last few changes Sami requested Created 5 years 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
OLDNEW
(Empty)
1 // Copyright 2015 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 "components/scheduler/renderer/throttling_helper.h"
6
7 #include "base/callback.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/test/simple_test_tick_clock.h"
10 #include "cc/test/ordered_simple_task_runner.h"
11 #include "components/scheduler/base/test_time_source.h"
12 #include "components/scheduler/child/scheduler_tqm_delegate_for_test.h"
13 #include "components/scheduler/renderer/renderer_scheduler_impl.h"
14 #include "components/scheduler/renderer/web_frame_scheduler_impl.h"
15 #include "components/scheduler/renderer/web_view_scheduler_impl.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 using testing::ElementsAre;
20
21 namespace scheduler {
22
23 class ThrottlingHelperTest : public testing::Test {
24 public:
25 ThrottlingHelperTest() {}
26 ~ThrottlingHelperTest() override {}
27
28 void SetUp() override {
29 clock_.reset(new base::SimpleTestTickClock());
30 clock_->Advance(base::TimeDelta::FromMicroseconds(5000));
31 mock_task_runner_ =
32 make_scoped_refptr(new cc::OrderedSimpleTaskRunner(clock_.get(), true));
33 delegate_ = SchedulerTqmDelegateForTest::Create(
34 mock_task_runner_, make_scoped_ptr(new TestTimeSource(clock_.get())));
35 scheduler_.reset(new RendererSchedulerImpl(delegate_));
36 throttling_helper_ = scheduler_->throttling_helper();
37 timer_queue_ = scheduler_->NewTimerTaskRunner("test_queue");
38 }
39
40 void TearDown() override {
41 scheduler_->Shutdown();
42 scheduler_.reset();
43 }
44
45 protected:
46 scoped_ptr<base::SimpleTestTickClock> clock_;
47 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_;
48 scoped_refptr<SchedulerTqmDelegate> delegate_;
49 scoped_ptr<RendererSchedulerImpl> scheduler_;
50 scoped_refptr<TaskQueue> timer_queue_;
51 ThrottlingHelper* throttling_helper_; // NOT OWNED
52
53 DISALLOW_COPY_AND_ASSIGN(ThrottlingHelperTest);
54 };
55
56 TEST_F(ThrottlingHelperTest, DelayToNextRunTimeInSeconds) {
57 EXPECT_EQ(base::TimeDelta::FromSecondsD(1.0),
58 ThrottlingHelper::DelayToNextRunTimeInSeconds(
59 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.0)));
60
61 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.9),
62 ThrottlingHelper::DelayToNextRunTimeInSeconds(
63 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.1)));
64
65 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.8),
66 ThrottlingHelper::DelayToNextRunTimeInSeconds(
67 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.2)));
68
69 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.5),
70 ThrottlingHelper::DelayToNextRunTimeInSeconds(
71 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.5)));
72
73 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.2),
74 ThrottlingHelper::DelayToNextRunTimeInSeconds(
75 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.8)));
76
77 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.1),
78 ThrottlingHelper::DelayToNextRunTimeInSeconds(
79 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.9)));
80
81 EXPECT_EQ(base::TimeDelta::FromSecondsD(1.0),
82 ThrottlingHelper::DelayToNextRunTimeInSeconds(
83 base::TimeTicks() + base::TimeDelta::FromSecondsD(1.0)));
84
85 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.9),
86 ThrottlingHelper::DelayToNextRunTimeInSeconds(
87 base::TimeTicks() + base::TimeDelta::FromSecondsD(1.1)));
88
89 EXPECT_EQ(base::TimeDelta::FromSecondsD(1.0),
90 ThrottlingHelper::DelayToNextRunTimeInSeconds(
91 base::TimeTicks() + base::TimeDelta::FromSecondsD(8.0)));
92
93 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.9),
94 ThrottlingHelper::DelayToNextRunTimeInSeconds(
95 base::TimeTicks() + base::TimeDelta::FromSecondsD(8.1)));
96 }
97
98 namespace {
99 void TestTask(std::vector<base::TimeTicks>* run_times,
100 base::SimpleTestTickClock* clock) {
101 run_times->push_back(clock->NowTicks());
102 }
103 } // namespace
104
105 TEST_F(ThrottlingHelperTest, TimerAlignment) {
106 std::vector<base::TimeTicks> run_times;
107 timer_queue_->PostDelayedTask(FROM_HERE,
108 base::Bind(&TestTask, &run_times, clock_.get()),
109 base::TimeDelta::FromMilliseconds(200.0));
110
111 timer_queue_->PostDelayedTask(FROM_HERE,
112 base::Bind(&TestTask, &run_times, clock_.get()),
113 base::TimeDelta::FromMilliseconds(800.0));
114
115 timer_queue_->PostDelayedTask(FROM_HERE,
116 base::Bind(&TestTask, &run_times, clock_.get()),
117 base::TimeDelta::FromMilliseconds(1200.0));
118
119 timer_queue_->PostDelayedTask(FROM_HERE,
120 base::Bind(&TestTask, &run_times, clock_.get()),
121 base::TimeDelta::FromMilliseconds(8300.0));
122
123 throttling_helper_->Throttle(timer_queue_.get());
124
125 mock_task_runner_->RunUntilIdle();
126
127 // Times are aligned to a multipple of 1000 milliseconds.
128 EXPECT_THAT(
129 run_times,
130 ElementsAre(
131 base::TimeTicks() + base::TimeDelta::FromMilliseconds(1000.0),
132 base::TimeTicks() + base::TimeDelta::FromMilliseconds(1000.0),
133 base::TimeTicks() + base::TimeDelta::FromMilliseconds(2000.0),
134 base::TimeTicks() + base::TimeDelta::FromMilliseconds(9000.0)));
135 }
136
137 TEST_F(ThrottlingHelperTest, TimerAlignment_Unthrottled) {
138 std::vector<base::TimeTicks> run_times;
139 base::TimeTicks start_time = clock_->NowTicks();
140 timer_queue_->PostDelayedTask(FROM_HERE,
141 base::Bind(&TestTask, &run_times, clock_.get()),
142 base::TimeDelta::FromMilliseconds(200.0));
143
144 timer_queue_->PostDelayedTask(FROM_HERE,
145 base::Bind(&TestTask, &run_times, clock_.get()),
146 base::TimeDelta::FromMilliseconds(800.0));
147
148 timer_queue_->PostDelayedTask(FROM_HERE,
149 base::Bind(&TestTask, &run_times, clock_.get()),
150 base::TimeDelta::FromMilliseconds(1200.0));
151
152 timer_queue_->PostDelayedTask(FROM_HERE,
153 base::Bind(&TestTask, &run_times, clock_.get()),
154 base::TimeDelta::FromMilliseconds(8300.0));
155
156 throttling_helper_->Throttle(timer_queue_.get());
157 throttling_helper_->Unthrottle(timer_queue_.get());
158
159 mock_task_runner_->RunUntilIdle();
160
161 // Times are not aligned.
162 EXPECT_THAT(
163 run_times,
164 ElementsAre(start_time + base::TimeDelta::FromMilliseconds(200.0),
165 start_time + base::TimeDelta::FromMilliseconds(800.0),
166 start_time + base::TimeDelta::FromMilliseconds(1200.0),
167 start_time + base::TimeDelta::FromMilliseconds(8300.0)));
168 }
169
170 TEST_F(ThrottlingHelperTest, WakeUpForNonDelayedTask) {
171 std::vector<base::TimeTicks> run_times;
172
173 // Nothing is posted on timer_queue_ so PumpThrottledTasks will not tick.
174 throttling_helper_->Throttle(timer_queue_.get());
175
176 // Posting a task should trigger the pump.
177 timer_queue_->PostTask(FROM_HERE,
178 base::Bind(&TestTask, &run_times, clock_.get()));
179
180 mock_task_runner_->RunUntilIdle();
181 EXPECT_THAT(run_times,
182 ElementsAre(base::TimeTicks() +
183 base::TimeDelta::FromMilliseconds(1000.0)));
184 }
185
186 TEST_F(ThrottlingHelperTest, WakeUpForDelayedTask) {
187 std::vector<base::TimeTicks> run_times;
188
189 // Nothing is posted on timer_queue_ so PumpThrottledTasks will not tick.
190 throttling_helper_->Throttle(timer_queue_.get());
191
192 // Posting a task should trigger the pump.
193 timer_queue_->PostDelayedTask(FROM_HERE,
194 base::Bind(&TestTask, &run_times, clock_.get()),
195 base::TimeDelta::FromMilliseconds(1200.0));
196
197 mock_task_runner_->RunUntilIdle();
198 EXPECT_THAT(run_times,
199 ElementsAre(base::TimeTicks() +
200 base::TimeDelta::FromMilliseconds(2000.0)));
201 }
202
203 } // namespace scheduler
OLDNEW
« no previous file with comments | « components/scheduler/renderer/throttling_helper.cc ('k') | components/scheduler/renderer/web_frame_scheduler_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698