OLD | NEW |
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 "content/browser/service_worker/service_worker_process_manager.h" | 5 #include "content/browser/service_worker/service_worker_process_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "content/browser/service_worker/service_worker_test_utils.h" |
11 #include "content/common/service_worker/embedded_worker_settings.h" | 12 #include "content/common/service_worker/embedded_worker_settings.h" |
12 #include "content/public/common/child_process_host.h" | 13 #include "content/public/common/child_process_host.h" |
13 #include "content/public/test/mock_render_process_host.h" | 14 #include "content/public/test/mock_render_process_host.h" |
14 #include "content/public/test/test_browser_context.h" | 15 #include "content/public/test/test_browser_context.h" |
15 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 std::unique_ptr<TestBrowserContext> browser_context_; | 63 std::unique_ptr<TestBrowserContext> browser_context_; |
63 std::unique_ptr<ServiceWorkerProcessManager> process_manager_; | 64 std::unique_ptr<ServiceWorkerProcessManager> process_manager_; |
64 GURL pattern_; | 65 GURL pattern_; |
65 GURL script_url_; | 66 GURL script_url_; |
66 | 67 |
67 private: | 68 private: |
68 content::TestBrowserThreadBundle thread_bundle_; | 69 content::TestBrowserThreadBundle thread_bundle_; |
69 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProcessManagerTest); | 70 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProcessManagerTest); |
70 }; | 71 }; |
71 | 72 |
72 TEST_F(ServiceWorkerProcessManagerTest, SortProcess) { | 73 class ServiceWorkerProcessManagerTestP |
| 74 : public MojoServiceWorkerTestP<ServiceWorkerProcessManagerTest> {}; |
| 75 |
| 76 TEST_P(ServiceWorkerProcessManagerTestP, SortProcess) { |
73 // Process 1 has 2 refs, 2 has 3 refs and 3 has 1 ref. | 77 // Process 1 has 2 refs, 2 has 3 refs and 3 has 1 ref. |
74 process_manager_->AddProcessReferenceToPattern(pattern_, 1); | 78 process_manager_->AddProcessReferenceToPattern(pattern_, 1); |
75 process_manager_->AddProcessReferenceToPattern(pattern_, 1); | 79 process_manager_->AddProcessReferenceToPattern(pattern_, 1); |
76 process_manager_->AddProcessReferenceToPattern(pattern_, 2); | 80 process_manager_->AddProcessReferenceToPattern(pattern_, 2); |
77 process_manager_->AddProcessReferenceToPattern(pattern_, 2); | 81 process_manager_->AddProcessReferenceToPattern(pattern_, 2); |
78 process_manager_->AddProcessReferenceToPattern(pattern_, 2); | 82 process_manager_->AddProcessReferenceToPattern(pattern_, 2); |
79 process_manager_->AddProcessReferenceToPattern(pattern_, 3); | 83 process_manager_->AddProcessReferenceToPattern(pattern_, 3); |
80 | 84 |
81 // Process 2 has the biggest # of references and it should be chosen. | 85 // Process 2 has the biggest # of references and it should be chosen. |
82 EXPECT_THAT(process_manager_->SortProcessesForPattern(pattern_), | 86 EXPECT_THAT(process_manager_->SortProcessesForPattern(pattern_), |
83 testing::ElementsAre(2, 1, 3)); | 87 testing::ElementsAre(2, 1, 3)); |
84 | 88 |
85 process_manager_->RemoveProcessReferenceFromPattern(pattern_, 1); | 89 process_manager_->RemoveProcessReferenceFromPattern(pattern_, 1); |
86 process_manager_->RemoveProcessReferenceFromPattern(pattern_, 1); | 90 process_manager_->RemoveProcessReferenceFromPattern(pattern_, 1); |
87 // Scores for each process: 2 : 3, 3 : 1, process 1 is removed. | 91 // Scores for each process: 2 : 3, 3 : 1, process 1 is removed. |
88 EXPECT_THAT(process_manager_->SortProcessesForPattern(pattern_), | 92 EXPECT_THAT(process_manager_->SortProcessesForPattern(pattern_), |
89 testing::ElementsAre(2, 3)); | 93 testing::ElementsAre(2, 3)); |
90 } | 94 } |
91 | 95 |
92 TEST_F(ServiceWorkerProcessManagerTest, FindAvailableProcess) { | 96 TEST_P(ServiceWorkerProcessManagerTestP, FindAvailableProcess) { |
93 std::unique_ptr<MockRenderProcessHost> host1(CreateRenderProcessHost()); | 97 std::unique_ptr<MockRenderProcessHost> host1(CreateRenderProcessHost()); |
94 std::unique_ptr<MockRenderProcessHost> host2(CreateRenderProcessHost()); | 98 std::unique_ptr<MockRenderProcessHost> host2(CreateRenderProcessHost()); |
95 std::unique_ptr<MockRenderProcessHost> host3(CreateRenderProcessHost()); | 99 std::unique_ptr<MockRenderProcessHost> host3(CreateRenderProcessHost()); |
96 | 100 |
97 // Process 1 has 2 refs, 2 has 3 refs and 3 has 1 ref. | 101 // Process 1 has 2 refs, 2 has 3 refs and 3 has 1 ref. |
98 process_manager_->AddProcessReferenceToPattern(pattern_, host1->GetID()); | 102 process_manager_->AddProcessReferenceToPattern(pattern_, host1->GetID()); |
99 process_manager_->AddProcessReferenceToPattern(pattern_, host1->GetID()); | 103 process_manager_->AddProcessReferenceToPattern(pattern_, host1->GetID()); |
100 process_manager_->AddProcessReferenceToPattern(pattern_, host2->GetID()); | 104 process_manager_->AddProcessReferenceToPattern(pattern_, host2->GetID()); |
101 process_manager_->AddProcessReferenceToPattern(pattern_, host2->GetID()); | 105 process_manager_->AddProcessReferenceToPattern(pattern_, host2->GetID()); |
102 process_manager_->AddProcessReferenceToPattern(pattern_, host2->GetID()); | 106 process_manager_->AddProcessReferenceToPattern(pattern_, host2->GetID()); |
(...skipping 11 matching lines...) Expand all Loading... |
114 // refcount should be chosen. | 118 // refcount should be chosen. |
115 host1->set_is_process_backgrounded(true); | 119 host1->set_is_process_backgrounded(true); |
116 host3->set_is_process_backgrounded(true); | 120 host3->set_is_process_backgrounded(true); |
117 EXPECT_EQ(host2->GetID(), process_manager_->FindAvailableProcess(pattern_)); | 121 EXPECT_EQ(host2->GetID(), process_manager_->FindAvailableProcess(pattern_)); |
118 | 122 |
119 // Process 3 should be chosen because it is the only foreground process. | 123 // Process 3 should be chosen because it is the only foreground process. |
120 host3->set_is_process_backgrounded(false); | 124 host3->set_is_process_backgrounded(false); |
121 EXPECT_EQ(host3->GetID(), process_manager_->FindAvailableProcess(pattern_)); | 125 EXPECT_EQ(host3->GetID(), process_manager_->FindAvailableProcess(pattern_)); |
122 } | 126 } |
123 | 127 |
124 TEST_F(ServiceWorkerProcessManagerTest, | 128 TEST_P(ServiceWorkerProcessManagerTestP, |
125 AllocateWorkerProcess_FindAvailableProcess) { | 129 AllocateWorkerProcess_FindAvailableProcess) { |
126 const int kEmbeddedWorkerId1 = 100; | 130 const int kEmbeddedWorkerId1 = 100; |
127 const int kEmbeddedWorkerId2 = 200; | 131 const int kEmbeddedWorkerId2 = 200; |
128 const int kEmbeddedWorkerId3 = 300; | 132 const int kEmbeddedWorkerId3 = 300; |
129 GURL scope1("http://example.com/scope1"); | 133 GURL scope1("http://example.com/scope1"); |
130 GURL scope2("http://example.com/scope2"); | 134 GURL scope2("http://example.com/scope2"); |
131 | 135 |
132 // Set up mock renderer process hosts. | 136 // Set up mock renderer process hosts. |
133 std::unique_ptr<MockRenderProcessHost> host1(CreateRenderProcessHost()); | 137 std::unique_ptr<MockRenderProcessHost> host1(CreateRenderProcessHost()); |
134 std::unique_ptr<MockRenderProcessHost> host2(CreateRenderProcessHost()); | 138 std::unique_ptr<MockRenderProcessHost> host2(CreateRenderProcessHost()); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 EXPECT_EQ(0, host2->worker_ref_count()); | 229 EXPECT_EQ(0, host2->worker_ref_count()); |
226 EXPECT_EQ(1u, instance_info.size()); | 230 EXPECT_EQ(1u, instance_info.size()); |
227 EXPECT_TRUE(base::ContainsKey(instance_info, kEmbeddedWorkerId2)); | 231 EXPECT_TRUE(base::ContainsKey(instance_info, kEmbeddedWorkerId2)); |
228 | 232 |
229 process_manager_->ReleaseWorkerProcess(kEmbeddedWorkerId2); | 233 process_manager_->ReleaseWorkerProcess(kEmbeddedWorkerId2); |
230 EXPECT_EQ(0, host1->worker_ref_count()); | 234 EXPECT_EQ(0, host1->worker_ref_count()); |
231 EXPECT_EQ(0, host2->worker_ref_count()); | 235 EXPECT_EQ(0, host2->worker_ref_count()); |
232 EXPECT_TRUE(instance_info.empty()); | 236 EXPECT_TRUE(instance_info.empty()); |
233 } | 237 } |
234 | 238 |
235 TEST_F(ServiceWorkerProcessManagerTest, AllocateWorkerProcess_InShutdown) { | 239 TEST_P(ServiceWorkerProcessManagerTestP, AllocateWorkerProcess_InShutdown) { |
236 process_manager_->Shutdown(); | 240 process_manager_->Shutdown(); |
237 ASSERT_TRUE(process_manager_->IsShutdown()); | 241 ASSERT_TRUE(process_manager_->IsShutdown()); |
238 | 242 |
239 base::RunLoop run_loop; | 243 base::RunLoop run_loop; |
240 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 244 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
241 int process_id = -10; | 245 int process_id = -10; |
242 bool is_new_process = true; | 246 bool is_new_process = true; |
243 process_manager_->AllocateWorkerProcess( | 247 process_manager_->AllocateWorkerProcess( |
244 1, pattern_, script_url_, true /* can_use_existing_process */, | 248 1, pattern_, script_url_, true /* can_use_existing_process */, |
245 base::Bind(&DidAllocateWorkerProcess, run_loop.QuitClosure(), &status, | 249 base::Bind(&DidAllocateWorkerProcess, run_loop.QuitClosure(), &status, |
246 &process_id, &is_new_process)); | 250 &process_id, &is_new_process)); |
247 run_loop.Run(); | 251 run_loop.Run(); |
248 | 252 |
249 // Allocating a process in shutdown should abort. | 253 // Allocating a process in shutdown should abort. |
250 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); | 254 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); |
251 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, process_id); | 255 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, process_id); |
252 EXPECT_FALSE(is_new_process); | 256 EXPECT_FALSE(is_new_process); |
253 EXPECT_TRUE(process_manager_->instance_info_.empty()); | 257 EXPECT_TRUE(process_manager_->instance_info_.empty()); |
254 } | 258 } |
255 | 259 |
| 260 INSTANTIATE_TEST_CASE_P(ServiceWorkerProcessManagerTest, |
| 261 ServiceWorkerProcessManagerTestP, |
| 262 testing::Bool()); |
| 263 |
256 } // namespace content | 264 } // namespace content |
OLD | NEW |