1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/multi_process_notification.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/environment.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "base/time.h"
#include "testing/multiprocess_func_list.h"
#if defined(OS_MACOSX)
// TODO(dmaclach): Remove defined(OS_MACOSX) once
// MultiProcessNotification is implemented on Win/Linux.
namespace {
const char kStartedNotificationName[] = "MultiProcessTestStartedNotification";
const char kQuitNotificationName[] = "MultiProcessTestQuitNotification";
void SpinRunLoop(int milliseconds) {
MessageLoop *loop = MessageLoop::current();
// Post a quit task so that this loop eventually ends and we don't hang
// in the case of a bad test. Usually, the run loop will quit sooner than
// that because all tests use a MultiProcessNotificationTestQuit which quits
// the current run loop when it gets a notification.
loop->PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), milliseconds);
loop->Run();
}
class SimpleDelegate
: public multi_process_notification::Listener::Delegate {
public:
SimpleDelegate()
: notification_received_(false), started_received_(false) { }
virtual ~SimpleDelegate();
virtual void OnNotificationReceived(
const std::string& name,
multi_process_notification::Domain domain) OVERRIDE;
virtual void OnListenerStarted(
const std::string& name, multi_process_notification::Domain domain,
bool success) OVERRIDE;
bool WasNotificationReceived() { return notification_received_; }
bool WasStartedReceived() { return started_received_; }
private:
bool notification_received_;
bool started_received_;
DISALLOW_COPY_AND_ASSIGN(SimpleDelegate);
};
SimpleDelegate::~SimpleDelegate() {
}
void SimpleDelegate::OnNotificationReceived(
const std::string& name, multi_process_notification::Domain domain) {
notification_received_ = true;
}
void SimpleDelegate::OnListenerStarted(
const std::string& name, multi_process_notification::Domain domain,
bool success) {
ASSERT_TRUE(success);
started_received_ = true;
MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
}
class QuitterDelegate : public SimpleDelegate {
public:
QuitterDelegate() : SimpleDelegate() { }
virtual ~QuitterDelegate();
virtual void OnNotificationReceived(
const std::string& name,
multi_process_notification::Domain domain) OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(QuitterDelegate);
};
QuitterDelegate::~QuitterDelegate() {
}
void QuitterDelegate::OnNotificationReceived(
const std::string& name, multi_process_notification::Domain domain) {
SimpleDelegate::OnNotificationReceived(name, domain);
MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
}
int MultiProcessNotificationMain(multi_process_notification::Domain domain) {
base::Thread thread("MultiProcessNotificationMainIOThread");
base::Thread::Options options(MessageLoop::TYPE_IO, 0);
EXPECT_TRUE(thread.StartWithOptions(options));
MessageLoop loop;
QuitterDelegate quitter;
multi_process_notification::Listener listener(
kQuitNotificationName, domain, &quitter);
EXPECT_TRUE(listener.Start(thread.message_loop()));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
EXPECT_TRUE(quitter.WasStartedReceived());
EXPECT_TRUE(multi_process_notification::Post(kStartedNotificationName,
domain));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
EXPECT_TRUE(quitter.WasNotificationReceived());
return 0;
}
} // namespace
class MultiProcessNotificationTest : public base::MultiProcessTest {
public:
MultiProcessNotificationTest();
void PostNotificationTest(multi_process_notification::Domain domain);
void CrossPostNotificationTest(multi_process_notification::Domain domain);
virtual void SetUp();
MessageLoop* IOMessageLoop() { return io_thread_.message_loop(); };
private:
MessageLoop loop_;
base::Thread io_thread_;
};
MultiProcessNotificationTest::MultiProcessNotificationTest()
: io_thread_("MultiProcessNotificationTestThread") {
}
void MultiProcessNotificationTest::SetUp() {
base::Thread::Options options(MessageLoop::TYPE_IO, 0);
ASSERT_TRUE(io_thread_.StartWithOptions(options));
}
void MultiProcessNotificationTest::PostNotificationTest(
multi_process_notification::Domain domain) {
QuitterDelegate process_started;
multi_process_notification::Listener listener(
kStartedNotificationName, domain, &process_started);
ASSERT_TRUE(listener.Start(IOMessageLoop()));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(process_started.WasStartedReceived());
std::string process_name;
switch (domain) {
case multi_process_notification::ProfileDomain:
process_name = "MultiProcessProfileNotificationMain";
break;
case multi_process_notification::UserDomain:
process_name = "MultiProcessUserNotificationMain";
break;
case multi_process_notification::SystemDomain:
process_name = "MultiProcessSystemNotificationMain";
break;
}
base::ProcessHandle handle = SpawnChild(process_name, false);
ASSERT_TRUE(handle);
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(process_started.WasNotificationReceived());
ASSERT_TRUE(multi_process_notification::Post(kQuitNotificationName, domain));
int exit_code = 0;
EXPECT_TRUE(base::WaitForExitCodeWithTimeout(
handle, &exit_code, TestTimeouts::action_max_timeout_ms()));
}
void MultiProcessNotificationTest::CrossPostNotificationTest(
multi_process_notification::Domain domain) {
// Check to make sure notifications sent to user domain aren't picked up
// by system domain listeners and vice versa.
std::string local_notification("QuitLocalNotification");
std::string final_notification("FinalQuitLocalNotification");
QuitterDelegate profile_quitter;
QuitterDelegate user_quitter;
QuitterDelegate system_quitter;
QuitterDelegate final_quitter;
multi_process_notification::Listener profile_listener(
local_notification, multi_process_notification::ProfileDomain,
&profile_quitter);
multi_process_notification::Listener user_listener(
local_notification, multi_process_notification::UserDomain,
&user_quitter);
multi_process_notification::Listener system_listener(
local_notification, multi_process_notification::SystemDomain,
&system_quitter);
multi_process_notification::Listener final_listener(
final_notification, multi_process_notification::UserDomain,
&final_quitter);
MessageLoop* message_loop = IOMessageLoop();
ASSERT_TRUE(profile_listener.Start(message_loop));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(profile_quitter.WasStartedReceived());
ASSERT_TRUE(user_listener.Start(message_loop));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(user_quitter.WasStartedReceived());
ASSERT_TRUE(system_listener.Start(message_loop));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(system_quitter.WasStartedReceived());
ASSERT_TRUE(multi_process_notification::Post(local_notification, domain));
SpinRunLoop(TestTimeouts::action_timeout_ms());
// Now send out a final_notification to queue up a notification
// after the local_notification and make sure that all listeners have had a
// chance to process local_notification before we check to see if they
// were called.
ASSERT_TRUE(final_listener.Start(message_loop));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
EXPECT_TRUE(final_quitter.WasStartedReceived());
ASSERT_TRUE(multi_process_notification::Post(
final_notification, multi_process_notification::UserDomain));
SpinRunLoop(TestTimeouts::action_timeout_ms());
ASSERT_TRUE(final_quitter.WasNotificationReceived());
switch (domain) {
case multi_process_notification::ProfileDomain:
ASSERT_TRUE(profile_quitter.WasNotificationReceived());
ASSERT_FALSE(user_quitter.WasNotificationReceived());
ASSERT_FALSE(system_quitter.WasNotificationReceived());
break;
case multi_process_notification::UserDomain:
ASSERT_FALSE(profile_quitter.WasNotificationReceived());
ASSERT_TRUE(user_quitter.WasNotificationReceived());
ASSERT_FALSE(system_quitter.WasNotificationReceived());
break;
case multi_process_notification::SystemDomain:
ASSERT_FALSE(profile_quitter.WasNotificationReceived());
ASSERT_FALSE(user_quitter.WasNotificationReceived());
ASSERT_TRUE(system_quitter.WasNotificationReceived());
break;
}
}
TEST_F(MultiProcessNotificationTest, BasicCreationTest) {
QuitterDelegate quitter;
multi_process_notification::Listener local_listener(
"BasicCreationTest", multi_process_notification::UserDomain, &quitter);
MessageLoop* message_loop = IOMessageLoop();
ASSERT_TRUE(local_listener.Start(message_loop));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(quitter.WasStartedReceived());
multi_process_notification::Listener system_listener(
"BasicCreationTest", multi_process_notification::SystemDomain, &quitter);
ASSERT_TRUE(system_listener.Start(message_loop));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(quitter.WasStartedReceived());
multi_process_notification::Listener local_listener2(
"BasicCreationTest", multi_process_notification::UserDomain, &quitter);
// Should fail because current message loop should not be an IOMessageLoop.
ASSERT_FALSE(local_listener2.Start(MessageLoop::current()));
}
TEST_F(MultiProcessNotificationTest, PostInProcessNotification) {
std::string local_notification("QuitLocalNotification");
QuitterDelegate quitter;
multi_process_notification::Listener listener(
local_notification, multi_process_notification::UserDomain, &quitter);
MessageLoop* message_loop = IOMessageLoop();
ASSERT_TRUE(listener.Start(message_loop));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(quitter.WasStartedReceived());
ASSERT_TRUE(multi_process_notification::Post(
local_notification, multi_process_notification::UserDomain));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(quitter.WasNotificationReceived());
}
TEST_F(MultiProcessNotificationTest, MultiListener) {
std::string local_notification("LocalNotification");
std::string quit_local_notification("QuitLocalNotification");
SimpleDelegate delegate1;
SimpleDelegate delegate2;
multi_process_notification::Listener local_listener1(
local_notification, multi_process_notification::UserDomain,
&delegate1);
multi_process_notification::Listener local_listener2(
local_notification, multi_process_notification::UserDomain,
&delegate2);
QuitterDelegate quitter;
multi_process_notification::Listener quit_listener(quit_local_notification,
multi_process_notification::UserDomain, &quitter);
MessageLoop* message_loop = IOMessageLoop();
ASSERT_TRUE(local_listener1.Start(message_loop));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(delegate1.WasStartedReceived());
ASSERT_TRUE(local_listener2.Start(message_loop));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(delegate2.WasStartedReceived());
ASSERT_TRUE(quit_listener.Start(message_loop));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(quitter.WasStartedReceived());
ASSERT_TRUE(multi_process_notification::Post(
local_notification, multi_process_notification::UserDomain));
ASSERT_TRUE(multi_process_notification::Post(
quit_local_notification, multi_process_notification::UserDomain));
SpinRunLoop(TestTimeouts::action_max_timeout_ms());
ASSERT_TRUE(delegate1.WasNotificationReceived());
ASSERT_TRUE(delegate2.WasNotificationReceived());
ASSERT_TRUE(quitter.WasNotificationReceived());
}
TEST_F(MultiProcessNotificationTest, PostProfileNotification) {
PostNotificationTest(multi_process_notification::ProfileDomain);
}
TEST_F(MultiProcessNotificationTest, PostUserNotification) {
PostNotificationTest(multi_process_notification::UserDomain);
}
TEST_F(MultiProcessNotificationTest, PostSystemNotification) {
PostNotificationTest(multi_process_notification::SystemDomain);
}
TEST_F(MultiProcessNotificationTest, ProfileCrossDomainPosting) {
CrossPostNotificationTest(multi_process_notification::ProfileDomain);
}
TEST_F(MultiProcessNotificationTest, UserCrossDomainPosting) {
CrossPostNotificationTest(multi_process_notification::UserDomain);
}
TEST_F(MultiProcessNotificationTest, SystemCrossDomainPosting) {
CrossPostNotificationTest(multi_process_notification::SystemDomain);
}
MULTIPROCESS_TEST_MAIN(MultiProcessProfileNotificationMain) {
return MultiProcessNotificationMain(
multi_process_notification::ProfileDomain);
}
MULTIPROCESS_TEST_MAIN(MultiProcessUserNotificationMain) {
return MultiProcessNotificationMain(multi_process_notification::UserDomain);
}
MULTIPROCESS_TEST_MAIN(MultiProcessSystemNotificationMain) {
return MultiProcessNotificationMain(multi_process_notification::SystemDomain);
}
#endif // defined(OS_MACOSX)
|