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

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

Issue 2927973002: Only use SiteInstance process reuse for ServiceWorker with no failures (Closed)
Patch Set: Created 3 years, 6 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
« no previous file with comments | « content/browser/service_worker/service_worker_process_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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/renderer_host/render_process_host_impl.h"
11 #include "content/browser/service_worker/service_worker_test_utils.h" 12 #include "content/browser/service_worker/service_worker_test_utils.h"
12 #include "content/common/service_worker/embedded_worker_settings.h" 13 #include "content/common/service_worker/embedded_worker_settings.h"
13 #include "content/public/common/child_process_host.h" 14 #include "content/public/common/child_process_host.h"
14 #include "content/public/test/mock_render_process_host.h" 15 #include "content/public/test/mock_render_process_host.h"
15 #include "content/public/test/test_browser_context.h" 16 #include "content/public/test/test_browser_context.h"
16 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 #include "url/gurl.h" 20 #include "url/gurl.h"
20 21
(...skipping 20 matching lines...) Expand all
41 class ServiceWorkerProcessManagerTest : public testing::Test { 42 class ServiceWorkerProcessManagerTest : public testing::Test {
42 public: 43 public:
43 ServiceWorkerProcessManagerTest() {} 44 ServiceWorkerProcessManagerTest() {}
44 45
45 void SetUp() override { 46 void SetUp() override {
46 browser_context_.reset(new TestBrowserContext); 47 browser_context_.reset(new TestBrowserContext);
47 process_manager_.reset( 48 process_manager_.reset(
48 new ServiceWorkerProcessManager(browser_context_.get())); 49 new ServiceWorkerProcessManager(browser_context_.get()));
49 pattern_ = GURL("http://www.example.com/"); 50 pattern_ = GURL("http://www.example.com/");
50 script_url_ = GURL("http://www.example.com/sw.js"); 51 script_url_ = GURL("http://www.example.com/sw.js");
52 render_process_host_factory_.reset(new MockRenderProcessHostFactory());
53 RenderProcessHostImpl::set_render_process_host_factory(
54 render_process_host_factory_.get());
51 } 55 }
52 56
53 void TearDown() override { 57 void TearDown() override {
54 process_manager_->Shutdown(); 58 process_manager_->Shutdown();
55 process_manager_.reset(); 59 process_manager_.reset();
60 RenderProcessHostImpl::set_render_process_host_factory(nullptr);
61 render_process_host_factory_.reset();
56 } 62 }
57 63
58 std::unique_ptr<MockRenderProcessHost> CreateRenderProcessHost() { 64 std::unique_ptr<MockRenderProcessHost> CreateRenderProcessHost() {
59 return base::MakeUnique<MockRenderProcessHost>(browser_context_.get()); 65 return base::MakeUnique<MockRenderProcessHost>(browser_context_.get());
60 } 66 }
61 67
62 protected: 68 protected:
63 std::unique_ptr<TestBrowserContext> browser_context_; 69 std::unique_ptr<TestBrowserContext> browser_context_;
64 std::unique_ptr<ServiceWorkerProcessManager> process_manager_; 70 std::unique_ptr<ServiceWorkerProcessManager> process_manager_;
65 GURL pattern_; 71 GURL pattern_;
66 GURL script_url_; 72 GURL script_url_;
67 73
68 private: 74 private:
75 std::unique_ptr<MockRenderProcessHostFactory> render_process_host_factory_;
69 content::TestBrowserThreadBundle thread_bundle_; 76 content::TestBrowserThreadBundle thread_bundle_;
70 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProcessManagerTest); 77 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProcessManagerTest);
71 }; 78 };
72 79
73 TEST_F(ServiceWorkerProcessManagerTest, SortProcess) { 80 TEST_F(ServiceWorkerProcessManagerTest, SortProcess) {
74 // Process 1 has 2 refs, 2 has 3 refs and 3 has 1 ref. 81 // Process 1 has 2 refs, 2 has 3 refs and 3 has 1 ref.
75 process_manager_->AddProcessReferenceToPattern(pattern_, 1); 82 process_manager_->AddProcessReferenceToPattern(pattern_, 1);
76 process_manager_->AddProcessReferenceToPattern(pattern_, 1); 83 process_manager_->AddProcessReferenceToPattern(pattern_, 1);
77 process_manager_->AddProcessReferenceToPattern(pattern_, 2); 84 process_manager_->AddProcessReferenceToPattern(pattern_, 2);
78 process_manager_->AddProcessReferenceToPattern(pattern_, 2); 85 process_manager_->AddProcessReferenceToPattern(pattern_, 2);
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 EXPECT_EQ(0u, host2->GetWorkerRefCount()); 233 EXPECT_EQ(0u, host2->GetWorkerRefCount());
227 EXPECT_EQ(1u, instance_info.size()); 234 EXPECT_EQ(1u, instance_info.size());
228 EXPECT_TRUE(base::ContainsKey(instance_info, kEmbeddedWorkerId2)); 235 EXPECT_TRUE(base::ContainsKey(instance_info, kEmbeddedWorkerId2));
229 236
230 process_manager_->ReleaseWorkerProcess(kEmbeddedWorkerId2); 237 process_manager_->ReleaseWorkerProcess(kEmbeddedWorkerId2);
231 EXPECT_EQ(0u, host1->GetWorkerRefCount()); 238 EXPECT_EQ(0u, host1->GetWorkerRefCount());
232 EXPECT_EQ(0u, host2->GetWorkerRefCount()); 239 EXPECT_EQ(0u, host2->GetWorkerRefCount());
233 EXPECT_TRUE(instance_info.empty()); 240 EXPECT_TRUE(instance_info.empty());
234 } 241 }
235 242
243 TEST_F(ServiceWorkerProcessManagerTest,
244 AllocateWorkerProcess_WithProcessReuse) {
245 const int kEmbeddedWorkerId = 100;
246 const GURL kSiteUrl = GURL("http://example.com");
247
248 // Create a process that is hosting a frame with URL |patter_|.
249 std::unique_ptr<MockRenderProcessHost> host(CreateRenderProcessHost());
250 RenderProcessHostImpl::AddFrameWithSite(browser_context_.get(), host.get(),
251 kSiteUrl);
252
253 std::map<int, ServiceWorkerProcessManager::ProcessInfo>& instance_info =
254 process_manager_->instance_info_;
255 EXPECT_TRUE(instance_info.empty());
256
257 // Allocate a process to a worker, when process reuse is authorized.
258 base::RunLoop run_loop;
259 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
260 int process_id = -10;
261 bool is_new_process = false;
262 process_manager_->AllocateWorkerProcess(
263 kEmbeddedWorkerId, pattern_, script_url_,
264 true /* can_use_existing_process */,
265 base::Bind(&DidAllocateWorkerProcess, run_loop.QuitClosure(), &status,
266 &process_id, &is_new_process));
267 run_loop.Run();
268
269 // An existing process should be allocated to the worker.
270 EXPECT_EQ(SERVICE_WORKER_OK, status);
271 EXPECT_EQ(host->GetID(), process_id);
272 EXPECT_TRUE(is_new_process);
273 EXPECT_EQ(1u, host->GetWorkerRefCount());
274 EXPECT_EQ(1u, instance_info.size());
275 std::map<int, ServiceWorkerProcessManager::ProcessInfo>::iterator found =
276 instance_info.find(kEmbeddedWorkerId);
277 ASSERT_TRUE(found != instance_info.end());
278 EXPECT_EQ(host->GetID(), found->second.process_id);
279
280 // Release the process.
281 process_manager_->ReleaseWorkerProcess(kEmbeddedWorkerId);
282 EXPECT_EQ(0u, host->GetWorkerRefCount());
283 EXPECT_TRUE(instance_info.empty());
284
285 RenderProcessHostImpl::RemoveFrameWithSite(browser_context_.get(), host.get(),
286 kSiteUrl);
287 }
288
289 TEST_F(ServiceWorkerProcessManagerTest,
290 AllocateWorkerProcess_WithoutProcessReuse) {
291 const int kEmbeddedWorkerId = 100;
292 const GURL kSiteUrl = GURL("http://example.com");
293
294 // Create a process that is hosting a frame with URL |patter_|.
295 std::unique_ptr<MockRenderProcessHost> host(CreateRenderProcessHost());
296 RenderProcessHostImpl::AddFrameWithSite(browser_context_.get(), host.get(),
297 kSiteUrl);
298
299 std::map<int, ServiceWorkerProcessManager::ProcessInfo>& instance_info =
300 process_manager_->instance_info_;
301 EXPECT_TRUE(instance_info.empty());
302
303 // Allocate a process to a worker, when process reuse is authorized.
304 base::RunLoop run_loop;
305 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
306 int process_id = -10;
307 bool is_new_process = false;
308 process_manager_->AllocateWorkerProcess(
309 kEmbeddedWorkerId, pattern_, script_url_,
310 false /* can_use_existing_process */,
311 base::Bind(&DidAllocateWorkerProcess, run_loop.QuitClosure(), &status,
312 &process_id, &is_new_process));
313 run_loop.Run();
314
315 // An new process should be allocated to the worker.
316 EXPECT_EQ(SERVICE_WORKER_OK, status);
317 EXPECT_NE(host->GetID(), process_id);
318 EXPECT_TRUE(is_new_process);
319 EXPECT_EQ(0u, host->GetWorkerRefCount());
320 EXPECT_EQ(1u, instance_info.size());
321 std::map<int, ServiceWorkerProcessManager::ProcessInfo>::iterator found =
322 instance_info.find(kEmbeddedWorkerId);
323 ASSERT_TRUE(found != instance_info.end());
324 EXPECT_NE(host->GetID(), found->second.process_id);
325
326 // Release the process.
327 process_manager_->ReleaseWorkerProcess(kEmbeddedWorkerId);
328 EXPECT_TRUE(instance_info.empty());
329
330 RenderProcessHostImpl::RemoveFrameWithSite(browser_context_.get(), host.get(),
331 kSiteUrl);
332 }
333
236 TEST_F(ServiceWorkerProcessManagerTest, AllocateWorkerProcess_InShutdown) { 334 TEST_F(ServiceWorkerProcessManagerTest, AllocateWorkerProcess_InShutdown) {
237 process_manager_->Shutdown(); 335 process_manager_->Shutdown();
238 ASSERT_TRUE(process_manager_->IsShutdown()); 336 ASSERT_TRUE(process_manager_->IsShutdown());
239 337
240 base::RunLoop run_loop; 338 base::RunLoop run_loop;
241 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; 339 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
242 int process_id = -10; 340 int process_id = -10;
243 bool is_new_process = true; 341 bool is_new_process = true;
244 process_manager_->AllocateWorkerProcess( 342 process_manager_->AllocateWorkerProcess(
245 1, pattern_, script_url_, true /* can_use_existing_process */, 343 1, pattern_, script_url_, true /* can_use_existing_process */,
246 base::Bind(&DidAllocateWorkerProcess, run_loop.QuitClosure(), &status, 344 base::Bind(&DidAllocateWorkerProcess, run_loop.QuitClosure(), &status,
247 &process_id, &is_new_process)); 345 &process_id, &is_new_process));
248 run_loop.Run(); 346 run_loop.Run();
249 347
250 // Allocating a process in shutdown should abort. 348 // Allocating a process in shutdown should abort.
251 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); 349 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status);
252 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, process_id); 350 EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, process_id);
253 EXPECT_FALSE(is_new_process); 351 EXPECT_FALSE(is_new_process);
254 EXPECT_TRUE(process_manager_->instance_info_.empty()); 352 EXPECT_TRUE(process_manager_->instance_info_.empty());
255 } 353 }
256 354
257 } // namespace content 355 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_process_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698