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

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

Issue 2929113002: Enable spare RenderProcessHost to be preinitialized. (Closed)
Patch Set: tweaks Created 3 years, 5 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_browser_main_parts.h"
22 #include "content/shell/browser/shell_content_browser_client.h"
20 #include "media/base/bind_to_current_loop.h" 23 #include "media/base/bind_to_current_loop.h"
21 #include "media/base/media_switches.h" 24 #include "media/base/media_switches.h"
22 #include "media/base/test_data_util.h" 25 #include "media/base/test_data_util.h"
23 #include "media/mojo/features.h" 26 #include "media/mojo/features.h"
24 #include "net/test/embedded_test_server/embedded_test_server.h" 27 #include "net/test/embedded_test_server/embedded_test_server.h"
25 28
26 #if defined(OS_WIN) 29 #if defined(OS_WIN)
27 #include "base/win/windows_version.h" 30 #include "base/win/windows_version.h"
28 #endif 31 #endif
29 32
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 GURL::Replacements replace_host; 126 GURL::Replacements replace_host;
124 replace_host.SetHostStr("localhost"); 127 replace_host.SetHostStr("localhost");
125 GURL another_url = embedded_test_server()->GetURL("/simple_page.html"); 128 GURL another_url = embedded_test_server()->GetURL("/simple_page.html");
126 another_url = another_url.ReplaceComponents(replace_host); 129 another_url = another_url.ReplaceComponents(replace_host);
127 NavigateToURL(CreateBrowser(), another_url); 130 NavigateToURL(CreateBrowser(), another_url);
128 131
129 // Expect that we got another process (the guest renderer was not reused). 132 // Expect that we got another process (the guest renderer was not reused).
130 EXPECT_EQ(2, RenderProcessHostCount()); 133 EXPECT_EQ(2, RenderProcessHostCount());
131 } 134 }
132 135
136 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostTaken) {
137 ASSERT_TRUE(embedded_test_server()->Start());
138
139 RenderProcessHost::WarmupSpareRenderProcessHost(
140 ShellContentBrowserClient::Get()->browser_context());
141 RenderProcessHost* spare_renderer =
142 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting();
143 EXPECT_NE(nullptr, spare_renderer);
144
145 GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
146 Shell* window = CreateBrowser();
147 NavigateToURL(window, test_url);
148
149 EXPECT_EQ(spare_renderer,
150 window->web_contents()->GetMainFrame()->GetProcess());
151
152 // The spare render process host should no longer be available.
153 EXPECT_EQ(nullptr,
154 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
155 }
156
157 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostNotTaken) {
158 ASSERT_TRUE(embedded_test_server()->Start());
159
160 RenderProcessHost::WarmupSpareRenderProcessHost(
161 ShellContentBrowserClient::Get()->off_the_record_browser_context());
162 RenderProcessHost* spare_renderer =
163 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting();
164 GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
165 Shell* window = CreateBrowser();
166 NavigateToURL(window, test_url);
167
168 // There should have been another process created for the navigation.
169 EXPECT_NE(spare_renderer,
170 window->web_contents()->GetMainFrame()->GetProcess());
171
172 // The spare RenderProcessHost should have been cleaned up. Note this
173 // behavior is identical to what would have happened if the RenderProcessHost
174 // were taken.
175 EXPECT_EQ(nullptr,
176 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
177 }
178
179 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostKilled) {
180 RenderProcessHost::WarmupSpareRenderProcessHost(
181 ShellContentBrowserClient::Get()->browser_context());
182
183 RenderProcessHost* spare_renderer =
184 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting();
185 mojom::TestServicePtr service;
186 ASSERT_NE(nullptr, spare_renderer);
187 BindInterface(spare_renderer, &service);
188
189 base::RunLoop run_loop;
190 set_process_exit_callback(run_loop.QuitClosure());
191 spare_renderer->AddObserver(this); // For process_exit_callback.
192
193 // Should reply with a bad message and cause process death.
194 service->DoSomething(base::Bind(&base::DoNothing));
195 run_loop.Run();
196
197 // The spare RenderProcessHost should disappear when its process dies.
198 EXPECT_EQ(nullptr,
199 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
200 }
201
202 // Test that the spare renderer works correctly when the limit on the maximum
203 // number of processes is small.
204 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest,
205 SpareRendererSurpressedMaxProcesses) {
206 ASSERT_TRUE(embedded_test_server()->Start());
207
208 RenderProcessHost::SetMaxRendererProcessCount(1);
209
210 // A process is created with shell startup, so with a maximum of one renderer
211 // process the spare RPH should not be created.
212 RenderProcessHost::WarmupSpareRenderProcessHost(
213 ShellContentBrowserClient::Get()->browser_context());
214 EXPECT_EQ(nullptr,
215 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
216
217 // A spare RPH should be created with a max of 2 renderer process.
Charlie Reis 2017/06/29 21:09:16 nit: s/process/processes/
mattcary 2017/06/30 12:37:56 Done.
218 RenderProcessHost::SetMaxRendererProcessCount(2);
219 RenderProcessHost::WarmupSpareRenderProcessHost(
220 ShellContentBrowserClient::Get()->browser_context());
221 RenderProcessHost* spare_renderer =
222 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting();
223 EXPECT_NE(nullptr, spare_renderer);
224
225 // Navigation will use the spare renderer half the time, choosing the spare
226 // renderer or the existing render for the new navigation at random.
Charlie Reis 2017/06/29 21:09:16 nit: s/render/renderer/
mattcary 2017/06/30 12:37:55 Done.
227 GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
228 NavigateToURL(CreateBrowser(), test_url);
229
230 // Revert to the default process limit.
231 RenderProcessHost::SetMaxRendererProcessCount(0);
232
233 // If the spare renderer wasn't picked up for the naviation above, perform
Charlie Reis 2017/06/29 21:09:16 nit: navigation
mattcary 2017/06/30 12:37:56 Done.
234 // another navigation in a new shell, without a process limit, and confirm
235 // that the spare renderer is used correctly. Note this branch is entered
236 // randomly, depending on the choice of renderer used above, so the check
237 // below will only fail in a flaky manner (but will not flake at all if
238 // everything is working correctly, of course!).
239 if (RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()) {
240 NavigateToURL(CreateBrowser(), test_url);
241 EXPECT_EQ(nullptr,
242 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
Charlie Reis 2017/06/29 21:09:16 This doesn't look right to me, and it's never fail
mattcary 2017/06/30 12:37:55 Right, this is what I meant about not being able t
243 }
244 }
245
133 class ShellCloser : public RenderProcessHostObserver { 246 class ShellCloser : public RenderProcessHostObserver {
134 public: 247 public:
135 ShellCloser(Shell* shell, std::string* logging_string) 248 ShellCloser(Shell* shell, std::string* logging_string)
136 : shell_(shell), logging_string_(logging_string) {} 249 : shell_(shell), logging_string_(logging_string) {}
137 250
138 protected: 251 protected:
139 // RenderProcessHostObserver: 252 // RenderProcessHostObserver:
140 void RenderProcessExited(RenderProcessHost* host, 253 void RenderProcessExited(RenderProcessHost* host,
141 base::TerminationStatus status, 254 base::TerminationStatus status,
142 int exit_code) override { 255 int exit_code) override {
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 // Verify shutdown went as expected. 435 // Verify shutdown went as expected.
323 EXPECT_EQ(0, rph->get_audio_stream_count_for_testing()); 436 EXPECT_EQ(0, rph->get_audio_stream_count_for_testing());
324 EXPECT_EQ(1, process_exits_); 437 EXPECT_EQ(1, process_exits_);
325 EXPECT_EQ(0, host_destructions_); 438 EXPECT_EQ(0, host_destructions_);
326 if (!host_destructions_) 439 if (!host_destructions_)
327 rph->RemoveObserver(this); 440 rph->RemoveObserver(this);
328 } 441 }
329 442
330 } // namespace 443 } // namespace
331 } // namespace content 444 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698