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

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

Issue 1525743002: Allow ServiceWorker events to have custom durations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments Created 5 years 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/message_port_service.h" 7 #include "content/browser/message_port_service.h"
8 #include "content/browser/service_worker/embedded_worker_registry.h" 8 #include "content/browser/service_worker/embedded_worker_registry.h"
9 #include "content/browser/service_worker/embedded_worker_test_helper.h" 9 #include "content/browser/service_worker/embedded_worker_test_helper.h"
10 #include "content/browser/service_worker/service_worker_context_core.h" 10 #include "content/browser/service_worker/service_worker_context_core.h"
11 #include "content/browser/service_worker/service_worker_registration.h" 11 #include "content/browser/service_worker/service_worker_registration.h"
12 #include "content/browser/service_worker/service_worker_test_utils.h" 12 #include "content/browser/service_worker/service_worker_test_utils.h"
13 #include "content/browser/service_worker/service_worker_version.h" 13 #include "content/browser/service_worker/service_worker_version.h"
14 #include "content/common/background_sync_service.mojom.h"
14 #include "content/common/service_worker/service_worker_utils.h" 15 #include "content/common/service_worker/service_worker_utils.h"
15 #include "content/public/test/mock_render_process_host.h" 16 #include "content/public/test/mock_render_process_host.h"
16 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "mojo/public/cpp/bindings/strong_binding.h"
17 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
18 20
19 // IPC messages for testing --------------------------------------------------- 21 // IPC messages for testing ---------------------------------------------------
20 22
21 #define IPC_MESSAGE_IMPL 23 #define IPC_MESSAGE_IMPL
22 #include "ipc/ipc_message_macros.h" 24 #include "ipc/ipc_message_macros.h"
23 25
24 #define IPC_MESSAGE_START TestMsgStart 26 #define IPC_MESSAGE_START TestMsgStart
25 27
26 IPC_MESSAGE_CONTROL0(TestMsg_Message) 28 IPC_MESSAGE_CONTROL0(TestMsg_Message)
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 63
62 private: 64 private:
63 void OnMessage() { 65 void OnMessage() {
64 // Do nothing. 66 // Do nothing.
65 } 67 }
66 68
67 int current_embedded_worker_id_; 69 int current_embedded_worker_id_;
68 DISALLOW_COPY_AND_ASSIGN(MessageReceiver); 70 DISALLOW_COPY_AND_ASSIGN(MessageReceiver);
69 }; 71 };
70 72
73 class MockBackgroundSyncServiceClient : public BackgroundSyncServiceClient {
74 public:
75 MockBackgroundSyncServiceClient(
76 mojo::InterfaceRequest<BackgroundSyncServiceClient> request)
77 : binding_(this, request.Pass()) {}
78
79 private:
80 // BackgroundSyncServiceClient overrides
81 void Sync(int64_t handle_id,
82 content::BackgroundSyncEventLastChance last_chance,
83 const SyncCallback& callback) override {
84 // Wait forever instead of calling the callback.
85 }
86
87 mojo::StrongBinding<BackgroundSyncServiceClient> binding_;
88
89 DISALLOW_COPY_AND_ASSIGN(MockBackgroundSyncServiceClient);
90 };
91
71 void VerifyCalled(bool* called) { 92 void VerifyCalled(bool* called) {
72 *called = true; 93 *called = true;
73 } 94 }
74 95
75 void ObserveStatusChanges(ServiceWorkerVersion* version, 96 void ObserveStatusChanges(ServiceWorkerVersion* version,
76 std::vector<ServiceWorkerVersion::Status>* statuses) { 97 std::vector<ServiceWorkerVersion::Status>* statuses) {
77 statuses->push_back(version->status()); 98 statuses->push_back(version->status());
78 version->RegisterStatusChangeCallback( 99 version->RegisterStatusChangeCallback(
79 base::Bind(&ObserveStatusChanges, base::Unretained(version), statuses)); 100 base::Bind(&ObserveStatusChanges, base::Unretained(version), statuses));
80 } 101 }
81 102
82 void ReceiveFetchResult(ServiceWorkerStatusCode* status, 103 void ReceiveFetchResult(ServiceWorkerStatusCode* status,
83 ServiceWorkerStatusCode actual_status, 104 ServiceWorkerStatusCode actual_status,
84 ServiceWorkerFetchEventResult actual_result, 105 ServiceWorkerFetchEventResult actual_result,
85 const ServiceWorkerResponse& response) { 106 const ServiceWorkerResponse& response) {
86 *status = actual_status; 107 *status = actual_status;
87 } 108 }
88 109
110 void ReceiveSyncStatus(ServiceWorkerStatusCode* status,
111 ServiceWorkerStatusCode actual_status) {
112 *status = actual_status;
113 }
114
89 // A specialized listener class to receive test messages from a worker. 115 // A specialized listener class to receive test messages from a worker.
90 class MessageReceiverFromWorker : public EmbeddedWorkerInstance::Listener { 116 class MessageReceiverFromWorker : public EmbeddedWorkerInstance::Listener {
91 public: 117 public:
92 explicit MessageReceiverFromWorker(EmbeddedWorkerInstance* instance) 118 explicit MessageReceiverFromWorker(EmbeddedWorkerInstance* instance)
93 : instance_(instance) { 119 : instance_(instance) {
94 instance_->AddListener(this); 120 instance_->AddListener(this);
95 } 121 }
96 ~MessageReceiverFromWorker() override { instance_->RemoveListener(this); } 122 ~MessageReceiverFromWorker() override { instance_->RemoveListener(this); }
97 123
98 void OnStarted() override { NOTREACHED(); } 124 void OnStarted() override { NOTREACHED(); }
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 version_.get(), 199 version_.get(),
174 CreateReceiverOnCurrentThread(&status)); 200 CreateReceiverOnCurrentThread(&status));
175 base::RunLoop().RunUntilIdle(); 201 base::RunLoop().RunUntilIdle();
176 ASSERT_EQ(SERVICE_WORKER_OK, status); 202 ASSERT_EQ(SERVICE_WORKER_OK, status);
177 203
178 // Simulate adding one process to the pattern. 204 // Simulate adding one process to the pattern.
179 helper_->SimulateAddProcessToPattern(pattern_, 205 helper_->SimulateAddProcessToPattern(pattern_,
180 helper_->mock_render_process_id()); 206 helper_->mock_render_process_id());
181 ASSERT_TRUE(helper_->context()->process_manager() 207 ASSERT_TRUE(helper_->context()->process_manager()
182 ->PatternHasProcessToRun(pattern_)); 208 ->PatternHasProcessToRun(pattern_));
209
210 // Create a mock BackgroundSyncServiceClient
211 mojo::InterfaceRequest<BackgroundSyncServiceClient> service_request =
Marijn Kruisselbrink 2015/12/16 19:09:57 FYI, after I clean up and land https://codereview.
jkarlin 2015/12/16 20:44:27 Nice!
212 mojo::GetProxy(&version_->background_sync_dispatcher_);
213 // The MocKBackgroundSyncServiceClient is bound to the client, and will be
Marijn Kruisselbrink 2015/12/16 19:09:57 s/MocK/Mock/
jkarlin 2015/12/16 20:44:27 Done.
214 // deleted when the client is deleted.
215 new MockBackgroundSyncServiceClient(service_request.Pass());
216 base::RunLoop().RunUntilIdle();
183 } 217 }
184 218
185 virtual scoped_ptr<MessageReceiver> GetMessageReceiver() { 219 virtual scoped_ptr<MessageReceiver> GetMessageReceiver() {
186 return make_scoped_ptr(new MessageReceiver()); 220 return make_scoped_ptr(new MessageReceiver());
187 } 221 }
188 222
189 void TearDown() override { 223 void TearDown() override {
190 version_ = 0; 224 version_ = 0;
191 registration_ = 0; 225 registration_ = 0;
192 helper_.reset(); 226 helper_.reset();
(...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after
746 base::Bind(&base::DoNothing), 780 base::Bind(&base::DoNothing),
747 base::Bind(&ReceiveFetchResult, &status)); 781 base::Bind(&ReceiveFetchResult, &status));
748 base::RunLoop().RunUntilIdle(); 782 base::RunLoop().RunUntilIdle();
749 783
750 // Callback has not completed yet. 784 // Callback has not completed yet.
751 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, status); 785 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, status);
752 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status()); 786 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
753 787
754 // Simulate timeout. 788 // Simulate timeout.
755 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); 789 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
756 version_->SetAllRequestTimes( 790 version_->SetAllRequestExpirations(base::TimeTicks::Now());
757 base::TimeTicks::Now() -
758 base::TimeDelta::FromMinutes(
759 ServiceWorkerVersion::kRequestTimeoutMinutes + 1));
760 version_->timeout_timer_.user_task().Run(); 791 version_->timeout_timer_.user_task().Run();
761 base::RunLoop().RunUntilIdle(); 792 base::RunLoop().RunUntilIdle();
762 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status); 793 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status);
763 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status()); 794 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
764 } 795 }
765 796
797 TEST_F(ServiceWorkerVersionTest, RequestCustomizedTimeout) {
798 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value
799 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
800
801 // Create a sync request that should expire Now().
802 version_->DispatchSyncEvent(0 /* sync handle id */,
803 BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE,
804 base::TimeTicks::Now(), /* expiration time */
805 base::Bind(&ReceiveSyncStatus, &status));
806 base::RunLoop().RunUntilIdle();
807 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
808 version_->timeout_timer_.user_task().Run();
809 base::RunLoop().RunUntilIdle();
810 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status);
811 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
812 }
813
814 TEST_F(ServiceWorkerWaitForeverInFetchTest, MixedRequestTimeouts) {
815 ServiceWorkerStatusCode sync_status =
816 SERVICE_WORKER_ERROR_NETWORK; // dummy value
817 ServiceWorkerStatusCode fetch_status =
818 SERVICE_WORKER_ERROR_NETWORK; // dummy value
819 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
820
821 // Create a fetch request that should expire sometime later.
822 version_->DispatchFetchEvent(ServiceWorkerFetchRequest(),
823 base::Bind(&base::DoNothing),
824 base::Bind(&ReceiveFetchResult, &fetch_status));
825 // Create a sync request that should expire Now().
826 version_->DispatchSyncEvent(0 /* sync handle id */,
827 BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE,
828 base::TimeTicks::Now(), /* expiration time */
829 base::Bind(&ReceiveSyncStatus, &sync_status));
830 base::RunLoop().RunUntilIdle();
831 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, sync_status);
832
833 // Verify the sync has timed out but not the fetch.
834 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
835 version_->timeout_timer_.user_task().Run();
836 base::RunLoop().RunUntilIdle();
837 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, sync_status);
838 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, fetch_status);
839 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
840
841 // Verify that the fetch times out later.
842 version_->SetAllRequestExpirations(base::TimeTicks::Now());
843 version_->timeout_timer_.user_task().Run();
844 base::RunLoop().RunUntilIdle();
845 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, fetch_status);
846 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
847 }
848
766 TEST_F(ServiceWorkerFailToStartTest, RendererCrash) { 849 TEST_F(ServiceWorkerFailToStartTest, RendererCrash) {
767 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value 850 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value
768 version_->StartWorker( 851 version_->StartWorker(
769 CreateReceiverOnCurrentThread(&status)); 852 CreateReceiverOnCurrentThread(&status));
770 base::RunLoop().RunUntilIdle(); 853 base::RunLoop().RunUntilIdle();
771 854
772 // Callback has not completed yet. 855 // Callback has not completed yet.
773 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, status); 856 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, status);
774 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status()); 857 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
775 858
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 GURL valid_scope_2("http://www.example.com/test/subscope"); 1015 GURL valid_scope_2("http://www.example.com/test/subscope");
933 version_->OnRegisterForeignFetchScopes(std::vector<GURL>(1, valid_scope_2)); 1016 version_->OnRegisterForeignFetchScopes(std::vector<GURL>(1, valid_scope_2));
934 base::RunLoop().RunUntilIdle(); 1017 base::RunLoop().RunUntilIdle();
935 EXPECT_EQ(3, helper_->mock_render_process_host()->bad_msg_count()); 1018 EXPECT_EQ(3, helper_->mock_render_process_host()->bad_msg_count());
936 EXPECT_EQ(2u, version_->foreign_fetch_scopes_.size()); 1019 EXPECT_EQ(2u, version_->foreign_fetch_scopes_.size());
937 EXPECT_EQ(valid_scope_1, version_->foreign_fetch_scopes_[0]); 1020 EXPECT_EQ(valid_scope_1, version_->foreign_fetch_scopes_[0]);
938 EXPECT_EQ(valid_scope_2, version_->foreign_fetch_scopes_[1]); 1021 EXPECT_EQ(valid_scope_2, version_->foreign_fetch_scopes_[1]);
939 } 1022 }
940 1023
941 } // namespace content 1024 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698