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

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

Issue 2787883003: [ServiceWorker] Add EmbeddedWorkerInstanceHost Interface. (Closed)
Patch Set: Refine code comments Created 3 years, 8 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 #include <tuple> 6 #include <tuple>
7 7
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern = 372 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern =
373 FindRegistrationForPattern(pattern); 373 FindRegistrationForPattern(pattern);
374 374
375 ASSERT_EQ(new_registration, old_registration); 375 ASSERT_EQ(new_registration, old_registration);
376 } 376 }
377 377
378 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { 378 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper {
379 public: 379 public:
380 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} 380 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {}
381 381
382 void OnStartWorker( 382 void OnStartWorker(int embedded_worker_id,
383 int embedded_worker_id, 383 int64_t service_worker_version_id,
384 int64_t service_worker_version_id, 384 const GURL& scope,
385 const GURL& scope, 385 const GURL& script_url,
386 const GURL& script_url, 386 bool pause_after_download,
387 bool pause_after_download, 387 mojom::ServiceWorkerEventDispatcherRequest request,
388 mojom::ServiceWorkerEventDispatcherRequest request) override { 388 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo
389 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 389 instance_host) override {
390 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id); 390 mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr;
391 instance_host_ptr.Bind(std::move(instance_host));
392 instance_host_ptr->OnStopped();
393 base::RunLoop().RunUntilIdle();
391 } 394 }
392 }; 395 };
393 396
394 TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) { 397 TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) {
395 helper_.reset(new FailToStartWorkerTestHelper); 398 helper_.reset(new FailToStartWorkerTestHelper);
396 399
397 scoped_refptr<ServiceWorkerRegistration> registration = 400 scoped_refptr<ServiceWorkerRegistration> registration =
398 RunRegisterJob(GURL("http://www.example.com/"), 401 RunRegisterJob(GURL("http://www.example.com/"),
399 GURL("http://www.example.com/service_worker.js"), 402 GURL("http://www.example.com/service_worker.js"),
400 SERVICE_WORKER_ERROR_START_WORKER_FAILED); 403 SERVICE_WORKER_ERROR_START_WORKER_FAILED);
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 EXPECT_TRUE(called); 836 EXPECT_TRUE(called);
834 EXPECT_TRUE(registration.get()); 837 EXPECT_TRUE(registration.get());
835 EXPECT_TRUE(registration->active_version()); 838 EXPECT_TRUE(registration->active_version());
836 EXPECT_FALSE(registration->installing_version()); 839 EXPECT_FALSE(registration->installing_version());
837 EXPECT_FALSE(registration->waiting_version()); 840 EXPECT_FALSE(registration->waiting_version());
838 registration_ = registration; 841 registration_ = registration;
839 return registration; 842 return registration;
840 } 843 }
841 844
842 // EmbeddedWorkerTestHelper overrides 845 // EmbeddedWorkerTestHelper overrides
843 void OnStartWorker( 846 void OnStartWorker(int embedded_worker_id,
844 int embedded_worker_id, 847 int64_t version_id,
845 int64_t version_id, 848 const GURL& scope,
846 const GURL& scope, 849 const GURL& script,
847 const GURL& script, 850 bool pause_after_download,
848 bool pause_after_download, 851 mojom::ServiceWorkerEventDispatcherRequest request,
849 mojom::ServiceWorkerEventDispatcherRequest request) override { 852 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo
853 instance_host) override {
850 const std::string kMockScriptBody = "mock_script"; 854 const std::string kMockScriptBody = "mock_script";
851 const uint64_t kMockScriptSize = 19284; 855 const uint64_t kMockScriptSize = 19284;
852 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); 856 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id);
853 ServiceWorkerRegistration* registration = 857 ServiceWorkerRegistration* registration =
854 context()->GetLiveRegistration(version->registration_id()); 858 context()->GetLiveRegistration(version->registration_id());
855 bool is_update = registration->active_version() && 859 bool is_update = registration->active_version() &&
856 version != registration->active_version(); 860 version != registration->active_version();
857 861
858 ASSERT_TRUE(version); 862 ASSERT_TRUE(version);
859 version->AddListener(this); 863 version->AddListener(this);
(...skipping 17 matching lines...) Expand all
877 version->SetMainScriptHttpResponseInfo( 881 version->SetMainScriptHttpResponseInfo(
878 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); 882 EmbeddedWorkerTestHelper::CreateHttpResponseInfo());
879 } else { 883 } else {
880 if (script.GetOrigin() == kNoChangeOrigin) { 884 if (script.GetOrigin() == kNoChangeOrigin) {
881 // Simulate fetching the updated script and finding it's identical to 885 // Simulate fetching the updated script and finding it's identical to
882 // the incumbent. 886 // the incumbent.
883 version->script_cache_map()->NotifyFinishedCaching( 887 version->script_cache_map()->NotifyFinishedCaching(
884 script, kMockScriptSize, net::ERR_FILE_EXISTS, std::string()); 888 script, kMockScriptSize, net::ERR_FILE_EXISTS, std::string());
885 version->SetMainScriptHttpResponseInfo( 889 version->SetMainScriptHttpResponseInfo(
886 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); 890 EmbeddedWorkerTestHelper::CreateHttpResponseInfo());
887 SimulateWorkerScriptLoaded(embedded_worker_id); 891
892 mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr;
893 instance_host_ptr.Bind(std::move(instance_host));
894 instance_host_ptr->OnScriptLoaded();
895 base::RunLoop().RunUntilIdle();
888 return; 896 return;
889 } 897 }
890 898
891 // Spoof caching the script for the new version. 899 // Spoof caching the script for the new version.
892 WriteStringResponse(storage(), resource_id, "mock_different_script"); 900 WriteStringResponse(storage(), resource_id, "mock_different_script");
893 version->script_cache_map()->NotifyFinishedCaching( 901 version->script_cache_map()->NotifyFinishedCaching(
894 script, kMockScriptSize, net::OK, std::string()); 902 script, kMockScriptSize, net::OK, std::string());
895 version->SetMainScriptHttpResponseInfo( 903 version->SetMainScriptHttpResponseInfo(
896 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); 904 EmbeddedWorkerTestHelper::CreateHttpResponseInfo());
897 } 905 }
898 906
899 EmbeddedWorkerTestHelper::OnStartWorker(embedded_worker_id, version_id, 907 EmbeddedWorkerTestHelper::OnStartWorker(
900 scope, script, pause_after_download, 908 embedded_worker_id, version_id, scope, script, pause_after_download,
901 std::move(request)); 909 std::move(request), std::move(instance_host));
902 } 910 }
903 911
904 void OnResumeAfterDownload(int embedded_worker_id) override { 912 void OnResumeAfterDownload(int embedded_worker_id) override {
905 if (!force_start_worker_failure_) { 913 if (!force_start_worker_failure_) {
906 EmbeddedWorkerTestHelper::OnResumeAfterDownload(embedded_worker_id); 914 EmbeddedWorkerTestHelper::OnResumeAfterDownload(embedded_worker_id);
907 } else { 915 } else {
908 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id); 916 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id,
917 GetNextProviderId());
909 SimulateWorkerScriptEvaluated(embedded_worker_id, false); 918 SimulateWorkerScriptEvaluated(embedded_worker_id, false);
910 } 919 }
911 } 920 }
912 921
913 // ServiceWorkerRegistration::Listener overrides 922 // ServiceWorkerRegistration::Listener overrides
914 void OnVersionAttributesChanged( 923 void OnVersionAttributesChanged(
915 ServiceWorkerRegistration* registration, 924 ServiceWorkerRegistration* registration,
916 ChangedVersionAttributesMask changed_mask, 925 ChangedVersionAttributesMask changed_mask,
917 const ServiceWorkerRegistrationInfo& info) override { 926 const ServiceWorkerRegistrationInfo& info) override {
918 AttributeChangeLogEntry entry; 927 AttributeChangeLogEntry entry;
(...skipping 27 matching lines...) Expand all
946 bool force_start_worker_failure_ = false; 955 bool force_start_worker_failure_ = false;
947 }; 956 };
948 957
949 // Helper class for update tests that evicts the active version when the update 958 // Helper class for update tests that evicts the active version when the update
950 // worker is about to be started. 959 // worker is about to be started.
951 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { 960 class EvictIncumbentVersionHelper : public UpdateJobTestHelper {
952 public: 961 public:
953 EvictIncumbentVersionHelper() {} 962 EvictIncumbentVersionHelper() {}
954 ~EvictIncumbentVersionHelper() override {} 963 ~EvictIncumbentVersionHelper() override {}
955 964
956 void OnStartWorker( 965 void OnStartWorker(int embedded_worker_id,
957 int embedded_worker_id, 966 int64_t version_id,
958 int64_t version_id, 967 const GURL& scope,
959 const GURL& scope, 968 const GURL& script,
960 const GURL& script, 969 bool pause_after_download,
961 bool pause_after_download, 970 mojom::ServiceWorkerEventDispatcherRequest request,
962 mojom::ServiceWorkerEventDispatcherRequest request) override { 971 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo
972 instance_host) override {
963 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); 973 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id);
964 ServiceWorkerRegistration* registration = 974 ServiceWorkerRegistration* registration =
965 context()->GetLiveRegistration(version->registration_id()); 975 context()->GetLiveRegistration(version->registration_id());
966 bool is_update = registration->active_version() && 976 bool is_update = registration->active_version() &&
967 version != registration->active_version(); 977 version != registration->active_version();
968 if (is_update) { 978 if (is_update) {
969 // Evict the incumbent worker. 979 // Evict the incumbent worker.
970 ASSERT_FALSE(registration->waiting_version()); 980 ASSERT_FALSE(registration->waiting_version());
971 registration->DeleteVersion( 981 registration->DeleteVersion(
972 make_scoped_refptr(registration->active_version())); 982 make_scoped_refptr(registration->active_version()));
973 } 983 }
974 UpdateJobTestHelper::OnStartWorker(embedded_worker_id, version_id, scope, 984 UpdateJobTestHelper::OnStartWorker(
975 script, pause_after_download, 985 embedded_worker_id, version_id, scope, script, pause_after_download,
976 std::move(request)); 986 std::move(request), std::move(instance_host));
977 } 987 }
978 988
979 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override { 989 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override {
980 registration_failed_ = true; 990 registration_failed_ = true;
981 } 991 }
982 992
983 bool registration_failed_ = false; 993 bool registration_failed_ = false;
984 }; 994 };
985 995
986 } // namespace 996 } // namespace
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after
1638 public: 1648 public:
1639 explicit CheckPauseAfterDownloadEmbeddedWorkerInstanceClient( 1649 explicit CheckPauseAfterDownloadEmbeddedWorkerInstanceClient(
1640 base::WeakPtr<EmbeddedWorkerTestHelper> helper) 1650 base::WeakPtr<EmbeddedWorkerTestHelper> helper)
1641 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} 1651 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {}
1642 int num_of_startworker() const { return num_of_startworker_; } 1652 int num_of_startworker() const { return num_of_startworker_; }
1643 void set_next_pause_after_download(bool expectation) { 1653 void set_next_pause_after_download(bool expectation) {
1644 next_pause_after_download_ = expectation; 1654 next_pause_after_download_ = expectation;
1645 } 1655 }
1646 1656
1647 protected: 1657 protected:
1648 void StartWorker( 1658 void StartWorker(const EmbeddedWorkerStartParams& params,
1649 const EmbeddedWorkerStartParams& params, 1659 mojom::ServiceWorkerEventDispatcherRequest request,
1650 mojom::ServiceWorkerEventDispatcherRequest request) override { 1660 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo
1661 instance_host) override {
1651 ASSERT_TRUE(next_pause_after_download_.has_value()); 1662 ASSERT_TRUE(next_pause_after_download_.has_value());
1652 EXPECT_EQ(next_pause_after_download_.value(), params.pause_after_download); 1663 EXPECT_EQ(next_pause_after_download_.value(), params.pause_after_download);
1653 num_of_startworker_++; 1664 num_of_startworker_++;
1654 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( 1665 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker(
1655 params, std::move(request)); 1666 params, std::move(request), std::move(instance_host));
1656 } 1667 }
1657 1668
1658 private: 1669 private:
1659 base::Optional<bool> next_pause_after_download_; 1670 base::Optional<bool> next_pause_after_download_;
1660 int num_of_startworker_ = 0; 1671 int num_of_startworker_ = 0;
1661 DISALLOW_COPY_AND_ASSIGN(CheckPauseAfterDownloadEmbeddedWorkerInstanceClient); 1672 DISALLOW_COPY_AND_ASSIGN(CheckPauseAfterDownloadEmbeddedWorkerInstanceClient);
1662 }; 1673 };
1663 1674
1664 TEST_F(ServiceWorkerJobTest, Update_PauseAfterDownload) { 1675 TEST_F(ServiceWorkerJobTest, Update_PauseAfterDownload) {
1665 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; 1676 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1738 // should not be promoted to ACTIVATED because failure occur 1749 // should not be promoted to ACTIVATED because failure occur
1739 // during shutdown. 1750 // during shutdown.
1740 runner->RunUntilIdle(); 1751 runner->RunUntilIdle();
1741 base::RunLoop().RunUntilIdle(); 1752 base::RunLoop().RunUntilIdle();
1742 EXPECT_EQ(new_version.get(), registration->active_version()); 1753 EXPECT_EQ(new_version.get(), registration->active_version());
1743 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); 1754 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
1744 registration->RemoveListener(update_helper); 1755 registration->RemoveListener(update_helper);
1745 } 1756 }
1746 1757
1747 } // namespace content 1758 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698