Chromium Code Reviews| Index: third_party/WebKit/Source/modules/compositorworker/CompositorWorkerThreadTest.cpp |
| diff --git a/third_party/WebKit/Source/modules/compositorworker/CompositorWorkerThreadTest.cpp b/third_party/WebKit/Source/modules/compositorworker/CompositorWorkerThreadTest.cpp |
| index e3b27ed360a17dcc3ceb7ceff630fce6c5fd00ea..2b80ac0fd40bef0aa432a888b0a4ec5fe9d60b01 100644 |
| --- a/third_party/WebKit/Source/modules/compositorworker/CompositorWorkerThreadTest.cpp |
| +++ b/third_party/WebKit/Source/modules/compositorworker/CompositorWorkerThreadTest.cpp |
| @@ -8,6 +8,7 @@ |
| #include "bindings/core/v8/V8GCController.h" |
| #include "core/inspector/ConsoleMessage.h" |
| #include "core/testing/DummyPageHolder.h" |
| +#include "core/workers/WorkerBackingThread.h" |
| #include "core/workers/WorkerLoaderProxy.h" |
| #include "core/workers/WorkerObjectProxy.h" |
| #include "core/workers/WorkerThreadStartupData.h" |
| @@ -24,48 +25,6 @@ |
| namespace blink { |
| namespace { |
| -class TestCompositorWorkerThread : public CompositorWorkerThread { |
| -public: |
| - TestCompositorWorkerThread(WorkerLoaderProxyProvider* loaderProxyProvider, WorkerObjectProxy& objectProxy, double timeOrigin, WaitableEvent* startEvent) |
| - : CompositorWorkerThread(WorkerLoaderProxy::create(loaderProxyProvider), objectProxy, timeOrigin) |
| - , m_startEvent(startEvent) |
| - { |
| - } |
| - |
| - ~TestCompositorWorkerThread() override {} |
| - |
| - void setCallbackAfterV8Termination(PassOwnPtr<Function<void()>> callback) |
| - { |
| - m_v8TerminationCallback = callback; |
| - } |
| - |
| -private: |
| - // WorkerThread: |
| - void didStartWorkerThread() override |
| - { |
| - m_startEvent->signal(); |
| - } |
| - |
| - void terminateV8Execution() override |
| - { |
| - // This could be called on worker thread, but not in the test. |
| - ASSERT(isMainThread()); |
| - CompositorWorkerThread::terminateV8Execution(); |
| - if (m_v8TerminationCallback) |
| - (*m_v8TerminationCallback)(); |
| - } |
| - |
| - void willDestroyIsolate() override |
| - { |
| - v8::Isolate::GetCurrent()->RequestGarbageCollectionForTesting(v8::Isolate::kFullGarbageCollection); |
| - Heap::collectAllGarbage(); |
| - CompositorWorkerThread::willDestroyIsolate(); |
| - } |
| - |
| - WaitableEvent* m_startEvent; |
| - OwnPtr<Function<void()>> m_v8TerminationCallback; |
| -}; |
| - |
| // A null WorkerObjectProxy, supplied when creating CompositorWorkerThreads. |
| class TestCompositorWorkerObjectProxy : public WorkerObjectProxy { |
| public: |
| @@ -123,6 +82,7 @@ class CompositorWorkerThreadTest : public ::testing::Test { |
| public: |
| void SetUp() override |
| { |
| + CompositorWorkerThread::resetSharedBackingThreadForTest(); |
| m_page = DummyPageHolder::create(); |
| m_objectProxy = TestCompositorWorkerObjectProxy::create(&m_page->document()); |
| m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://fake.url/")); |
| @@ -130,14 +90,13 @@ public: |
| void TearDown() override |
| { |
| - ASSERT(!hasThread()); |
| - ASSERT(!hasIsolate()); |
| m_page.clear(); |
| + CompositorWorkerThread::resetSharedBackingThreadForTest(); |
| } |
| - PassOwnPtr<TestCompositorWorkerThread> createCompositorWorker(WaitableEvent* startEvent) |
| + PassOwnPtr<CompositorWorkerThread> createCompositorWorker() |
| { |
| - TestCompositorWorkerThread* workerThread = new TestCompositorWorkerThread(nullptr, *m_objectProxy, 0, startEvent); |
| + OwnPtr<CompositorWorkerThread> workerThread = CompositorWorkerThread::create(nullptr, *m_objectProxy, 0); |
| OwnPtrWillBeRawPtr<WorkerClients> clients = nullptr; |
| workerThread->start(WorkerThreadStartupData::create( |
| KURL(ParsedURLString, "http://fake.url/"), |
| @@ -150,42 +109,22 @@ public: |
| clients.release(), |
| WebAddressSpaceLocal, |
| V8CacheOptionsDefault)); |
| - return adoptPtr(workerThread); |
| - } |
| - |
| - void createWorkerAdapter(OwnPtr<CompositorWorkerThread>* workerThread, WaitableEvent* creationEvent) |
| - { |
| - *workerThread = createCompositorWorker(creationEvent); |
| + return workerThread.release(); |
| } |
| // Attempts to run some simple script for |worker|. |
| void checkWorkerCanExecuteScript(WorkerThread* worker) |
| { |
| OwnPtr<WaitableEvent> waitEvent = adoptPtr(new WaitableEvent()); |
| - worker->backingThread().platformThread().getWebTaskRunner()->postTask(BLINK_FROM_HERE, threadSafeBind(&CompositorWorkerThreadTest::executeScriptInWorker, AllowCrossThreadAccess(this), |
| + worker->workerBackingThread().backingThread().postTask(BLINK_FROM_HERE, threadSafeBind(&CompositorWorkerThreadTest::executeScriptInWorker, AllowCrossThreadAccess(this), |
| AllowCrossThreadAccess(worker), AllowCrossThreadAccess(waitEvent.get()))); |
| waitEvent->wait(); |
| } |
| - void waitForWaitableEventAfterIteratingCurrentLoop(WaitableEvent* waitEvent) |
| - { |
| - testing::runPendingTasks(); |
| - waitEvent->wait(); |
| - } |
| - |
| - bool hasThread() const |
| - { |
| - return CompositorWorkerThread::hasThreadForTest(); |
| - } |
| - |
| - bool hasIsolate() const |
| - { |
| - return CompositorWorkerThread::hasIsolateForTest(); |
| - } |
| - |
| private: |
| void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) |
| { |
| + EXPECT_GT(worker->workerBackingThread().workerScriptCount(), 0u); |
| WorkerOrWorkletScriptController* scriptController = worker->workerGlobalScope()->scriptController(); |
| bool evaluateResult = scriptController->evaluate(ScriptSourceCode("var counter = 0; ++counter;")); |
| ASSERT_UNUSED(evaluateResult, evaluateResult); |
| @@ -200,9 +139,7 @@ private: |
| TEST_F(CompositorWorkerThreadTest, Basic) |
| { |
| - OwnPtr<WaitableEvent> creationEvent = adoptPtr(new WaitableEvent()); |
| - OwnPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(creationEvent.get()); |
| - waitForWaitableEventAfterIteratingCurrentLoop(creationEvent.get()); |
| + OwnPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(); |
| checkWorkerCanExecuteScript(compositorWorker.get()); |
| compositorWorker->terminateAndWait(); |
| } |
| @@ -211,28 +148,23 @@ TEST_F(CompositorWorkerThreadTest, Basic) |
| TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) |
| { |
| // Create the first worker and wait until it is initialized. |
| - OwnPtr<WaitableEvent> firstCreationEvent = adoptPtr(new WaitableEvent()); |
| - OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(firstCreationEvent.get()); |
| - WebThreadSupportingGC* firstThread = CompositorWorkerThread::sharedBackingThread(); |
| - ASSERT(firstThread); |
| - waitForWaitableEventAfterIteratingCurrentLoop(firstCreationEvent.get()); |
| + OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); |
| + WebThreadSupportingGC* firstThread = &firstWorker->workerBackingThread().backingThread(); |
| + checkWorkerCanExecuteScript(firstWorker.get()); |
| v8::Isolate* firstIsolate = firstWorker->isolate(); |
| - ASSERT(firstIsolate); |
| + ASSERT_TRUE(firstIsolate); |
| // Create the second worker and immediately destroy the first worker. |
| - OwnPtr<WaitableEvent> secondCreationEvent = adoptPtr(new WaitableEvent()); |
| - OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(secondCreationEvent.get()); |
| + OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); |
| firstWorker->terminateAndWait(); |
| // Wait until the second worker is initialized. Verify that the second worker is using the same |
| // thread and Isolate as the first worker. |
| - WebThreadSupportingGC* secondThread = CompositorWorkerThread::sharedBackingThread(); |
| - ASSERT(secondThread); |
| - waitForWaitableEventAfterIteratingCurrentLoop(secondCreationEvent.get()); |
| - EXPECT_EQ(firstThread, secondThread); |
| + WebThreadSupportingGC* secondThread = &secondWorker->workerBackingThread().backingThread(); |
| + ASSERT_EQ(firstThread, secondThread); |
| v8::Isolate* secondIsolate = secondWorker->isolate(); |
| - ASSERT(secondIsolate); |
| + ASSERT_TRUE(secondIsolate); |
| EXPECT_EQ(firstIsolate, secondIsolate); |
| // Verify that the worker can still successfully execute script. |
| @@ -241,36 +173,26 @@ TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) |
| secondWorker->terminateAndWait(); |
| } |
| -static void checkCurrentIsolate(v8::Isolate* isolate, WaitableEvent* event) |
| -{ |
| - EXPECT_EQ(v8::Isolate::GetCurrent(), isolate); |
| - event->signal(); |
| -} |
| - |
| // Tests that a new WebThread is created if all existing workers are terminated before a new worker is created. |
|
Ian Vollick
2016/03/26 14:13:26
This comment no longer reflects what the test does
|
| TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) |
| { |
| // Create the first worker, wait until it is initialized, and terminate it. |
| - OwnPtr<WaitableEvent> creationEvent = adoptPtr(new WaitableEvent()); |
| - OwnPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(creationEvent.get()); |
| - WebThreadSupportingGC* firstThread = CompositorWorkerThread::sharedBackingThread(); |
| - waitForWaitableEventAfterIteratingCurrentLoop(creationEvent.get()); |
| - ASSERT(compositorWorker->isolate()); |
| + OwnPtr<CompositorWorkerThread> compositorWorker = createCompositorWorker(); |
| + RefPtr<WorkerBackingThread> workerBackingThread = &compositorWorker->workerBackingThread(); |
| + WebThreadSupportingGC* firstThread = &compositorWorker->workerBackingThread().backingThread(); |
| + checkWorkerCanExecuteScript(compositorWorker.get()); |
| + |
| + ASSERT_EQ(1u, workerBackingThread->workerScriptCount()); |
| compositorWorker->terminateAndWait(); |
| - // Create the second worker. Verify that the second worker lives in a different WebThread since the first |
| - // thread will have been destroyed after destroying the first worker. |
| - creationEvent = adoptPtr(new WaitableEvent()); |
| - compositorWorker = createCompositorWorker(creationEvent.get()); |
| - WebThreadSupportingGC* secondThread = CompositorWorkerThread::sharedBackingThread(); |
| - EXPECT_NE(firstThread, secondThread); |
| - waitForWaitableEventAfterIteratingCurrentLoop(creationEvent.get()); |
| - |
| - // Jump over to the worker's thread to verify that the Isolate is set up correctly and execute script. |
| - OwnPtr<WaitableEvent> checkEvent = adoptPtr(new WaitableEvent()); |
| - secondThread->platformThread().getWebTaskRunner()->postTask(BLINK_FROM_HERE, threadSafeBind(&checkCurrentIsolate, AllowCrossThreadAccess(compositorWorker->isolate()), AllowCrossThreadAccess(checkEvent.get()))); |
| - waitForWaitableEventAfterIteratingCurrentLoop(checkEvent.get()); |
| + ASSERT_EQ(0u, workerBackingThread->workerScriptCount()); |
| + |
| + // Create the second worker. The backing thread is same. |
| + compositorWorker = createCompositorWorker(); |
| + WebThreadSupportingGC* secondThread = &compositorWorker->workerBackingThread().backingThread(); |
| + EXPECT_EQ(firstThread, secondThread); |
| checkWorkerCanExecuteScript(compositorWorker.get()); |
| + ASSERT_EQ(1u, workerBackingThread->workerScriptCount()); |
| compositorWorker->terminateAndWait(); |
| } |
| @@ -278,23 +200,23 @@ TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) |
| // Tests that v8::Isolate and WebThread are correctly set-up if a worker is created while another is terminating. |
| TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) |
| { |
| - OwnPtr<WaitableEvent> firstCreationEvent = adoptPtr(new WaitableEvent()); |
| - OwnPtr<TestCompositorWorkerThread> firstWorker = createCompositorWorker(firstCreationEvent.get()); |
| - waitForWaitableEventAfterIteratingCurrentLoop(firstCreationEvent.get()); |
| + OwnPtr<CompositorWorkerThread> firstWorker = createCompositorWorker(); |
| + checkWorkerCanExecuteScript(firstWorker.get()); |
| v8::Isolate* firstIsolate = firstWorker->isolate(); |
| - ASSERT(firstIsolate); |
| + ASSERT_TRUE(firstIsolate); |
| - // Request termination of the first worker, and set-up to make sure the second worker is created right as |
| - // the first worker terminates its isolate. |
| - OwnPtr<WaitableEvent> secondCreationEvent = adoptPtr(new WaitableEvent()); |
| - OwnPtr<CompositorWorkerThread> secondWorker; |
| - firstWorker->setCallbackAfterV8Termination(bind(&CompositorWorkerThreadTest::createWorkerAdapter, this, &secondWorker, secondCreationEvent.get())); |
| - firstWorker->terminateAndWait(); |
| - ASSERT(secondWorker); |
| + // Request termination of the first worker and create the second worker |
| + // as soon as possible. |
| + EXPECT_EQ(1u, firstWorker->workerBackingThread().workerScriptCount()); |
| + firstWorker->terminate(); |
| + // We don't wait for its termination. |
| + // Note: We rely on the assumption that the termination steps don't run |
| + // on the worker thread so quickly. This could be a source of flakiness. |
| + |
| + OwnPtr<CompositorWorkerThread> secondWorker = createCompositorWorker(); |
| - waitForWaitableEventAfterIteratingCurrentLoop(secondCreationEvent.get()); |
| v8::Isolate* secondIsolate = secondWorker->isolate(); |
| - ASSERT(secondIsolate); |
| + ASSERT_TRUE(secondIsolate); |
| EXPECT_EQ(firstIsolate, secondIsolate); |
| // Verify that the isolate can run some scripts correctly in the second worker. |