| 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 "bindings/core/v8/WorkerOrWorkletScriptController.h" | 10 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "platform/testing/UnitTestHelpers.h" | 24 #include "platform/testing/UnitTestHelpers.h" |
| 25 #include "public/platform/Platform.h" | 25 #include "public/platform/Platform.h" |
| 26 #include "public/platform/WebAddressSpace.h" | 26 #include "public/platform/WebAddressSpace.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "wtf/PtrUtil.h" | 28 #include "wtf/PtrUtil.h" |
| 29 #include <memory> | 29 #include <memory> |
| 30 | 30 |
| 31 namespace blink { | 31 namespace blink { |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // A null InProcessWorkerObjectProxy, supplied when creating CompositorWorkerThr
eads. | 34 // A null InProcessWorkerObjectProxy, supplied when creating |
| 35 // CompositorWorkerThreads. |
| 35 class TestCompositorWorkerObjectProxy : public InProcessWorkerObjectProxy { | 36 class TestCompositorWorkerObjectProxy : public InProcessWorkerObjectProxy { |
| 36 public: | 37 public: |
| 37 static std::unique_ptr<TestCompositorWorkerObjectProxy> create( | 38 static std::unique_ptr<TestCompositorWorkerObjectProxy> create( |
| 38 ExecutionContext* context) { | 39 ExecutionContext* context) { |
| 39 return wrapUnique(new TestCompositorWorkerObjectProxy(context)); | 40 return wrapUnique(new TestCompositorWorkerObjectProxy(context)); |
| 40 } | 41 } |
| 41 | 42 |
| 42 // (Empty) WorkerReportingProxy implementation: | 43 // (Empty) WorkerReportingProxy implementation: |
| 43 virtual void dispatchErrorEvent(const String& errorMessage, | 44 virtual void dispatchErrorEvent(const String& errorMessage, |
| 44 std::unique_ptr<SourceLocation>, | 45 std::unique_ptr<SourceLocation>, |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 CompositorWorkerTestPlatform m_testPlatform; | 157 CompositorWorkerTestPlatform m_testPlatform; |
| 157 }; | 158 }; |
| 158 | 159 |
| 159 TEST_F(CompositorWorkerThreadTest, Basic) { | 160 TEST_F(CompositorWorkerThreadTest, Basic) { |
| 160 std::unique_ptr<CompositorWorkerThread> compositorWorker = | 161 std::unique_ptr<CompositorWorkerThread> compositorWorker = |
| 161 createCompositorWorker(); | 162 createCompositorWorker(); |
| 162 checkWorkerCanExecuteScript(compositorWorker.get()); | 163 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 163 compositorWorker->terminateAndWait(); | 164 compositorWorker->terminateAndWait(); |
| 164 } | 165 } |
| 165 | 166 |
| 166 // Tests that the same WebThread is used for new workers if the WebThread is sti
ll alive. | 167 // Tests that the same WebThread is used for new workers if the WebThread is |
| 168 // still alive. |
| 167 TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) { | 169 TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) { |
| 168 // Create the first worker and wait until it is initialized. | 170 // Create the first worker and wait until it is initialized. |
| 169 std::unique_ptr<CompositorWorkerThread> firstWorker = | 171 std::unique_ptr<CompositorWorkerThread> firstWorker = |
| 170 createCompositorWorker(); | 172 createCompositorWorker(); |
| 171 WebThreadSupportingGC* firstThread = | 173 WebThreadSupportingGC* firstThread = |
| 172 &firstWorker->workerBackingThread().backingThread(); | 174 &firstWorker->workerBackingThread().backingThread(); |
| 173 checkWorkerCanExecuteScript(firstWorker.get()); | 175 checkWorkerCanExecuteScript(firstWorker.get()); |
| 174 v8::Isolate* firstIsolate = firstWorker->isolate(); | 176 v8::Isolate* firstIsolate = firstWorker->isolate(); |
| 175 ASSERT_TRUE(firstIsolate); | 177 ASSERT_TRUE(firstIsolate); |
| 176 | 178 |
| 177 // Create the second worker and immediately destroy the first worker. | 179 // Create the second worker and immediately destroy the first worker. |
| 178 std::unique_ptr<CompositorWorkerThread> secondWorker = | 180 std::unique_ptr<CompositorWorkerThread> secondWorker = |
| 179 createCompositorWorker(); | 181 createCompositorWorker(); |
| 180 // We don't use terminateAndWait here to avoid forcible termination. | 182 // We don't use terminateAndWait here to avoid forcible termination. |
| 181 firstWorker->terminate(); | 183 firstWorker->terminate(); |
| 182 firstWorker->waitForShutdownForTesting(); | 184 firstWorker->waitForShutdownForTesting(); |
| 183 | 185 |
| 184 // Wait until the second worker is initialized. Verify that the second worker
is using the same | 186 // Wait until the second worker is initialized. Verify that the second worker |
| 185 // thread and Isolate as the first worker. | 187 // is using the same thread and Isolate as the first worker. |
| 186 WebThreadSupportingGC* secondThread = | 188 WebThreadSupportingGC* secondThread = |
| 187 &secondWorker->workerBackingThread().backingThread(); | 189 &secondWorker->workerBackingThread().backingThread(); |
| 188 ASSERT_EQ(firstThread, secondThread); | 190 ASSERT_EQ(firstThread, secondThread); |
| 189 | 191 |
| 190 v8::Isolate* secondIsolate = secondWorker->isolate(); | 192 v8::Isolate* secondIsolate = secondWorker->isolate(); |
| 191 ASSERT_TRUE(secondIsolate); | 193 ASSERT_TRUE(secondIsolate); |
| 192 EXPECT_EQ(firstIsolate, secondIsolate); | 194 EXPECT_EQ(firstIsolate, secondIsolate); |
| 193 | 195 |
| 194 // Verify that the worker can still successfully execute script. | 196 // Verify that the worker can still successfully execute script. |
| 195 checkWorkerCanExecuteScript(secondWorker.get()); | 197 checkWorkerCanExecuteScript(secondWorker.get()); |
| 196 | 198 |
| 197 secondWorker->terminateAndWait(); | 199 secondWorker->terminateAndWait(); |
| 198 } | 200 } |
| 199 | 201 |
| 200 // Tests that a new WebThread is created if all existing workers are terminated
before a new worker is created. | 202 // Tests that a new WebThread is created if all existing workers are terminated |
| 203 // before a new worker is created. |
| 201 TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) { | 204 TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) { |
| 202 // Create the first worker, wait until it is initialized, and terminate it. | 205 // Create the first worker, wait until it is initialized, and terminate it. |
| 203 std::unique_ptr<CompositorWorkerThread> compositorWorker = | 206 std::unique_ptr<CompositorWorkerThread> compositorWorker = |
| 204 createCompositorWorker(); | 207 createCompositorWorker(); |
| 205 WebThreadSupportingGC* firstThread = | 208 WebThreadSupportingGC* firstThread = |
| 206 &compositorWorker->workerBackingThread().backingThread(); | 209 &compositorWorker->workerBackingThread().backingThread(); |
| 207 checkWorkerCanExecuteScript(compositorWorker.get()); | 210 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 208 | 211 |
| 209 // We don't use terminateAndWait here to avoid forcible termination. | 212 // We don't use terminateAndWait here to avoid forcible termination. |
| 210 compositorWorker->terminate(); | 213 compositorWorker->terminate(); |
| 211 compositorWorker->waitForShutdownForTesting(); | 214 compositorWorker->waitForShutdownForTesting(); |
| 212 | 215 |
| 213 // Create the second worker. The backing thread is same. | 216 // Create the second worker. The backing thread is same. |
| 214 compositorWorker = createCompositorWorker(); | 217 compositorWorker = createCompositorWorker(); |
| 215 WebThreadSupportingGC* secondThread = | 218 WebThreadSupportingGC* secondThread = |
| 216 &compositorWorker->workerBackingThread().backingThread(); | 219 &compositorWorker->workerBackingThread().backingThread(); |
| 217 EXPECT_EQ(firstThread, secondThread); | 220 EXPECT_EQ(firstThread, secondThread); |
| 218 checkWorkerCanExecuteScript(compositorWorker.get()); | 221 checkWorkerCanExecuteScript(compositorWorker.get()); |
| 219 | 222 |
| 220 compositorWorker->terminateAndWait(); | 223 compositorWorker->terminateAndWait(); |
| 221 } | 224 } |
| 222 | 225 |
| 223 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is crea
ted while another is terminating. | 226 // Tests that v8::Isolate and WebThread are correctly set-up if a worker is |
| 227 // created while another is terminating. |
| 224 TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) { | 228 TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) { |
| 225 std::unique_ptr<CompositorWorkerThread> firstWorker = | 229 std::unique_ptr<CompositorWorkerThread> firstWorker = |
| 226 createCompositorWorker(); | 230 createCompositorWorker(); |
| 227 checkWorkerCanExecuteScript(firstWorker.get()); | 231 checkWorkerCanExecuteScript(firstWorker.get()); |
| 228 v8::Isolate* firstIsolate = firstWorker->isolate(); | 232 v8::Isolate* firstIsolate = firstWorker->isolate(); |
| 229 ASSERT_TRUE(firstIsolate); | 233 ASSERT_TRUE(firstIsolate); |
| 230 | 234 |
| 231 // Request termination of the first worker and create the second worker | 235 // Request termination of the first worker and create the second worker |
| 232 // as soon as possible. | 236 // as soon as possible. |
| 233 firstWorker->terminate(); | 237 firstWorker->terminate(); |
| 234 // We don't wait for its termination. | 238 // We don't wait for its termination. |
| 235 // Note: We rely on the assumption that the termination steps don't run | 239 // Note: We rely on the assumption that the termination steps don't run |
| 236 // on the worker thread so quickly. This could be a source of flakiness. | 240 // on the worker thread so quickly. This could be a source of flakiness. |
| 237 | 241 |
| 238 std::unique_ptr<CompositorWorkerThread> secondWorker = | 242 std::unique_ptr<CompositorWorkerThread> secondWorker = |
| 239 createCompositorWorker(); | 243 createCompositorWorker(); |
| 240 | 244 |
| 241 v8::Isolate* secondIsolate = secondWorker->isolate(); | 245 v8::Isolate* secondIsolate = secondWorker->isolate(); |
| 242 ASSERT_TRUE(secondIsolate); | 246 ASSERT_TRUE(secondIsolate); |
| 243 EXPECT_EQ(firstIsolate, secondIsolate); | 247 EXPECT_EQ(firstIsolate, secondIsolate); |
| 244 | 248 |
| 245 // Verify that the isolate can run some scripts correctly in the second worker
. | 249 // Verify that the isolate can run some scripts correctly in the second |
| 250 // worker. |
| 246 checkWorkerCanExecuteScript(secondWorker.get()); | 251 checkWorkerCanExecuteScript(secondWorker.get()); |
| 247 secondWorker->terminateAndWait(); | 252 secondWorker->terminateAndWait(); |
| 248 } | 253 } |
| 249 | 254 |
| 250 } // namespace blink | 255 } // namespace blink |
| OLD | NEW |