| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <list> | 13 #include <list> |
| 14 | 14 |
| 15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
| 16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/process_util.h" | 18 #include "base/process_util.h" |
| 19 #include "base/ref_counted.h" | 19 #include "base/ref_counted.h" |
| 20 #include "base/scoped_temp_dir.h" |
| 20 #include "base/thread.h" | 21 #include "base/thread.h" |
| 21 #include "base/waitable_event.h" | 22 #include "base/waitable_event.h" |
| 22 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
| 23 #include "chrome/common/chrome_constants.h" | 24 #include "chrome/common/chrome_constants.h" |
| 24 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/test/test_launcher_utils.h" |
| 25 #include "chrome/test/ui/ui_test.h" | 27 #include "chrome/test/ui/ui_test.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 // This is for the code that is to be ran in multiple threads at once, | 32 // This is for the code that is to be ran in multiple threads at once, |
| 31 // to stress a race condition on first process start. | 33 // to stress a race condition on first process start. |
| 32 // We use the thread safe ref counted base class so that we can use the | 34 // We use the thread safe ref counted base class so that we can use the |
| 33 // NewRunnableMethod class to run the StartChrome methods in many threads. | 35 // NewRunnableMethod class to run the StartChrome methods in many threads. |
| 34 class ChromeStarter : public base::RefCountedThreadSafe<ChromeStarter> { | 36 class ChromeStarter : public base::RefCountedThreadSafe<ChromeStarter> { |
| 35 public: | 37 public: |
| 36 explicit ChromeStarter(int timeout_ms) | 38 explicit ChromeStarter(int timeout_ms, const FilePath& user_data_dir) |
| 37 : ready_event_(false /* manual */, false /* signaled */), | 39 : ready_event_(false /* manual */, false /* signaled */), |
| 38 done_event_(false /* manual */, false /* signaled */), | 40 done_event_(false /* manual */, false /* signaled */), |
| 39 process_handle_(NULL), | 41 process_handle_(NULL), |
| 40 process_terminated_(false), | 42 process_terminated_(false), |
| 41 timeout_ms_(timeout_ms) { | 43 timeout_ms_(timeout_ms), |
| 44 user_data_dir_(user_data_dir) { |
| 42 } | 45 } |
| 43 | 46 |
| 44 // We must reset some data members since we reuse the same ChromeStarter | 47 // We must reset some data members since we reuse the same ChromeStarter |
| 45 // object and start/stop it a few times. We must start fresh! :-) | 48 // object and start/stop it a few times. We must start fresh! :-) |
| 46 void Reset() { | 49 void Reset() { |
| 47 ready_event_.Reset(); | 50 ready_event_.Reset(); |
| 48 done_event_.Reset(); | 51 done_event_.Reset(); |
| 49 if (process_handle_ != base::kNullProcessHandle) | 52 if (process_handle_ != base::kNullProcessHandle) |
| 50 base::CloseProcessHandle(process_handle_); | 53 base::CloseProcessHandle(process_handle_); |
| 51 process_handle_ = base::kNullProcessHandle; | 54 process_handle_ = base::kNullProcessHandle; |
| 52 process_terminated_ = false; | 55 process_terminated_ = false; |
| 53 } | 56 } |
| 54 | 57 |
| 55 void StartChrome(base::WaitableEvent* start_event, bool first_run) { | 58 void StartChrome(base::WaitableEvent* start_event, bool first_run) { |
| 56 // TODO(mattm): maybe stuff should be refactored to use | 59 // TODO(mattm): maybe stuff should be refactored to use |
| 57 // UITest::LaunchBrowserHelper somehow? | 60 // UITest::LaunchBrowserHelper somehow? |
| 58 FilePath browser_directory; | 61 FilePath browser_directory; |
| 59 PathService::Get(chrome::DIR_APP, &browser_directory); | 62 PathService::Get(chrome::DIR_APP, &browser_directory); |
| 60 CommandLine command_line(browser_directory.Append( | 63 CommandLine command_line(browser_directory.Append( |
| 61 FilePath::FromWStringHack(chrome::kBrowserProcessExecutablePath))); | 64 FilePath::FromWStringHack(chrome::kBrowserProcessExecutablePath))); |
| 62 | 65 |
| 63 FilePath user_data_directory; | 66 command_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir_); |
| 64 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); | |
| 65 command_line.AppendSwitchPath(switches::kUserDataDir, user_data_directory); | |
| 66 | 67 |
| 67 if (first_run) | 68 if (first_run) |
| 68 command_line.AppendSwitch(switches::kFirstRun); | 69 command_line.AppendSwitch(switches::kFirstRun); |
| 69 else | 70 else |
| 70 command_line.AppendSwitch(switches::kNoFirstRun); | 71 command_line.AppendSwitch(switches::kNoFirstRun); |
| 71 | 72 |
| 73 // Add the normal test-mode switches, except for the ones we're adding |
| 74 // ourselves. |
| 75 CommandLine standard_switches(CommandLine::ARGUMENTS_ONLY); |
| 76 test_launcher_utils::PrepareBrowserCommandLineForTests(&standard_switches); |
| 77 const CommandLine::SwitchMap& switch_map = standard_switches.GetSwitches(); |
| 78 for (CommandLine::SwitchMap::const_iterator i = switch_map.begin(); |
| 79 i != switch_map.end(); ++i) { |
| 80 const std::string& switch_name = i->first; |
| 81 if (switch_name == switches::kUserDataDir || |
| 82 switch_name == switches::kFirstRun || |
| 83 switch_name == switches::kNoFirstRun) |
| 84 continue; |
| 85 |
| 86 command_line.AppendSwitchNative(switch_name, i->second); |
| 87 } |
| 88 |
| 72 // Try to get all threads to launch the app at the same time. | 89 // Try to get all threads to launch the app at the same time. |
| 73 // So let the test know we are ready. | 90 // So let the test know we are ready. |
| 74 ready_event_.Signal(); | 91 ready_event_.Signal(); |
| 75 // And then wait for the test to tell us to GO! | 92 // And then wait for the test to tell us to GO! |
| 76 ASSERT_NE(static_cast<base::WaitableEvent*>(NULL), start_event); | 93 ASSERT_NE(static_cast<base::WaitableEvent*>(NULL), start_event); |
| 77 ASSERT_TRUE(start_event->Wait()); | 94 ASSERT_TRUE(start_event->Wait()); |
| 78 | 95 |
| 79 // Here we don't wait for the app to be terminated because one of the | 96 // Here we don't wait for the app to be terminated because one of the |
| 80 // process will stay alive while the others will be restarted. If we would | 97 // process will stay alive while the others will be restarted. If we would |
| 81 // wait here, we would never get a handle to the main process... | 98 // wait here, we would never get a handle to the main process... |
| (...skipping 18 matching lines...) Expand all Loading... |
| 100 | 117 |
| 101 private: | 118 private: |
| 102 friend class base::RefCountedThreadSafe<ChromeStarter>; | 119 friend class base::RefCountedThreadSafe<ChromeStarter>; |
| 103 | 120 |
| 104 ~ChromeStarter() { | 121 ~ChromeStarter() { |
| 105 if (process_handle_ != base::kNullProcessHandle) | 122 if (process_handle_ != base::kNullProcessHandle) |
| 106 base::CloseProcessHandle(process_handle_); | 123 base::CloseProcessHandle(process_handle_); |
| 107 } | 124 } |
| 108 | 125 |
| 109 int timeout_ms_; | 126 int timeout_ms_; |
| 127 FilePath user_data_dir_; |
| 110 | 128 |
| 111 DISALLOW_COPY_AND_ASSIGN(ChromeStarter); | 129 DISALLOW_COPY_AND_ASSIGN(ChromeStarter); |
| 112 }; | 130 }; |
| 113 | 131 |
| 114 // Our test fixture that initializes and holds onto a few global vars. | 132 // Our test fixture that initializes and holds onto a few global vars. |
| 115 class ProcessSingletonTest : public UITest { | 133 class ProcessSingletonTest : public UITest { |
| 116 public: | 134 public: |
| 117 ProcessSingletonTest() | 135 ProcessSingletonTest() |
| 118 // We use a manual reset so that all threads wake up at once when signaled | 136 // We use a manual reset so that all threads wake up at once when signaled |
| 119 // and thus we must manually reset it for each attempt. | 137 // and thus we must manually reset it for each attempt. |
| 120 : threads_waker_(true /* manual */, false /* signaled */) { | 138 : threads_waker_(true /* manual */, false /* signaled */) { |
| 139 temp_profile_dir_.CreateUniqueTempDir(); |
| 121 } | 140 } |
| 122 | 141 |
| 123 void SetUp() { | 142 void SetUp() { |
| 124 // Start the threads and create the starters. | 143 // Start the threads and create the starters. |
| 125 for (size_t i = 0; i < kNbThreads; ++i) { | 144 for (size_t i = 0; i < kNbThreads; ++i) { |
| 126 chrome_starter_threads_[i].reset(new base::Thread("ChromeStarter")); | 145 chrome_starter_threads_[i].reset(new base::Thread("ChromeStarter")); |
| 127 ASSERT_TRUE(chrome_starter_threads_[i]->Start()); | 146 ASSERT_TRUE(chrome_starter_threads_[i]->Start()); |
| 128 chrome_starters_[i] = new ChromeStarter(action_max_timeout_ms()); | 147 chrome_starters_[i] = new ChromeStarter(action_max_timeout_ms(), |
| 148 temp_profile_dir_.path()); |
| 129 } | 149 } |
| 130 } | 150 } |
| 131 | 151 |
| 132 void TearDown() { | 152 void TearDown() { |
| 133 // Stop the threads. | 153 // Stop the threads. |
| 134 for (size_t i = 0; i < kNbThreads; ++i) | 154 for (size_t i = 0; i < kNbThreads; ++i) |
| 135 chrome_starter_threads_[i]->Stop(); | 155 chrome_starter_threads_[i]->Stop(); |
| 136 } | 156 } |
| 137 | 157 |
| 138 // This method is used to make sure we kill the main browser process after | 158 // This method is used to make sure we kill the main browser process after |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 static const size_t kNbAttempts = 5; | 203 static const size_t kNbAttempts = 5; |
| 184 | 204 |
| 185 // The idea is to start chrome from multiple threads all at once. | 205 // The idea is to start chrome from multiple threads all at once. |
| 186 static const size_t kNbThreads = 5; | 206 static const size_t kNbThreads = 5; |
| 187 scoped_refptr<ChromeStarter> chrome_starters_[kNbThreads]; | 207 scoped_refptr<ChromeStarter> chrome_starters_[kNbThreads]; |
| 188 scoped_ptr<base::Thread> chrome_starter_threads_[kNbThreads]; | 208 scoped_ptr<base::Thread> chrome_starter_threads_[kNbThreads]; |
| 189 | 209 |
| 190 // The event that will get all threads to wake up simultaneously and try | 210 // The event that will get all threads to wake up simultaneously and try |
| 191 // to start a chrome process at the same time. | 211 // to start a chrome process at the same time. |
| 192 base::WaitableEvent threads_waker_; | 212 base::WaitableEvent threads_waker_; |
| 213 |
| 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_; |
| 193 }; | 217 }; |
| 194 | 218 |
| 195 #if defined(OS_LINUX) | 219 #if defined(OS_LINUX) |
| 196 // http://crbug.com/47979 | 220 // http://crbug.com/47979 |
| 197 #define MAYBE_StartupRaceCondition FAILS_StartupRaceCondition | 221 #define MAYBE_StartupRaceCondition FAILS_StartupRaceCondition |
| 198 #else | 222 #else |
| 199 #define MAYBE_StartupRaceCondition StartupRaceCondition | 223 #define MAYBE_StartupRaceCondition StartupRaceCondition |
| 200 #endif | 224 #endif |
| 201 TEST_F(ProcessSingletonTest, MAYBE_StartupRaceCondition) { | 225 TEST_F(ProcessSingletonTest, MAYBE_StartupRaceCondition) { |
| 202 // We use this to stop the attempts loop on the first failure. | 226 // We use this to stop the attempts loop on the first failure. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 pending_starters.empty(); | 317 pending_starters.empty(); |
| 294 if (chrome_starters_[last_index]->process_handle_ != | 318 if (chrome_starters_[last_index]->process_handle_ != |
| 295 base::kNullProcessHandle) { | 319 base::kNullProcessHandle) { |
| 296 KillProcessTree(chrome_starters_[last_index]->process_handle_); | 320 KillProcessTree(chrome_starters_[last_index]->process_handle_); |
| 297 chrome_starters_[last_index]->done_event_.Wait(); | 321 chrome_starters_[last_index]->done_event_.Wait(); |
| 298 } | 322 } |
| 299 } | 323 } |
| 300 } | 324 } |
| 301 | 325 |
| 302 } // namespace | 326 } // namespace |
| OLD | NEW |