| 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 "core/workers/WorkerThread.h" | 5 #include "core/workers/WorkerThread.h" |
| 6 | 6 |
| 7 #include <memory> |
| 8 #include "core/dom/TaskRunnerHelper.h" |
| 7 #include "core/workers/WorkerThreadTestHelper.h" | 9 #include "core/workers/WorkerThreadTestHelper.h" |
| 8 #include "platform/WaitableEvent.h" | 10 #include "platform/WaitableEvent.h" |
| 9 #include "platform/testing/UnitTestHelpers.h" | 11 #include "platform/testing/UnitTestHelpers.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "wtf/PtrUtil.h" | 14 #include "wtf/PtrUtil.h" |
| 13 #include <memory> | |
| 14 | 15 |
| 15 using testing::_; | 16 using testing::_; |
| 16 using testing::AnyNumber; | 17 using testing::AnyNumber; |
| 17 using testing::AtMost; | 18 using testing::AtMost; |
| 18 | 19 |
| 19 namespace blink { | 20 namespace blink { |
| 20 | 21 |
| 21 using ExitCode = WorkerThread::ExitCode; | 22 using ExitCode = WorkerThread::ExitCode; |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Used as a debugger task. Waits for a signal from the main thread. | 26 // Used as a debugger task. Waits for a signal from the main thread. |
| 26 void waitForSignalTask(WorkerThread* workerThread, | 27 void waitForSignalTask(WorkerThread* workerThread, |
| 27 WaitableEvent* waitableEvent) { | 28 WaitableEvent* waitableEvent) { |
| 28 EXPECT_TRUE(workerThread->isCurrentThread()); | 29 EXPECT_TRUE(workerThread->isCurrentThread()); |
| 29 | 30 |
| 30 // Notify the main thread that the debugger task is waiting for the signal. | 31 // Notify the main thread that the debugger task is waiting for the signal. |
| 31 workerThread->getParentFrameTaskRunners() | 32 FrameTaskRunnerHelper::get(TaskType::UnspecedTimer, |
| 32 ->get(TaskType::UnspecedTimer) | 33 workerThread->globalScope()) |
| 33 ->postTask(BLINK_FROM_HERE, crossThreadBind(&testing::exitRunLoop)); | 34 ->postTask(BLINK_FROM_HERE, crossThreadBind(&testing::exitRunLoop)); |
| 34 waitableEvent->wait(); | 35 waitableEvent->wait(); |
| 35 } | 36 } |
| 36 | 37 |
| 37 } // namespace | 38 } // namespace |
| 38 | 39 |
| 39 class WorkerThreadTest : public ::testing::Test { | 40 class WorkerThreadTest : public ::testing::Test { |
| 40 public: | 41 public: |
| 41 WorkerThreadTest() {} | 42 WorkerThreadTest() {} |
| 42 | 43 |
| 43 void SetUp() override { | 44 void SetUp() override { |
| 44 m_loaderProxyProvider = WTF::makeUnique<MockWorkerLoaderProxyProvider>(); | 45 m_loaderProxyProvider = WTF::makeUnique<MockWorkerLoaderProxyProvider>(); |
| 45 m_reportingProxy = WTF::makeUnique<MockWorkerReportingProxy>(); | 46 m_reportingProxy = WTF::makeUnique<MockWorkerReportingProxy>(); |
| 46 m_securityOrigin = | 47 m_securityOrigin = |
| 47 SecurityOrigin::create(KURL(ParsedURLString, "http://fake.url/")); | 48 SecurityOrigin::create(KURL(ParsedURLString, "http://fake.url/")); |
| 48 m_workerThread = WTF::wrapUnique(new WorkerThreadForTest( | 49 m_workerThread = WTF::wrapUnique(new WorkerThreadForTest( |
| 49 m_loaderProxyProvider.get(), *m_reportingProxy)); | 50 m_loaderProxyProvider.get(), *m_reportingProxy)); |
| 50 m_lifecycleObserver = new MockWorkerThreadLifecycleObserver( | 51 m_lifecycleObserver = new MockWorkerThreadLifecycleObserver( |
| 51 m_workerThread->getWorkerThreadLifecycleContext()); | 52 m_workerThread->getWorkerThreadLifecycleContext()); |
| 52 } | 53 } |
| 53 | 54 |
| 54 void TearDown() override { | 55 void TearDown() override { |
| 55 m_workerThread->workerLoaderProxy()->detachProvider( | 56 m_workerThread->workerLoaderProxy()->detachProvider( |
| 56 m_loaderProxyProvider.get()); | 57 m_loaderProxyProvider.get()); |
| 57 } | 58 } |
| 58 | 59 |
| 59 void start() { | 60 void start() { |
| 60 m_workerThread->startWithSourceCode( | 61 m_workerThread->startWithSourceCode( |
| 61 m_securityOrigin.get(), "//fake source code", | 62 m_securityOrigin.get(), "//fake source code", |
| 62 ParentFrameTaskRunners::create(nullptr)); | 63 FrameTaskRunnersHolder::create(nullptr)); |
| 63 } | 64 } |
| 64 | 65 |
| 65 void startWithSourceCodeNotToFinish() { | 66 void startWithSourceCodeNotToFinish() { |
| 66 // Use a JavaScript source code that makes an infinite loop so that we | 67 // Use a JavaScript source code that makes an infinite loop so that we |
| 67 // can catch some kind of issues as a timeout. | 68 // can catch some kind of issues as a timeout. |
| 68 m_workerThread->startWithSourceCode( | 69 m_workerThread->startWithSourceCode( |
| 69 m_securityOrigin.get(), "while(true) {}", | 70 m_securityOrigin.get(), "while(true) {}", |
| 70 ParentFrameTaskRunners::create(nullptr)); | 71 FrameTaskRunnersHolder::create(nullptr)); |
| 71 } | 72 } |
| 72 | 73 |
| 73 void setForcibleTerminationDelayInMs(long long forcibleTerminationDelayInMs) { | 74 void setForcibleTerminationDelayInMs(long long forcibleTerminationDelayInMs) { |
| 74 m_workerThread->m_forcibleTerminationDelayInMs = | 75 m_workerThread->m_forcibleTerminationDelayInMs = |
| 75 forcibleTerminationDelayInMs; | 76 forcibleTerminationDelayInMs; |
| 76 } | 77 } |
| 77 | 78 |
| 78 bool isForcibleTerminationTaskScheduled() { | 79 bool isForcibleTerminationTaskScheduled() { |
| 79 return m_workerThread->m_forcibleTerminationTaskHandle.isActive(); | 80 return m_workerThread->m_forcibleTerminationTaskHandle.isActive(); |
| 80 } | 81 } |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 // on initialziation to run debugger tasks. | 278 // on initialziation to run debugger tasks. |
| 278 std::unique_ptr<WorkerThreadStartupData> startupData = | 279 std::unique_ptr<WorkerThreadStartupData> startupData = |
| 279 WorkerThreadStartupData::create( | 280 WorkerThreadStartupData::create( |
| 280 KURL(ParsedURLString, "http://fake.url/"), "fake user agent", | 281 KURL(ParsedURLString, "http://fake.url/"), "fake user agent", |
| 281 "//fake source code", nullptr, /* cachedMetaData */ | 282 "//fake source code", nullptr, /* cachedMetaData */ |
| 282 PauseWorkerGlobalScopeOnStart, headers.get(), "", | 283 PauseWorkerGlobalScopeOnStart, headers.get(), "", |
| 283 m_securityOrigin.get(), nullptr, /* workerClients */ | 284 m_securityOrigin.get(), nullptr, /* workerClients */ |
| 284 WebAddressSpaceLocal, nullptr /* originTrialToken */, | 285 WebAddressSpaceLocal, nullptr /* originTrialToken */, |
| 285 nullptr /* WorkerSettings */, WorkerV8Settings::Default()); | 286 nullptr /* WorkerSettings */, WorkerV8Settings::Default()); |
| 286 m_workerThread->start(std::move(startupData), | 287 m_workerThread->start(std::move(startupData), |
| 287 ParentFrameTaskRunners::create(nullptr)); | 288 FrameTaskRunnersHolder::create(nullptr)); |
| 288 | 289 |
| 289 // Used to wait for worker thread termination in a debugger task on the | 290 // Used to wait for worker thread termination in a debugger task on the |
| 290 // worker thread. | 291 // worker thread. |
| 291 WaitableEvent waitableEvent; | 292 WaitableEvent waitableEvent; |
| 292 m_workerThread->appendDebuggerTask(crossThreadBind( | 293 m_workerThread->appendDebuggerTask(crossThreadBind( |
| 293 &waitForSignalTask, crossThreadUnretained(m_workerThread.get()), | 294 &waitForSignalTask, crossThreadUnretained(m_workerThread.get()), |
| 294 crossThreadUnretained(&waitableEvent))); | 295 crossThreadUnretained(&waitableEvent))); |
| 295 | 296 |
| 296 // Wait for the debugger task. | 297 // Wait for the debugger task. |
| 297 testing::enterRunLoop(); | 298 testing::enterRunLoop(); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 | 358 |
| 358 // Resume the debugger task. Shutdown starts after that. | 359 // Resume the debugger task. Shutdown starts after that. |
| 359 waitableEvent.signal(); | 360 waitableEvent.signal(); |
| 360 m_workerThread->waitForShutdownForTesting(); | 361 m_workerThread->waitForShutdownForTesting(); |
| 361 EXPECT_EQ(ExitCode::GracefullyTerminated, getExitCode()); | 362 EXPECT_EQ(ExitCode::GracefullyTerminated, getExitCode()); |
| 362 } | 363 } |
| 363 | 364 |
| 364 // TODO(nhiroki): Add tests for terminateAndWaitForAllWorkers. | 365 // TODO(nhiroki): Add tests for terminateAndWaitForAllWorkers. |
| 365 | 366 |
| 366 } // namespace blink | 367 } // namespace blink |
| OLD | NEW |