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 "config.h" |
| 6 #include "modules/compositorworker/CompositorWorkerManager.h" |
| 7 |
| 8 #include "bindings/core/v8/ScriptSourceCode.h" |
| 9 #include "core/testing/DummyPageHolder.h" |
| 10 #include "core/workers/WorkerLoaderProxy.h" |
| 11 #include "core/workers/WorkerObjectProxy.h" |
| 12 #include "core/workers/WorkerReportingProxy.h" |
| 13 #include "core/workers/WorkerThreadStartupData.h" |
| 14 #include "modules/compositorworker/CompositorWorkerThread.h" |
| 15 #include "platform/NotImplemented.h" |
| 16 #include "platform/ThreadSafeFunctional.h" |
| 17 #include "public/platform/Platform.h" |
| 18 #include "public/platform/WebWaitableEvent.h" |
| 19 #include <gtest/gtest.h> |
| 20 |
| 21 namespace blink { |
| 22 namespace { |
| 23 |
| 24 class TestCompositorWorkerThread : public CompositorWorkerThread { |
| 25 public: |
| 26 TestCompositorWorkerThread(WorkerLoaderProxyProvider* loaderProxyProvider, W
orkerObjectProxy& objectProxy, double timeOrigin, WebWaitableEvent* startEvent) |
| 27 : CompositorWorkerThread(WorkerLoaderProxy::create(loaderProxyProvider),
objectProxy, timeOrigin) |
| 28 , m_startEvent(startEvent) |
| 29 { |
| 30 } |
| 31 |
| 32 ~TestCompositorWorkerThread() override { } |
| 33 |
| 34 void setCallbackAfterV8Termination(PassOwnPtr<Function<void()>> callback) |
| 35 { |
| 36 m_v8TerminationCallback = callback; |
| 37 } |
| 38 |
| 39 private: |
| 40 // WorkerThread: |
| 41 void didStartRunLoop() override |
| 42 { |
| 43 m_startEvent->signal(); |
| 44 } |
| 45 void terminateV8Execution() override |
| 46 { |
| 47 CompositorWorkerThread::terminateV8Execution(); |
| 48 if (m_v8TerminationCallback) |
| 49 (*m_v8TerminationCallback)(); |
| 50 } |
| 51 |
| 52 WebWaitableEvent* m_startEvent; |
| 53 OwnPtr<Function<void()>> m_v8TerminationCallback; |
| 54 }; |
| 55 |
| 56 } // namespace |
| 57 |
| 58 class CompositorWorkerManagerTest : public testing::Test { |
| 59 public: |
| 60 void SetUp() override |
| 61 { |
| 62 m_page = DummyPageHolder::create(); |
| 63 m_objectProxy = WorkerObjectProxy::create(&m_page->document(), nullptr); |
| 64 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://
fake.url/")); |
| 65 } |
| 66 |
| 67 void TearDown() override |
| 68 { |
| 69 ASSERT(!managerHasThread()); |
| 70 ASSERT(!managerHasIsolate()); |
| 71 m_page.clear(); |
| 72 } |
| 73 |
| 74 PassRefPtr<TestCompositorWorkerThread> createCompositorWorker(WebWaitableEve
nt* startEvent) |
| 75 { |
| 76 TestCompositorWorkerThread* workerThread = new TestCompositorWorkerThrea
d(nullptr, *m_objectProxy, 0, startEvent); |
| 77 workerThread->start(WorkerThreadStartupData::create( |
| 78 KURL(ParsedURLString, "http://fake.url/"), |
| 79 "fake user agent", |
| 80 "//fake source code", |
| 81 nullptr, |
| 82 DontPauseWorkerGlobalScopeOnStart, |
| 83 adoptPtr(new Vector<CSPHeaderAndType>()), |
| 84 m_securityOrigin.get(), |
| 85 WorkerClients::create(), |
| 86 V8CacheOptionsDefault)); |
| 87 return adoptRef(workerThread); |
| 88 } |
| 89 |
| 90 void createWorkerAdapter(RefPtr<CompositorWorkerThread>* workerThread, WebWa
itableEvent* creationEvent) |
| 91 { |
| 92 *workerThread = createCompositorWorker(creationEvent); |
| 93 } |
| 94 |
| 95 // Attempts to run some simple script for |worker|. |
| 96 void checkWorkerCanExecuteScript(WorkerThread* worker) |
| 97 { |
| 98 OwnPtr<WebWaitableEvent> waitEvent = adoptPtr(Platform::current()->creat
eWaitableEvent()); |
| 99 worker->backingThread().platformThread().postTask(FROM_HERE, threadSafeB
ind(&CompositorWorkerManagerTest::executeScriptInWorker, AllowCrossThreadAccess(
this), |
| 100 AllowCrossThreadAccess(worker), AllowCrossThreadAccess(waitEvent.get
()))); |
| 101 waitEvent->wait(); |
| 102 } |
| 103 |
| 104 bool managerHasThread() const |
| 105 { |
| 106 return CompositorWorkerManager::instance()->m_thread; |
| 107 } |
| 108 |
| 109 bool managerHasIsolate() const |
| 110 { |
| 111 return CompositorWorkerManager::instance()->m_isolate; |
| 112 } |
| 113 |
| 114 private: |
| 115 void executeScriptInWorker(WorkerThread* worker, WebWaitableEvent* waitEvent
) |
| 116 { |
| 117 WorkerScriptController* scriptController = worker->workerGlobalScope()->
script(); |
| 118 ASSERT(scriptController->evaluate(ScriptSourceCode("var counter = 0; ++c
ounter;"))); |
| 119 waitEvent->signal(); |
| 120 } |
| 121 |
| 122 OwnPtr<DummyPageHolder> m_page; |
| 123 RefPtr<SecurityOrigin> m_securityOrigin; |
| 124 OwnPtr<WorkerObjectProxy> m_objectProxy; |
| 125 }; |
| 126 |
| 127 TEST_F(CompositorWorkerManagerTest, Basic) |
| 128 { |
| 129 OwnPtr<WebWaitableEvent> creationEvent = adoptPtr(Platform::current()->creat
eWaitableEvent()); |
| 130 RefPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(cre
ationEvent.get()); |
| 131 creationEvent->wait(); |
| 132 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 133 compositorWorker->terminateAndWait(); |
| 134 } |
| 135 |
| 136 // Tests that the same WebThread is used for new workers if the WebThread is sti
ll alive. |
| 137 TEST_F(CompositorWorkerManagerTest, CreateSecondAndTerminateFirst) |
| 138 { |
| 139 // Create the first worker and wait until it is initialized. |
| 140 OwnPtr<WebWaitableEvent> firstCreationEvent = adoptPtr(Platform::current()->
createWaitableEvent()); |
| 141 RefPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(firstCre
ationEvent.get()); |
| 142 WebThreadSupportingGC* firstThread = &CompositorWorkerManager::instance()->c
ompositorWorkerThread(); |
| 143 ASSERT(firstThread); |
| 144 firstCreationEvent->wait(); |
| 145 v8::Isolate* firstIsolate = firstWorker->isolate(); |
| 146 ASSERT(firstIsolate); |
| 147 |
| 148 // Create the second worker and immediately destroy the first worker. |
| 149 OwnPtr<WebWaitableEvent> secondCreationEvent = adoptPtr(Platform::current()-
>createWaitableEvent()); |
| 150 RefPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(secondC
reationEvent.get()); |
| 151 firstWorker->terminateAndWait(); |
| 152 |
| 153 // Wait until the second worker is initialized. Verify that the second worke
r is using the same |
| 154 // thread and Isolate as the first worker. |
| 155 WebThreadSupportingGC* secondThread = &CompositorWorkerManager::instance()->
compositorWorkerThread(); |
| 156 ASSERT(secondThread); |
| 157 secondCreationEvent->wait(); |
| 158 EXPECT_EQ(firstThread, secondThread); |
| 159 |
| 160 v8::Isolate* secondIsolate = secondWorker->isolate(); |
| 161 ASSERT(secondIsolate); |
| 162 EXPECT_EQ(firstIsolate, secondIsolate); |
| 163 |
| 164 // Verify that the worker can still successfully execute script. |
| 165 checkWorkerCanExecuteScript(secondWorker.get()); |
| 166 |
| 167 secondWorker->terminateAndWait(); |
| 168 } |
| 169 |
| 170 static void checkCurrentIsolate(v8::Isolate* isolate, WebWaitableEvent* event) |
| 171 { |
| 172 EXPECT_EQ(v8::Isolate::GetCurrent(), isolate); |
| 173 event->signal(); |
| 174 } |
| 175 |
| 176 // Tests that a new WebThread is created if all existing workers are terminated
before a new worker is created. |
| 177 TEST_F(CompositorWorkerManagerTest, TerminateFirstAndCreateSecond) |
| 178 { |
| 179 // Create the first worker, wait until it is initialized, and terminate it. |
| 180 OwnPtr<WebWaitableEvent> creationEvent = adoptPtr(Platform::current()->creat
eWaitableEvent()); |
| 181 RefPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(cre
ationEvent.get()); |
| 182 WebThreadSupportingGC* firstThread = &CompositorWorkerManager::instance()->c
ompositorWorkerThread(); |
| 183 creationEvent->wait(); |
| 184 ASSERT(compositorWorker->isolate()); |
| 185 compositorWorker->terminateAndWait(); |
| 186 |
| 187 // Create the second worker. Verify that the second worker lives in a differ
ent WebThread since the first |
| 188 // thread will have been destroyed after destroying the first worker. |
| 189 creationEvent = adoptPtr(Platform::current()->createWaitableEvent()); |
| 190 compositorWorker = createCompositorWorker(creationEvent.get()); |
| 191 WebThreadSupportingGC* secondThread = &CompositorWorkerManager::instance()->
compositorWorkerThread(); |
| 192 EXPECT_NE(firstThread, secondThread); |
| 193 creationEvent->wait(); |
| 194 |
| 195 // Jump over to the worker's thread to verify that the Isolate is set up cor
rectly and execute script. |
| 196 OwnPtr<WebWaitableEvent> checkEvent = adoptPtr(Platform::current()->createWa
itableEvent()); |
| 197 secondThread->platformThread().postTask(FROM_HERE, threadSafeBind(&checkCurr
entIsolate, AllowCrossThreadAccess(compositorWorker->isolate()), AllowCrossThrea
dAccess(checkEvent.get()))); |
| 198 checkEvent->wait(); |
| 199 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 200 |
| 201 compositorWorker->terminateAndWait(); |
| 202 } |
| 203 |
| 204 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is crea
ted while another is terminating. |
| 205 TEST_F(CompositorWorkerManagerTest, CreatingSecondDuringTerminationOfFirst) |
| 206 { |
| 207 OwnPtr<WebWaitableEvent> firstCreationEvent = adoptPtr(Platform::current()->
createWaitableEvent()); |
| 208 RefPtr<TestCompositorWorkerThread> firstWorker = createCompositorWorker(firs
tCreationEvent.get()); |
| 209 firstCreationEvent->wait(); |
| 210 v8::Isolate* firstIsolate = firstWorker->isolate(); |
| 211 ASSERT(firstIsolate); |
| 212 |
| 213 // Request termination of the first worker, and set-up to make sure the seco
nd worker is created right as |
| 214 // the first worker terminates its isolate. |
| 215 OwnPtr<WebWaitableEvent> secondCreationEvent = adoptPtr(Platform::current()-
>createWaitableEvent()); |
| 216 RefPtr<CompositorWorkerThread> secondWorker; |
| 217 firstWorker->setCallbackAfterV8Termination(bind(&CompositorWorkerManagerTest
::createWorkerAdapter, this, &secondWorker, secondCreationEvent.get())); |
| 218 firstWorker->terminateAndWait(); |
| 219 ASSERT(secondWorker); |
| 220 |
| 221 secondCreationEvent->wait(); |
| 222 v8::Isolate* secondIsolate = secondWorker->isolate(); |
| 223 ASSERT(secondIsolate); |
| 224 EXPECT_EQ(firstIsolate, secondIsolate); |
| 225 |
| 226 // Verify that the isolate can run some scripts correctly in the second work
er. |
| 227 checkWorkerCanExecuteScript(secondWorker.get()); |
| 228 secondWorker->terminateAndWait(); |
| 229 } |
| 230 |
| 231 } // namespace blink |
OLD | NEW |