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

Side by Side Diff: chrome/browser/process_singleton_uitest.cc

Issue 2721007: Fix ProcessSingletonWinTest using default profile.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 10 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 | Annotate | Revision Log
« no previous file with comments | « base/process_util_posix.cc ('k') | chrome/browser/process_singleton_win_uitest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:mergeinfo
OLDNEW
1 // Copyright (c) 2009 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 ran on the windows platform only but has been 8 // It is currently compiled and run on Windows and Posix(non-Mac) platforms.
9 // written in a platform independent way (using the process/threads/sync 9 // Mac uses system services and ProcessSingletonMac is a noop. (Maybe it still
10 // routines from base). So it does compile fine on Mac and Linux but fails to 10 // makes sense to test that the system services are giving the behavior we
11 // launch the app and thus have not been tested for success/failures. Since it 11 // want?)
12 // was written to validate a change made to fix a bug only seen on Windows, it
13 // was left as is until it gets to be needed on the other platforms.
14
15 12
16 #include <list> 13 #include <list>
17 14
18 #include "base/file_path.h" 15 #include "base/file_path.h"
19 #include "base/file_util.h" 16 #include "base/file_util.h"
17 #include "base/path_service.h"
20 #include "base/process_util.h" 18 #include "base/process_util.h"
21 #include "base/ref_counted.h" 19 #include "base/ref_counted.h"
22 #include "base/thread.h" 20 #include "base/thread.h"
23 #include "base/waitable_event.h" 21 #include "base/waitable_event.h"
22 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/chrome_constants.h" 23 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/test/ui/ui_test.h" 25 #include "chrome/test/ui/ui_test.h"
26 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
27 27
28 namespace { 28 namespace {
29 29
30 // This is for the code that is to be ran in multiple threads at once, 30 // 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. 31 // 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 32 // 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. 33 // NewRunnableMethod class to run the StartChrome methods in many threads.
34 class ChromeStarter : public base::RefCountedThreadSafe<ChromeStarter> { 34 class ChromeStarter : public base::RefCountedThreadSafe<ChromeStarter> {
35 public: 35 public:
36 explicit ChromeStarter(int timeout_ms) 36 explicit ChromeStarter(int timeout_ms)
37 : ready_event_(false /* manual */, false /* signaled */), 37 : ready_event_(false /* manual */, false /* signaled */),
38 done_event_(false /* manual */, false /* signaled */), 38 done_event_(false /* manual */, false /* signaled */),
39 process_handle_(NULL), 39 process_handle_(NULL),
40 process_terminated_(false), 40 process_terminated_(false),
41 timeout_ms_(timeout_ms) { 41 timeout_ms_(timeout_ms) {
42 } 42 }
43 43
44 // We must reset some data members since we reuse the same ChromeStarter 44 // 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! :-) 45 // object and start/stop it a few times. We must start fresh! :-)
46 void Reset() { 46 void Reset() {
47 ready_event_.Reset(); 47 ready_event_.Reset();
48 done_event_.Reset(); 48 done_event_.Reset();
49 if (process_handle_ != NULL) 49 if (process_handle_ != base::kNullProcessHandle)
50 base::CloseProcessHandle(process_handle_); 50 base::CloseProcessHandle(process_handle_);
51 process_handle_ = NULL; 51 process_handle_ = base::kNullProcessHandle;
52 process_terminated_ = false; 52 process_terminated_ = false;
53 } 53 }
54 54
55 void StartChrome(base::WaitableEvent* start_event) { 55 void StartChrome(base::WaitableEvent* start_event, bool first_run) {
56 // TODO(port): For some reason the LaunchApp call below fails even though 56 // TODO(mattm): maybe stuff should be refactored to use
57 // we use the platform independent constant for the executable path. 57 // UITest::LaunchBrowserHelper somehow?
58 // This is the current blocker for running this test on Mac & Linux. 58 FilePath browser_directory;
59 CommandLine command_line(FilePath::FromWStringHack( 59 PathService::Get(chrome::DIR_APP, &browser_directory);
60 chrome::kBrowserProcessExecutablePath)); 60 CommandLine command_line(browser_directory.Append(
61 FilePath::FromWStringHack(chrome::kBrowserProcessExecutablePath)));
62
63 FilePath user_data_directory;
64 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory);
65 command_line.AppendSwitchWithValue(switches::kUserDataDir,
66 user_data_directory.ToWStringHack());
67
68 if (first_run)
69 command_line.AppendSwitch(switches::kFirstRun);
70 else
71 command_line.AppendSwitch(switches::kNoFirstRun);
61 72
62 // Try to get all threads to launch the app at the same time. 73 // Try to get all threads to launch the app at the same time.
63 // So let the test know we are ready. 74 // So let the test know we are ready.
64 ready_event_.Signal(); 75 ready_event_.Signal();
65 // And then wait for the test to tell us to GO! 76 // And then wait for the test to tell us to GO!
66 ASSERT_NE(static_cast<base::WaitableEvent*>(NULL), start_event); 77 ASSERT_NE(static_cast<base::WaitableEvent*>(NULL), start_event);
67 ASSERT_TRUE(start_event->Wait()); 78 ASSERT_TRUE(start_event->Wait());
68 79
69 // Here we don't wait for the app to be terminated because one of the 80 // Here we don't wait for the app to be terminated because one of the
70 // process will stay alive while the others will be restarted. If we would 81 // process will stay alive while the others will be restarted. If we would
71 // wait here, we would never get a handle to the main process... 82 // wait here, we would never get a handle to the main process...
72 base::LaunchApp(command_line, false /* wait */, 83 base::LaunchApp(command_line, false /* wait */,
73 false /* hidden */, &process_handle_); 84 false /* hidden */, &process_handle_);
74 ASSERT_NE(static_cast<base::ProcessHandle>(NULL), process_handle_); 85 ASSERT_NE(base::kNullProcessHandle, process_handle_);
75 86
76 // We can wait on the handle here, we should get stuck on one and only 87 // We can wait on the handle here, we should get stuck on one and only
77 // one process. The test below will take care of killing that process 88 // one process. The test below will take care of killing that process
78 // to unstuck us once it confirms there is only one. 89 // to unstuck us once it confirms there is only one.
79 process_terminated_ = base::WaitForSingleProcess(process_handle_, 90 process_terminated_ = base::WaitForSingleProcess(process_handle_,
80 timeout_ms_); 91 timeout_ms_);
81 // Let the test know we are done. 92 // Let the test know we are done.
82 done_event_.Signal(); 93 done_event_.Signal();
83 } 94 }
84 95
85 // Public access to simplify the test code using them. 96 // Public access to simplify the test code using them.
86 base::WaitableEvent ready_event_; 97 base::WaitableEvent ready_event_;
87 base::WaitableEvent done_event_; 98 base::WaitableEvent done_event_;
88 base::ProcessHandle process_handle_; 99 base::ProcessHandle process_handle_;
89 bool process_terminated_; 100 bool process_terminated_;
90 101
91 private: 102 private:
92 friend class base::RefCountedThreadSafe<ChromeStarter>; 103 friend class base::RefCountedThreadSafe<ChromeStarter>;
93 104
94 ~ChromeStarter() { 105 ~ChromeStarter() {
95 if (process_handle_ != NULL) 106 if (process_handle_ != base::kNullProcessHandle)
96 base::CloseProcessHandle(process_handle_); 107 base::CloseProcessHandle(process_handle_);
97 } 108 }
98 109
99 int timeout_ms_; 110 int timeout_ms_;
100 111
101 DISALLOW_COPY_AND_ASSIGN(ChromeStarter); 112 DISALLOW_COPY_AND_ASSIGN(ChromeStarter);
102 }; 113 };
103 114
104 // Our test fixture that initializes and holds onto a few global vars. 115 // Our test fixture that initializes and holds onto a few global vars.
105 class ProcessSingletonWinTest : public UITest { 116 class ProcessSingletonTest : public UITest {
106 public: 117 public:
107 ProcessSingletonWinTest() 118 ProcessSingletonTest()
108 // We use a manual reset so that all threads wake up at once when signaled 119 // We use a manual reset so that all threads wake up at once when signaled
109 // and thus we must manually reset it for each attempt. 120 // and thus we must manually reset it for each attempt.
110 : threads_waker_(true /* manual */, false /* signaled */) { 121 : threads_waker_(true /* manual */, false /* signaled */) {
111 } 122 }
112 123
113 void SetUp() { 124 void SetUp() {
114 // Start the threads and create the starters. 125 // Start the threads and create the starters.
115 for (size_t i = 0; i < kNbThreads; ++i) { 126 for (size_t i = 0; i < kNbThreads; ++i) {
116 chrome_starter_threads_[i].reset(new base::Thread("ChromeStarter")); 127 chrome_starter_threads_[i].reset(new base::Thread("ChromeStarter"));
117 ASSERT_TRUE(chrome_starter_threads_[i]->Start()); 128 ASSERT_TRUE(chrome_starter_threads_[i]->Start());
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 // The idea is to start chrome from multiple threads all at once. 186 // The idea is to start chrome from multiple threads all at once.
176 static const size_t kNbThreads = 5; 187 static const size_t kNbThreads = 5;
177 scoped_refptr<ChromeStarter> chrome_starters_[kNbThreads]; 188 scoped_refptr<ChromeStarter> chrome_starters_[kNbThreads];
178 scoped_ptr<base::Thread> chrome_starter_threads_[kNbThreads]; 189 scoped_ptr<base::Thread> chrome_starter_threads_[kNbThreads];
179 190
180 // The event that will get all threads to wake up simultaneously and try 191 // The event that will get all threads to wake up simultaneously and try
181 // to start a chrome process at the same time. 192 // to start a chrome process at the same time.
182 base::WaitableEvent threads_waker_; 193 base::WaitableEvent threads_waker_;
183 }; 194 };
184 195
185 // http://crbug.com/38572 196 TEST_F(ProcessSingletonTest, StartupRaceCondition) {
186 TEST_F(ProcessSingletonWinTest, FAILS_StartupRaceCondition) {
187 // We use this to stop the attempts loop on the first failure. 197 // We use this to stop the attempts loop on the first failure.
188 bool failed = false; 198 bool failed = false;
189 for (size_t attempt = 0; attempt < kNbAttempts && !failed; ++attempt) { 199 for (size_t attempt = 0; attempt < kNbAttempts && !failed; ++attempt) {
190 SCOPED_TRACE(testing::Message() << "Attempt: " << attempt << "."); 200 SCOPED_TRACE(testing::Message() << "Attempt: " << attempt << ".");
191 // We use a single event to get all threads to do the AppLaunch at the same 201 // We use a single event to get all threads to do the AppLaunch at the same
192 // time... 202 // time...
193 threads_waker_.Reset(); 203 threads_waker_.Reset();
194 204
205 // Test both with and without the first-run dialog, since they exercise
206 // different paths.
207 #if defined(OS_POSIX)
208 // TODO(mattm): test first run dialog singleton handling on linux too.
209 // On posix if we test the first run dialog, GracefulShutdownHandler gets
210 // the TERM signal, but since the message loop isn't running during the gtk
211 // first run dialog, the ShutdownDetector never handles it, and KillProcess
212 // has to time out (60 sec!) and SIGKILL.
213 bool first_run = false;
214 #else
215 // Test for races in both regular start up and first run start up cases.
216 bool first_run = attempt % 2;
217 #endif
218
195 // Here we prime all the threads with a ChromeStarter that will wait for 219 // Here we prime all the threads with a ChromeStarter that will wait for
196 // our signal to launch its chrome process. 220 // our signal to launch its chrome process.
197 for (size_t i = 0; i < kNbThreads; ++i) { 221 for (size_t i = 0; i < kNbThreads; ++i) {
198 ASSERT_NE(static_cast<ChromeStarter*>(NULL), chrome_starters_[i].get()); 222 ASSERT_NE(static_cast<ChromeStarter*>(NULL), chrome_starters_[i].get());
199 chrome_starters_[i]->Reset(); 223 chrome_starters_[i]->Reset();
200 224
201 ASSERT_TRUE(chrome_starter_threads_[i]->IsRunning()); 225 ASSERT_TRUE(chrome_starter_threads_[i]->IsRunning());
202 ASSERT_NE(static_cast<MessageLoop*>(NULL), 226 ASSERT_NE(static_cast<MessageLoop*>(NULL),
203 chrome_starter_threads_[i]->message_loop()); 227 chrome_starter_threads_[i]->message_loop());
204 228
205 chrome_starter_threads_[i]->message_loop()->PostTask( 229 chrome_starter_threads_[i]->message_loop()->PostTask(
206 FROM_HERE, NewRunnableMethod(chrome_starters_[i].get(), 230 FROM_HERE, NewRunnableMethod(chrome_starters_[i].get(),
207 &ChromeStarter::StartChrome, 231 &ChromeStarter::StartChrome,
208 &threads_waker_)); 232 &threads_waker_,
233 first_run));
209 } 234 }
210 235
211 // Wait for all the starters to be ready. 236 // Wait for all the starters to be ready.
212 // We could replace this loop if we ever implement a WaitAll(). 237 // We could replace this loop if we ever implement a WaitAll().
213 for (size_t i = 0; i < kNbThreads; ++i) { 238 for (size_t i = 0; i < kNbThreads; ++i) {
214 SCOPED_TRACE(testing::Message() << "Waiting on thread: " << i << "."); 239 SCOPED_TRACE(testing::Message() << "Waiting on thread: " << i << ".");
215 ASSERT_TRUE(chrome_starters_[i]->ready_event_.Wait()); 240 ASSERT_TRUE(chrome_starters_[i]->ready_event_.Wait());
216 } 241 }
217 // GO! 242 // GO!
218 threads_waker_.Signal(); 243 threads_waker_.Signal();
(...skipping 23 matching lines...) Expand all
242 // If the starter is done but has not marked itself as terminated, 267 // If the starter is done but has not marked itself as terminated,
243 // it is because it timed out of its WaitForSingleProcess(). Only the 268 // it is because it timed out of its WaitForSingleProcess(). Only the
244 // last one standing should be left waiting... So we failed... 269 // last one standing should be left waiting... So we failed...
245 EXPECT_TRUE(chrome_starters_[starter_index]->process_terminated_ || 270 EXPECT_TRUE(chrome_starters_[starter_index]->process_terminated_ ||
246 failed) << "There is more than one main process."; 271 failed) << "There is more than one main process.";
247 if (!chrome_starters_[starter_index]->process_terminated_) { 272 if (!chrome_starters_[starter_index]->process_terminated_) {
248 // This will stop the "for kNbAttempts" loop. 273 // This will stop the "for kNbAttempts" loop.
249 failed = true; 274 failed = true;
250 // But we let the last loop turn finish so that we can properly 275 // But we let the last loop turn finish so that we can properly
251 // kill all remaining processes. Starting with this one... 276 // kill all remaining processes. Starting with this one...
252 if (chrome_starters_[starter_index]->process_handle_ != NULL) { 277 if (chrome_starters_[starter_index]->process_handle_ !=
278 base::kNullProcessHandle) {
253 KillProcessTree(chrome_starters_[starter_index]->process_handle_); 279 KillProcessTree(chrome_starters_[starter_index]->process_handle_);
254 } 280 }
255 } 281 }
256 pending_starters.erase(pending_starters.begin() + done_index); 282 pending_starters.erase(pending_starters.begin() + done_index);
257 } 283 }
258 284
259 // "There can be only one!" :-) 285 // "There can be only one!" :-)
260 ASSERT_EQ(static_cast<size_t>(1), pending_starters.size()); 286 ASSERT_EQ(static_cast<size_t>(1), pending_starters.size());
261 size_t last_index = pending_starters.front(); 287 size_t last_index = pending_starters.front();
262 pending_starters.empty(); 288 pending_starters.empty();
263 if (chrome_starters_[last_index]->process_handle_ != NULL) { 289 if (chrome_starters_[last_index]->process_handle_ !=
290 base::kNullProcessHandle) {
264 KillProcessTree(chrome_starters_[last_index]->process_handle_); 291 KillProcessTree(chrome_starters_[last_index]->process_handle_);
265 chrome_starters_[last_index]->done_event_.Wait(); 292 chrome_starters_[last_index]->done_event_.Wait();
266 } 293 }
267 } 294 }
268 } 295 }
269 296
270 } // namespace 297 } // namespace
OLDNEW
« no previous file with comments | « base/process_util_posix.cc ('k') | chrome/browser/process_singleton_win_uitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698