Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(410)

Unified Diff: third_party/WebKit/Source/core/workers/WorkerThreadTest.cpp

Issue 2262023002: Worker: Wait for termination by WorkerThreadLifecycleObserver instead of WaitableEvent (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address review comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/core/workers/WorkerThreadLifecycleObserver.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/core/workers/WorkerThreadTest.cpp
diff --git a/third_party/WebKit/Source/core/workers/WorkerThreadTest.cpp b/third_party/WebKit/Source/core/workers/WorkerThreadTest.cpp
index 2f6153ee108472d18422a58fd74dbaee6a86cacc..958689e69f9de18a2a8db2ae53181624a38e7e8b 100644
--- a/third_party/WebKit/Source/core/workers/WorkerThreadTest.cpp
+++ b/third_party/WebKit/Source/core/workers/WorkerThreadTest.cpp
@@ -5,6 +5,7 @@
#include "core/workers/WorkerThread.h"
#include "core/workers/WorkerThreadTestHelper.h"
+#include "platform/WaitableEvent.h"
#include "platform/testing/UnitTestHelpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -20,17 +21,43 @@ namespace {
// Called from WorkerThread::startRunningDebuggerTasksOnPauseOnWorkerThread as a
// debugger task.
-void waitForTermination(WorkerThread* workerThread)
+void waitForTermination(WorkerThread* workerThread, WaitableEvent* waitableEvent)
{
EXPECT_TRUE(workerThread->isCurrentThread());
// Notify the main thread that the debugger task is waiting for termination.
Platform::current()->mainThread()->getWebTaskRunner()->postTask(BLINK_FROM_HERE, crossThreadBind(&testing::exitRunLoop));
- workerThread->terminationEvent()->wait();
+ waitableEvent->wait();
}
} // namespace
+class WorkerThreadLifecycleObserverForTest final : public GarbageCollectedFinalized<WorkerThreadLifecycleObserverForTest>, public WorkerThreadLifecycleObserver {
+ USING_GARBAGE_COLLECTED_MIXIN(WorkerThreadLifecycleObserverForTest);
+ WTF_MAKE_NONCOPYABLE(WorkerThreadLifecycleObserverForTest);
+public:
+ explicit WorkerThreadLifecycleObserverForTest(WorkerThreadLifecycleContext* context)
+ : WorkerThreadLifecycleObserver(context)
+ {
+ DCHECK(!wasContextDestroyedBeforeObserverCreation());
+ }
+ ~WorkerThreadLifecycleObserverForTest() override {}
+
+ void contextDestroyed() override
+ {
+ if (m_closure)
+ (*m_closure)();
+ }
+
+ void setClosure(std::unique_ptr<WTF::Closure> closure)
+ {
+ m_closure = std::move(closure);
+ }
+
+private:
+ std::unique_ptr<WTF::Closure> m_closure;
+};
+
class WorkerThreadTest : public ::testing::Test {
public:
void SetUp() override
@@ -41,7 +68,7 @@ public:
m_workerThread = wrapUnique(new WorkerThreadForTest(
m_mockWorkerLoaderProxyProvider.get(),
*m_mockWorkerReportingProxy));
- m_mockWorkerThreadLifecycleObserver = new MockWorkerThreadLifecycleObserver(m_workerThread->getWorkerThreadLifecycleContext());
+ m_workerThreadLifecycleObserver = new WorkerThreadLifecycleObserverForTest(m_workerThread->getWorkerThreadLifecycleContext());
}
void TearDown() override
@@ -78,7 +105,6 @@ protected:
EXPECT_CALL(*m_mockWorkerReportingProxy, didEvaluateWorkerScript(true)).Times(1);
EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()).Times(1);
EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()).Times(1);
- EXPECT_CALL(*m_mockWorkerThreadLifecycleObserver, contextDestroyed()).Times(1);
}
void expectReportingCallsForWorkerPossiblyTerminatedBeforeInitialization()
@@ -87,7 +113,6 @@ protected:
EXPECT_CALL(*m_mockWorkerReportingProxy, didEvaluateWorkerScript(_)).Times(AtMost(1));
EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()).Times(1);
EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()).Times(AtMost(1));
- EXPECT_CALL(*m_mockWorkerThreadLifecycleObserver, contextDestroyed()).Times(1);
}
void expectReportingCallsForWorkerForciblyTerminated()
@@ -96,14 +121,13 @@ protected:
EXPECT_CALL(*m_mockWorkerReportingProxy, didEvaluateWorkerScript(false)).Times(1);
EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()).Times(1);
EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()).Times(1);
- EXPECT_CALL(*m_mockWorkerThreadLifecycleObserver, contextDestroyed()).Times(1);
}
RefPtr<SecurityOrigin> m_securityOrigin;
std::unique_ptr<MockWorkerLoaderProxyProvider> m_mockWorkerLoaderProxyProvider;
std::unique_ptr<MockWorkerReportingProxy> m_mockWorkerReportingProxy;
std::unique_ptr<WorkerThreadForTest> m_workerThread;
- Persistent<MockWorkerThreadLifecycleObserver> m_mockWorkerThreadLifecycleObserver;
+ Persistent<WorkerThreadLifecycleObserverForTest> m_workerThreadLifecycleObserver;
};
TEST_F(WorkerThreadTest, StartAndTerminate_AsyncTerminate)
@@ -166,7 +190,6 @@ TEST_F(WorkerThreadTest, StartAndTerminateOnInitialization_TerminateWhileDebugge
EXPECT_CALL(*m_mockWorkerReportingProxy, workerGlobalScopeStarted(_)).Times(1);
EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()).Times(1);
EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()).Times(1);
- EXPECT_CALL(*m_mockWorkerThreadLifecycleObserver, contextDestroyed()).Times(1);
std::unique_ptr<Vector<CSPHeaderAndType>> headers = wrapUnique(new Vector<CSPHeaderAndType>());
CSPHeaderAndType headerAndType("contentSecurityPolicy", ContentSecurityPolicyHeaderTypeReport);
@@ -190,7 +213,12 @@ TEST_F(WorkerThreadTest, StartAndTerminateOnInitialization_TerminateWhileDebugge
V8CacheOptionsDefault);
m_workerThread->start(std::move(startupData));
- m_workerThread->appendDebuggerTask(crossThreadBind(&waitForTermination, crossThreadUnretained(m_workerThread.get())));
+ // Used to wait for worker thread termination in a debugger task on the
+ // worker thread. Signaled when WorkerThreadLifecycleContext is destroyed on
+ // the main thread.
+ WaitableEvent waitableEvent(WaitableEvent::ResetPolicy::Manual, WaitableEvent::InitialState::NonSignaled);
+ m_workerThread->appendDebuggerTask(crossThreadBind(&waitForTermination, crossThreadUnretained(m_workerThread.get()), crossThreadUnretained(&waitableEvent)));
+ m_workerThreadLifecycleObserver->setClosure(WTF::bind(&WaitableEvent::signal, unretained(&waitableEvent)));
// Wait for the debugger task.
testing::enterRunLoop();
« no previous file with comments | « third_party/WebKit/Source/core/workers/WorkerThreadLifecycleObserver.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698