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

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

Issue 1675613002: service worker: use 200 OK for update requests even in the no update case (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: asan and fix win compile? Created 4 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 <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"
11 #include "content/browser/browser_thread_impl.h" 11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/service_worker/embedded_worker_registry.h" 12 #include "content/browser/service_worker/embedded_worker_registry.h"
13 #include "content/browser/service_worker/embedded_worker_test_helper.h" 13 #include "content/browser/service_worker/embedded_worker_test_helper.h"
14 #include "content/browser/service_worker/service_worker_context_core.h" 14 #include "content/browser/service_worker/service_worker_context_core.h"
15 #include "content/browser/service_worker/service_worker_disk_cache.h" 15 #include "content/browser/service_worker/service_worker_disk_cache.h"
16 #include "content/browser/service_worker/service_worker_job_coordinator.h" 16 #include "content/browser/service_worker/service_worker_job_coordinator.h"
17 #include "content/browser/service_worker/service_worker_registration.h" 17 #include "content/browser/service_worker/service_worker_registration.h"
18 #include "content/browser/service_worker/service_worker_registration_status.h" 18 #include "content/browser/service_worker/service_worker_registration_status.h"
19 #include "content/browser/service_worker/service_worker_test_utils.h" 19 #include "content/browser/service_worker/service_worker_test_utils.h"
20 #include "content/common/service_worker/embedded_worker_messages.h"
20 #include "content/common/service_worker/service_worker_messages.h" 21 #include "content/common/service_worker/service_worker_messages.h"
21 #include "content/public/test/test_browser_thread_bundle.h" 22 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "ipc/ipc_test_sink.h" 23 #include "ipc/ipc_test_sink.h"
23 #include "net/base/io_buffer.h" 24 #include "net/base/io_buffer.h"
24 #include "net/base/net_errors.h" 25 #include "net/base/net_errors.h"
25 #include "net/base/test_completion_callback.h" 26 #include "net/base/test_completion_callback.h"
26 #include "net/http/http_response_headers.h" 27 #include "net/http/http_response_headers.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 29
29 using net::IOBuffer; 30 using net::IOBuffer;
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 ASSERT_EQ(new_registration, old_registration); 355 ASSERT_EQ(new_registration, old_registration);
355 } 356 }
356 357
357 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { 358 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper {
358 public: 359 public:
359 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} 360 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {}
360 361
361 void OnStartWorker(int embedded_worker_id, 362 void OnStartWorker(int embedded_worker_id,
362 int64_t service_worker_version_id, 363 int64_t service_worker_version_id,
363 const GURL& scope, 364 const GURL& scope,
364 const GURL& script_url) override { 365 const GURL& script_url,
366 bool pause_after_download) override {
365 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 367 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
366 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id); 368 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id);
367 } 369 }
368 }; 370 };
369 371
370 TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) { 372 TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) {
371 helper_.reset(new FailToStartWorkerTestHelper); 373 helper_.reset(new FailToStartWorkerTestHelper);
372 374
373 scoped_refptr<ServiceWorkerRegistration> registration = 375 scoped_refptr<ServiceWorkerRegistration> registration =
374 RunRegisterJob(GURL("http://www.example.com/"), 376 RunRegisterJob(GURL("http://www.example.com/"),
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 EXPECT_FALSE(registration->installing_version()); 819 EXPECT_FALSE(registration->installing_version());
818 EXPECT_FALSE(registration->waiting_version()); 820 EXPECT_FALSE(registration->waiting_version());
819 registration_ = registration; 821 registration_ = registration;
820 return registration; 822 return registration;
821 } 823 }
822 824
823 // EmbeddedWorkerTestHelper overrides 825 // EmbeddedWorkerTestHelper overrides
824 void OnStartWorker(int embedded_worker_id, 826 void OnStartWorker(int embedded_worker_id,
825 int64_t version_id, 827 int64_t version_id,
826 const GURL& scope, 828 const GURL& scope,
827 const GURL& script) override { 829 const GURL& script,
830 bool pause_after_download) override {
828 const std::string kMockScriptBody = "mock_script"; 831 const std::string kMockScriptBody = "mock_script";
829 const uint64_t kMockScriptSize = 19284; 832 const uint64_t kMockScriptSize = 19284;
830 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); 833 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id);
831 ServiceWorkerRegistration* registration = 834 ServiceWorkerRegistration* registration =
832 context()->GetLiveRegistration(version->registration_id()); 835 context()->GetLiveRegistration(version->registration_id());
833 bool is_update = registration->active_version() && 836 bool is_update = registration->active_version() &&
834 version != registration->active_version(); 837 version != registration->active_version();
835 838
836 ASSERT_TRUE(version); 839 ASSERT_TRUE(version);
837 version->AddListener(this); 840 version->AddListener(this);
838 841
839 if (force_bypass_cache_for_scripts()) 842 if (force_bypass_cache_for_scripts())
840 version->set_force_bypass_cache_for_scripts(true); 843 version->set_force_bypass_cache_for_scripts(true);
844
841 if (!is_update) { 845 if (!is_update) {
842 // Spoof caching the script for the initial version. 846 // Spoof caching the script for the initial version.
843 int64_t resource_id = storage()->NewResourceId(); 847 int64_t resource_id = storage()->NewResourceId();
844 version->script_cache_map()->NotifyStartedCaching(script, resource_id); 848 version->script_cache_map()->NotifyStartedCaching(script, resource_id);
845 WriteStringResponse(storage(), resource_id, kMockScriptBody); 849 WriteStringResponse(storage(), resource_id, kMockScriptBody);
846 version->script_cache_map()->NotifyFinishedCaching( 850 version->script_cache_map()->NotifyFinishedCaching(
847 script, kMockScriptSize, net::URLRequestStatus(), std::string()); 851 script, kMockScriptSize, net::URLRequestStatus(), std::string());
848 } else { 852 } else {
849 if (script.GetOrigin() == kNoChangeOrigin) { 853 if (script.GetOrigin() == kNoChangeOrigin) {
850 version->SetStartWorkerStatusCode(SERVICE_WORKER_ERROR_EXISTS); 854 version->SetStartWorkerStatusCode(SERVICE_WORKER_ERROR_EXISTS);
851 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id); 855 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id);
852 return; 856 return;
853 } 857 }
854 858
855 // Spoof caching the script for the new version. 859 // Spoof caching the script for the new version.
856 int64_t resource_id = storage()->NewResourceId(); 860 int64_t resource_id = storage()->NewResourceId();
857 version->script_cache_map()->NotifyStartedCaching(script, resource_id); 861 version->script_cache_map()->NotifyStartedCaching(script, resource_id);
858 WriteStringResponse(storage(), resource_id, "mock_different_script"); 862 WriteStringResponse(storage(), resource_id, "mock_different_script");
859 version->script_cache_map()->NotifyFinishedCaching( 863 version->script_cache_map()->NotifyFinishedCaching(
860 script, kMockScriptSize, net::URLRequestStatus(), std::string()); 864 script, kMockScriptSize, net::URLRequestStatus(), std::string());
861 } 865 }
866
867 EmbeddedWorkerTestHelper::OnStartWorker(
868 embedded_worker_id, version_id, scope, script, pause_after_download);
869 }
870
871 void OnResumeAfterDownload(int embedded_worker_id) override {
862 if (!force_start_worker_failure_) { 872 if (!force_start_worker_failure_) {
863 EmbeddedWorkerTestHelper::OnStartWorker(embedded_worker_id, version_id, 873 EmbeddedWorkerTestHelper::OnResumeAfterDownload(embedded_worker_id);
864 scope, script);
865 } else { 874 } else {
866 (embedded_worker_id_service_worker_version_id_map())[embedded_worker_id] =
867 version_id;
868 SimulateWorkerReadyForInspection(embedded_worker_id);
869 SimulateWorkerScriptCached(embedded_worker_id);
870 SimulateWorkerScriptLoaded(embedded_worker_id);
871 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id); 875 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id);
872 SimulateWorkerScriptEvaluated(embedded_worker_id, false); 876 SimulateWorkerScriptEvaluated(embedded_worker_id, false);
873 } 877 }
874 } 878 }
875 879
876 // ServiceWorkerRegistration::Listener overrides 880 // ServiceWorkerRegistration::Listener overrides
877 void OnVersionAttributesChanged( 881 void OnVersionAttributesChanged(
878 ServiceWorkerRegistration* registration, 882 ServiceWorkerRegistration* registration,
879 ChangedVersionAttributesMask changed_mask, 883 ChangedVersionAttributesMask changed_mask,
880 const ServiceWorkerRegistrationInfo& info) override { 884 const ServiceWorkerRegistrationInfo& info) override {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
913 // Helper class for update tests that evicts the active version when the update 917 // Helper class for update tests that evicts the active version when the update
914 // worker is about to be started. 918 // worker is about to be started.
915 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { 919 class EvictIncumbentVersionHelper : public UpdateJobTestHelper {
916 public: 920 public:
917 EvictIncumbentVersionHelper() {} 921 EvictIncumbentVersionHelper() {}
918 ~EvictIncumbentVersionHelper() override {} 922 ~EvictIncumbentVersionHelper() override {}
919 923
920 void OnStartWorker(int embedded_worker_id, 924 void OnStartWorker(int embedded_worker_id,
921 int64_t version_id, 925 int64_t version_id,
922 const GURL& scope, 926 const GURL& scope,
923 const GURL& script) override { 927 const GURL& script,
928 bool pause_after_download) override {
924 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); 929 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id);
925 ServiceWorkerRegistration* registration = 930 ServiceWorkerRegistration* registration =
926 context()->GetLiveRegistration(version->registration_id()); 931 context()->GetLiveRegistration(version->registration_id());
927 bool is_update = registration->active_version() && 932 bool is_update = registration->active_version() &&
928 version != registration->active_version(); 933 version != registration->active_version();
929 if (is_update) { 934 if (is_update) {
930 // Evict the incumbent worker. 935 // Evict the incumbent worker.
931 ASSERT_FALSE(registration->waiting_version()); 936 ASSERT_FALSE(registration->waiting_version());
932 registration->DeleteVersion( 937 registration->DeleteVersion(
933 make_scoped_refptr(registration->active_version())); 938 make_scoped_refptr(registration->active_version()));
934 } 939 }
935 UpdateJobTestHelper::OnStartWorker(embedded_worker_id, version_id, scope, 940 UpdateJobTestHelper::OnStartWorker(embedded_worker_id, version_id, scope,
936 script); 941 script, pause_after_download);
937 } 942 }
938 943
939 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override { 944 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override {
940 registration_failed_ = true; 945 registration_failed_ = true;
941 } 946 }
942 947
943 bool registration_failed_ = false; 948 bool registration_failed_ = false;
944 }; 949 };
945 950
946 } // namespace 951 } // namespace
(...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after
1526 // Verify the registration remains. 1531 // Verify the registration remains.
1527 EXPECT_FALSE(registration->is_uninstalling()); 1532 EXPECT_FALSE(registration->is_uninstalling());
1528 EXPECT_FALSE(registration->is_uninstalled()); 1533 EXPECT_FALSE(registration->is_uninstalled());
1529 1534
1530 EXPECT_EQ(ServiceWorkerVersion::STOPPED, old_version->running_status()); 1535 EXPECT_EQ(ServiceWorkerVersion::STOPPED, old_version->running_status());
1531 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, old_version->status()); 1536 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, old_version->status());
1532 1537
1533 FindRegistrationForPattern(pattern, SERVICE_WORKER_OK); 1538 FindRegistrationForPattern(pattern, SERVICE_WORKER_OK);
1534 } 1539 }
1535 1540
1541 TEST_F(ServiceWorkerJobTest, Update_PauseAfterDownload) {
1542 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper;
1543 helper_.reset(update_helper);
1544 IPC::TestSink* sink = update_helper->ipc_sink();
1545
1546 // The initial version should not pause after download.
1547 scoped_refptr<ServiceWorkerRegistration> registration =
1548 update_helper->SetupInitialRegistration(kNewVersionOrigin);
1549 {
1550 const IPC::Message* start_msg =
1551 sink->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID);
1552 ASSERT_TRUE(start_msg);
1553 EmbeddedWorkerMsg_StartWorker::Param param;
1554 EmbeddedWorkerMsg_StartWorker::Read(start_msg, &param);
1555 EmbeddedWorkerMsg_StartWorker_Params start_params = base::get<0>(param);
1556 EXPECT_FALSE(start_params.pause_after_download);
1557 sink->ClearMessages();
1558 }
1559
1560 // The updated version should pause after download.
1561 registration->AddListener(update_helper);
1562 registration->active_version()->StartUpdate();
1563 base::RunLoop().RunUntilIdle();
1564 {
1565 const IPC::Message* start_msg =
1566 sink->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID);
1567 ASSERT_TRUE(start_msg);
1568 EmbeddedWorkerMsg_StartWorker::Param param;
1569 EmbeddedWorkerMsg_StartWorker::Read(start_msg, &param);
1570 EmbeddedWorkerMsg_StartWorker_Params start_params = base::get<0>(param);
1571 EXPECT_TRUE(start_params.pause_after_download);
1572 sink->ClearMessages();
1573 }
1574 }
1575
1536 } // namespace content 1576 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698