OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This test validates that the ProcessSingleton class properly makes sure | 5 // This test validates that the ProcessSingleton class properly makes sure |
6 // that there is only one main browser process. | 6 // that there is only one main browser process. |
7 // | 7 // |
8 // It is currently compiled and run on Windows and Posix(non-Mac) platforms. | 8 // It is currently compiled and run on Windows and Posix(non-Mac) platforms. |
9 // Mac uses system services and ProcessSingletonMac is a noop. (Maybe it still | 9 // Mac uses system services and ProcessSingletonMac is a noop. (Maybe it still |
10 // makes sense to test that the system services are giving the behavior we | 10 // makes sense to test that the system services are giving the behavior we |
11 // want?) | 11 // want?) |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
19 #include "base/process/kill.h" | |
20 #include "base/process/launch.h" | 19 #include "base/process/launch.h" |
21 #include "base/process/process.h" | 20 #include "base/process/process.h" |
22 #include "base/process/process_iterator.h" | 21 #include "base/process/process_iterator.h" |
23 #include "base/synchronization/waitable_event.h" | 22 #include "base/synchronization/waitable_event.h" |
24 #include "base/test/test_timeouts.h" | 23 #include "base/test/test_timeouts.h" |
25 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
26 #include "chrome/common/chrome_constants.h" | 25 #include "chrome/common/chrome_constants.h" |
27 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
28 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/test/base/in_process_browser_test.h" | 28 #include "chrome/test/base/in_process_browser_test.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 } | 152 } |
154 | 153 |
155 // This method is used to make sure we kill the main browser process after | 154 // This method is used to make sure we kill the main browser process after |
156 // all of its child processes have successfully attached to it. This was added | 155 // all of its child processes have successfully attached to it. This was added |
157 // when we realized that if we just kill the parent process right away, we | 156 // when we realized that if we just kill the parent process right away, we |
158 // sometimes end up with dangling child processes. If we Sleep for a certain | 157 // sometimes end up with dangling child processes. If we Sleep for a certain |
159 // amount of time, we are OK... So we introduced this method to avoid a | 158 // amount of time, we are OK... So we introduced this method to avoid a |
160 // flaky wait. Instead, we kill all descendants of the main process after we | 159 // flaky wait. Instead, we kill all descendants of the main process after we |
161 // killed it, relying on the fact that we can still get the parent id of a | 160 // killed it, relying on the fact that we can still get the parent id of a |
162 // child process, even when the parent dies. | 161 // child process, even when the parent dies. |
163 void KillProcessTree(base::ProcessHandle process_handle) { | 162 void KillProcessTree(const base::Process& process) { |
164 class ProcessTreeFilter : public base::ProcessFilter { | 163 class ProcessTreeFilter : public base::ProcessFilter { |
165 public: | 164 public: |
166 explicit ProcessTreeFilter(base::ProcessId parent_pid) { | 165 explicit ProcessTreeFilter(base::ProcessId parent_pid) { |
167 ancestor_pids_.insert(parent_pid); | 166 ancestor_pids_.insert(parent_pid); |
168 } | 167 } |
169 bool Includes(const base::ProcessEntry& entry) const override { | 168 bool Includes(const base::ProcessEntry& entry) const override { |
170 if (ancestor_pids_.find(entry.parent_pid()) != ancestor_pids_.end()) { | 169 if (ancestor_pids_.find(entry.parent_pid()) != ancestor_pids_.end()) { |
171 ancestor_pids_.insert(entry.pid()); | 170 ancestor_pids_.insert(entry.pid()); |
172 return true; | 171 return true; |
173 } else { | 172 } else { |
174 return false; | 173 return false; |
175 } | 174 } |
176 } | 175 } |
177 private: | 176 private: |
178 mutable std::set<base::ProcessId> ancestor_pids_; | 177 mutable std::set<base::ProcessId> ancestor_pids_; |
179 } process_tree_filter(base::GetProcId(process_handle)); | 178 } process_tree_filter(process.Pid()); |
180 | 179 |
181 // Start by explicitly killing the main process we know about... | 180 // Start by explicitly killing the main process we know about... |
182 static const int kExitCode = 42; | 181 static const int kExitCode = 42; |
183 EXPECT_TRUE(base::KillProcess(process_handle, kExitCode, true /* wait */)); | 182 EXPECT_TRUE(process.Terminate(kExitCode, true /* wait */)); |
cpu_(ooo_6.6-7.5)
2015/03/17 20:36:31
woa, Terminate is a const method?
rvargas (doing something else)
2015/03/17 22:40:53
Yes. I added const when adding the second argument
| |
184 | 183 |
185 // Then loop until we can't find any of its descendant. | 184 // Then loop until we can't find any of its descendant. |
186 // But don't try more than kNbTries times... | 185 // But don't try more than kNbTries times... |
187 static const int kNbTries = 10; | 186 static const int kNbTries = 10; |
188 int num_tries = 0; | 187 int num_tries = 0; |
189 base::FilePath program; | 188 base::FilePath program; |
190 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &program)); | 189 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &program)); |
191 base::FilePath::StringType exe_name = program.BaseName().value(); | 190 base::FilePath::StringType exe_name = program.BaseName().value(); |
192 while (base::GetProcessCount(exe_name, &process_tree_filter) > 0 && | 191 while (base::GetProcessCount(exe_name, &process_tree_filter) > 0 && |
193 num_tries++ < kNbTries) { | 192 num_tries++ < kNbTries) { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 // it is because it timed out of its WaitForExitCodeWithTimeout(). Only | 295 // it is because it timed out of its WaitForExitCodeWithTimeout(). Only |
297 // the last one standing should be left waiting... So we failed... | 296 // the last one standing should be left waiting... So we failed... |
298 EXPECT_TRUE(chrome_starters_[starter_index]->process_terminated_ || | 297 EXPECT_TRUE(chrome_starters_[starter_index]->process_terminated_ || |
299 failed) << "There is more than one main process."; | 298 failed) << "There is more than one main process."; |
300 if (!chrome_starters_[starter_index]->process_terminated_) { | 299 if (!chrome_starters_[starter_index]->process_terminated_) { |
301 // This will stop the "for kNbAttempts" loop. | 300 // This will stop the "for kNbAttempts" loop. |
302 failed = true; | 301 failed = true; |
303 // But we let the last loop turn finish so that we can properly | 302 // But we let the last loop turn finish so that we can properly |
304 // kill all remaining processes. Starting with this one... | 303 // kill all remaining processes. Starting with this one... |
305 if (chrome_starters_[starter_index]->process_.IsValid()) { | 304 if (chrome_starters_[starter_index]->process_.IsValid()) { |
306 KillProcessTree(chrome_starters_[starter_index]->process_.Handle()); | 305 KillProcessTree(chrome_starters_[starter_index]->process_); |
307 } | 306 } |
308 } | 307 } |
309 pending_starters.erase(pending_starters.begin() + done_index); | 308 pending_starters.erase(pending_starters.begin() + done_index); |
310 } | 309 } |
311 | 310 |
312 // "There can be only one!" :-) | 311 // "There can be only one!" :-) |
313 ASSERT_EQ(static_cast<size_t>(1), pending_starters.size()); | 312 ASSERT_EQ(static_cast<size_t>(1), pending_starters.size()); |
314 size_t last_index = pending_starters.front(); | 313 size_t last_index = pending_starters.front(); |
315 pending_starters.clear(); | 314 pending_starters.clear(); |
316 if (chrome_starters_[last_index]->process_.IsValid()) { | 315 if (chrome_starters_[last_index]->process_.IsValid()) { |
317 KillProcessTree(chrome_starters_[last_index]->process_.Handle()); | 316 KillProcessTree(chrome_starters_[last_index]->process_); |
318 chrome_starters_[last_index]->done_event_.Wait(); | 317 chrome_starters_[last_index]->done_event_.Wait(); |
319 } | 318 } |
320 } | 319 } |
321 } | 320 } |
OLD | NEW |