Chromium Code Reviews| Index: Source/core/workers/WorkerThreadTest.cpp |
| diff --git a/Source/core/workers/WorkerThreadTest.cpp b/Source/core/workers/WorkerThreadTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a583d2395c240a90970c2c8539119b98edc401bc |
| --- /dev/null |
| +++ b/Source/core/workers/WorkerThreadTest.cpp |
| @@ -0,0 +1,198 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
|
Sami
2015/04/09 10:52:29
If we want to make this patch smaller, landing thi
alex clarke (OOO till 29th)
2015/04/10 15:29:39
Acknowledged.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "core/workers/WorkerThread.h" |
| + |
| +#include "bindings/core/v8/ScriptWrappable.h" |
| +#include "core/inspector/ConsoleMessage.h" |
| +#include "core/workers/SharedWorkerGlobalScope.h" |
| +#include "core/workers/SharedWorkerThread.h" |
| +#include "core/workers/WorkerGlobalScope.h" |
| +#include "core/workers/WorkerReportingProxy.h" |
| +#include "core/workers/WorkerThreadStartupData.h" |
| +#include <gmock/gmock.h> |
| +#include <gtest/gtest.h> |
| + |
| +using testing::_; |
| +using testing::Invoke; |
| +using testing::Return; |
| + |
| +namespace blink { |
| + |
| +namespace { |
| +class MockWorkerLoaderProxy : public WorkerLoaderProxy { |
| +public: |
| + MockWorkerLoaderProxy() : WorkerLoaderProxy(nullptr) { } |
| + ~MockWorkerLoaderProxy() override { } |
| + |
| + MOCK_METHOD1(postTaskToLoader, void(PassOwnPtr<ExecutionContextTask>)); |
| + MOCK_METHOD1(postTaskToWorkerGlobalScope, bool(PassOwnPtr<ExecutionContextTask>)); |
| +}; |
| + |
| +class MockWorkerReportingProxy : public WorkerReportingProxy { |
| +public: |
| + MockWorkerReportingProxy() { } |
| + ~MockWorkerReportingProxy() override { } |
| + |
| + MOCK_METHOD5(reportException, void(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, int exceptionId)); |
| + MOCK_METHOD1(reportConsoleMessage, void(PassRefPtrWillBeRawPtr<ConsoleMessage>)); |
| + MOCK_METHOD1(postMessageToPageInspector, void(const String&)); |
| + MOCK_METHOD0(postWorkerConsoleAgentEnabled, void()); |
| + MOCK_METHOD1(didEvaluateWorkerScript, void(bool success)); |
| + MOCK_METHOD1(workerGlobalScopeStarted, void(WorkerGlobalScope*)); |
| + MOCK_METHOD0(workerGlobalScopeClosed, void()); |
| + MOCK_METHOD0(workerThreadTerminated, void()); |
| + MOCK_METHOD0(willDestroyWorkerGlobalScope, void()); |
| +}; |
| + |
| +class FakeWorkerGlobalScope : public WorkerGlobalScope { |
| +public: |
| + typedef WorkerGlobalScope Base; |
| + |
| + FakeWorkerGlobalScope(const KURL& url, const String& userAgent, WorkerThread* thread, const SecurityOrigin* starterOrigin, PassOwnPtrWillBeRawPtr<WorkerClients> workerClients) |
| + : WorkerGlobalScope(url, userAgent, thread, monotonicallyIncreasingTime(), starterOrigin, workerClients) |
| + { |
| + } |
| + |
| + ~FakeWorkerGlobalScope() override |
| + { |
| + } |
| + |
| + virtual bool isSharedWorkerGlobalScope() const override { return true; } |
| + |
| + // EventTarget |
| + virtual const AtomicString& interfaceName() const override |
| + { |
| + return EventTargetNames::SharedWorkerGlobalScope; |
| + } |
| + |
| + virtual void logExceptionToConsole(const String&, int , const String&, int, int, PassRefPtrWillBeRawPtr<ScriptCallStack>) override |
| + { |
| + } |
| + |
| + // Setters/Getters for attributes in SharedWorkerGlobalScope.idl |
| + DEFINE_ATTRIBUTE_EVENT_LISTENER(connect); |
| + String name() const { return "FakeWorkerGlobalScope"; } |
| + |
| + DECLARE_VIRTUAL_TRACE(); |
| +}; |
| + |
| +DEFINE_TRACE(FakeWorkerGlobalScope) |
| +{ |
| + WorkerGlobalScope::trace(visitor); |
| +} |
| + |
| +class WorkerThreadForTest : public WorkerThread { |
| +public: |
| + WorkerThreadForTest( |
| + WorkerLoaderProxy* mockWorkerLoaderProxy, |
| + WorkerReportingProxy& mockWorkerReportingProxy, |
| + PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> workerThreadStartupData) |
| + : WorkerThread(mockWorkerLoaderProxy, mockWorkerReportingProxy, workerThreadStartupData) |
| + { |
| + } |
| + |
| + ~WorkerThreadForTest() override { } |
| + |
| + using WorkerThread::threadForTesting; |
| + using WorkerThread::setMinQuietPeriodBeforeDoingIdleGcForTesting; |
| + |
| + MOCK_METHOD1(doIdleGc, bool(double deadlineSeconds)); |
| + |
| + PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScope(PassOwnPtr<WorkerThreadStartupData> startupData) override |
| + { |
| + return adoptRefWillBeNoop(new FakeWorkerGlobalScope(startupData->m_scriptURL, startupData->m_userAgent, this, startupData->m_starterOrigin, startupData->m_workerClients.release())); |
| + } |
| +}; |
| + |
| +class WakeupTask : public WebThread::Task { |
| +public: |
| + WakeupTask() { } |
| + |
| + ~WakeupTask() override { } |
| + |
| + void run() override { } |
| +}; |
| + |
| +class PostDelayedWakeupTask : public WebThread::Task { |
| +public: |
| + PostDelayedWakeupTask(WebThreadSupportingGC* gc, long long delay) : m_gc(gc), m_delay(delay) { } |
| + |
| + ~PostDelayedWakeupTask() override { } |
| + |
| + void run() override |
| + { |
| + m_gc->postDelayedTask(FROM_HERE, new WakeupTask(), m_delay); |
| + } |
| + |
| + WebThreadSupportingGC* m_gc; |
| + long long m_delay; |
| +}; |
| + |
| +} // namespace |
| + |
| +class WorkerThreadTest : public testing::Test { |
| +public: |
| + void SetUp() override |
| + { |
| + m_mockWorkerLoaderProxy = new MockWorkerLoaderProxy(); |
| + m_mockWorkerReportingProxy = adoptPtr(new MockWorkerReportingProxy()); |
| + m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://fake.url/")); |
| + m_workerThread = adoptRef(new WorkerThreadForTest( |
| + m_mockWorkerLoaderProxy.get(), |
| + *m_mockWorkerReportingProxy, |
| + WorkerThreadStartupData::create( |
| + KURL(ParsedURLString, "http://fake.url/"), |
| + "fake user agent", |
| + "//fake source code", |
| + nullptr, |
| + DontPauseWorkerGlobalScopeOnStart, |
| + "contentSecurityPolicy", |
| + ContentSecurityPolicyHeaderTypeReport, |
| + m_securityOrigin.get(), |
| + WorkerClients::create(), |
| + V8CacheOptionsDefault))); |
| + } |
| + |
| + RefPtr<SecurityOrigin> m_securityOrigin; |
| + RefPtr<MockWorkerLoaderProxy> m_mockWorkerLoaderProxy; |
| + OwnPtr<MockWorkerReportingProxy> m_mockWorkerReportingProxy; |
| + RefPtr<WorkerThreadForTest> m_workerThread; |
| +}; |
| + |
| +TEST_F(WorkerThreadTest, GcOccursWhileIdle) |
| +{ |
| + bool gcDone = false; |
| + |
| + ON_CALL(*m_workerThread, doIdleGc(_)).WillByDefault(Invoke( |
| + [&gcDone](double) |
| + { |
| + gcDone = true; |
| + return false; |
| + })); |
| + |
| + EXPECT_CALL(*m_workerThread, doIdleGc(_)).Times(1); |
| + EXPECT_CALL(*m_mockWorkerReportingProxy, workerGlobalScopeStarted(_)).Times(1); |
|
Sami
2015/04/09 10:52:29
Could these be moved to a helper setup function? T
alex clarke (OOO till 29th)
2015/04/10 15:29:38
Done.
|
| + EXPECT_CALL(*m_mockWorkerReportingProxy, didEvaluateWorkerScript(true)).Times(1); |
| + EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()).Times(1); |
| + EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()).Times(1); |
| + |
| + m_workerThread->setMinQuietPeriodBeforeDoingIdleGcForTesting(100ul); |
| + m_workerThread->start(); |
| + WebThreadSupportingGC* thread = m_workerThread->threadForTesting(); |
| + |
| + // The idle task will get posted on an after wake up queue, so we need another task |
| + // posted at the right time to wake the system up. We don't know the right delay here |
| + // since the thread can take a variable length of time to be responsive, however this |
| + // isn't a problem when posting a delayed task from within a task on the worker thread. |
| + thread->postTask(FROM_HERE, new PostDelayedWakeupTask(thread, 110ul)); |
| + |
| + while (!gcDone) |
| + Platform::current()->yieldCurrentThread(); |
| + |
| + m_workerThread->terminateAndWait(); |
| +}; |
| + |
| +} // namespace blink |