| Index: Source/core/workers/WorkerThreadTest.cpp
|
| diff --git a/Source/core/workers/WorkerThreadTest.cpp b/Source/core/workers/WorkerThreadTest.cpp
|
| index 9ef4ac34812c637b7a1b41c1a53ad1527671b033..e888efbdca263ca8517cfab73d47c733afd72eef 100644
|
| --- a/Source/core/workers/WorkerThreadTest.cpp
|
| +++ b/Source/core/workers/WorkerThreadTest.cpp
|
| @@ -8,6 +8,7 @@
|
| #include "core/inspector/ConsoleMessage.h"
|
| #include "core/workers/WorkerReportingProxy.h"
|
| #include "core/workers/WorkerThreadStartupData.h"
|
| +#include "public/platform/WebScheduler.h"
|
| #include "wtf/ThreadingPrimitives.h"
|
| #include <gmock/gmock.h>
|
| #include <gtest/gtest.h>
|
| @@ -101,6 +102,8 @@ public:
|
| return *m_thread;
|
| }
|
|
|
| + 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()));
|
| @@ -110,6 +113,30 @@ private:
|
| OwnPtr<WebThreadSupportingGC> m_thread;
|
| };
|
|
|
| +class WakeupTask : public WebThread::Task {
|
| +public:
|
| + WakeupTask() { }
|
| +
|
| + ~WakeupTask() override { }
|
| +
|
| + void run() override { }
|
| +};
|
| +
|
| +class PostDelayedWakeupTask : public WebThread::Task {
|
| +public:
|
| + PostDelayedWakeupTask(WebScheduler* scheduler, long long delay) : m_scheduler(scheduler), m_delay(delay) { }
|
| +
|
| + ~PostDelayedWakeupTask() override { }
|
| +
|
| + void run() override
|
| + {
|
| + m_scheduler->postTimerTask(FROM_HERE, new WakeupTask(), m_delay);
|
| + }
|
| +
|
| + WebScheduler* m_scheduler; // NOT OWNED
|
| + long long m_delay;
|
| +};
|
| +
|
| class SignalTask : public WebThread::Task {
|
| public:
|
| SignalTask(ThreadCondition* completion, Mutex* mutex) : m_completion(completion), m_mutex(mutex) { }
|
| @@ -172,6 +199,17 @@ public:
|
| }
|
| }
|
|
|
| + void postWakeUpTask(long long waitMs)
|
| + {
|
| + WebScheduler* scheduler = m_workerThread->backingThread().platformThread().scheduler();
|
| +
|
| + // 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.
|
| + scheduler->postLoadingTask(FROM_HERE, new PostDelayedWakeupTask(scheduler, waitMs));
|
| + }
|
| +
|
| protected:
|
| void ExpectWorkerLifetimeReportingCalls()
|
| {
|
| @@ -193,4 +231,187 @@ TEST_F(WorkerThreadTest, StartAndStop)
|
| m_workerThread->terminateAndWait();
|
| }
|
|
|
| +TEST_F(WorkerThreadTest, GcOccursWhileIdle)
|
| +{
|
| + ThreadCondition gcDone;
|
| + Mutex mutex;
|
| +
|
| + ON_CALL(*m_workerThread, doIdleGc(_)).WillByDefault(Invoke(
|
| + [&gcDone, &mutex](double)
|
| + {
|
| + WTF::Locker<Mutex> lock(mutex);
|
| + gcDone.signal();
|
| + return false;
|
| + }));
|
| +
|
| + EXPECT_CALL(*m_workerThread, doIdleGc(_)).Times(1);
|
| +
|
| + startAndWaitForInit();
|
| + postWakeUpTask(310ul); // 10ms after the quiescent period ends.
|
| +
|
| + {
|
| + WTF::Locker<Mutex> lock(mutex);
|
| + gcDone.wait(mutex);
|
| + }
|
| +
|
| + m_workerThread->terminateAndWait();
|
| +};
|
| +
|
| +class RepeatingTask : public WebThread::Task {
|
| +public:
|
| + RepeatingTask(WebScheduler* scheduler, ThreadCondition* completion, Mutex* mutex)
|
| + : RepeatingTask(scheduler, completion, mutex, 0) { }
|
| +
|
| + ~RepeatingTask() override { }
|
| +
|
| + void run() override
|
| + {
|
| + m_taskCount++;
|
| + if (m_taskCount == 10) {
|
| + WTF::Locker<Mutex> lock(*m_mutex);
|
| + m_completion->signal();
|
| + }
|
| +
|
| + m_scheduler->postTimerTask(
|
| + FROM_HERE, new RepeatingTask(m_scheduler, m_completion, m_mutex, m_taskCount), 50ul);
|
| + m_scheduler->postLoadingTask(FROM_HERE, new WakeupTask());
|
| +
|
| + }
|
| +
|
| +private:
|
| + RepeatingTask(WebScheduler* scheduler, ThreadCondition* completion, Mutex* mutex, int taskCount)
|
| + : m_scheduler(scheduler)
|
| + , m_completion(completion)
|
| + , m_mutex(mutex)
|
| + , m_taskCount(taskCount)
|
| + { }
|
| +
|
| + WebScheduler* m_scheduler; // NOT OWNED
|
| + ThreadCondition* m_completion;
|
| + Mutex* m_mutex;
|
| + int m_taskCount;
|
| +};
|
| +
|
| +TEST_F(WorkerThreadTest, GcDoesNotOccurIfGapBetweenDelayedTasksIsTooSmall)
|
| +{
|
| + ThreadCondition completion;
|
| + Mutex mutex;
|
| +
|
| + EXPECT_CALL(*m_workerThread, doIdleGc(_)).Times(0);
|
| +
|
| + startAndWaitForInit();
|
| +
|
| + WebScheduler* scheduler = m_workerThread->backingThread().platformThread().scheduler();
|
| +
|
| + // Post a repeating task that should prevent any GC from happening.
|
| + scheduler->postLoadingTask(FROM_HERE, new RepeatingTask(scheduler, &completion, &mutex));
|
| +
|
| + {
|
| + WTF::Locker<Mutex> lock(mutex);
|
| + completion.wait(mutex);
|
| + }
|
| +
|
| + // Make sure doIdleGc has not been called by this stage.
|
| + Mock::VerifyAndClearExpectations(m_workerThread.get());
|
| +
|
| + m_workerThread->terminateAndWait();
|
| +}
|
| +
|
| +TEST_F(WorkerThreadTest, LongGcDeadline_NoFutureTasks)
|
| +{
|
| + ThreadCondition gcDone;
|
| + Mutex mutex;
|
| + double deadlineLength = 0;
|
| +
|
| + ON_CALL(*m_workerThread, doIdleGc(_)).WillByDefault(Invoke(
|
| + [&gcDone, &mutex, &deadlineLength](double deadline)
|
| + {
|
| + WTF::Locker<Mutex> lock(mutex);
|
| + gcDone.signal();
|
| + deadlineLength = deadline -Platform::current()->monotonicallyIncreasingTime();
|
| + return false;
|
| + }));
|
| +
|
| + EXPECT_CALL(*m_workerThread, doIdleGc(_)).Times(1);
|
| +
|
| + startAndWaitForInit();
|
| + postWakeUpTask(310ul);
|
| +
|
| + {
|
| + WTF::Locker<Mutex> lock(mutex);
|
| + gcDone.wait(mutex);
|
| +
|
| + // The deadline should be close to 1s in duration if there are no tasks that need to run soon.
|
| + EXPECT_GT(deadlineLength, 0.9);
|
| + }
|
| +
|
| + m_workerThread->terminateAndWait();
|
| +}
|
| +
|
| +TEST_F(WorkerThreadTest, LongGcDeadline_NextTaskAfterIdlePeriod)
|
| +{
|
| + ThreadCondition gcDone;
|
| + Mutex mutex;
|
| + double deadlineLength = 0;
|
| +
|
| + ON_CALL(*m_workerThread, doIdleGc(_)).WillByDefault(Invoke(
|
| + [&gcDone, &mutex, &deadlineLength](double deadline)
|
| + {
|
| + WTF::Locker<Mutex> lock(mutex);
|
| + gcDone.signal();
|
| + deadlineLength = deadline -Platform::current()->monotonicallyIncreasingTime();
|
| + return false;
|
| + }));
|
| +
|
| + EXPECT_CALL(*m_workerThread, doIdleGc(_)).Times(1);
|
| +
|
| + startAndWaitForInit();
|
| + postWakeUpTask(310ul);
|
| + postWakeUpTask(675ul); // Task that runs shortly after the 50ms idle period ends.
|
| +
|
| + {
|
| + WTF::Locker<Mutex> lock(mutex);
|
| + gcDone.wait(mutex);
|
| +
|
| + // The worker thread calls canExceedIdleDeadlineIfRequired which only considers if
|
| + // there are any delayed tasks scheduled for the current long idle period. Since the
|
| + // next task is in the following idle period, a long gc deadline is allowed.
|
| + EXPECT_GT(deadlineLength, 0.9);
|
| + }
|
| +
|
| + m_workerThread->terminateAndWait();
|
| +}
|
| +
|
| +TEST_F(WorkerThreadTest, ShortGcDeadline)
|
| +{
|
| + ThreadCondition gcDone;
|
| + Mutex mutex;
|
| + double deadlineLength = 0;
|
| +
|
| + ON_CALL(*m_workerThread, doIdleGc(_)).WillByDefault(Invoke(
|
| + [&gcDone, &mutex, &deadlineLength](double deadline)
|
| + {
|
| + WTF::Locker<Mutex> lock(mutex);
|
| + gcDone.signal();
|
| + deadlineLength = deadline - Platform::current()->monotonicallyIncreasingTime();
|
| + return false;
|
| + }));
|
| +
|
| + EXPECT_CALL(*m_workerThread, doIdleGc(_)).Times(1);
|
| +
|
| + startAndWaitForInit();
|
| + postWakeUpTask(310ul);
|
| + postWakeUpTask(625ul); // Task that runs during the idle period.
|
| +
|
| + {
|
| + WTF::Locker<Mutex> lock(mutex);
|
| + gcDone.wait(mutex);
|
| +
|
| + // The deadline should be < 50ms if there's a task that needs to run during the idle period.
|
| + EXPECT_LT(deadlineLength, 0.025);
|
| + }
|
| +
|
| + m_workerThread->terminateAndWait();
|
| +}
|
| +
|
| } // namespace blink
|
|
|