OLD | NEW |
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 "components/scheduler/child/webthread_impl_for_worker_scheduler.h" | 5 #include "components/scheduler/child/webthread_impl_for_worker_scheduler.h" |
6 | 6 |
7 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
8 #include "components/scheduler/child/web_scheduler_impl.h" | 8 #include "components/scheduler/child/web_scheduler_impl.h" |
9 #include "components/scheduler/child/worker_scheduler_impl.h" | 9 #include "components/scheduler/child/worker_scheduler_impl.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "third_party/WebKit/public/platform/WebTraceLocation.h" | 12 #include "third_party/WebKit/public/platform/WebTraceLocation.h" |
13 | 13 |
14 using testing::_; | 14 using testing::_; |
15 using testing::AnyOf; | 15 using testing::AnyOf; |
16 using testing::ElementsAre; | 16 using testing::ElementsAre; |
17 using testing::Invoke; | 17 using testing::Invoke; |
18 | 18 |
19 namespace scheduler { | 19 namespace scheduler { |
20 namespace { | 20 namespace { |
21 | 21 |
22 class NopTask : public blink::WebThread::Task { | 22 class NopTask : public blink::WebTaskRunner::Task { |
23 public: | 23 public: |
24 ~NopTask() override {} | 24 ~NopTask() override {} |
25 | 25 |
26 void run() {} | 26 void run() {} |
27 }; | 27 }; |
28 | 28 |
29 class MockTask : public blink::WebThread::Task { | 29 class MockTask : public blink::WebTaskRunner::Task { |
30 public: | 30 public: |
31 ~MockTask() override {} | 31 ~MockTask() override {} |
32 | 32 |
33 MOCK_METHOD0(run, void()); | 33 MOCK_METHOD0(run, void()); |
34 }; | 34 }; |
35 | 35 |
36 class MockIdleTask : public blink::WebThread::IdleTask { | 36 class MockIdleTask : public blink::WebThread::IdleTask { |
37 public: | 37 public: |
38 ~MockIdleTask() override {} | 38 ~MockIdleTask() override {} |
39 | 39 |
40 MOCK_METHOD1(run, void(double deadline)); | 40 MOCK_METHOD1(run, void(double deadline)); |
41 }; | 41 }; |
42 | 42 |
43 class TestObserver : public blink::WebThread::TaskObserver { | 43 class TestObserver : public blink::WebThread::TaskObserver { |
44 public: | 44 public: |
45 explicit TestObserver(std::string* calls) : calls_(calls) {} | 45 explicit TestObserver(std::string* calls) : calls_(calls) {} |
46 | 46 |
47 ~TestObserver() override {} | 47 ~TestObserver() override {} |
48 | 48 |
49 void willProcessTask() override { calls_->append(" willProcessTask"); } | 49 void willProcessTask() override { calls_->append(" willProcessTask"); } |
50 | 50 |
51 void didProcessTask() override { calls_->append(" didProcessTask"); } | 51 void didProcessTask() override { calls_->append(" didProcessTask"); } |
52 | 52 |
53 private: | 53 private: |
54 std::string* calls_; // NOT OWNED | 54 std::string* calls_; // NOT OWNED |
55 }; | 55 }; |
56 | 56 |
57 class TestTask : public blink::WebThread::Task { | 57 class TestTask : public blink::WebTaskRunner::Task { |
58 public: | 58 public: |
59 explicit TestTask(std::string* calls) : calls_(calls) {} | 59 explicit TestTask(std::string* calls) : calls_(calls) {} |
60 | 60 |
61 ~TestTask() override {} | 61 ~TestTask() override {} |
62 | 62 |
63 void run() override { calls_->append(" run"); } | 63 void run() override { calls_->append(" run"); } |
64 | 64 |
65 private: | 65 private: |
66 std::string* calls_; // NOT OWNED | 66 std::string* calls_; // NOT OWNED |
67 }; | 67 }; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 }; | 117 }; |
118 | 118 |
119 TEST_F(WebThreadImplForWorkerSchedulerTest, TestDefaultTask) { | 119 TEST_F(WebThreadImplForWorkerSchedulerTest, TestDefaultTask) { |
120 scoped_ptr<MockTask> task(new MockTask()); | 120 scoped_ptr<MockTask> task(new MockTask()); |
121 base::WaitableEvent completion(false, false); | 121 base::WaitableEvent completion(false, false); |
122 | 122 |
123 EXPECT_CALL(*task, run()); | 123 EXPECT_CALL(*task, run()); |
124 ON_CALL(*task, run()) | 124 ON_CALL(*task, run()) |
125 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); | 125 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); |
126 | 126 |
127 thread_->postTask(blink::WebTraceLocation(), task.release()); | 127 thread_->taskRunner()->postTask(blink::WebTraceLocation(), task.release()); |
128 completion.Wait(); | 128 completion.Wait(); |
129 } | 129 } |
130 | 130 |
131 TEST_F(WebThreadImplForWorkerSchedulerTest, | 131 TEST_F(WebThreadImplForWorkerSchedulerTest, |
132 TestTaskExecutedBeforeThreadDeletion) { | 132 TestTaskExecutedBeforeThreadDeletion) { |
133 scoped_ptr<MockTask> task(new MockTask()); | 133 scoped_ptr<MockTask> task(new MockTask()); |
134 base::WaitableEvent completion(false, false); | 134 base::WaitableEvent completion(false, false); |
135 | 135 |
136 EXPECT_CALL(*task, run()); | 136 EXPECT_CALL(*task, run()); |
137 ON_CALL(*task, run()) | 137 ON_CALL(*task, run()) |
138 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); | 138 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); |
139 | 139 |
140 thread_->postTask(blink::WebTraceLocation(), task.release()); | 140 thread_->taskRunner()->postTask(blink::WebTraceLocation(), task.release()); |
141 thread_.reset(); | 141 thread_.reset(); |
142 } | 142 } |
143 | 143 |
144 TEST_F(WebThreadImplForWorkerSchedulerTest, TestIdleTask) { | 144 TEST_F(WebThreadImplForWorkerSchedulerTest, TestIdleTask) { |
145 scoped_ptr<MockIdleTask> task(new MockIdleTask()); | 145 scoped_ptr<MockIdleTask> task(new MockIdleTask()); |
146 base::WaitableEvent completion(false, false); | 146 base::WaitableEvent completion(false, false); |
147 | 147 |
148 EXPECT_CALL(*task, run(_)); | 148 EXPECT_CALL(*task, run(_)); |
149 ON_CALL(*task, run(_)) | 149 ON_CALL(*task, run(_)) |
150 .WillByDefault(Invoke([&completion](double) { completion.Signal(); })); | 150 .WillByDefault(Invoke([&completion](double) { completion.Signal(); })); |
151 | 151 |
152 thread_->postIdleTask(blink::WebTraceLocation(), task.release()); | 152 thread_->postIdleTask(blink::WebTraceLocation(), task.release()); |
153 // We need to post a wakeup task or idle work will never happen. | 153 // We need to post a wakeup task or idle work will never happen. |
154 thread_->postDelayedTask(blink::WebTraceLocation(), new NopTask(), 50ul); | 154 thread_->taskRunner()->postDelayedTask(blink::WebTraceLocation(), |
| 155 new NopTask(), 50ll); |
155 | 156 |
156 completion.Wait(); | 157 completion.Wait(); |
157 } | 158 } |
158 | 159 |
159 TEST_F(WebThreadImplForWorkerSchedulerTest, TestTaskObserver) { | 160 TEST_F(WebThreadImplForWorkerSchedulerTest, TestTaskObserver) { |
160 std::string calls; | 161 std::string calls; |
161 TestObserver observer(&calls); | 162 TestObserver observer(&calls); |
162 | 163 |
163 RunOnWorkerThread(FROM_HERE, | 164 RunOnWorkerThread(FROM_HERE, |
164 base::Bind(&addTaskObserver, thread_.get(), &observer)); | 165 base::Bind(&addTaskObserver, thread_.get(), &observer)); |
165 thread_->postTask(blink::WebTraceLocation(), new TestTask(&calls)); | 166 thread_->taskRunner()->postTask(blink::WebTraceLocation(), |
| 167 new TestTask(&calls)); |
166 RunOnWorkerThread(FROM_HERE, | 168 RunOnWorkerThread(FROM_HERE, |
167 base::Bind(&removeTaskObserver, thread_.get(), &observer)); | 169 base::Bind(&removeTaskObserver, thread_.get(), &observer)); |
168 | 170 |
169 // We need to be careful what we test here. We want to make sure the | 171 // We need to be careful what we test here. We want to make sure the |
170 // observers are un in the expected order before and after the task. | 172 // observers are un in the expected order before and after the task. |
171 // Sometimes we get an internal scheduler task running before or after | 173 // Sometimes we get an internal scheduler task running before or after |
172 // TestTask as well. This is not a bug, and we need to make sure the test | 174 // TestTask as well. This is not a bug, and we need to make sure the test |
173 // doesn't fail when that happens. | 175 // doesn't fail when that happens. |
174 EXPECT_THAT(calls, testing::HasSubstr("willProcessTask run didProcessTask")); | 176 EXPECT_THAT(calls, testing::HasSubstr("willProcessTask run didProcessTask")); |
175 } | 177 } |
176 | 178 |
177 TEST_F(WebThreadImplForWorkerSchedulerTest, TestShutdown) { | 179 TEST_F(WebThreadImplForWorkerSchedulerTest, TestShutdown) { |
178 scoped_ptr<MockTask> task(new MockTask()); | 180 scoped_ptr<MockTask> task(new MockTask()); |
179 scoped_ptr<MockTask> delayed_task(new MockTask()); | 181 scoped_ptr<MockTask> delayed_task(new MockTask()); |
180 | 182 |
181 EXPECT_CALL(*task, run()).Times(0); | 183 EXPECT_CALL(*task, run()).Times(0); |
182 EXPECT_CALL(*delayed_task, run()).Times(0); | 184 EXPECT_CALL(*delayed_task, run()).Times(0); |
183 | 185 |
184 RunOnWorkerThread(FROM_HERE, base::Bind(&shutdownOnThread, thread_.get())); | 186 RunOnWorkerThread(FROM_HERE, base::Bind(&shutdownOnThread, thread_.get())); |
185 thread_->postTask(blink::WebTraceLocation(), task.release()); | 187 thread_->taskRunner()->postTask(blink::WebTraceLocation(), task.release()); |
186 thread_->postDelayedTask(blink::WebTraceLocation(), task.release(), 50ul); | 188 thread_->taskRunner()->postDelayedTask(blink::WebTraceLocation(), |
| 189 task.release(), 50ll); |
187 thread_.reset(); | 190 thread_.reset(); |
188 } | 191 } |
189 | 192 |
190 } // namespace scheduler | 193 } // namespace scheduler |
OLD | NEW |