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 "modules/compositorworker/CompositorWorkerThread.h" | 5 #include "modules/compositorworker/CompositorWorkerThread.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptSourceCode.h" | 7 #include "bindings/core/v8/ScriptSourceCode.h" |
8 #include "bindings/core/v8/SourceLocation.h" | 8 #include "bindings/core/v8/SourceLocation.h" |
9 #include "bindings/core/v8/V8GCController.h" | 9 #include "bindings/core/v8/V8GCController.h" |
10 #include "core/dom/CompositorProxyClient.h" | 10 #include "core/dom/CompositorProxyClient.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 { | 97 { |
98 CompositorWorkerThread::createSharedBackingThreadForTest(); | 98 CompositorWorkerThread::createSharedBackingThreadForTest(); |
99 m_page = DummyPageHolder::create(); | 99 m_page = DummyPageHolder::create(); |
100 m_objectProxy = TestCompositorWorkerObjectProxy::create(&m_page->documen
t()); | 100 m_objectProxy = TestCompositorWorkerObjectProxy::create(&m_page->documen
t()); |
101 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://
fake.url/")); | 101 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://
fake.url/")); |
102 } | 102 } |
103 | 103 |
104 void TearDown() override | 104 void TearDown() override |
105 { | 105 { |
106 m_page.reset(); | 106 m_page.reset(); |
107 CompositorWorkerThread::terminateExecution(); | |
108 CompositorWorkerThread::clearSharedBackingThread(); | 107 CompositorWorkerThread::clearSharedBackingThread(); |
109 } | 108 } |
110 | 109 |
111 PassOwnPtr<CompositorWorkerThread> createCompositorWorker() | 110 PassOwnPtr<CompositorWorkerThread> createCompositorWorker() |
112 { | 111 { |
113 OwnPtr<CompositorWorkerThread> workerThread = CompositorWorkerThread::cr
eate(nullptr, *m_objectProxy, 0); | 112 OwnPtr<CompositorWorkerThread> workerThread = CompositorWorkerThread::cr
eate(nullptr, *m_objectProxy, 0); |
114 WorkerClients* clients = WorkerClients::create(); | 113 WorkerClients* clients = WorkerClients::create(); |
115 provideCompositorProxyClientTo(clients, new TestCompositorProxyClient); | 114 provideCompositorProxyClientTo(clients, new TestCompositorProxyClient); |
116 workerThread->start(WorkerThreadStartupData::create( | 115 workerThread->start(WorkerThreadStartupData::create( |
117 KURL(ParsedURLString, "http://fake.url/"), | 116 KURL(ParsedURLString, "http://fake.url/"), |
(...skipping 15 matching lines...) Expand all Loading... |
133 { | 132 { |
134 OwnPtr<WaitableEvent> waitEvent = adoptPtr(new WaitableEvent()); | 133 OwnPtr<WaitableEvent> waitEvent = adoptPtr(new WaitableEvent()); |
135 worker->workerBackingThread().backingThread().postTask(BLINK_FROM_HERE,
threadSafeBind(&CompositorWorkerThreadTest::executeScriptInWorker, AllowCrossThr
eadAccess(this), | 134 worker->workerBackingThread().backingThread().postTask(BLINK_FROM_HERE,
threadSafeBind(&CompositorWorkerThreadTest::executeScriptInWorker, AllowCrossThr
eadAccess(this), |
136 AllowCrossThreadAccess(worker), AllowCrossThreadAccess(waitEvent.get
()))); | 135 AllowCrossThreadAccess(worker), AllowCrossThreadAccess(waitEvent.get
()))); |
137 waitEvent->wait(); | 136 waitEvent->wait(); |
138 } | 137 } |
139 | 138 |
140 private: | 139 private: |
141 void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) | 140 void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) |
142 { | 141 { |
143 EXPECT_GT(worker->workerBackingThread().workerScriptCount(), 0u); | |
144 WorkerOrWorkletScriptController* scriptController = worker->workerGlobal
Scope()->scriptController(); | 142 WorkerOrWorkletScriptController* scriptController = worker->workerGlobal
Scope()->scriptController(); |
145 bool evaluateResult = scriptController->evaluate(ScriptSourceCode("var c
ounter = 0; ++counter;")); | 143 bool evaluateResult = scriptController->evaluate(ScriptSourceCode("var c
ounter = 0; ++counter;")); |
146 ASSERT_UNUSED(evaluateResult, evaluateResult); | 144 ASSERT_UNUSED(evaluateResult, evaluateResult); |
147 waitEvent->signal(); | 145 waitEvent->signal(); |
148 } | 146 } |
149 | 147 |
150 OwnPtr<DummyPageHolder> m_page; | 148 OwnPtr<DummyPageHolder> m_page; |
151 RefPtr<SecurityOrigin> m_securityOrigin; | 149 RefPtr<SecurityOrigin> m_securityOrigin; |
152 OwnPtr<InProcessWorkerObjectProxy> m_objectProxy; | 150 OwnPtr<InProcessWorkerObjectProxy> m_objectProxy; |
153 CompositorWorkerTestPlatform m_testPlatform; | 151 CompositorWorkerTestPlatform m_testPlatform; |
(...skipping 11 matching lines...) Expand all Loading... |
165 { | 163 { |
166 // Create the first worker and wait until it is initialized. | 164 // Create the first worker and wait until it is initialized. |
167 OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); | 165 OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); |
168 WebThreadSupportingGC* firstThread = &firstWorker->workerBackingThread().bac
kingThread(); | 166 WebThreadSupportingGC* firstThread = &firstWorker->workerBackingThread().bac
kingThread(); |
169 checkWorkerCanExecuteScript(firstWorker.get()); | 167 checkWorkerCanExecuteScript(firstWorker.get()); |
170 v8::Isolate* firstIsolate = firstWorker->isolate(); | 168 v8::Isolate* firstIsolate = firstWorker->isolate(); |
171 ASSERT_TRUE(firstIsolate); | 169 ASSERT_TRUE(firstIsolate); |
172 | 170 |
173 // Create the second worker and immediately destroy the first worker. | 171 // Create the second worker and immediately destroy the first worker. |
174 OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); | 172 OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); |
175 firstWorker->terminateAndWait(); | 173 // We don't use terminateAndWait here to avoid forcible termination. |
| 174 firstWorker->terminate(); |
| 175 firstWorker->waitForShutdownForTesting(); |
176 | 176 |
177 // Wait until the second worker is initialized. Verify that the second worke
r is using the same | 177 // Wait until the second worker is initialized. Verify that the second worke
r is using the same |
178 // thread and Isolate as the first worker. | 178 // thread and Isolate as the first worker. |
179 WebThreadSupportingGC* secondThread = &secondWorker->workerBackingThread().b
ackingThread(); | 179 WebThreadSupportingGC* secondThread = &secondWorker->workerBackingThread().b
ackingThread(); |
180 ASSERT_EQ(firstThread, secondThread); | 180 ASSERT_EQ(firstThread, secondThread); |
181 | 181 |
182 v8::Isolate* secondIsolate = secondWorker->isolate(); | 182 v8::Isolate* secondIsolate = secondWorker->isolate(); |
183 ASSERT_TRUE(secondIsolate); | 183 ASSERT_TRUE(secondIsolate); |
184 EXPECT_EQ(firstIsolate, secondIsolate); | 184 EXPECT_EQ(firstIsolate, secondIsolate); |
185 | 185 |
186 // Verify that the worker can still successfully execute script. | 186 // Verify that the worker can still successfully execute script. |
187 checkWorkerCanExecuteScript(secondWorker.get()); | 187 checkWorkerCanExecuteScript(secondWorker.get()); |
188 | 188 |
189 secondWorker->terminateAndWait(); | 189 secondWorker->terminateAndWait(); |
190 } | 190 } |
191 | 191 |
192 // Tests that a new WebThread is created if all existing workers are terminated
before a new worker is created. | 192 // Tests that a new WebThread is created if all existing workers are terminated
before a new worker is created. |
193 TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) | 193 TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) |
194 { | 194 { |
195 // Create the first worker, wait until it is initialized, and terminate it. | 195 // Create the first worker, wait until it is initialized, and terminate it. |
196 OwnPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(); | 196 OwnPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(); |
197 WorkerBackingThread* workerBackingThread = &compositorWorker->workerBackingT
hread(); | |
198 WebThreadSupportingGC* firstThread = &compositorWorker->workerBackingThread(
).backingThread(); | 197 WebThreadSupportingGC* firstThread = &compositorWorker->workerBackingThread(
).backingThread(); |
199 checkWorkerCanExecuteScript(compositorWorker.get()); | 198 checkWorkerCanExecuteScript(compositorWorker.get()); |
200 | 199 |
201 ASSERT_EQ(2u, workerBackingThread->workerScriptCount()); | 200 // We don't use terminateAndWait here to avoid forcible termination. |
202 compositorWorker->terminateAndWait(); | 201 compositorWorker->terminate(); |
203 | 202 compositorWorker->waitForShutdownForTesting(); |
204 ASSERT_EQ(1u, workerBackingThread->workerScriptCount()); | |
205 | 203 |
206 // Create the second worker. The backing thread is same. | 204 // Create the second worker. The backing thread is same. |
207 compositorWorker = createCompositorWorker(); | 205 compositorWorker = createCompositorWorker(); |
208 WebThreadSupportingGC* secondThread = &compositorWorker->workerBackingThread
().backingThread(); | 206 WebThreadSupportingGC* secondThread = &compositorWorker->workerBackingThread
().backingThread(); |
209 EXPECT_EQ(firstThread, secondThread); | 207 EXPECT_EQ(firstThread, secondThread); |
210 checkWorkerCanExecuteScript(compositorWorker.get()); | 208 checkWorkerCanExecuteScript(compositorWorker.get()); |
211 ASSERT_EQ(2u, workerBackingThread->workerScriptCount()); | |
212 | 209 |
213 compositorWorker->terminateAndWait(); | 210 compositorWorker->terminateAndWait(); |
214 } | 211 } |
215 | 212 |
216 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is crea
ted while another is terminating. | 213 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is crea
ted while another is terminating. |
217 TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) | 214 TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) |
218 { | 215 { |
219 OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); | 216 OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); |
220 checkWorkerCanExecuteScript(firstWorker.get()); | 217 checkWorkerCanExecuteScript(firstWorker.get()); |
221 v8::Isolate* firstIsolate = firstWorker->isolate(); | 218 v8::Isolate* firstIsolate = firstWorker->isolate(); |
222 ASSERT_TRUE(firstIsolate); | 219 ASSERT_TRUE(firstIsolate); |
223 | 220 |
224 // Request termination of the first worker and create the second worker | 221 // Request termination of the first worker and create the second worker |
225 // as soon as possible. | 222 // as soon as possible. |
226 EXPECT_EQ(2u, firstWorker->workerBackingThread().workerScriptCount()); | |
227 firstWorker->terminate(); | 223 firstWorker->terminate(); |
228 // We don't wait for its termination. | 224 // We don't wait for its termination. |
229 // Note: We rely on the assumption that the termination steps don't run | 225 // Note: We rely on the assumption that the termination steps don't run |
230 // on the worker thread so quickly. This could be a source of flakiness. | 226 // on the worker thread so quickly. This could be a source of flakiness. |
231 | 227 |
232 OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); | 228 OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); |
233 | 229 |
234 v8::Isolate* secondIsolate = secondWorker->isolate(); | 230 v8::Isolate* secondIsolate = secondWorker->isolate(); |
235 ASSERT_TRUE(secondIsolate); | 231 ASSERT_TRUE(secondIsolate); |
236 EXPECT_EQ(firstIsolate, secondIsolate); | 232 EXPECT_EQ(firstIsolate, secondIsolate); |
237 | 233 |
238 // Verify that the isolate can run some scripts correctly in the second work
er. | 234 // Verify that the isolate can run some scripts correctly in the second work
er. |
239 checkWorkerCanExecuteScript(secondWorker.get()); | 235 checkWorkerCanExecuteScript(secondWorker.get()); |
240 secondWorker->terminateAndWait(); | 236 secondWorker->terminateAndWait(); |
241 } | 237 } |
242 | 238 |
243 } // namespace blink | 239 } // namespace blink |
OLD | NEW |