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

Side by Side Diff: Source/platform/TimerTest.cpp

Issue 1303153005: Introduce WebTaskRunner Patch 3/5 (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Add missing #include Created 5 years, 3 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 | « Source/platform/Timer.cpp ('k') | Source/platform/WebScheduler.cpp » ('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 2015 The Chromium Authors. All rights reserved. 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 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 "config.h" 5 #include "config.h"
6 #include "platform/Timer.h" 6 #include "platform/Timer.h"
7 7
8 #include "public/platform/Platform.h" 8 #include "public/platform/Platform.h"
9 #include "public/platform/WebScheduler.h" 9 #include "public/platform/WebScheduler.h"
10 #include "public/platform/WebThread.h" 10 #include "public/platform/WebThread.h"
11 #include <gmock/gmock.h> 11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h> 12 #include <gtest/gtest.h>
13 #include <queue> 13 #include <queue>
14 14
15 using testing::ElementsAre; 15 using testing::ElementsAre;
16 16
17 namespace blink { 17 namespace blink {
18 namespace { 18 namespace {
19 double gCurrentTimeSecs = 0.0; 19 double gCurrentTimeSecs = 0.0;
20 20
21 double currentTime() 21 double currentTime()
22 { 22 {
23 return gCurrentTimeSecs; 23 return gCurrentTimeSecs;
24 } 24 }
25 25
26 // This class exists because gcc doesn't know how to move an OwnPtr. 26 // This class exists because gcc doesn't know how to move an OwnPtr.
27 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> { 27 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> {
28 public: 28 public:
29 explicit RefCountedTaskContainer(WebThread::Task* task) : m_task(adoptPtr(ta sk)) { } 29 explicit RefCountedTaskContainer(WebTaskRunner::Task* task) : m_task(adoptPt r(task)) { }
30 30
31 ~RefCountedTaskContainer() { } 31 ~RefCountedTaskContainer() { }
32 32
33 void run() 33 void run()
34 { 34 {
35 m_task->run(); 35 m_task->run();
36 } 36 }
37 37
38 private: 38 private:
39 OwnPtr<WebThread::Task> m_task; 39 OwnPtr<WebTaskRunner::Task> m_task;
40 }; 40 };
41 41
42 class DelayedTask { 42 class DelayedTask {
43 public: 43 public:
44 DelayedTask(WebThread::Task* task, long long delayMs) 44 DelayedTask(WebTaskRunner::Task* task, long long delayMs)
45 : m_task(adoptRef(new RefCountedTaskContainer(task))) 45 : m_task(adoptRef(new RefCountedTaskContainer(task)))
46 , m_runTimeSecs(monotonicallyIncreasingTime() + 0.001 * static_cast<doub le>(delayMs)) 46 , m_runTimeSecs(monotonicallyIncreasingTime() + 0.001 * static_cast<doub le>(delayMs))
47 , m_delayMs(delayMs) { } 47 , m_delayMs(delayMs) { }
48 48
49 bool operator<(const DelayedTask& other) const 49 bool operator<(const DelayedTask& other) const
50 { 50 {
51 return m_runTimeSecs > other.m_runTimeSecs; 51 return m_runTimeSecs > other.m_runTimeSecs;
52 } 52 }
53 53
54 void run() const 54 void run() const
(...skipping 10 matching lines...) Expand all
65 { 65 {
66 return m_delayMs; 66 return m_delayMs;
67 } 67 }
68 68
69 private: 69 private:
70 RefPtr<RefCountedTaskContainer> m_task; 70 RefPtr<RefCountedTaskContainer> m_task;
71 double m_runTimeSecs; 71 double m_runTimeSecs;
72 long long m_delayMs; 72 long long m_delayMs;
73 }; 73 };
74 74
75 class MockWebTaskRunner : public WebTaskRunner {
76 public:
77 explicit MockWebTaskRunner(std::priority_queue<DelayedTask>* timerTasks) : m _timerTasks(timerTasks) { }
78 ~MockWebTaskRunner() override { }
79
80 virtual void postTask(const WebTraceLocation&, Task* task)
81 {
82 m_timerTasks->push(DelayedTask(task, 0));
83 }
84
85 void postDelayedTask(const WebTraceLocation&, Task* task, long long delayMs) override
86 {
87 m_timerTasks->push(DelayedTask(task, delayMs));
88 }
89
90 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED
91 };
92
75 class MockWebScheduler : public WebScheduler { 93 class MockWebScheduler : public WebScheduler {
76 public: 94 public:
77 MockWebScheduler() { } 95 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { }
78 ~MockWebScheduler() override { } 96 ~MockWebScheduler() override { }
79 97
80 bool shouldYieldForHighPriorityWork() override 98 bool shouldYieldForHighPriorityWork() override
81 { 99 {
82 return false; 100 return false;
83 } 101 }
84 102
85 bool canExceedIdleDeadlineIfRequired() override 103 bool canExceedIdleDeadlineIfRequired() override
86 { 104 {
87 return false; 105 return false;
88 } 106 }
89 107
90 void postIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override 108 void postIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override
91 { 109 {
92 } 110 }
93 111
94 void postNonNestableIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override 112 void postNonNestableIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override
95 { 113 {
96 } 114 }
97 115
98 void postIdleTaskAfterWakeup(const WebTraceLocation&, WebThread::IdleTask*) override 116 void postIdleTaskAfterWakeup(const WebTraceLocation&, WebThread::IdleTask*) override
99 { 117 {
100 } 118 }
101 119
102 void postLoadingTask(const WebTraceLocation&, WebThread::Task*) override 120 WebTaskRunner* timerTaskRunner() override
103 { 121 {
122 return &m_timerWebTaskRunner;
104 } 123 }
105 124
106 void postTimerTask(const WebTraceLocation&, WebThread::Task* task, long long delayMs) override 125 WebTaskRunner* loadingTaskRunner() override
107 { 126 {
108 m_timerTasks.push(DelayedTask(task, delayMs)); 127 ASSERT_NOT_REACHED();
128 return nullptr;
109 } 129 }
110 130
111 void postTimerTaskAt(const WebTraceLocation&, WebThread::Task* task, double monotonicTime) override 131 void postTimerTaskAt(const WebTraceLocation&, WebTaskRunner::Task* task, dou ble monotonicTime) override
112 { 132 {
113 m_timerTasks.push(DelayedTask(task, (monotonicTime - monotonicallyIncrea singTime()) * 1000)); 133 m_timerTasks.push(DelayedTask(task, (monotonicTime - monotonicallyIncrea singTime()) * 1000));
114 } 134 }
115 135
116 void runUntilIdle() 136 void runUntilIdle()
117 { 137 {
118 while (!m_timerTasks.empty()) { 138 while (!m_timerTasks.empty()) {
119 gCurrentTimeSecs = m_timerTasks.top().runTimeSecs(); 139 gCurrentTimeSecs = m_timerTasks.top().runTimeSecs();
120 m_timerTasks.top().run(); 140 m_timerTasks.top().run();
121 m_timerTasks.pop(); 141 m_timerTasks.pop();
(...skipping 19 matching lines...) Expand all
141 } 161 }
142 162
143 long nextTimerTaskDelayMillis() const 163 long nextTimerTaskDelayMillis() const
144 { 164 {
145 ASSERT(hasOneTimerTask()); 165 ASSERT(hasOneTimerTask());
146 return m_timerTasks.top().delayMs(); 166 return m_timerTasks.top().delayMs();
147 } 167 }
148 168
149 private: 169 private:
150 std::priority_queue<DelayedTask> m_timerTasks; 170 std::priority_queue<DelayedTask> m_timerTasks;
171 MockWebTaskRunner m_timerWebTaskRunner;
151 }; 172 };
152 173
153 class FakeWebThread : public WebThread { 174 class FakeWebThread : public WebThread {
154 public: 175 public:
155 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { } 176 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { }
156 ~FakeWebThread() override { } 177 ~FakeWebThread() override { }
157 178
158 // WebThread implementation:
159 void postTask(const WebTraceLocation&, Task*)
160 {
161 ASSERT_NOT_REACHED();
162 }
163
164 virtual void postDelayedTask(const WebTraceLocation&, Task*, long long)
165 {
166 ASSERT_NOT_REACHED();
167 }
168
169 virtual bool isCurrentThread() const 179 virtual bool isCurrentThread() const
170 { 180 {
171 ASSERT_NOT_REACHED(); 181 ASSERT_NOT_REACHED();
172 return true; 182 return true;
173 } 183 }
174 184
175 virtual PlatformThreadId threadId() const 185 virtual PlatformThreadId threadId() const
176 { 186 {
177 ASSERT_NOT_REACHED(); 187 ASSERT_NOT_REACHED();
178 return 0; 188 return 0;
179 } 189 }
180 190
191 WebTaskRunner* taskRunner() override
192 {
193 ASSERT_NOT_REACHED();
194 return nullptr;
195 }
196
181 WebScheduler* scheduler() const override 197 WebScheduler* scheduler() const override
182 { 198 {
183 return m_webScheduler.get(); 199 return m_webScheduler.get();
184 } 200 }
185 201
186 virtual void enterRunLoop() 202 virtual void enterRunLoop()
187 { 203 {
188 ASSERT_NOT_REACHED(); 204 ASSERT_NOT_REACHED();
189 } 205 }
190 206
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime()); 748 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime());
733 749
734 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); 750 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
735 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); 751 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval());
736 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); 752 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
737 } 753 }
738 754
739 755
740 } // namespace 756 } // namespace
741 } // namespace blink 757 } // namespace blink
OLDNEW
« no previous file with comments | « Source/platform/Timer.cpp ('k') | Source/platform/WebScheduler.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698