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

Side by Side Diff: chrome/browser/process_singleton_win_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 | « chrome/browser/process_singleton_uitest.cc ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // 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 //
8 // It is currently compiled and ran on the windows platform only but has been
9 // written in a platform independent way (using the process/threads/sync
10 // routines from base). So it does compile fine on Mac and Linux but fails to
11 // launch the app and thus have not been tested for success/failures. Since it
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
16 #include <list>
17
18 #include "base/file_path.h"
19 #include "base/file_util.h"
20 #include "base/process_util.h"
21 #include "base/ref_counted.h"
22 #include "base/thread.h"
23 #include "base/waitable_event.h"
24 #include "chrome/common/chrome_constants.h"
25 #include "chrome/test/ui/ui_test.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 namespace {
29
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.
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.
34 class ChromeStarter : public base::RefCountedThreadSafe<ChromeStarter> {
35 public:
36 explicit ChromeStarter(int timeout_ms)
37 : ready_event_(false /* manual */, false /* signaled */),
38 done_event_(false /* manual */, false /* signaled */),
39 process_handle_(NULL),
40 process_terminated_(false),
41 timeout_ms_(timeout_ms) {
42 }
43
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! :-)
46 void Reset() {
47 ready_event_.Reset();
48 done_event_.Reset();
49 if (process_handle_ != NULL)
50 base::CloseProcessHandle(process_handle_);
51 process_handle_ = NULL;
52 process_terminated_ = false;
53 }
54
55 void StartChrome(base::WaitableEvent* start_event) {
56 // TODO(port): For some reason the LaunchApp call below fails even though
57 // we use the platform independent constant for the executable path.
58 // This is the current blocker for running this test on Mac & Linux.
59 CommandLine command_line(FilePath::FromWStringHack(
60 chrome::kBrowserProcessExecutablePath));
61
62 // Try to get all threads to launch the app at the same time.
63 // So let the test know we are ready.
64 ready_event_.Signal();
65 // And then wait for the test to tell us to GO!
66 ASSERT_NE(static_cast<base::WaitableEvent*>(NULL), start_event);
67 ASSERT_TRUE(start_event->Wait());
68
69 // 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
71 // wait here, we would never get a handle to the main process...
72 base::LaunchApp(command_line, false /* wait */,
73 false /* hidden */, &process_handle_);
74 ASSERT_NE(static_cast<base::ProcessHandle>(NULL), process_handle_);
75
76 // 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
78 // to unstuck us once it confirms there is only one.
79 process_terminated_ = base::WaitForSingleProcess(process_handle_,
80 timeout_ms_);
81 // Let the test know we are done.
82 done_event_.Signal();
83 }
84
85 // Public access to simplify the test code using them.
86 base::WaitableEvent ready_event_;
87 base::WaitableEvent done_event_;
88 base::ProcessHandle process_handle_;
89 bool process_terminated_;
90
91 private:
92 friend class base::RefCountedThreadSafe<ChromeStarter>;
93
94 ~ChromeStarter() {
95 if (process_handle_ != NULL)
96 base::CloseProcessHandle(process_handle_);
97 }
98
99 int timeout_ms_;
100
101 DISALLOW_COPY_AND_ASSIGN(ChromeStarter);
102 };
103
104 // Our test fixture that initializes and holds onto a few global vars.
105 class ProcessSingletonWinTest : public UITest {
106 public:
107 ProcessSingletonWinTest()
108 // 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.
110 : threads_waker_(true /* manual */, false /* signaled */) {
111 }
112
113 void SetUp() {
114 // Start the threads and create the starters.
115 for (size_t i = 0; i < kNbThreads; ++i) {
116 chrome_starter_threads_[i].reset(new base::Thread("ChromeStarter"));
117 ASSERT_TRUE(chrome_starter_threads_[i]->Start());
118 chrome_starters_[i] = new ChromeStarter(action_max_timeout_ms());
119 }
120 }
121
122 void TearDown() {
123 // Stop the threads.
124 for (size_t i = 0; i < kNbThreads; ++i)
125 chrome_starter_threads_[i]->Stop();
126 }
127
128 // This method is used to make sure we kill the main browser process after
129 // all of its child processes have successfully attached to it. This was added
130 // when we realized that if we just kill the parent process right away, we
131 // sometimes end up with dangling child processes. If we Sleep for a certain
132 // amount of time, we are OK... So we introduced this method to avoid a
133 // flaky wait. Instead, we kill all descendants of the main process after we
134 // killed it, relying on the fact that we can still get the parent id of a
135 // child process, even when the parent dies.
136 void KillProcessTree(base::ProcessHandle process_handle) {
137 class ProcessTreeFilter : public base::ProcessFilter {
138 public:
139 explicit ProcessTreeFilter(base::ProcessId parent_pid) {
140 ancestor_pids_.insert(parent_pid);
141 }
142 virtual bool Includes(const base::ProcessEntry & entry) const {
143 if (ancestor_pids_.find(entry.parent_pid()) != ancestor_pids_.end()) {
144 ancestor_pids_.insert(entry.pid());
145 return true;
146 } else {
147 return false;
148 }
149 }
150 private:
151 mutable std::set<base::ProcessId> ancestor_pids_;
152 } process_tree_filter(base::GetProcId(process_handle));
153
154 // Start by explicitly killing the main process we know about...
155 static const int kExitCode = 42;
156 EXPECT_TRUE(base::KillProcess(process_handle, kExitCode, true /* wait */));
157
158 // Then loop until we can't find any of its descendant.
159 // But don't try more than kNbTries times...
160 static const int kNbTries = 10;
161 int num_tries = 0;
162 while (base::GetProcessCount(chrome::kBrowserProcessExecutablePath,
163 &process_tree_filter) > 0 && num_tries++ < kNbTries) {
164 base::KillProcesses(chrome::kBrowserProcessExecutablePath,
165 kExitCode, &process_tree_filter);
166 }
167 DLOG_IF(ERROR, num_tries >= kNbTries) << "Failed to kill all processes!";
168 }
169
170 // Since this is a hard to reproduce problem, we make a few attempts.
171 // We stop the attempts at the first error, and when there are no errors,
172 // we don't time-out of any wait, so it executes quite fast anyway.
173 static const size_t kNbAttempts = 5;
174
175 // The idea is to start chrome from multiple threads all at once.
176 static const size_t kNbThreads = 5;
177 scoped_refptr<ChromeStarter> chrome_starters_[kNbThreads];
178 scoped_ptr<base::Thread> chrome_starter_threads_[kNbThreads];
179
180 // The event that will get all threads to wake up simultaneously and try
181 // to start a chrome process at the same time.
182 base::WaitableEvent threads_waker_;
183 };
184
185 // http://crbug.com/38572
186 TEST_F(ProcessSingletonWinTest, FAILS_StartupRaceCondition) {
187 // We use this to stop the attempts loop on the first failure.
188 bool failed = false;
189 for (size_t attempt = 0; attempt < kNbAttempts && !failed; ++attempt) {
190 SCOPED_TRACE(testing::Message() << "Attempt: " << attempt << ".");
191 // We use a single event to get all threads to do the AppLaunch at the same
192 // time...
193 threads_waker_.Reset();
194
195 // Here we prime all the threads with a ChromeStarter that will wait for
196 // our signal to launch its chrome process.
197 for (size_t i = 0; i < kNbThreads; ++i) {
198 ASSERT_NE(static_cast<ChromeStarter*>(NULL), chrome_starters_[i].get());
199 chrome_starters_[i]->Reset();
200
201 ASSERT_TRUE(chrome_starter_threads_[i]->IsRunning());
202 ASSERT_NE(static_cast<MessageLoop*>(NULL),
203 chrome_starter_threads_[i]->message_loop());
204
205 chrome_starter_threads_[i]->message_loop()->PostTask(
206 FROM_HERE, NewRunnableMethod(chrome_starters_[i].get(),
207 &ChromeStarter::StartChrome,
208 &threads_waker_));
209 }
210
211 // Wait for all the starters to be ready.
212 // We could replace this loop if we ever implement a WaitAll().
213 for (size_t i = 0; i < kNbThreads; ++i) {
214 SCOPED_TRACE(testing::Message() << "Waiting on thread: " << i << ".");
215 ASSERT_TRUE(chrome_starters_[i]->ready_event_.Wait());
216 }
217 // GO!
218 threads_waker_.Signal();
219
220 // As we wait for all threads to signal that they are done, we remove their
221 // index from this vector so that we get left with only the index of
222 // the thread that started the main process.
223 std::vector<size_t> pending_starters(kNbThreads);
224 for (size_t i = 0; i < kNbThreads; ++i)
225 pending_starters[i] = i;
226
227 // We use a local array of starter's done events we must wait on...
228 // These are collected from the starters that we have not yet been removed
229 // from the pending_starters vector.
230 base::WaitableEvent* starters_done_events[kNbThreads];
231 // At the end, "There can be only one" main browser process alive.
232 while (pending_starters.size() > 1) {
233 SCOPED_TRACE(testing::Message() << pending_starters.size() <<
234 " starters left.");
235 for (size_t i = 0; i < pending_starters.size(); ++i) {
236 starters_done_events[i] =
237 &chrome_starters_[pending_starters[i]]->done_event_;
238 }
239 size_t done_index = base::WaitableEvent::WaitMany(
240 starters_done_events, pending_starters.size());
241 size_t starter_index = pending_starters[done_index];
242 // If the starter is done but has not marked itself as terminated,
243 // it is because it timed out of its WaitForSingleProcess(). Only the
244 // last one standing should be left waiting... So we failed...
245 EXPECT_TRUE(chrome_starters_[starter_index]->process_terminated_ ||
246 failed) << "There is more than one main process.";
247 if (!chrome_starters_[starter_index]->process_terminated_) {
248 // This will stop the "for kNbAttempts" loop.
249 failed = true;
250 // But we let the last loop turn finish so that we can properly
251 // kill all remaining processes. Starting with this one...
252 if (chrome_starters_[starter_index]->process_handle_ != NULL) {
253 KillProcessTree(chrome_starters_[starter_index]->process_handle_);
254 }
255 }
256 pending_starters.erase(pending_starters.begin() + done_index);
257 }
258
259 // "There can be only one!" :-)
260 ASSERT_EQ(static_cast<size_t>(1), pending_starters.size());
261 size_t last_index = pending_starters.front();
262 pending_starters.empty();
263 if (chrome_starters_[last_index]->process_handle_ != NULL) {
264 KillProcessTree(chrome_starters_[last_index]->process_handle_);
265 chrome_starters_[last_index]->done_event_.Wait();
266 }
267 }
268 }
269
270 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/process_singleton_uitest.cc ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698