| 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/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "components/scheduler/child/web_scheduler_impl.h" | 9 #include "components/scheduler/child/web_scheduler_impl.h" |
| 10 #include "components/scheduler/child/worker_scheduler_impl.h" | 10 #include "components/scheduler/child/worker_scheduler_impl.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 | 91 |
| 92 ~WebThreadImplForWorkerSchedulerTest() override {} | 92 ~WebThreadImplForWorkerSchedulerTest() override {} |
| 93 | 93 |
| 94 void SetUp() override { | 94 void SetUp() override { |
| 95 thread_.reset(new WebThreadImplForWorkerScheduler("test thread")); | 95 thread_.reset(new WebThreadImplForWorkerScheduler("test thread")); |
| 96 thread_->Init(); | 96 thread_->Init(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void RunOnWorkerThread(const tracked_objects::Location& from_here, | 99 void RunOnWorkerThread(const tracked_objects::Location& from_here, |
| 100 const base::Closure& task) { | 100 const base::Closure& task) { |
| 101 base::WaitableEvent completion(false, false); | 101 base::WaitableEvent completion( |
| 102 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 103 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 102 thread_->GetTaskRunner()->PostTask( | 104 thread_->GetTaskRunner()->PostTask( |
| 103 from_here, | 105 from_here, |
| 104 base::Bind(&WebThreadImplForWorkerSchedulerTest::RunOnWorkerThreadTask, | 106 base::Bind(&WebThreadImplForWorkerSchedulerTest::RunOnWorkerThreadTask, |
| 105 base::Unretained(this), task, &completion)); | 107 base::Unretained(this), task, &completion)); |
| 106 completion.Wait(); | 108 completion.Wait(); |
| 107 } | 109 } |
| 108 | 110 |
| 109 protected: | 111 protected: |
| 110 void RunOnWorkerThreadTask(const base::Closure& task, | 112 void RunOnWorkerThreadTask(const base::Closure& task, |
| 111 base::WaitableEvent* completion) { | 113 base::WaitableEvent* completion) { |
| 112 task.Run(); | 114 task.Run(); |
| 113 completion->Signal(); | 115 completion->Signal(); |
| 114 } | 116 } |
| 115 | 117 |
| 116 std::unique_ptr<WebThreadImplForWorkerScheduler> thread_; | 118 std::unique_ptr<WebThreadImplForWorkerScheduler> thread_; |
| 117 | 119 |
| 118 DISALLOW_COPY_AND_ASSIGN(WebThreadImplForWorkerSchedulerTest); | 120 DISALLOW_COPY_AND_ASSIGN(WebThreadImplForWorkerSchedulerTest); |
| 119 }; | 121 }; |
| 120 | 122 |
| 121 TEST_F(WebThreadImplForWorkerSchedulerTest, TestDefaultTask) { | 123 TEST_F(WebThreadImplForWorkerSchedulerTest, TestDefaultTask) { |
| 122 std::unique_ptr<MockTask> task(new MockTask()); | 124 std::unique_ptr<MockTask> task(new MockTask()); |
| 123 base::WaitableEvent completion(false, false); | 125 base::WaitableEvent completion( |
| 126 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 127 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 124 | 128 |
| 125 EXPECT_CALL(*task, run()); | 129 EXPECT_CALL(*task, run()); |
| 126 ON_CALL(*task, run()) | 130 ON_CALL(*task, run()) |
| 127 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); | 131 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); |
| 128 | 132 |
| 129 thread_->getWebTaskRunner()->postTask(blink::WebTraceLocation(), | 133 thread_->getWebTaskRunner()->postTask(blink::WebTraceLocation(), |
| 130 task.release()); | 134 task.release()); |
| 131 completion.Wait(); | 135 completion.Wait(); |
| 132 } | 136 } |
| 133 | 137 |
| 134 TEST_F(WebThreadImplForWorkerSchedulerTest, | 138 TEST_F(WebThreadImplForWorkerSchedulerTest, |
| 135 TestTaskExecutedBeforeThreadDeletion) { | 139 TestTaskExecutedBeforeThreadDeletion) { |
| 136 std::unique_ptr<MockTask> task(new MockTask()); | 140 std::unique_ptr<MockTask> task(new MockTask()); |
| 137 base::WaitableEvent completion(false, false); | 141 base::WaitableEvent completion( |
| 142 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 143 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 138 | 144 |
| 139 EXPECT_CALL(*task, run()); | 145 EXPECT_CALL(*task, run()); |
| 140 ON_CALL(*task, run()) | 146 ON_CALL(*task, run()) |
| 141 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); | 147 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); |
| 142 | 148 |
| 143 thread_->getWebTaskRunner()->postTask(blink::WebTraceLocation(), | 149 thread_->getWebTaskRunner()->postTask(blink::WebTraceLocation(), |
| 144 task.release()); | 150 task.release()); |
| 145 thread_.reset(); | 151 thread_.reset(); |
| 146 } | 152 } |
| 147 | 153 |
| 148 TEST_F(WebThreadImplForWorkerSchedulerTest, TestIdleTask) { | 154 TEST_F(WebThreadImplForWorkerSchedulerTest, TestIdleTask) { |
| 149 std::unique_ptr<MockIdleTask> task(new MockIdleTask()); | 155 std::unique_ptr<MockIdleTask> task(new MockIdleTask()); |
| 150 base::WaitableEvent completion(false, false); | 156 base::WaitableEvent completion( |
| 157 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 158 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 151 | 159 |
| 152 EXPECT_CALL(*task, run(_)); | 160 EXPECT_CALL(*task, run(_)); |
| 153 ON_CALL(*task, run(_)) | 161 ON_CALL(*task, run(_)) |
| 154 .WillByDefault(Invoke([&completion](double) { completion.Signal(); })); | 162 .WillByDefault(Invoke([&completion](double) { completion.Signal(); })); |
| 155 | 163 |
| 156 thread_->postIdleTask(blink::WebTraceLocation(), task.release()); | 164 thread_->postIdleTask(blink::WebTraceLocation(), task.release()); |
| 157 // We need to post a wakeup task or idle work will never happen. | 165 // We need to post a wakeup task or idle work will never happen. |
| 158 thread_->getWebTaskRunner()->postDelayedTask(blink::WebTraceLocation(), | 166 thread_->getWebTaskRunner()->postDelayedTask(blink::WebTraceLocation(), |
| 159 new NopTask(), 50ll); | 167 new NopTask(), 50ll); |
| 160 | 168 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 189 | 197 |
| 190 RunOnWorkerThread(FROM_HERE, base::Bind(&shutdownOnThread, thread_.get())); | 198 RunOnWorkerThread(FROM_HERE, base::Bind(&shutdownOnThread, thread_.get())); |
| 191 thread_->getWebTaskRunner()->postTask(blink::WebTraceLocation(), | 199 thread_->getWebTaskRunner()->postTask(blink::WebTraceLocation(), |
| 192 task.release()); | 200 task.release()); |
| 193 thread_->getWebTaskRunner()->postDelayedTask(blink::WebTraceLocation(), | 201 thread_->getWebTaskRunner()->postDelayedTask(blink::WebTraceLocation(), |
| 194 task.release(), 50ll); | 202 task.release(), 50ll); |
| 195 thread_.reset(); | 203 thread_.reset(); |
| 196 } | 204 } |
| 197 | 205 |
| 198 } // namespace scheduler | 206 } // namespace scheduler |
| OLD | NEW |