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

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

Issue 1038763005: Service Worker: Reset "idle" timer upon completion of an event. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: reliably test timer being reset Created 5 years, 9 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
« no previous file with comments | « content/browser/service_worker/service_worker_version.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 404
405 // Verify that we could successfully observe repeated status changes. 405 // Verify that we could successfully observe repeated status changes.
406 ASSERT_EQ(5U, statuses.size()); 406 ASSERT_EQ(5U, statuses.size());
407 ASSERT_EQ(ServiceWorkerVersion::INSTALLING, statuses[0]); 407 ASSERT_EQ(ServiceWorkerVersion::INSTALLING, statuses[0]);
408 ASSERT_EQ(ServiceWorkerVersion::INSTALLED, statuses[1]); 408 ASSERT_EQ(ServiceWorkerVersion::INSTALLED, statuses[1]);
409 ASSERT_EQ(ServiceWorkerVersion::ACTIVATING, statuses[2]); 409 ASSERT_EQ(ServiceWorkerVersion::ACTIVATING, statuses[2]);
410 ASSERT_EQ(ServiceWorkerVersion::ACTIVATED, statuses[3]); 410 ASSERT_EQ(ServiceWorkerVersion::ACTIVATED, statuses[3]);
411 ASSERT_EQ(ServiceWorkerVersion::REDUNDANT, statuses[4]); 411 ASSERT_EQ(ServiceWorkerVersion::REDUNDANT, statuses[4]);
412 } 412 }
413 413
414 TEST_F(ServiceWorkerVersionTest, ScheduleStopWorker) { 414 TEST_F(ServiceWorkerVersionTest, IdleTimeout) {
415 // Used to reliably test when the idle time gets reset regardless of clock
416 // granularity.
417 const base::TimeDelta kOneSecond = base::TimeDelta::FromSeconds(1);
418
415 // Verify the timer is not running when version initializes its status. 419 // Verify the timer is not running when version initializes its status.
416 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); 420 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
417 EXPECT_FALSE(version_->timeout_timer_.IsRunning()); 421 EXPECT_FALSE(version_->timeout_timer_.IsRunning());
418 422
419 // Verify the timer is running after the worker is started. 423 // Verify the timer is running after the worker is started.
420 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 424 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
421 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); 425 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
422 base::RunLoop().RunUntilIdle(); 426 base::RunLoop().RunUntilIdle();
423 EXPECT_EQ(SERVICE_WORKER_OK, status); 427 EXPECT_EQ(SERVICE_WORKER_OK, status);
424 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); 428 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
429 EXPECT_FALSE(version_->idle_time_.is_null());
425 430
426 // The timer should be running if the worker is restarted without controllee. 431 // The idle time should be reset if the worker is restarted without
432 // controllee.
427 status = SERVICE_WORKER_ERROR_FAILED; 433 status = SERVICE_WORKER_ERROR_FAILED;
434 version_->idle_time_ -= kOneSecond;
435 base::TimeTicks idle_time = version_->idle_time_;
428 version_->StopWorker(CreateReceiverOnCurrentThread(&status)); 436 version_->StopWorker(CreateReceiverOnCurrentThread(&status));
429 base::RunLoop().RunUntilIdle(); 437 base::RunLoop().RunUntilIdle();
430 EXPECT_EQ(SERVICE_WORKER_OK, status); 438 EXPECT_EQ(SERVICE_WORKER_OK, status);
431 status = SERVICE_WORKER_ERROR_FAILED; 439 status = SERVICE_WORKER_ERROR_FAILED;
432 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); 440 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
433 base::RunLoop().RunUntilIdle(); 441 base::RunLoop().RunUntilIdle();
434 EXPECT_EQ(SERVICE_WORKER_OK, status); 442 EXPECT_EQ(SERVICE_WORKER_OK, status);
435 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); 443 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
444 EXPECT_LT(idle_time, version_->idle_time_);
436 445
437 // Adding controllee doesn't stop the stop-worker-timer. 446 // Adding a controllee resets the idle time.
447 version_->idle_time_ -= kOneSecond;
448 idle_time = version_->idle_time_;
438 scoped_ptr<ServiceWorkerProviderHost> host( 449 scoped_ptr<ServiceWorkerProviderHost> host(
439 new ServiceWorkerProviderHost(33 /* dummy render process id */, 450 new ServiceWorkerProviderHost(33 /* dummy render process id */,
440 MSG_ROUTING_NONE /* render_frame_id */, 451 MSG_ROUTING_NONE /* render_frame_id */,
441 1 /* dummy provider_id */, 452 1 /* dummy provider_id */,
442 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, 453 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE,
443 helper_->context()->AsWeakPtr(), 454 helper_->context()->AsWeakPtr(),
444 NULL)); 455 NULL));
445 version_->AddControllee(host.get()); 456 version_->AddControllee(host.get());
446 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); 457 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
458 EXPECT_LT(idle_time, version_->idle_time_);
459
460 // Completing an event resets the idle time.
461 status = SERVICE_WORKER_ERROR_FAILED;
462 version_->idle_time_ -= kOneSecond;
463 idle_time = version_->idle_time_;
464 version_->DispatchFetchEvent(ServiceWorkerFetchRequest(),
465 base::Bind(&base::DoNothing),
466 base::Bind(&ReceiveFetchResult, &status));
467 base::RunLoop().RunUntilIdle();
468
469 EXPECT_EQ(SERVICE_WORKER_OK, status);
470 EXPECT_LT(idle_time, version_->idle_time_);
447 } 471 }
448 472
449 473
450 TEST_F(ServiceWorkerVersionTest, SetDevToolsAttached) { 474 TEST_F(ServiceWorkerVersionTest, SetDevToolsAttached) {
451 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 475 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
452 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); 476 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
453 477
454 ASSERT_EQ(ServiceWorkerVersion::STARTING, version_->running_status()); 478 ASSERT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
455 479
456 ASSERT_TRUE(version_->timeout_timer_.IsRunning()); 480 ASSERT_TRUE(version_->timeout_timer_.IsRunning());
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 base::TimeTicks::Now() - 567 base::TimeTicks::Now() -
544 base::TimeDelta::FromMinutes( 568 base::TimeDelta::FromMinutes(
545 ServiceWorkerVersion::kStartWorkerTimeoutMinutes + 1); 569 ServiceWorkerVersion::kStartWorkerTimeoutMinutes + 1);
546 version_->timeout_timer_.user_task().Run(); 570 version_->timeout_timer_.user_task().Run();
547 base::RunLoop().RunUntilIdle(); 571 base::RunLoop().RunUntilIdle();
548 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status); 572 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status);
549 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status()); 573 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
550 } 574 }
551 575
552 } // namespace content 576 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_version.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698