| OLD | NEW |
| (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/child/webthread_impl_for_worker_scheduler.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "base/synchronization/waitable_event.h" | |
| 9 #include "components/scheduler/child/web_scheduler_impl.h" | |
| 10 #include "components/scheduler/child/worker_scheduler_impl.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 #include "third_party/WebKit/public/platform/WebTraceLocation.h" | |
| 14 | |
| 15 using testing::_; | |
| 16 using testing::AnyOf; | |
| 17 using testing::ElementsAre; | |
| 18 using testing::Invoke; | |
| 19 | |
| 20 namespace scheduler { | |
| 21 namespace { | |
| 22 | |
| 23 class NopTask : public blink::WebTaskRunner::Task { | |
| 24 public: | |
| 25 ~NopTask() override {} | |
| 26 | |
| 27 void run() override {} | |
| 28 }; | |
| 29 | |
| 30 class MockTask : public blink::WebTaskRunner::Task { | |
| 31 public: | |
| 32 ~MockTask() override {} | |
| 33 | |
| 34 MOCK_METHOD0(run, void()); | |
| 35 }; | |
| 36 | |
| 37 class MockIdleTask : public blink::WebThread::IdleTask { | |
| 38 public: | |
| 39 ~MockIdleTask() override {} | |
| 40 | |
| 41 MOCK_METHOD1(run, void(double deadline)); | |
| 42 }; | |
| 43 | |
| 44 class TestObserver : public blink::WebThread::TaskObserver { | |
| 45 public: | |
| 46 explicit TestObserver(std::string* calls) : calls_(calls) {} | |
| 47 | |
| 48 ~TestObserver() override {} | |
| 49 | |
| 50 void willProcessTask() override { calls_->append(" willProcessTask"); } | |
| 51 | |
| 52 void didProcessTask() override { calls_->append(" didProcessTask"); } | |
| 53 | |
| 54 private: | |
| 55 std::string* calls_; // NOT OWNED | |
| 56 }; | |
| 57 | |
| 58 class TestTask : public blink::WebTaskRunner::Task { | |
| 59 public: | |
| 60 explicit TestTask(std::string* calls) : calls_(calls) {} | |
| 61 | |
| 62 ~TestTask() override {} | |
| 63 | |
| 64 void run() override { calls_->append(" run"); } | |
| 65 | |
| 66 private: | |
| 67 std::string* calls_; // NOT OWNED | |
| 68 }; | |
| 69 | |
| 70 void addTaskObserver(WebThreadImplForWorkerScheduler* thread, | |
| 71 TestObserver* observer) { | |
| 72 thread->addTaskObserver(observer); | |
| 73 } | |
| 74 | |
| 75 void removeTaskObserver(WebThreadImplForWorkerScheduler* thread, | |
| 76 TestObserver* observer) { | |
| 77 thread->removeTaskObserver(observer); | |
| 78 } | |
| 79 | |
| 80 void shutdownOnThread(WebThreadImplForWorkerScheduler* thread) { | |
| 81 WebSchedulerImpl* web_scheduler_impl = | |
| 82 static_cast<WebSchedulerImpl*>(thread->scheduler()); | |
| 83 web_scheduler_impl->shutdown(); | |
| 84 } | |
| 85 | |
| 86 } // namespace | |
| 87 | |
| 88 class WebThreadImplForWorkerSchedulerTest : public testing::Test { | |
| 89 public: | |
| 90 WebThreadImplForWorkerSchedulerTest() {} | |
| 91 | |
| 92 ~WebThreadImplForWorkerSchedulerTest() override {} | |
| 93 | |
| 94 void SetUp() override { | |
| 95 thread_.reset(new WebThreadImplForWorkerScheduler("test thread")); | |
| 96 thread_->Init(); | |
| 97 } | |
| 98 | |
| 99 void RunOnWorkerThread(const tracked_objects::Location& from_here, | |
| 100 const base::Closure& task) { | |
| 101 base::WaitableEvent completion( | |
| 102 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 103 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 104 thread_->GetTaskRunner()->PostTask( | |
| 105 from_here, | |
| 106 base::Bind(&WebThreadImplForWorkerSchedulerTest::RunOnWorkerThreadTask, | |
| 107 base::Unretained(this), task, &completion)); | |
| 108 completion.Wait(); | |
| 109 } | |
| 110 | |
| 111 protected: | |
| 112 void RunOnWorkerThreadTask(const base::Closure& task, | |
| 113 base::WaitableEvent* completion) { | |
| 114 task.Run(); | |
| 115 completion->Signal(); | |
| 116 } | |
| 117 | |
| 118 std::unique_ptr<WebThreadImplForWorkerScheduler> thread_; | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(WebThreadImplForWorkerSchedulerTest); | |
| 121 }; | |
| 122 | |
| 123 TEST_F(WebThreadImplForWorkerSchedulerTest, TestDefaultTask) { | |
| 124 std::unique_ptr<MockTask> task(new MockTask()); | |
| 125 base::WaitableEvent completion( | |
| 126 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 127 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 128 | |
| 129 EXPECT_CALL(*task, run()); | |
| 130 ON_CALL(*task, run()) | |
| 131 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); | |
| 132 | |
| 133 thread_->getWebTaskRunner()->postTask(blink::WebTraceLocation(), | |
| 134 task.release()); | |
| 135 completion.Wait(); | |
| 136 } | |
| 137 | |
| 138 TEST_F(WebThreadImplForWorkerSchedulerTest, | |
| 139 TestTaskExecutedBeforeThreadDeletion) { | |
| 140 std::unique_ptr<MockTask> task(new MockTask()); | |
| 141 base::WaitableEvent completion( | |
| 142 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 143 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 144 | |
| 145 EXPECT_CALL(*task, run()); | |
| 146 ON_CALL(*task, run()) | |
| 147 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); | |
| 148 | |
| 149 thread_->getWebTaskRunner()->postTask(blink::WebTraceLocation(), | |
| 150 task.release()); | |
| 151 thread_.reset(); | |
| 152 } | |
| 153 | |
| 154 TEST_F(WebThreadImplForWorkerSchedulerTest, TestIdleTask) { | |
| 155 std::unique_ptr<MockIdleTask> task(new MockIdleTask()); | |
| 156 base::WaitableEvent completion( | |
| 157 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 158 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 159 | |
| 160 EXPECT_CALL(*task, run(_)); | |
| 161 ON_CALL(*task, run(_)) | |
| 162 .WillByDefault(Invoke([&completion](double) { completion.Signal(); })); | |
| 163 | |
| 164 thread_->postIdleTask(blink::WebTraceLocation(), task.release()); | |
| 165 // We need to post a wakeup task or idle work will never happen. | |
| 166 thread_->getWebTaskRunner()->postDelayedTask(blink::WebTraceLocation(), | |
| 167 new NopTask(), 50ll); | |
| 168 | |
| 169 completion.Wait(); | |
| 170 } | |
| 171 | |
| 172 TEST_F(WebThreadImplForWorkerSchedulerTest, TestTaskObserver) { | |
| 173 std::string calls; | |
| 174 TestObserver observer(&calls); | |
| 175 | |
| 176 RunOnWorkerThread(FROM_HERE, | |
| 177 base::Bind(&addTaskObserver, thread_.get(), &observer)); | |
| 178 thread_->getWebTaskRunner()->postTask(blink::WebTraceLocation(), | |
| 179 new TestTask(&calls)); | |
| 180 RunOnWorkerThread(FROM_HERE, | |
| 181 base::Bind(&removeTaskObserver, thread_.get(), &observer)); | |
| 182 | |
| 183 // We need to be careful what we test here. We want to make sure the | |
| 184 // observers are un in the expected order before and after the task. | |
| 185 // Sometimes we get an internal scheduler task running before or after | |
| 186 // TestTask as well. This is not a bug, and we need to make sure the test | |
| 187 // doesn't fail when that happens. | |
| 188 EXPECT_THAT(calls, testing::HasSubstr("willProcessTask run didProcessTask")); | |
| 189 } | |
| 190 | |
| 191 TEST_F(WebThreadImplForWorkerSchedulerTest, TestShutdown) { | |
| 192 std::unique_ptr<MockTask> task(new MockTask()); | |
| 193 std::unique_ptr<MockTask> delayed_task(new MockTask()); | |
| 194 | |
| 195 EXPECT_CALL(*task, run()).Times(0); | |
| 196 EXPECT_CALL(*delayed_task, run()).Times(0); | |
| 197 | |
| 198 RunOnWorkerThread(FROM_HERE, base::Bind(&shutdownOnThread, thread_.get())); | |
| 199 thread_->getWebTaskRunner()->postTask(blink::WebTraceLocation(), | |
| 200 task.release()); | |
| 201 thread_->getWebTaskRunner()->postDelayedTask(blink::WebTraceLocation(), | |
| 202 task.release(), 50ll); | |
| 203 thread_.reset(); | |
| 204 } | |
| 205 | |
| 206 } // namespace scheduler | |
| OLD | NEW |