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

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

Issue 981223004: Add test for ProcessSingleton hung rendezvous case. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix speling[sic]. Created 5 years, 9 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
OLDNEW
(Empty)
1 // Copyright 2015 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 #include "chrome/browser/process_singleton.h"
6
7 #include <windows.h>
8
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/compiler_specific.h"
12 #include "base/files/file_path.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/process/launch.h"
16 #include "base/process/process.h"
17 #include "base/process/process_handle.h"
18 #include "base/strings/string16.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/test/multiprocess_test.h"
21 #include "base/win/scoped_handle.h"
22 #include "base/win/wrapped_window_proc.h"
23 #include "chrome/browser/chrome_process_finder_win.h"
24 #include "chrome/common/chrome_constants.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "content/public/common/result_codes.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "testing/multiprocess_func_list.h"
29
30 namespace {
31
32 const char kReadyEventNameFlag[] = "ready_event_name";
33 const char kContinueEventNameFlag[] = "continue_event_name";
34 const char kCreateWindowFlag[] = "create_window";
35 const int kErrorResultCode = 0x345;
36
37 bool NotificationCallback(
38 const base::CommandLine& command_line,
39 const base::FilePath& current_directory) {
40 return true;
gab 2015/03/11 13:20:40 Remind the reader with a comment of what it means
Sigurður Ásgeirsson 2015/03/11 14:32:37 Done.
41 }
42
43 HWND CreateVisibleWindow() {
44 WNDCLASSEX wnd_cls = {};
45 base::win::InitializeWindowClass(
46 L"ProcessSingletonTest",
47 base::win::WrappedWindowProc<::DefWindowProc>,
48 0, // style
49 0, // class_extra
50 0, // window_extra
51 NULL, // cursor
52 NULL, // background
53 L"", // menu_name
54 NULL, // large_icon
55 NULL, // small_icon
56 &wnd_cls);
57
58 ATOM clazz = ::RegisterClassEx(&wnd_cls);
59
60 // Create a visible popup window to trigger user interaction in the
61 // process singleton.
62 HWND window = ::CreateWindow(reinterpret_cast<LPCWSTR>(clazz),
63 0, WS_POPUP, 0, 0, 0, 0, 0, 0, NULL, 0);
64 if (window)
gab 2015/03/11 13:20:39 Can this ever be NULL in a valid run of this test?
Sigurður Ásgeirsson 2015/03/11 14:32:37 This happens in the remote instance - error is fla
65 ::ShowWindow(window, SW_SHOW);
66
67 return window;
68 }
69
70 MULTIPROCESS_TEST_MAIN(ProcessSingletonTestProcessMain) {
71 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
72 base::FilePath user_data_dir =
73 cmd_line->GetSwitchValuePath(switches::kUserDataDir);
74 if (user_data_dir.empty())
gab 2015/03/11 13:20:40 Doesn't ASSERT semantic work here and below rather
Sigurður Ásgeirsson 2015/03/11 14:32:37 I just tested this, and there's no support for the
75 return kErrorResultCode;
76
77 base::string16 ready_event_name =
78 cmd_line->GetSwitchValueNative(kReadyEventNameFlag);
79
80 base::win::ScopedHandle ready_event(
81 ::OpenEvent(EVENT_MODIFY_STATE, FALSE, ready_event_name.c_str()));
82 if (!ready_event.IsValid())
83 return kErrorResultCode;
84
85 base::string16 continue_event_name =
86 cmd_line->GetSwitchValueNative(kContinueEventNameFlag);
87
88 base::win::ScopedHandle continue_event(
89 ::OpenEvent(SYNCHRONIZE, FALSE, continue_event_name.c_str()));
90 if (!continue_event.IsValid())
91 return kErrorResultCode;
92
93 if (cmd_line->HasSwitch(kCreateWindowFlag)) {
94 HWND window = CreateVisibleWindow();
95 if (!window)
96 return kErrorResultCode;
97 }
98
99 // Instantiate the process singleton.
100 ProcessSingleton process_singleton(user_data_dir,
101 base::Bind(&NotificationCallback));
102
103 if (!process_singleton.Create())
104 return kErrorResultCode;
105
106 // Signal ready and block for the continue event.
107 if (!::SetEvent(ready_event.Get()))
108 return kErrorResultCode;
109
110 if (::WaitForSingleObject(continue_event.Get(), INFINITE) != WAIT_OBJECT_0)
111 return kErrorResultCode;
112
113 return 0;
114 }
115
116 class ProcessSingletonTest : public base::MultiProcessTest {
117 typedef base::MultiProcessTest Super;
gab 2015/03/11 13:20:40 I've never seen this style in Chromium, tests typi
Sigurður Ásgeirsson 2015/03/11 14:32:37 Done.
118 public:
gab 2015/03/11 13:20:39 s/public/protected (I was recently told to do so
Sigurður Ásgeirsson 2015/03/11 14:32:37 Done.
119 ProcessSingletonTest() : window_option_(NO_WINDOW), msg_box_called_(false) {
120 }
121
122 void SetUp() override {
123 ASSERT_NO_FATAL_FAILURE(Super::SetUp());
124
125 // Drop the process finder notification timeout to one second for testing.
126 old_notification_timeout_ =
127 chrome::SetNotificationTimeoutInSecondsForTesting(1);
128 }
129
130 void TearDown() override {
131 chrome::SetNotificationTimeoutInSecondsForTesting(
132 old_notification_timeout_);
gab 2015/03/11 13:20:40 Why bother restoring it? Other unit tests shouldn'
Sigurður Ásgeirsson 2015/03/11 14:32:37 There are apparently cases where multiple tests ru
gab 2015/03/11 15:19:43 Right, tests do run in parallel. But by setting th
133
134 if (browser_victim_.IsValid()) {
135 EXPECT_TRUE(::SetEvent(continue_event_.Get()));
136 int exit_code = 0;
137 EXPECT_TRUE(browser_victim_.WaitForExit(&exit_code));
138 }
139
140 Super::TearDown();
141 }
142
143 enum WindowOption {
gab 2015/03/11 13:20:39 The Google C++ style-guide requires that typedefs/
Sigurður Ásgeirsson 2015/03/11 14:32:37 Done.
144 WITH_WINDOW,
145 NO_WINDOW
146 };
147
148 void LaunchHungBrowserProcess(WindowOption window_option) {
149 // Create a unique user data dir to rendezvous on.
150 ASSERT_TRUE(user_data_dir_.CreateUniqueTempDir());
151
152 // Create the named "ready" event, this is unique to our process.
153 ready_event_name_ =
154 base::StringPrintf(L"ready-event-%d", base::GetCurrentProcId());
155 base::win::ScopedHandle ready_event(
156 ::CreateEvent(NULL, TRUE, FALSE, ready_event_name_.c_str()));
157 ASSERT_TRUE(ready_event.IsValid());
158
159 // Create the named "continue" event, this is unique to our process.
160 continue_event_name_ =
161 base::StringPrintf(L"continue-event-%d", base::GetCurrentProcId());
162 continue_event_.Set(
163 ::CreateEvent(NULL, TRUE, FALSE, continue_event_name_.c_str()));
164 ASSERT_TRUE(continue_event_.IsValid());
165
166 window_option_ = window_option;
167
168 base::LaunchOptions options;
169 options.start_hidden = true;
170 browser_victim_ =
171 SpawnChildWithOptions("ProcessSingletonTestProcessMain", options);
172
173 // Wait for the ready event (or process exit).
174 HANDLE handles[] = { ready_event.Get(), browser_victim_.Handle() };
175 DWORD result = ::WaitForMultipleObjects(arraysize(handles),
176 handles,
177 FALSE,
178 INFINITE);
179 ASSERT_EQ(result, WAIT_OBJECT_0);
gab 2015/03/11 13:20:39 Assert that |ready_event| was signaled? Other piec
Sigurður Ásgeirsson 2015/03/11 14:32:37 I don't follow - WAIT_OBJECT_0 == ready_event?
gab 2015/03/11 15:19:42 Oh actually nvm, I just realized that this is alre
180 }
181
182 base::CommandLine MakeCmdLine(const std::string& procname) override {
183 base::CommandLine cmd_line = Super::MakeCmdLine(procname);
184
185 cmd_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir_.path());
186 cmd_line.AppendSwitchNative(kReadyEventNameFlag, ready_event_name_);
187 cmd_line.AppendSwitchNative(kContinueEventNameFlag, continue_event_name_);
188 if (window_option_ == WITH_WINDOW)
189 cmd_line.AppendSwitch(kCreateWindowFlag);
190
191 return cmd_line;
192 }
193
194 void PrepareTest(WindowOption window_option, bool allow_kill) {
195 ASSERT_NO_FATAL_FAILURE(LaunchHungBrowserProcess(window_option));
196
197 // The ready event has been signalled - the process singleton is held by
198 // the hung sub process.
199 test_singleton_.reset(
200 new ProcessSingleton(user_data_dir(),
201 base::Bind(&NotificationCallback)));
gab 2015/03/11 13:20:39 nit: + single space (suggest running "git cl forma
Sigurður Ásgeirsson 2015/03/11 14:32:37 Done.
202
203 test_singleton_->OverrideKillMessageBoxCallbackForTesting(
204 base::Bind(&ProcessSingletonTest::MockDisplayKillMessageBox,
205 base::Unretained(this), allow_kill));
206 }
207
208 base::Process& browser_victim() { return browser_victim_; }
gab 2015/03/11 13:20:40 Use a * (non-const & are not-allowed by style guid
Sigurður Ásgeirsson 2015/03/11 14:32:37 Done.
209 const base::FilePath& user_data_dir() const { return user_data_dir_.path(); }
210 ProcessSingleton* test_singleton() const { return test_singleton_.get(); }
211 bool msg_box_called() const { return msg_box_called_; }
212
213 private:
214 bool MockDisplayKillMessageBox(bool result) {
gab 2015/03/11 13:20:40 s/result/allow_kill ? (I had to piece things aroun
Sigurður Ásgeirsson 2015/03/11 14:32:37 Done.
215 msg_box_called_ = true;
216 return result;
217 }
218
219 base::string16 ready_event_name_;
220 base::string16 continue_event_name_;
221
222 WindowOption window_option_;
223 base::ScopedTempDir user_data_dir_;
224 base::Process browser_victim_;
225 base::win::ScopedHandle continue_event_;
226
227 scoped_ptr<ProcessSingleton> test_singleton_;
228
229 int old_notification_timeout_;
230 int msg_box_called_;
231
232 DISALLOW_COPY_AND_ASSIGN(ProcessSingletonTest);
233 };
234
235 } // namespace
236
237 TEST_F(ProcessSingletonTest, KillsHungBrowserWithNoWindows) {
238 ASSERT_NO_FATAL_FAILURE(PrepareTest(NO_WINDOW, false));
239
240 // As the hung browser has no visible window, it'll be killed without
241 // user interaction.
242 ProcessSingleton::NotifyResult notify_result =
243 test_singleton()->NotifyOtherProcessOrCreate();
244 ASSERT_EQ(notify_result, ProcessSingleton::PROFILE_IN_USE);
245
246 // The message box should not have been invoked, as the "browser" had no
247 // windows.
248 EXPECT_FALSE(msg_box_called());
249
250 int exit_code = 0;
251 EXPECT_TRUE(
252 browser_victim().WaitForExitWithTimeout(base::TimeDelta(), &exit_code));
253 EXPECT_EQ(exit_code, content::RESULT_CODE_HUNG);
gab 2015/03/11 13:20:39 Add a comment stating that RESULT_CODE_HUNG is exp
Sigurður Ásgeirsson 2015/03/11 14:32:37 Done.
254 }
255
256 TEST_F(ProcessSingletonTest, DoesntKillWithoutUserPermission) {
257 ASSERT_NO_FATAL_FAILURE(PrepareTest(WITH_WINDOW, false));
258
259 // As the hung browser has a visible window, this should query the user
260 // before killing the hung process.
261 ProcessSingleton::NotifyResult notify_result =
262 test_singleton()->NotifyOtherProcessOrCreate();
263 ASSERT_EQ(notify_result, ProcessSingleton::PROCESS_NOTIFIED);
264
265 // The message box should have been invoked, as the "browser" has a
266 // visible window.
267 EXPECT_TRUE(msg_box_called());
268
269 // Make sure the process hasn't been killed.
270 int exit_code = 0;
271 EXPECT_FALSE(
272 browser_victim().WaitForExitWithTimeout(base::TimeDelta(), &exit_code));
273 }
274
275 TEST_F(ProcessSingletonTest, KillWithUserPermission) {
276 ASSERT_NO_FATAL_FAILURE(PrepareTest(WITH_WINDOW, true));
277
278 // As the hung browser has a visible window, this should query the user
279 // before killing the hung process.
280 ProcessSingleton::NotifyResult notify_result =
281 test_singleton()->NotifyOtherProcessOrCreate();
282 ASSERT_EQ(notify_result, ProcessSingleton::PROFILE_IN_USE);
283
284 // The message box should have been invoked, as the "browser" has a
285 // visible window.
286 EXPECT_TRUE(msg_box_called());
287
288 int exit_code = 0;
289 EXPECT_TRUE(
290 browser_victim().WaitForExitWithTimeout(base::TimeDelta(), &exit_code));
291 EXPECT_EQ(exit_code, content::RESULT_CODE_HUNG);
292 }
gab 2015/03/11 13:20:39 Should we also add a test that the normal use case
Sigurður Ásgeirsson 2015/03/11 14:32:37 There are other tests for that, which test the pla
gab 2015/03/11 15:19:42 I see, maybe stress that in a comment on line 116
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698