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

Side by Side Diff: content/browser/renderer_host/render_process_host_browsertest.cc

Issue 2929113002: Enable spare RenderProcessHost to be preinitialized. (Closed)
Patch Set: browsertests 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/bind.h" 5 #include "base/bind.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 #include "content/browser/renderer_host/render_process_host_impl.h" 9 #include "content/browser/renderer_host/render_process_host_impl.h"
10 #include "content/common/child_process_messages.h" 10 #include "content/common/child_process_messages.h"
11 #include "content/public/browser/render_frame_host.h" 11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/render_process_host.h" 12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/render_process_host_observer.h" 13 #include "content/public/browser/render_process_host_observer.h"
14 #include "content/public/browser/web_contents.h" 14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/url_constants.h" 15 #include "content/public/common/url_constants.h"
16 #include "content/public/test/content_browser_test.h" 16 #include "content/public/test/content_browser_test.h"
17 #include "content/public/test/content_browser_test_utils.h" 17 #include "content/public/test/content_browser_test_utils.h"
18 #include "content/public/test/test_service.mojom.h" 18 #include "content/public/test/test_service.mojom.h"
19 #include "content/shell/browser/shell.h" 19 #include "content/shell/browser/shell.h"
20 #include "content/shell/browser/shell_browser_context.h"
21 #include "content/shell/browser/shell_content_browser_client.h"
20 #include "media/base/bind_to_current_loop.h" 22 #include "media/base/bind_to_current_loop.h"
21 #include "media/base/media_switches.h" 23 #include "media/base/media_switches.h"
22 #include "media/base/test_data_util.h" 24 #include "media/base/test_data_util.h"
23 #include "media/mojo/features.h" 25 #include "media/mojo/features.h"
24 #include "net/test/embedded_test_server/embedded_test_server.h" 26 #include "net/test/embedded_test_server/embedded_test_server.h"
25 27
26 #if defined(OS_WIN) 28 #if defined(OS_WIN)
27 #include "base/win/windows_version.h" 29 #include "base/win/windows_version.h"
28 #endif 30 #endif
29 31
30 namespace content { 32 namespace content {
31 namespace { 33 namespace {
32 34
33 int RenderProcessHostCount() { 35 int RenderProcessHostCount() {
34 content::RenderProcessHost::iterator hosts = 36 content::RenderProcessHost::iterator hosts =
35 content::RenderProcessHost::AllHostsIterator(); 37 content::RenderProcessHost::AllHostsIterator();
36 int count = 0; 38 int count = 0;
37 while (!hosts.IsAtEnd()) { 39 while (!hosts.IsAtEnd()) {
38 if (hosts.GetCurrentValue()->HasConnection()) 40 if (hosts.GetCurrentValue()->HasConnection())
39 count++; 41 count++;
40 hosts.Advance(); 42 hosts.Advance();
41 } 43 }
42 return count; 44 return count;
43 } 45 }
44 46
47 int32_t FirstRenderProcessHostKey() {
48 content::RenderProcessHost::iterator hosts =
49 content::RenderProcessHost::AllHostsIterator();
50 if (!hosts.IsAtEnd()) {
51 return hosts.GetCurrentKey();
52 }
53 return 0;
54 }
55
45 class RenderProcessHostTest : public ContentBrowserTest, 56 class RenderProcessHostTest : public ContentBrowserTest,
46 public RenderProcessHostObserver { 57 public RenderProcessHostObserver {
47 public: 58 public:
48 RenderProcessHostTest() : process_exits_(0), host_destructions_(0) {} 59 RenderProcessHostTest() : process_exits_(0), host_destructions_(0) {}
49 60
50 void SetUpCommandLine(base::CommandLine* command_line) override { 61 void SetUpCommandLine(base::CommandLine* command_line) override {
51 command_line->AppendSwitch(switches::kIgnoreAutoplayRestrictionsForTests); 62 command_line->AppendSwitch(switches::kIgnoreAutoplayRestrictionsForTests);
52 } 63 }
53 64
54 protected: 65 protected:
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 set_is_for_guests_only_for_testing(true); 130 set_is_for_guests_only_for_testing(true);
120 EXPECT_EQ(1, RenderProcessHostCount()); 131 EXPECT_EQ(1, RenderProcessHostCount());
121 132
122 // Navigate to a different page. 133 // Navigate to a different page.
123 GURL::Replacements replace_host; 134 GURL::Replacements replace_host;
124 replace_host.SetHostStr("localhost"); 135 replace_host.SetHostStr("localhost");
125 GURL another_url = embedded_test_server()->GetURL("/simple_page.html"); 136 GURL another_url = embedded_test_server()->GetURL("/simple_page.html");
126 another_url = another_url.ReplaceComponents(replace_host); 137 another_url = another_url.ReplaceComponents(replace_host);
127 NavigateToURL(CreateBrowser(), another_url); 138 NavigateToURL(CreateBrowser(), another_url);
128 139
129 // Expect that we got another process (the guest renderer was not reused). 140 // Expect that we got another process (the guest RenderProcessHost was not
141 // reused).
130 EXPECT_EQ(2, RenderProcessHostCount()); 142 EXPECT_EQ(2, RenderProcessHostCount());
131 } 143 }
132 144
145 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostTaken) {
146 ASSERT_TRUE(embedded_test_server()->Start());
147
148 RenderProcessHost::WarmupSpareRenderProcessHost(
149 ShellContentBrowserClient::Get()->browser_context());
150 int32_t spare_key = FirstRenderProcessHostKey();
Benoit L 2017/06/19 11:30:35 nit: EXPECT_NE(0, spare_key); ?
mattcary 2017/06/19 12:55:10 Done.
151
152 GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
153 NavigateToURL(CreateBrowser(), test_url);
154
155 EXPECT_EQ(spare_key, FirstRenderProcessHostKey());
156
157 // The spare render process host should no longer be available.
158 EXPECT_EQ(nullptr,
159 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
160 }
161
162 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostNotTaken) {
163 ASSERT_TRUE(embedded_test_server()->Start());
164
165 RenderProcessHost::WarmupSpareRenderProcessHost(
166 ShellContentBrowserClient::Get()->off_the_record_browser_context());
167 int32_t spare_key = FirstRenderProcessHostKey();
Benoit L 2017/06/19 11:30:35 Question: Actually, should we warmup a spare rende
mattcary 2017/06/19 12:55:10 That's a good question, although shouldn't that be
Benoit L 2017/06/19 13:20:42 Acknowledged.
168 GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
169 NavigateToURL(CreateBrowser(), test_url);
170
171 // There should have been another process created for the navigation.
172 EXPECT_NE(spare_key, FirstRenderProcessHostKey());
173
174 // The spare RenderProcessHost should have been cleaned up. Note this
175 // behavior is identical to what would have happened if the RenderProcessHost
176 // were taken.
177 EXPECT_EQ(nullptr,
178 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
179 }
180
181 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostKilled) {
182 RenderProcessHost::WarmupSpareRenderProcessHost(
183 ShellContentBrowserClient::Get()->browser_context());
184
185 RenderProcessHost* spare =
186 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting();
187 mojom::TestServicePtr service;
188 ASSERT_NE(nullptr, spare);
189 BindInterface(spare, &service);
190
191 base::RunLoop run_loop;
192 set_process_exit_callback(run_loop.QuitClosure());
193 spare->AddObserver(this); // For process_exit_callback.
194
195 // Should reply with a bad message and cause process death.
196 service->DoSomething(base::Bind(&base::DoNothing));
197 run_loop.Run();
198
199 // The spare RenderProcessHost should disappear when its process dies.
200 EXPECT_EQ(nullptr,
201 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
202 }
203
204 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest,
205 SpareRenderProcessHostStoragePartitionKilled) {
206 RenderProcessHost::WarmupSpareRenderProcessHost(
207 ShellContentBrowserClient::Get()->browser_context());
208 EXPECT_NE(nullptr,
209 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
210
211 ShellContentBrowserClient::Get()
212 ->browser_context()
213 ->ShutdownStoragePartitions();
214
215 // The spare RenderProcessHost should disappear along with the
216 // StoragePartitions.
217 EXPECT_EQ(nullptr,
218 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
219 }
220
133 class ShellCloser : public RenderProcessHostObserver { 221 class ShellCloser : public RenderProcessHostObserver {
134 public: 222 public:
135 ShellCloser(Shell* shell, std::string* logging_string) 223 ShellCloser(Shell* shell, std::string* logging_string)
136 : shell_(shell), logging_string_(logging_string) {} 224 : shell_(shell), logging_string_(logging_string) {}
137 225
138 protected: 226 protected:
139 // RenderProcessHostObserver: 227 // RenderProcessHostObserver:
140 void RenderProcessExited(RenderProcessHost* host, 228 void RenderProcessExited(RenderProcessHost* host,
141 base::TerminationStatus status, 229 base::TerminationStatus status,
142 int exit_code) override { 230 int exit_code) override {
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 // Verify shutdown went as expected. 410 // Verify shutdown went as expected.
323 EXPECT_EQ(0, rph->get_audio_stream_count_for_testing()); 411 EXPECT_EQ(0, rph->get_audio_stream_count_for_testing());
324 EXPECT_EQ(1, process_exits_); 412 EXPECT_EQ(1, process_exits_);
325 EXPECT_EQ(0, host_destructions_); 413 EXPECT_EQ(0, host_destructions_);
326 if (!host_destructions_) 414 if (!host_destructions_)
327 rph->RemoveObserver(this); 415 rph->RemoveObserver(this);
328 } 416 }
329 417
330 } // namespace 418 } // namespace
331 } // namespace content 419 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698