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

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

Issue 1381153004: Service Worker: Change the criteria for bumping the last update check time (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 11 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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 770 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 ~UpdateJobTestHelper() override { 781 ~UpdateJobTestHelper() override {
782 if (registration_.get()) 782 if (registration_.get())
783 registration_->RemoveListener(this); 783 registration_->RemoveListener(this);
784 } 784 }
785 785
786 ServiceWorkerStorage* storage() { return context()->storage(); } 786 ServiceWorkerStorage* storage() { return context()->storage(); }
787 ServiceWorkerJobCoordinator* job_coordinator() { 787 ServiceWorkerJobCoordinator* job_coordinator() {
788 return context()->job_coordinator(); 788 return context()->job_coordinator();
789 } 789 }
790 790
791 bool force_bypass_cache_for_scripts() const {
792 return force_bypass_cache_for_scripts_;
793 }
794 void set_force_bypass_cache_for_scripts(bool force_bypass_cache_for_scripts) {
795 force_bypass_cache_for_scripts_ = force_bypass_cache_for_scripts;
796 }
797
798 void set_force_start_worker_failure(bool force_start_worker_failure) {
799 force_start_worker_failure_ = force_start_worker_failure;
800 }
801
791 scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration( 802 scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration(
792 const GURL& test_origin) { 803 const GURL& test_origin) {
793 scoped_refptr<ServiceWorkerRegistration> registration; 804 scoped_refptr<ServiceWorkerRegistration> registration;
794 bool called = false; 805 bool called = false;
795 job_coordinator()->Register( 806 job_coordinator()->Register(
796 test_origin.Resolve(kScope), 807 test_origin.Resolve(kScope),
797 test_origin.Resolve(kScript), 808 test_origin.Resolve(kScript),
798 NULL, 809 NULL,
799 SaveRegistration(SERVICE_WORKER_OK, &called, &registration)); 810 SaveRegistration(SERVICE_WORKER_OK, &called, &registration));
800 base::RunLoop().RunUntilIdle(); 811 base::RunLoop().RunUntilIdle();
(...skipping 15 matching lines...) Expand all
816 const uint64_t kMockScriptSize = 19284; 827 const uint64_t kMockScriptSize = 19284;
817 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); 828 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id);
818 ServiceWorkerRegistration* registration = 829 ServiceWorkerRegistration* registration =
819 context()->GetLiveRegistration(version->registration_id()); 830 context()->GetLiveRegistration(version->registration_id());
820 bool is_update = registration->active_version() && 831 bool is_update = registration->active_version() &&
821 version != registration->active_version(); 832 version != registration->active_version();
822 833
823 ASSERT_TRUE(version); 834 ASSERT_TRUE(version);
824 version->AddListener(this); 835 version->AddListener(this);
825 836
837 if (force_bypass_cache_for_scripts())
838 version->set_force_bypass_cache_for_scripts(true);
826 if (!is_update) { 839 if (!is_update) {
827 // Spoof caching the script for the initial version. 840 // Spoof caching the script for the initial version.
828 int64_t resource_id = storage()->NewResourceId(); 841 int64_t resource_id = storage()->NewResourceId();
829 version->script_cache_map()->NotifyStartedCaching(script, resource_id); 842 version->script_cache_map()->NotifyStartedCaching(script, resource_id);
830 WriteStringResponse(storage(), resource_id, kMockScriptBody); 843 WriteStringResponse(storage(), resource_id, kMockScriptBody);
831 version->script_cache_map()->NotifyFinishedCaching( 844 version->script_cache_map()->NotifyFinishedCaching(
832 script, kMockScriptSize, net::URLRequestStatus(), std::string()); 845 script, kMockScriptSize, net::URLRequestStatus(), std::string());
833 } else { 846 } else {
834 if (script.GetOrigin() == kNoChangeOrigin) { 847 if (script.GetOrigin() == kNoChangeOrigin) {
835 version->SetStartWorkerStatusCode(SERVICE_WORKER_ERROR_EXISTS); 848 version->SetStartWorkerStatusCode(SERVICE_WORKER_ERROR_EXISTS);
836 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id); 849 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id);
837 return; 850 return;
838 } 851 }
839 852
840 // Spoof caching the script for the new version. 853 // Spoof caching the script for the new version.
841 int64_t resource_id = storage()->NewResourceId(); 854 int64_t resource_id = storage()->NewResourceId();
842 version->script_cache_map()->NotifyStartedCaching(script, resource_id); 855 version->script_cache_map()->NotifyStartedCaching(script, resource_id);
843 WriteStringResponse(storage(), resource_id, "mock_different_script"); 856 WriteStringResponse(storage(), resource_id, "mock_different_script");
844 version->script_cache_map()->NotifyFinishedCaching( 857 version->script_cache_map()->NotifyFinishedCaching(
845 script, kMockScriptSize, net::URLRequestStatus(), std::string()); 858 script, kMockScriptSize, net::URLRequestStatus(), std::string());
846 } 859 }
847 EmbeddedWorkerTestHelper::OnStartWorker(embedded_worker_id, version_id, 860 if (!force_start_worker_failure_) {
848 scope, script); 861 EmbeddedWorkerTestHelper::OnStartWorker(embedded_worker_id, version_id,
862 scope, script);
863 } else {
864 (embedded_worker_id_service_worker_version_id_map())[embedded_worker_id] =
865 version_id;
866 SimulateWorkerReadyForInspection(embedded_worker_id);
867 SimulateWorkerScriptCached(embedded_worker_id);
868 SimulateWorkerScriptLoaded(embedded_worker_id);
869 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id);
870 SimulateWorkerScriptEvaluated(embedded_worker_id, false);
871 }
849 } 872 }
850 873
851 // ServiceWorkerRegistration::Listener overrides 874 // ServiceWorkerRegistration::Listener overrides
852 void OnVersionAttributesChanged( 875 void OnVersionAttributesChanged(
853 ServiceWorkerRegistration* registration, 876 ServiceWorkerRegistration* registration,
854 ChangedVersionAttributesMask changed_mask, 877 ChangedVersionAttributesMask changed_mask,
855 const ServiceWorkerRegistrationInfo& info) override { 878 const ServiceWorkerRegistrationInfo& info) override {
856 AttributeChangeLogEntry entry; 879 AttributeChangeLogEntry entry;
857 entry.registration_id = registration->id(); 880 entry.registration_id = registration->id();
858 entry.mask = changed_mask; 881 entry.mask = changed_mask;
859 entry.info = info; 882 entry.info = info;
860 attribute_change_log_.push_back(entry); 883 attribute_change_log_.push_back(entry);
861 } 884 }
862 885
863 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override { 886 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override {
864 NOTREACHED(); 887 NOTREACHED();
865 } 888 }
866 889
867 void OnUpdateFound(ServiceWorkerRegistration* registration) override { 890 void OnUpdateFound(ServiceWorkerRegistration* registration) override {
868 ASSERT_FALSE(update_found_);
869 update_found_ = true; 891 update_found_ = true;
870 } 892 }
871 893
872 // ServiceWorkerVersion::Listener overrides 894 // ServiceWorkerVersion::Listener overrides
873 void OnVersionStateChanged(ServiceWorkerVersion* version) override { 895 void OnVersionStateChanged(ServiceWorkerVersion* version) override {
874 StateChangeLogEntry entry; 896 StateChangeLogEntry entry;
875 entry.version_id = version->version_id(); 897 entry.version_id = version->version_id();
876 entry.status = version->status(); 898 entry.status = version->status();
877 state_change_log_.push_back(entry); 899 state_change_log_.push_back(entry);
878 } 900 }
879 901
880 scoped_refptr<ServiceWorkerRegistration> registration_; 902 scoped_refptr<ServiceWorkerRegistration> registration_;
881 903
882 std::vector<AttributeChangeLogEntry> attribute_change_log_; 904 std::vector<AttributeChangeLogEntry> attribute_change_log_;
883 std::vector<StateChangeLogEntry> state_change_log_; 905 std::vector<StateChangeLogEntry> state_change_log_;
884 bool update_found_ = false; 906 bool update_found_ = false;
907 bool force_bypass_cache_for_scripts_ = false;
908 bool force_start_worker_failure_ = false;
885 }; 909 };
886 910
887 // Helper class for update tests that evicts the active version when the update 911 // Helper class for update tests that evicts the active version when the update
888 // worker is about to be started. 912 // worker is about to be started.
889 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { 913 class EvictIncumbentVersionHelper : public UpdateJobTestHelper {
890 public: 914 public:
891 EvictIncumbentVersionHelper() {} 915 EvictIncumbentVersionHelper() {}
892 ~EvictIncumbentVersionHelper() override {} 916 ~EvictIncumbentVersionHelper() override {}
893 917
894 void OnStartWorker(int embedded_worker_id, 918 void OnStartWorker(int embedded_worker_id,
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
958 } 982 }
959 983
960 TEST_F(ServiceWorkerJobTest, Update_BumpLastUpdateCheckTime) { 984 TEST_F(ServiceWorkerJobTest, Update_BumpLastUpdateCheckTime) {
961 const base::Time kToday = base::Time::Now(); 985 const base::Time kToday = base::Time::Now();
962 const base::Time kYesterday = 986 const base::Time kYesterday =
963 kToday - base::TimeDelta::FromDays(1) - base::TimeDelta::FromHours(1); 987 kToday - base::TimeDelta::FromDays(1) - base::TimeDelta::FromHours(1);
964 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; 988 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper;
965 helper_.reset(update_helper); 989 helper_.reset(update_helper);
966 scoped_refptr<ServiceWorkerRegistration> registration = 990 scoped_refptr<ServiceWorkerRegistration> registration =
967 update_helper->SetupInitialRegistration(kNoChangeOrigin); 991 update_helper->SetupInitialRegistration(kNoChangeOrigin);
992 ASSERT_TRUE(registration.get());
968 993
969 // Run an update where the last update check was less than 24 hours ago. The 994 registration->AddListener(update_helper);
970 // check time shouldn't change, as we didn't bypass cache. 995
996 // Run an update that does not bypass the network cache. The check time
997 // should not be updated.
971 registration->set_last_update_check(kToday); 998 registration->set_last_update_check(kToday);
972 registration->active_version()->StartUpdate(); 999 registration->active_version()->StartUpdate();
973 base::RunLoop().RunUntilIdle(); 1000 base::RunLoop().RunUntilIdle();
974 EXPECT_EQ(kToday, registration->last_update_check()); 1001 EXPECT_EQ(kToday, registration->last_update_check());
1002 registration->RemoveListener(update_helper);
975 1003
976 // Run an update where the last update check was over 24 hours ago. The 1004 registration = update_helper->SetupInitialRegistration(kNewVersionOrigin);
977 // check time should change, as the cache was bypassed. 1005 ASSERT_TRUE(registration.get());
1006
1007 registration->AddListener(update_helper);
1008
1009 // Run an update that bypasses the network cache. The check time should be
1010 // updated.
1011 update_helper->set_force_bypass_cache_for_scripts(true);
978 registration->set_last_update_check(kYesterday); 1012 registration->set_last_update_check(kYesterday);
979 registration->active_version()->StartUpdate(); 1013 registration->active_version()->StartUpdate();
980 base::RunLoop().RunUntilIdle(); 1014 base::RunLoop().RunUntilIdle();
1015 EXPECT_LT(kYesterday, registration->last_update_check());
1016
1017 // Run an update to a worker that loads successfully but fails to start up
1018 // (script evaluation failure). The check time should be updated.
1019 update_helper->set_force_bypass_cache_for_scripts(true);
1020 update_helper->set_force_start_worker_failure(true);
1021 registration->set_last_update_check(kYesterday);
1022 registration->active_version()->StartUpdate();
1023 base::RunLoop().RunUntilIdle();
981 EXPECT_LT(kYesterday, registration->last_update_check()); 1024 EXPECT_LT(kYesterday, registration->last_update_check());
982 } 1025 }
983 1026
984 TEST_F(ServiceWorkerJobTest, Update_NewVersion) { 1027 TEST_F(ServiceWorkerJobTest, Update_NewVersion) {
985 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; 1028 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper;
986 helper_.reset(update_helper); 1029 helper_.reset(update_helper);
987 scoped_refptr<ServiceWorkerRegistration> registration = 1030 scoped_refptr<ServiceWorkerRegistration> registration =
988 update_helper->SetupInitialRegistration(kNewVersionOrigin); 1031 update_helper->SetupInitialRegistration(kNewVersionOrigin);
989 ASSERT_TRUE(registration.get()); 1032 ASSERT_TRUE(registration.get());
990 update_helper->state_change_log_.clear(); 1033 update_helper->state_change_log_.clear();
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
1482 EXPECT_FALSE(registration->is_uninstalling()); 1525 EXPECT_FALSE(registration->is_uninstalling());
1483 EXPECT_FALSE(registration->is_uninstalled()); 1526 EXPECT_FALSE(registration->is_uninstalled());
1484 1527
1485 EXPECT_EQ(ServiceWorkerVersion::STOPPED, old_version->running_status()); 1528 EXPECT_EQ(ServiceWorkerVersion::STOPPED, old_version->running_status());
1486 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, old_version->status()); 1529 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, old_version->status());
1487 1530
1488 FindRegistrationForPattern(pattern, SERVICE_WORKER_OK); 1531 FindRegistrationForPattern(pattern, SERVICE_WORKER_OK);
1489 } 1532 }
1490 1533
1491 } // namespace content 1534 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698