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 32771eeb611e7d47d643709f3d59bb5e2e765fc4..534df2fa5eaa15d35f0bc0b47d5775489146ebd4 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" |
@@ -23,48 +24,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: |
@@ -122,6 +81,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/")); |
@@ -129,14 +89,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); |
WorkerClients* clients = nullptr; |
workerThread->start(WorkerThreadStartupData::create( |
KURL(ParsedURLString, "http://fake.url/"), |
@@ -149,42 +108,22 @@ public: |
clients, |
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); |
@@ -199,9 +138,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(); |
} |
@@ -210,28 +147,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. |
@@ -240,36 +172,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. |
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(); |
+ 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(); |
} |
@@ -277,23 +199,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. |