blob: bff043a933c5ce535607ec5f276189ae9cde8cef [file] [log] [blame]
[email protected]d64295932011-01-19 22:37:311// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]bbef41f02010-03-04 16:16:192// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This test validates that the ProcessSingleton class properly makes sure
6// that there is only one main browser process.
7//
[email protected]bf4878d2010-06-16 20:12:018// 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
10// makes sense to test that the system services are giving the behavior we
11// want?)
[email protected]bbef41f02010-03-04 16:16:1912
13#include <list>
14
15#include "base/file_path.h"
16#include "base/file_util.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/ref_counted.h"
[email protected]bf4878d2010-06-16 20:12:0118#include "base/path_service.h"
[email protected]bbef41f02010-03-04 16:16:1919#include "base/process_util.h"
[email protected]e0785902011-05-19 23:34:1720#include "base/scoped_temp_dir.h"
[email protected]44f9c952011-01-02 06:05:3921#include "base/synchronization/waitable_event.h"
[email protected]e0785902011-05-19 23:34:1722#include "base/test/test_timeouts.h"
23#include "base/threading/thread.h"
[email protected]bbef41f02010-03-04 16:16:1924#include "chrome/common/chrome_constants.h"
[email protected]e0785902011-05-19 23:34:1725#include "chrome/common/chrome_paths.h"
[email protected]bf4878d2010-06-16 20:12:0126#include "chrome/common/chrome_switches.h"
[email protected]aecb6ca2011-07-29 22:56:1427#include "chrome/test/base/test_launcher_utils.h"
[email protected]bbef41f02010-03-04 16:16:1928#include "chrome/test/ui/ui_test.h"
29#include "testing/gtest/include/gtest/gtest.h"
30
31namespace {
32
33// This is for the code that is to be ran in multiple threads at once,
34// to stress a race condition on first process start.
35// We use the thread safe ref counted base class so that we can use the
36// NewRunnableMethod class to run the StartChrome methods in many threads.
37class ChromeStarter : public base::RefCountedThreadSafe<ChromeStarter> {
38 public:
[email protected]e7e38032011-07-26 17:25:2539 ChromeStarter(int timeout_ms, const FilePath& user_data_dir)
[email protected]bbef41f02010-03-04 16:16:1940 : ready_event_(false /* manual */, false /* signaled */),
41 done_event_(false /* manual */, false /* signaled */),
[email protected]8b8fab972011-01-13 16:27:1842 process_handle_(base::kNullProcessHandle),
[email protected]0e60b642010-03-30 10:21:2743 process_terminated_(false),
[email protected]1a30a2f32010-10-06 02:03:0444 timeout_ms_(timeout_ms),
45 user_data_dir_(user_data_dir) {
[email protected]bbef41f02010-03-04 16:16:1946 }
47
48 // We must reset some data members since we reuse the same ChromeStarter
49 // object and start/stop it a few times. We must start fresh! :-)
50 void Reset() {
51 ready_event_.Reset();
52 done_event_.Reset();
[email protected]bf4878d2010-06-16 20:12:0153 if (process_handle_ != base::kNullProcessHandle)
[email protected]bbef41f02010-03-04 16:16:1954 base::CloseProcessHandle(process_handle_);
[email protected]bf4878d2010-06-16 20:12:0155 process_handle_ = base::kNullProcessHandle;
[email protected]bbef41f02010-03-04 16:16:1956 process_terminated_ = false;
57 }
58
[email protected]bf4878d2010-06-16 20:12:0159 void StartChrome(base::WaitableEvent* start_event, bool first_run) {
60 // TODO(mattm): maybe stuff should be refactored to use
61 // UITest::LaunchBrowserHelper somehow?
62 FilePath browser_directory;
63 PathService::Get(chrome::DIR_APP, &browser_directory);
64 CommandLine command_line(browser_directory.Append(
[email protected]4f260d02010-12-23 18:35:4265 chrome::kBrowserProcessExecutablePath));
[email protected]bf4878d2010-06-16 20:12:0166
[email protected]1a30a2f32010-10-06 02:03:0467 command_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir_);
[email protected]bf4878d2010-06-16 20:12:0168
69 if (first_run)
70 command_line.AppendSwitch(switches::kFirstRun);
71 else
72 command_line.AppendSwitch(switches::kNoFirstRun);
[email protected]bbef41f02010-03-04 16:16:1973
[email protected]1a30a2f32010-10-06 02:03:0474 // Add the normal test-mode switches, except for the ones we're adding
75 // ourselves.
[email protected]947446b2010-10-21 03:36:3176 CommandLine standard_switches(CommandLine::NO_PROGRAM);
[email protected]1a30a2f32010-10-06 02:03:0477 test_launcher_utils::PrepareBrowserCommandLineForTests(&standard_switches);
78 const CommandLine::SwitchMap& switch_map = standard_switches.GetSwitches();
79 for (CommandLine::SwitchMap::const_iterator i = switch_map.begin();
80 i != switch_map.end(); ++i) {
81 const std::string& switch_name = i->first;
82 if (switch_name == switches::kUserDataDir ||
83 switch_name == switches::kFirstRun ||
84 switch_name == switches::kNoFirstRun)
85 continue;
86
87 command_line.AppendSwitchNative(switch_name, i->second);
88 }
89
[email protected]bbef41f02010-03-04 16:16:1990 // Try to get all threads to launch the app at the same time.
91 // So let the test know we are ready.
92 ready_event_.Signal();
93 // And then wait for the test to tell us to GO!
94 ASSERT_NE(static_cast<base::WaitableEvent*>(NULL), start_event);
95 ASSERT_TRUE(start_event->Wait());
96
97 // Here we don't wait for the app to be terminated because one of the
98 // process will stay alive while the others will be restarted. If we would
99 // wait here, we would never get a handle to the main process...
[email protected]e5992182011-07-15 16:47:02100 base::LaunchProcess(command_line, base::LaunchOptions(), &process_handle_);
[email protected]bf4878d2010-06-16 20:12:01101 ASSERT_NE(base::kNullProcessHandle, process_handle_);
[email protected]bbef41f02010-03-04 16:16:19102
103 // We can wait on the handle here, we should get stuck on one and only
104 // one process. The test below will take care of killing that process
105 // to unstuck us once it confirms there is only one.
[email protected]bbef41f02010-03-04 16:16:19106 process_terminated_ = base::WaitForSingleProcess(process_handle_,
[email protected]0e60b642010-03-30 10:21:27107 timeout_ms_);
[email protected]bbef41f02010-03-04 16:16:19108 // Let the test know we are done.
109 done_event_.Signal();
110 }
111
112 // Public access to simplify the test code using them.
113 base::WaitableEvent ready_event_;
114 base::WaitableEvent done_event_;
115 base::ProcessHandle process_handle_;
116 bool process_terminated_;
117
118 private:
119 friend class base::RefCountedThreadSafe<ChromeStarter>;
[email protected]0e60b642010-03-30 10:21:27120
[email protected]bbef41f02010-03-04 16:16:19121 ~ChromeStarter() {
[email protected]bf4878d2010-06-16 20:12:01122 if (process_handle_ != base::kNullProcessHandle)
[email protected]bbef41f02010-03-04 16:16:19123 base::CloseProcessHandle(process_handle_);
124 }
[email protected]0e60b642010-03-30 10:21:27125
126 int timeout_ms_;
[email protected]1a30a2f32010-10-06 02:03:04127 FilePath user_data_dir_;
[email protected]0e60b642010-03-30 10:21:27128
[email protected]bbef41f02010-03-04 16:16:19129 DISALLOW_COPY_AND_ASSIGN(ChromeStarter);
130};
131
132// Our test fixture that initializes and holds onto a few global vars.
[email protected]bf4878d2010-06-16 20:12:01133class ProcessSingletonTest : public UITest {
[email protected]bbef41f02010-03-04 16:16:19134 public:
[email protected]bf4878d2010-06-16 20:12:01135 ProcessSingletonTest()
[email protected]bbef41f02010-03-04 16:16:19136 // We use a manual reset so that all threads wake up at once when signaled
137 // and thus we must manually reset it for each attempt.
138 : threads_waker_(true /* manual */, false /* signaled */) {
[email protected]2d57f5d2011-01-13 14:20:12139 EXPECT_TRUE(temp_profile_dir_.CreateUniqueTempDir());
[email protected]bbef41f02010-03-04 16:16:19140 }
141
142 void SetUp() {
143 // Start the threads and create the starters.
144 for (size_t i = 0; i < kNbThreads; ++i) {
145 chrome_starter_threads_[i].reset(new base::Thread("ChromeStarter"));
146 ASSERT_TRUE(chrome_starter_threads_[i]->Start());
[email protected]d64295932011-01-19 22:37:31147 chrome_starters_[i] = new ChromeStarter(
148 TestTimeouts::action_max_timeout_ms(), temp_profile_dir_.path());
[email protected]bbef41f02010-03-04 16:16:19149 }
150 }
151
152 void TearDown() {
153 // Stop the threads.
154 for (size_t i = 0; i < kNbThreads; ++i)
155 chrome_starter_threads_[i]->Stop();
156 }
157
158 // This method is used to make sure we kill the main browser process after
159 // all of its child processes have successfully attached to it. This was added
160 // when we realized that if we just kill the parent process right away, we
161 // sometimes end up with dangling child processes. If we Sleep for a certain
162 // amount of time, we are OK... So we introduced this method to avoid a
163 // flaky wait. Instead, we kill all descendants of the main process after we
164 // killed it, relying on the fact that we can still get the parent id of a
165 // child process, even when the parent dies.
166 void KillProcessTree(base::ProcessHandle process_handle) {
167 class ProcessTreeFilter : public base::ProcessFilter {
168 public:
169 explicit ProcessTreeFilter(base::ProcessId parent_pid) {
170 ancestor_pids_.insert(parent_pid);
171 }
[email protected]b6128aa2010-04-29 17:44:42172 virtual bool Includes(const base::ProcessEntry & entry) const {
173 if (ancestor_pids_.find(entry.parent_pid()) != ancestor_pids_.end()) {
174 ancestor_pids_.insert(entry.pid());
[email protected]bbef41f02010-03-04 16:16:19175 return true;
176 } else {
177 return false;
178 }
179 }
180 private:
181 mutable std::set<base::ProcessId> ancestor_pids_;
182 } process_tree_filter(base::GetProcId(process_handle));
183
184 // Start by explicitly killing the main process we know about...
185 static const int kExitCode = 42;
186 EXPECT_TRUE(base::KillProcess(process_handle, kExitCode, true /* wait */));
187
188 // Then loop until we can't find any of its descendant.
189 // But don't try more than kNbTries times...
190 static const int kNbTries = 10;
191 int num_tries = 0;
192 while (base::GetProcessCount(chrome::kBrowserProcessExecutablePath,
193 &process_tree_filter) > 0 && num_tries++ < kNbTries) {
194 base::KillProcesses(chrome::kBrowserProcessExecutablePath,
195 kExitCode, &process_tree_filter);
196 }
197 DLOG_IF(ERROR, num_tries >= kNbTries) << "Failed to kill all processes!";
198 }
199
200 // Since this is a hard to reproduce problem, we make a few attempts.
201 // We stop the attempts at the first error, and when there are no errors,
202 // we don't time-out of any wait, so it executes quite fast anyway.
203 static const size_t kNbAttempts = 5;
204
205 // The idea is to start chrome from multiple threads all at once.
206 static const size_t kNbThreads = 5;
207 scoped_refptr<ChromeStarter> chrome_starters_[kNbThreads];
208 scoped_ptr<base::Thread> chrome_starter_threads_[kNbThreads];
209
210 // The event that will get all threads to wake up simultaneously and try
211 // to start a chrome process at the same time.
212 base::WaitableEvent threads_waker_;
[email protected]1a30a2f32010-10-06 02:03:04213
214 // We don't want to use the default profile, but can't use UITest's since we
215 // don't use UITest::LaunchBrowser.
216 ScopedTempDir temp_profile_dir_;
[email protected]bbef41f02010-03-04 16:16:19217};
218
[email protected]b03a1382010-10-06 23:52:05219#if defined(OS_LINUX) && defined(TOOLKIT_VIEWS)
220// http://crbug.com/58219
221#define MAYBE_StartupRaceCondition FAILS_StartupRaceCondition
222#else
223#define MAYBE_StartupRaceCondition StartupRaceCondition
224#endif
225TEST_F(ProcessSingletonTest, MAYBE_StartupRaceCondition) {
[email protected]bbef41f02010-03-04 16:16:19226 // We use this to stop the attempts loop on the first failure.
227 bool failed = false;
228 for (size_t attempt = 0; attempt < kNbAttempts && !failed; ++attempt) {
229 SCOPED_TRACE(testing::Message() << "Attempt: " << attempt << ".");
230 // We use a single event to get all threads to do the AppLaunch at the same
231 // time...
232 threads_waker_.Reset();
233
[email protected]bf4878d2010-06-16 20:12:01234 // Test both with and without the first-run dialog, since they exercise
235 // different paths.
236#if defined(OS_POSIX)
237 // TODO(mattm): test first run dialog singleton handling on linux too.
238 // On posix if we test the first run dialog, GracefulShutdownHandler gets
239 // the TERM signal, but since the message loop isn't running during the gtk
240 // first run dialog, the ShutdownDetector never handles it, and KillProcess
241 // has to time out (60 sec!) and SIGKILL.
242 bool first_run = false;
243#else
244 // Test for races in both regular start up and first run start up cases.
245 bool first_run = attempt % 2;
246#endif
247
[email protected]bbef41f02010-03-04 16:16:19248 // Here we prime all the threads with a ChromeStarter that will wait for
249 // our signal to launch its chrome process.
250 for (size_t i = 0; i < kNbThreads; ++i) {
251 ASSERT_NE(static_cast<ChromeStarter*>(NULL), chrome_starters_[i].get());
252 chrome_starters_[i]->Reset();
253
254 ASSERT_TRUE(chrome_starter_threads_[i]->IsRunning());
255 ASSERT_NE(static_cast<MessageLoop*>(NULL),
256 chrome_starter_threads_[i]->message_loop());
257
258 chrome_starter_threads_[i]->message_loop()->PostTask(
259 FROM_HERE, NewRunnableMethod(chrome_starters_[i].get(),
260 &ChromeStarter::StartChrome,
[email protected]bf4878d2010-06-16 20:12:01261 &threads_waker_,
262 first_run));
[email protected]bbef41f02010-03-04 16:16:19263 }
264
265 // Wait for all the starters to be ready.
266 // We could replace this loop if we ever implement a WaitAll().
267 for (size_t i = 0; i < kNbThreads; ++i) {
268 SCOPED_TRACE(testing::Message() << "Waiting on thread: " << i << ".");
269 ASSERT_TRUE(chrome_starters_[i]->ready_event_.Wait());
270 }
271 // GO!
272 threads_waker_.Signal();
273
274 // As we wait for all threads to signal that they are done, we remove their
275 // index from this vector so that we get left with only the index of
276 // the thread that started the main process.
277 std::vector<size_t> pending_starters(kNbThreads);
278 for (size_t i = 0; i < kNbThreads; ++i)
279 pending_starters[i] = i;
280
281 // We use a local array of starter's done events we must wait on...
282 // These are collected from the starters that we have not yet been removed
283 // from the pending_starters vector.
284 base::WaitableEvent* starters_done_events[kNbThreads];
285 // At the end, "There can be only one" main browser process alive.
286 while (pending_starters.size() > 1) {
287 SCOPED_TRACE(testing::Message() << pending_starters.size() <<
288 " starters left.");
289 for (size_t i = 0; i < pending_starters.size(); ++i) {
290 starters_done_events[i] =
291 &chrome_starters_[pending_starters[i]]->done_event_;
292 }
293 size_t done_index = base::WaitableEvent::WaitMany(
294 starters_done_events, pending_starters.size());
295 size_t starter_index = pending_starters[done_index];
296 // If the starter is done but has not marked itself as terminated,
297 // it is because it timed out of its WaitForSingleProcess(). Only the
298 // last one standing should be left waiting... So we failed...
299 EXPECT_TRUE(chrome_starters_[starter_index]->process_terminated_ ||
300 failed) << "There is more than one main process.";
301 if (!chrome_starters_[starter_index]->process_terminated_) {
302 // This will stop the "for kNbAttempts" loop.
303 failed = true;
304 // But we let the last loop turn finish so that we can properly
305 // kill all remaining processes. Starting with this one...
[email protected]bf4878d2010-06-16 20:12:01306 if (chrome_starters_[starter_index]->process_handle_ !=
307 base::kNullProcessHandle) {
[email protected]bbef41f02010-03-04 16:16:19308 KillProcessTree(chrome_starters_[starter_index]->process_handle_);
309 }
310 }
311 pending_starters.erase(pending_starters.begin() + done_index);
312 }
313
314 // "There can be only one!" :-)
315 ASSERT_EQ(static_cast<size_t>(1), pending_starters.size());
316 size_t last_index = pending_starters.front();
317 pending_starters.empty();
[email protected]bf4878d2010-06-16 20:12:01318 if (chrome_starters_[last_index]->process_handle_ !=
319 base::kNullProcessHandle) {
[email protected]bbef41f02010-03-04 16:16:19320 KillProcessTree(chrome_starters_[last_index]->process_handle_);
321 chrome_starters_[last_index]->done_event_.Wait();
322 }
323 }
324}
325
326} // namespace