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

Side by Side Diff: content/browser/service_worker/service_worker_version_unittest.cc

Issue 912753002: Stop Service Workers that execute JavaScript for too long. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase? Created 5 years, 10 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/run_loop.h" 6 #include "base/run_loop.h"
7 #include "content/browser/service_worker/embedded_worker_registry.h" 7 #include "content/browser/service_worker/embedded_worker_registry.h"
8 #include "content/browser/service_worker/embedded_worker_test_helper.h" 8 #include "content/browser/service_worker/embedded_worker_test_helper.h"
9 #include "content/browser/service_worker/service_worker_context_core.h" 9 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_registration.h" 10 #include "content/browser/service_worker/service_worker_registration.h"
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 : ServiceWorkerVersionTest() {} 180 : ServiceWorkerVersionTest() {}
181 181
182 scoped_ptr<MessageReceiver> GetMessageReceiver() override { 182 scoped_ptr<MessageReceiver> GetMessageReceiver() override {
183 return make_scoped_ptr(new MessageReceiverDisallowStart()); 183 return make_scoped_ptr(new MessageReceiverDisallowStart());
184 } 184 }
185 185
186 private: 186 private:
187 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFailToStartTest); 187 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFailToStartTest);
188 }; 188 };
189 189
190 class MessageReceiverHangAtScriptEvaluation : public MessageReceiver {
191 public:
192 MessageReceiverHangAtScriptEvaluation() : MessageReceiver() {}
193 ~MessageReceiverHangAtScriptEvaluation() override {}
194
195 void OnStartWorker(int embedded_worker_id,
196 int64 service_worker_version_id,
197 const GURL& scope,
198 const GURL& script_url,
199 bool pause_after_download) override {
200 if (pause_after_download) {
201 SimulatePausedAfterDownload(embedded_worker_id);
202 return;
203 }
204 SimulateWorkerReadyForInspection(embedded_worker_id);
205 SimulateWorkerScriptLoaded(next_thread_id_++, embedded_worker_id);
206 // To simulate while(true) {} in the script, don't call the following:
207 // SimulateWorkerScriptEvaluated(embedded_worker_id);
208 // SimulateWorkerStarted(embedded_worker_id);
209 }
210
211 private:
212 DISALLOW_COPY_AND_ASSIGN(MessageReceiverHangAtScriptEvaluation);
213 };
214
215 class MessageReceiverHangAtInstall : public MessageReceiver {
216 public:
217 MessageReceiverHangAtInstall() : MessageReceiver() {}
218 ~MessageReceiverHangAtInstall() override {}
219
220 void OnInstallEvent(int embedded_worker_id,
221 int request_id,
222 int active_version_id) override {
223 // Do nothing.
224 }
225
226 private:
227 DISALLOW_COPY_AND_ASSIGN(MessageReceiverHangAtInstall);
228 };
229
230 class ServiceWorkerStartTimeoutTest : public ServiceWorkerVersionTest {
231 protected:
232 ServiceWorkerStartTimeoutTest() : ServiceWorkerVersionTest() {}
233
234 scoped_ptr<MessageReceiver> GetMessageReceiver() override {
235 return make_scoped_ptr(new MessageReceiverHangAtScriptEvaluation());
236 }
237
238 private:
239 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStartTimeoutTest);
240 };
241
242 class ServiceWorkerEventTimeoutTest : public ServiceWorkerVersionTest {
243 protected:
244 ServiceWorkerEventTimeoutTest() : ServiceWorkerVersionTest() {}
245
246 scoped_ptr<MessageReceiver> GetMessageReceiver() override {
247 return make_scoped_ptr(new MessageReceiverHangAtInstall());
248 }
249
250 private:
251 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerEventTimeoutTest);
252 };
253
190 TEST_F(ServiceWorkerVersionTest, ConcurrentStartAndStop) { 254 TEST_F(ServiceWorkerVersionTest, ConcurrentStartAndStop) {
191 // Call StartWorker() multiple times. 255 // Call StartWorker() multiple times.
192 ServiceWorkerStatusCode status1 = SERVICE_WORKER_ERROR_FAILED; 256 ServiceWorkerStatusCode status1 = SERVICE_WORKER_ERROR_FAILED;
193 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_FAILED; 257 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_FAILED;
194 ServiceWorkerStatusCode status3 = SERVICE_WORKER_ERROR_FAILED; 258 ServiceWorkerStatusCode status3 = SERVICE_WORKER_ERROR_FAILED;
195 version_->StartWorker(CreateReceiverOnCurrentThread(&status1)); 259 version_->StartWorker(CreateReceiverOnCurrentThread(&status1));
196 version_->StartWorker(CreateReceiverOnCurrentThread(&status2)); 260 version_->StartWorker(CreateReceiverOnCurrentThread(&status2));
197 261
198 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status()); 262 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
199 base::RunLoop().RunUntilIdle(); 263 base::RunLoop().RunUntilIdle();
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 helper_->context()->RemoveAllProviderHostsForProcess(process_id); 499 helper_->context()->RemoveAllProviderHostsForProcess(process_id);
436 helper_->context()->embedded_worker_registry()->RemoveChildProcessSender( 500 helper_->context()->embedded_worker_registry()->RemoveChildProcessSender(
437 process_id); 501 process_id);
438 base::RunLoop().RunUntilIdle(); 502 base::RunLoop().RunUntilIdle();
439 503
440 // Callback completed. 504 // Callback completed.
441 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); 505 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status);
442 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status()); 506 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
443 } 507 }
444 508
509 // Test starting a "while(true) {}" worker. StartWorker should timeout.
510 TEST_F(ServiceWorkerStartTimeoutTest, ScriptEvaluationTimeout) {
511 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value
512 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
513 base::RunLoop().RunUntilIdle();
514
515 // The start callback has not completed yet.
516 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, status);
517 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
518
519 // The pings have started.
520 EXPECT_TRUE(version_->ping_worker_timer_.IsRunning());
521
522 // Simulate timing out.
523 version_->PingWorker();
524 version_->OnPingTimeout();
525 base::RunLoop().RunUntilIdle();
526
527 // The start callback should have completed with failure.
528 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status);
529 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
530 }
531
532 // Test "while(true) {}" after successful startup (e.g., in an event
533 // handler). The worker should be stopped.
534 TEST_F(ServiceWorkerEventTimeoutTest, EventTimeout) {
535 ServiceWorkerStatusCode status;
536 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
537 base::RunLoop().RunUntilIdle();
538
539 EXPECT_EQ(SERVICE_WORKER_OK, status);
540
541 // The pings have started.
542 EXPECT_TRUE(version_->ping_worker_timer_.IsRunning());
543
544 // Simulate a successful ping.
545 version_->PingWorker();
546 version_->OnPongFromWorker();
547 EXPECT_TRUE(version_->ping_worker_timer_.IsRunning());
548
549 // Dispatch an event.
550 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
551 version_->DispatchInstallEvent(0, CreateReceiverOnCurrentThread(&status));
552
553 // Simulate timing out.
554 EXPECT_TRUE(version_->ping_worker_timer_.IsRunning());
555 version_->PingWorker();
556 version_->OnPingTimeout();
557 base::RunLoop().RunUntilIdle();
558
559 // The event callback should have completed with failure.
560 EXPECT_EQ(SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED, status);
561 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
562 }
563
445 } // namespace content 564 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698