TEST_P(DedicatedWorkerTest, PendingActivity_SetTimeoutOnMessageEvent) { // Start an oneshot timer on a message event. const String sourceCode = "addEventListener('message', function(event) {" " setTimeout(function() {}, 0);" "});"; startWithSourceCode(sourceCode); // Worker initialization should be counted as a pending activity. EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); workerMessagingProxy()->waitUntil(WaitUntilMode::PendingActivityReported); EXPECT_FALSE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); // A message starts the oneshot timer that is counted as a pending activity. dispatchMessageEvent(); EXPECT_EQ(1u, workerMessagingProxy()->unconfirmedMessageCount()); EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); workerMessagingProxy()->waitUntil(WaitUntilMode::MessageConfirmed); EXPECT_EQ(0u, workerMessagingProxy()->unconfirmedMessageCount()); EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); // The timer is fired soon and there should be no pending activities after // that. workerMessagingProxy()->waitUntil(WaitUntilMode::PendingActivityReported); EXPECT_FALSE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); }
TEST_P(DedicatedWorkerTest, PendingActivity_SetInterval) { // Start a repeated timer on initial script evaluation, and stop it when a // message is received. The timer needs a non-zero delay or else worker // activities would not run. const String sourceCode = "var id = setInterval(function() {}, 50);" "addEventListener('message', function(event) { clearInterval(id); });"; startWithSourceCode(sourceCode); // Worker initialization should be counted as a pending activity. EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); // Stop the timer. dispatchMessageEvent(); EXPECT_EQ(1u, workerMessagingProxy()->unconfirmedMessageCount()); EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); workerMessagingProxy()->waitUntil(WaitUntilMode::MessageConfirmed); EXPECT_EQ(0u, workerMessagingProxy()->unconfirmedMessageCount()); EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); // There should be no pending activities after the timer is stopped. workerMessagingProxy()->waitUntil(WaitUntilMode::PendingActivityReported); EXPECT_FALSE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); }
TEST_P(DedicatedWorkerTest, PendingActivity_SetIntervalOnMessageEvent) { // Start a repeated timer on a message event, and stop it when another // message is received. The timer needs a non-zero delay or else worker // activities would not run. const String sourceCode = "var count = 0;" "var id;" "addEventListener('message', function(event) {" " if (count++ == 0) {" " id = setInterval(function() {}, 50);" " } else {" " clearInterval(id);" " }" "});"; startWithSourceCode(sourceCode); // Worker initialization should be counted as a pending activity. EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); workerMessagingProxy()->waitUntil(WaitUntilMode::PendingActivityReported); EXPECT_FALSE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); // The first message event sets the active timer that is counted as a // pending activity. dispatchMessageEvent(); EXPECT_EQ(1u, workerMessagingProxy()->unconfirmedMessageCount()); EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); workerMessagingProxy()->waitUntil(WaitUntilMode::MessageConfirmed); EXPECT_EQ(0u, workerMessagingProxy()->unconfirmedMessageCount()); EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); // Run the message loop for a while to make sure the timer is counted as a // pending activity until it's stopped. The delay is equal to the max // interval so that the pending activity timer may be able to have a chance // to run before the next expectation check. const double kDelayInMs = kMaxIntervalInSec * 1000; testing::runDelayedTasks(kDelayInMs); EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); // Stop the timer. dispatchMessageEvent(); EXPECT_EQ(1u, workerMessagingProxy()->unconfirmedMessageCount()); EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); workerMessagingProxy()->waitUntil(WaitUntilMode::MessageConfirmed); EXPECT_EQ(0u, workerMessagingProxy()->unconfirmedMessageCount()); EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); // There should be no pending activities after the timer is stopped. workerMessagingProxy()->waitUntil(WaitUntilMode::PendingActivityReported); EXPECT_FALSE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); }
TEST_P(DedicatedWorkerTest, PendingActivity_NoActivity) { const String sourceCode = "// Do nothing"; startWithSourceCode(sourceCode); // Worker initialization should be counted as a pending activity. EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); // There should be no pending activities after the initialization. workerMessagingProxy()->waitUntil(WaitUntilMode::PendingActivityReported); EXPECT_FALSE(workerMessagingProxy()->hasPendingActivity()); }
TEST_P(DedicatedWorkerTest, PendingActivity_SetTimeout) { // Start an oneshot timer on initial script evaluation. const String sourceCode = "setTimeout(function() {}, 0);"; startWithSourceCode(sourceCode); // Worker initialization should be counted as a pending activity. EXPECT_TRUE( workerMessagingProxy()->workerGlobalScopeMayHavePendingActivity()); // The timer is fired soon and there should be no pending activities after // that. workerMessagingProxy()->waitUntil(WaitUntilMode::PendingActivityReported); EXPECT_FALSE(workerMessagingProxy()->hasPendingActivity()); }
TEST_F(WorkerThreadTest, StartAndStopOnScriptLoaded) { // Use a JavaScript source code that makes an infinite loop so that we can // catch some kind of issues as a timeout. const String source("while(true) {}"); EXPECT_CALL(*m_mockWorkerReportingProxy, workerGlobalScopeStarted(_)) .Times(AtMost(1)); EXPECT_CALL(*m_mockWorkerReportingProxy, didEvaluateWorkerScript(_)) .Times(AtMost(1)); EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()) .Times(AtMost(1)); EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()) .Times(AtMost(1)); startWithSourceCode(source); m_workerThread->waitUntilScriptLoaded(); m_workerThread->terminateAndWait(); }
void start() { startWithSourceCode("//fake source code"); }