blob: be40f394fb9342984ba259c233d6796928f5ac10 [file] [log] [blame]
[email protected]b90d7e802011-01-09 16:32:201// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]b7243c42010-07-23 05:23:135#include <vector>
6
[email protected]b224f792011-04-20 16:02:237#include "base/bind.h"
8#include "base/bind_helpers.h"
9#include "base/compiler_specific.h"
[email protected]9cfb89a2010-06-09 21:20:4110#include "base/eintr_wrapper.h"
initial.commitd7cae122008-07-26 21:49:3811#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
initial.commitd7cae122008-07-26 21:49:3813#include "base/message_loop.h"
[email protected]9cfb89a2010-06-09 21:20:4114#include "base/task.h"
[email protected]ce072a72010-12-31 20:02:1615#include "base/threading/platform_thread.h"
[email protected]34b99632011-01-01 01:01:0616#include "base/threading/thread.h"
initial.commitd7cae122008-07-26 21:49:3817#include "testing/gtest/include/gtest/gtest.h"
18
[email protected]295039bd2008-08-15 04:32:5719#if defined(OS_WIN)
20#include "base/message_pump_win.h"
[email protected]b90d7e802011-01-09 16:32:2021#include "base/win/scoped_handle.h"
[email protected]295039bd2008-08-15 04:32:5722#endif
[email protected]f74c8962009-04-22 20:01:3623#if defined(OS_POSIX)
24#include "base/message_pump_libevent.h"
25#endif
[email protected]295039bd2008-08-15 04:32:5726
[email protected]ce072a72010-12-31 20:02:1627using base::PlatformThread;
[email protected]4d9bdfaf2008-08-26 05:53:5728using base::Thread;
[email protected]e1acf6f2008-10-27 20:43:3329using base::Time;
30using base::TimeDelta;
[email protected]b224f792011-04-20 16:02:2331using base::TimeTicks;
[email protected]4d9bdfaf2008-08-26 05:53:5732
33// TODO(darin): Platform-specific MessageLoop tests should be grouped together
34// to avoid chopping this file up with so many #ifdefs.
[email protected]b16ef312008-08-19 18:36:2335
initial.commitd7cae122008-07-26 21:49:3836namespace {
37
[email protected]4d9bdfaf2008-08-26 05:53:5738class MessageLoopTest : public testing::Test {};
initial.commitd7cae122008-07-26 21:49:3839
40class Foo : public base::RefCounted<Foo> {
41 public:
42 Foo() : test_count_(0) {
43 }
44
45 void Test0() {
46 ++test_count_;
47 }
48
49 void Test1ConstRef(const std::string& a) {
50 ++test_count_;
51 result_.append(a);
52 }
53
54 void Test1Ptr(std::string* a) {
55 ++test_count_;
56 result_.append(*a);
57 }
58
59 void Test1Int(int a) {
60 test_count_ += a;
61 }
62
63 void Test2Ptr(std::string* a, std::string* b) {
64 ++test_count_;
65 result_.append(*a);
66 result_.append(*b);
67 }
68
69 void Test2Mixed(const std::string& a, std::string* b) {
70 ++test_count_;
71 result_.append(a);
72 result_.append(*b);
73 }
74
75 int test_count() const { return test_count_; }
76 const std::string& result() const { return result_; }
77
78 private:
[email protected]877d55d2009-11-05 21:53:0879 friend class base::RefCounted<Foo>;
80
81 ~Foo() {}
82
initial.commitd7cae122008-07-26 21:49:3883 int test_count_;
84 std::string result_;
85};
86
[email protected]b224f792011-04-20 16:02:2387// TODO(ajwong): Remove this once we've finished getting rid of the PostTask()
88// compatibility methods.
89void RunTest_PostLegacyTask(MessageLoop::Type message_loop_type) {
[email protected]4d9bdfaf2008-08-26 05:53:5790 MessageLoop loop(message_loop_type);
initial.commitd7cae122008-07-26 21:49:3891
initial.commitd7cae122008-07-26 21:49:3892 // Add tests to message loop
[email protected]ad8e04a2010-11-01 04:16:2793 scoped_refptr<Foo> foo(new Foo());
initial.commitd7cae122008-07-26 21:49:3894 std::string a("a"), b("b"), c("c"), d("d");
95 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
96 foo.get(), &Foo::Test0));
97 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
98 foo.get(), &Foo::Test1ConstRef, a));
99 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
100 foo.get(), &Foo::Test1Ptr, &b));
101 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
102 foo.get(), &Foo::Test1Int, 100));
103 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
104 foo.get(), &Foo::Test2Ptr, &a, &c));
105 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
106 foo.get(), &Foo::Test2Mixed, a, &d));
107
108 // After all tests, post a message that will shut down the message loop
[email protected]b224f792011-04-20 16:02:23109 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
110 &MessageLoop::Quit, base::Unretained(MessageLoop::current())));
111
112 // Now kick things off
113 MessageLoop::current()->Run();
114
115 EXPECT_EQ(foo->test_count(), 105);
116 EXPECT_EQ(foo->result(), "abacad");
117}
118
119void RunTest_PostTask(MessageLoop::Type message_loop_type) {
120 MessageLoop loop(message_loop_type);
121
122 // Add tests to message loop
123 scoped_refptr<Foo> foo(new Foo());
124 std::string a("a"), b("b"), c("c"), d("d");
125 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
126 &Foo::Test0, foo.get()));
127 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
128 &Foo::Test1ConstRef, foo.get(), a));
129 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
130 &Foo::Test1Ptr, foo.get(), &b));
131 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
132 &Foo::Test1Int, foo.get(), 100));
133 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
134 &Foo::Test2Ptr, foo.get(), &a, &c));
135 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
136 &Foo::Test2Mixed, foo.get(), a, &d));
137
138 // After all tests, post a message that will shut down the message loop
139 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
140 &MessageLoop::Quit, base::Unretained(MessageLoop::current())));
initial.commitd7cae122008-07-26 21:49:38141
142 // Now kick things off
143 MessageLoop::current()->Run();
144
145 EXPECT_EQ(foo->test_count(), 105);
146 EXPECT_EQ(foo->result(), "abacad");
147}
148
[email protected]4d9bdfaf2008-08-26 05:53:57149void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) {
150 MessageLoop loop(message_loop_type);
151
initial.commitd7cae122008-07-26 21:49:38152 // Add tests to message loop
[email protected]ad8e04a2010-11-01 04:16:27153 scoped_refptr<Foo> foo(new Foo());
initial.commitd7cae122008-07-26 21:49:38154 std::string a("a"), b("b"), c("c"), d("d");
[email protected]b224f792011-04-20 16:02:23155 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
156 &Foo::Test0, foo.get()));
157 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
158 &Foo::Test1ConstRef, foo.get(), a));
159 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
160 &Foo::Test1Ptr, foo.get(), &b));
161 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
162 &Foo::Test1Int, foo.get(), 100));
163 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
164 &Foo::Test2Ptr, foo.get(), &a, &c));
165 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
166 &Foo::Test2Mixed, foo.get(), a, &d));
initial.commitd7cae122008-07-26 21:49:38167
168 // After all tests, post a message that will shut down the message loop
[email protected]b224f792011-04-20 16:02:23169 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
170 &MessageLoop::Quit, base::Unretained(MessageLoop::current())));
initial.commitd7cae122008-07-26 21:49:38171
172 // Now kick things off with the SEH block active.
173 MessageLoop::current()->set_exception_restoration(true);
174 MessageLoop::current()->Run();
175 MessageLoop::current()->set_exception_restoration(false);
176
177 EXPECT_EQ(foo->test_count(), 105);
178 EXPECT_EQ(foo->result(), "abacad");
179}
180
[email protected]b224f792011-04-20 16:02:23181// This function runs slowly to simulate a large amount of work being done.
182static void SlowFunc(int pause_ms, int* quit_counter) {
183 PlatformThread::Sleep(pause_ms);
184 if (--(*quit_counter) == 0)
[email protected]752578562008-09-07 08:08:29185 MessageLoop::current()->Quit();
[email protected]b224f792011-04-20 16:02:23186}
[email protected]752578562008-09-07 08:08:29187
[email protected]b224f792011-04-20 16:02:23188// This function records the time when Run was called in a Time object, which is
[email protected]752578562008-09-07 08:08:29189// useful for building a variety of MessageLoop tests.
[email protected]b224f792011-04-20 16:02:23190static void RecordRunTimeFunc(Time* run_time, int* quit_counter) {
191 *run_time = Time::Now();
192
[email protected]752578562008-09-07 08:08:29193 // Cause our Run function to take some time to execute. As a result we can
[email protected]b224f792011-04-20 16:02:23194 // count on subsequent RecordRunTimeFunc()s running at a future time,
[email protected]752578562008-09-07 08:08:29195 // without worry about the resolution of our system clock being an issue.
[email protected]b224f792011-04-20 16:02:23196 SlowFunc(10, quit_counter);
197}
[email protected]752578562008-09-07 08:08:29198
199void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) {
200 MessageLoop loop(message_loop_type);
201
202 // Test that PostDelayedTask results in a delayed task.
203
204 const int kDelayMS = 100;
205
206 int num_tasks = 1;
207 Time run_time;
208
209 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23210 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks),
211 kDelayMS);
[email protected]752578562008-09-07 08:08:29212
213 Time time_before_run = Time::Now();
214 loop.Run();
215 Time time_after_run = Time::Now();
216
217 EXPECT_EQ(0, num_tasks);
218 EXPECT_LT(kDelayMS, (time_after_run - time_before_run).InMilliseconds());
219}
220
[email protected]b224f792011-04-20 16:02:23221void RunTest_PostDelayedTask_InDelayOrder(
222 MessageLoop::Type message_loop_type) {
[email protected]752578562008-09-07 08:08:29223 MessageLoop loop(message_loop_type);
224
225 // Test that two tasks with different delays run in the right order.
[email protected]752578562008-09-07 08:08:29226 int num_tasks = 2;
227 Time run_time1, run_time2;
228
229 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23230 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), 200);
[email protected]752578562008-09-07 08:08:29231 // If we get a large pause in execution (due to a context switch) here, this
232 // test could fail.
233 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23234 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 10);
[email protected]752578562008-09-07 08:08:29235
236 loop.Run();
237 EXPECT_EQ(0, num_tasks);
238
239 EXPECT_TRUE(run_time2 < run_time1);
240}
241
[email protected]b224f792011-04-20 16:02:23242void RunTest_PostDelayedTask_InPostOrder(
243 MessageLoop::Type message_loop_type) {
[email protected]752578562008-09-07 08:08:29244 MessageLoop loop(message_loop_type);
245
246 // Test that two tasks with the same delay run in the order in which they
247 // were posted.
248 //
249 // NOTE: This is actually an approximate test since the API only takes a
250 // "delay" parameter, so we are not exactly simulating two tasks that get
251 // posted at the exact same time. It would be nice if the API allowed us to
252 // specify the desired run time.
253
254 const int kDelayMS = 100;
255
256 int num_tasks = 2;
257 Time run_time1, run_time2;
258
259 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23260 FROM_HERE,
261 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), kDelayMS);
[email protected]752578562008-09-07 08:08:29262 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23263 FROM_HERE,
264 base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), kDelayMS);
[email protected]752578562008-09-07 08:08:29265
266 loop.Run();
267 EXPECT_EQ(0, num_tasks);
268
269 EXPECT_TRUE(run_time1 < run_time2);
270}
271
[email protected]32cda29d2008-10-09 23:58:43272void RunTest_PostDelayedTask_InPostOrder_2(
273 MessageLoop::Type message_loop_type) {
[email protected]752578562008-09-07 08:08:29274 MessageLoop loop(message_loop_type);
275
276 // Test that a delayed task still runs after a normal tasks even if the
277 // normal tasks take a long time to run.
278
279 const int kPauseMS = 50;
280
281 int num_tasks = 2;
282 Time run_time;
283
[email protected]b224f792011-04-20 16:02:23284 loop.PostTask(FROM_HERE, base::Bind(&SlowFunc, kPauseMS, &num_tasks));
[email protected]752578562008-09-07 08:08:29285 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23286 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), 10);
[email protected]752578562008-09-07 08:08:29287
288 Time time_before_run = Time::Now();
289 loop.Run();
290 Time time_after_run = Time::Now();
291
292 EXPECT_EQ(0, num_tasks);
293
294 EXPECT_LT(kPauseMS, (time_after_run - time_before_run).InMilliseconds());
295}
296
[email protected]32cda29d2008-10-09 23:58:43297void RunTest_PostDelayedTask_InPostOrder_3(
298 MessageLoop::Type message_loop_type) {
[email protected]752578562008-09-07 08:08:29299 MessageLoop loop(message_loop_type);
300
301 // Test that a delayed task still runs after a pile of normal tasks. The key
302 // difference between this test and the previous one is that here we return
303 // the MessageLoop a lot so we give the MessageLoop plenty of opportunities
304 // to maybe run the delayed task. It should know not to do so until the
305 // delayed task's delay has passed.
306
307 int num_tasks = 11;
308 Time run_time1, run_time2;
309
310 // Clutter the ML with tasks.
311 for (int i = 1; i < num_tasks; ++i)
[email protected]b224f792011-04-20 16:02:23312 loop.PostTask(FROM_HERE,
313 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks));
[email protected]752578562008-09-07 08:08:29314
315 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23316 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 1);
[email protected]752578562008-09-07 08:08:29317
318 loop.Run();
319 EXPECT_EQ(0, num_tasks);
320
321 EXPECT_TRUE(run_time2 > run_time1);
322}
323
[email protected]b224f792011-04-20 16:02:23324void RunTest_PostDelayedTask_SharedTimer(
325 MessageLoop::Type message_loop_type) {
[email protected]72deacd2008-09-23 19:19:20326 MessageLoop loop(message_loop_type);
327
328 // Test that the interval of the timer, used to run the next delayed task, is
329 // set to a value corresponding to when the next delayed task should run.
330
331 // By setting num_tasks to 1, we ensure that the first task to run causes the
332 // run loop to exit.
333 int num_tasks = 1;
334 Time run_time1, run_time2;
335
336 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23337 FROM_HERE,
338 base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks),
339 1000000);
[email protected]72deacd2008-09-23 19:19:20340 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23341 FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 10);
[email protected]72deacd2008-09-23 19:19:20342
343 Time start_time = Time::Now();
344
345 loop.Run();
346 EXPECT_EQ(0, num_tasks);
347
348 // Ensure that we ran in far less time than the slower timer.
[email protected]4615f1982008-09-23 19:22:33349 TimeDelta total_time = Time::Now() - start_time;
350 EXPECT_GT(5000, total_time.InMilliseconds());
[email protected]32cda29d2008-10-09 23:58:43351
[email protected]72deacd2008-09-23 19:19:20352 // In case both timers somehow run at nearly the same time, sleep a little
353 // and then run all pending to force them both to have run. This is just
354 // encouraging flakiness if there is any.
355 PlatformThread::Sleep(100);
356 loop.RunAllPending();
357
358 EXPECT_TRUE(run_time1.is_null());
359 EXPECT_FALSE(run_time2.is_null());
360}
361
362#if defined(OS_WIN)
363
[email protected]b224f792011-04-20 16:02:23364void SubPumpFunc() {
365 MessageLoop::current()->SetNestableTasksAllowed(true);
366 MSG msg;
367 while (GetMessage(&msg, NULL, 0, 0)) {
368 TranslateMessage(&msg);
369 DispatchMessage(&msg);
[email protected]72deacd2008-09-23 19:19:20370 }
[email protected]b224f792011-04-20 16:02:23371 MessageLoop::current()->Quit();
372}
[email protected]72deacd2008-09-23 19:19:20373
374void RunTest_PostDelayedTask_SharedTimer_SubPump() {
375 MessageLoop loop(MessageLoop::TYPE_UI);
376
377 // Test that the interval of the timer, used to run the next delayed task, is
378 // set to a value corresponding to when the next delayed task should run.
379
380 // By setting num_tasks to 1, we ensure that the first task to run causes the
381 // run loop to exit.
382 int num_tasks = 1;
383 Time run_time;
384
[email protected]b224f792011-04-20 16:02:23385 loop.PostTask(FROM_HERE, base::Bind(&SubPumpFunc));
[email protected]72deacd2008-09-23 19:19:20386
387 // This very delayed task should never run.
388 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23389 FROM_HERE,
390 base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks),
391 1000000);
[email protected]72deacd2008-09-23 19:19:20392
[email protected]b224f792011-04-20 16:02:23393 // This slightly delayed task should run from within SubPumpFunc).
394 loop.PostDelayedTask(FROM_HERE, base::Bind(&PostQuitMessage, 0), 10);
[email protected]72deacd2008-09-23 19:19:20395
396 Time start_time = Time::Now();
397
398 loop.Run();
399 EXPECT_EQ(1, num_tasks);
400
401 // Ensure that we ran in far less time than the slower timer.
[email protected]4615f1982008-09-23 19:22:33402 TimeDelta total_time = Time::Now() - start_time;
403 EXPECT_GT(5000, total_time.InMilliseconds());
[email protected]72deacd2008-09-23 19:19:20404
405 // In case both timers somehow run at nearly the same time, sleep a little
406 // and then run all pending to force them both to have run. This is just
407 // encouraging flakiness if there is any.
408 PlatformThread::Sleep(100);
409 loop.RunAllPending();
410
411 EXPECT_TRUE(run_time.is_null());
412}
413
414#endif // defined(OS_WIN)
415
[email protected]b224f792011-04-20 16:02:23416// This is used to inject a test point for recording the destructor calls for
417// Closure objects send to MessageLoop::PostTask(). It is awkward usage since we
418// are trying to hook the actual destruction, which is not a common operation.
419class RecordDeletionProbe : public base::RefCounted<RecordDeletionProbe> {
[email protected]001747c2008-09-10 00:37:07420 public:
[email protected]b224f792011-04-20 16:02:23421 RecordDeletionProbe(RecordDeletionProbe* post_on_delete, bool* was_deleted)
[email protected]001747c2008-09-10 00:37:07422 : post_on_delete_(post_on_delete), was_deleted_(was_deleted) {
423 }
[email protected]b224f792011-04-20 16:02:23424 ~RecordDeletionProbe() {
[email protected]001747c2008-09-10 00:37:07425 *was_deleted_ = true;
426 if (post_on_delete_)
[email protected]b224f792011-04-20 16:02:23427 MessageLoop::current()->PostTask(
428 FROM_HERE,
429 base::Bind(&RecordDeletionProbe::Run, post_on_delete_.get()));
[email protected]001747c2008-09-10 00:37:07430 }
431 virtual void Run() {}
432 private:
[email protected]b224f792011-04-20 16:02:23433 scoped_refptr<RecordDeletionProbe> post_on_delete_;
[email protected]001747c2008-09-10 00:37:07434 bool* was_deleted_;
435};
436
[email protected]b224f792011-04-20 16:02:23437void RunTest_EnsureDeletion(MessageLoop::Type message_loop_type) {
[email protected]001747c2008-09-10 00:37:07438 bool a_was_deleted = false;
439 bool b_was_deleted = false;
440 {
441 MessageLoop loop(message_loop_type);
442 loop.PostTask(
[email protected]b224f792011-04-20 16:02:23443 FROM_HERE, base::Bind(&RecordDeletionProbe::Run,
444 new RecordDeletionProbe(NULL, &a_was_deleted)));
[email protected]001747c2008-09-10 00:37:07445 loop.PostDelayedTask(
[email protected]b224f792011-04-20 16:02:23446 FROM_HERE, base::Bind(&RecordDeletionProbe::Run,
447 new RecordDeletionProbe(NULL, &b_was_deleted)),
448 1000); // TODO(ajwong): Do we really need 1000ms here?
[email protected]001747c2008-09-10 00:37:07449 }
450 EXPECT_TRUE(a_was_deleted);
451 EXPECT_TRUE(b_was_deleted);
452}
453
[email protected]b224f792011-04-20 16:02:23454void RunTest_EnsureDeletion_Chain(MessageLoop::Type message_loop_type) {
[email protected]001747c2008-09-10 00:37:07455 bool a_was_deleted = false;
456 bool b_was_deleted = false;
457 bool c_was_deleted = false;
458 {
459 MessageLoop loop(message_loop_type);
[email protected]b224f792011-04-20 16:02:23460 // The scoped_refptr for each of the below is held either by the chained
461 // RecordDeletionProbe, or the bound RecordDeletionProbe::Run() callback.
462 RecordDeletionProbe* a = new RecordDeletionProbe(NULL, &a_was_deleted);
463 RecordDeletionProbe* b = new RecordDeletionProbe(a, &b_was_deleted);
464 RecordDeletionProbe* c = new RecordDeletionProbe(b, &c_was_deleted);
465 loop.PostTask(FROM_HERE, base::Bind(&RecordDeletionProbe::Run, c));
[email protected]001747c2008-09-10 00:37:07466 }
467 EXPECT_TRUE(a_was_deleted);
468 EXPECT_TRUE(b_was_deleted);
469 EXPECT_TRUE(c_was_deleted);
470}
471
[email protected]b224f792011-04-20 16:02:23472void NestingFunc(int* depth) {
473 if (*depth > 0) {
474 *depth -= 1;
475 MessageLoop::current()->PostTask(FROM_HERE,
476 base::Bind(&NestingFunc, depth));
initial.commitd7cae122008-07-26 21:49:38477
[email protected]b224f792011-04-20 16:02:23478 MessageLoop::current()->SetNestableTasksAllowed(true);
479 MessageLoop::current()->Run();
initial.commitd7cae122008-07-26 21:49:38480 }
[email protected]b224f792011-04-20 16:02:23481 MessageLoop::current()->Quit();
482}
initial.commitd7cae122008-07-26 21:49:38483
[email protected]b16ef312008-08-19 18:36:23484#if defined(OS_WIN)
485
initial.commitd7cae122008-07-26 21:49:38486LONG WINAPI BadExceptionHandler(EXCEPTION_POINTERS *ex_info) {
487 ADD_FAILURE() << "bad exception handler";
488 ::ExitProcess(ex_info->ExceptionRecord->ExceptionCode);
489 return EXCEPTION_EXECUTE_HANDLER;
490}
491
492// This task throws an SEH exception: initially write to an invalid address.
493// If the right SEH filter is installed, it will fix the error.
[email protected]b224f792011-04-20 16:02:23494class Crasher : public base::RefCounted<Crasher> {
initial.commitd7cae122008-07-26 21:49:38495 public:
496 // Ctor. If trash_SEH_handler is true, the task will override the unhandled
497 // exception handler with one sure to crash this test.
[email protected]b224f792011-04-20 16:02:23498 explicit Crasher(bool trash_SEH_handler)
initial.commitd7cae122008-07-26 21:49:38499 : trash_SEH_handler_(trash_SEH_handler) {
500 }
[email protected]b224f792011-04-20 16:02:23501
initial.commitd7cae122008-07-26 21:49:38502 void Run() {
[email protected]b16ef312008-08-19 18:36:23503 PlatformThread::Sleep(1);
initial.commitd7cae122008-07-26 21:49:38504 if (trash_SEH_handler_)
505 ::SetUnhandledExceptionFilter(&BadExceptionHandler);
506 // Generate a SEH fault. We do it in asm to make sure we know how to undo
507 // the damage.
[email protected]c88873922008-07-30 13:02:03508
509#if defined(_M_IX86)
510
initial.commitd7cae122008-07-26 21:49:38511 __asm {
[email protected]b224f792011-04-20 16:02:23512 mov eax, dword ptr [Crasher::bad_array_]
initial.commitd7cae122008-07-26 21:49:38513 mov byte ptr [eax], 66
514 }
[email protected]c88873922008-07-30 13:02:03515
516#elif defined(_M_X64)
517
518 bad_array_[0] = 66;
519
[email protected]b16ef312008-08-19 18:36:23520#else
521#error "needs architecture support"
[email protected]c88873922008-07-30 13:02:03522#endif
523
initial.commitd7cae122008-07-26 21:49:38524 MessageLoop::current()->Quit();
525 }
526 // Points the bad array to a valid memory location.
527 static void FixError() {
528 bad_array_ = &valid_store_;
529 }
530
531 private:
532 bool trash_SEH_handler_;
533 static volatile char* bad_array_;
534 static char valid_store_;
535};
536
[email protected]b224f792011-04-20 16:02:23537volatile char* Crasher::bad_array_ = 0;
538char Crasher::valid_store_ = 0;
initial.commitd7cae122008-07-26 21:49:38539
540// This SEH filter fixes the problem and retries execution. Fixing requires
[email protected]b224f792011-04-20 16:02:23541// that the last instruction: mov eax, [Crasher::bad_array_] to be retried
initial.commitd7cae122008-07-26 21:49:38542// so we move the instruction pointer 5 bytes back.
[email protected]b224f792011-04-20 16:02:23543LONG WINAPI HandleCrasherException(EXCEPTION_POINTERS *ex_info) {
initial.commitd7cae122008-07-26 21:49:38544 if (ex_info->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
545 return EXCEPTION_EXECUTE_HANDLER;
546
[email protected]b224f792011-04-20 16:02:23547 Crasher::FixError();
[email protected]c88873922008-07-30 13:02:03548
549#if defined(_M_IX86)
550
initial.commitd7cae122008-07-26 21:49:38551 ex_info->ContextRecord->Eip -= 5;
[email protected]c88873922008-07-30 13:02:03552
553#elif defined(_M_X64)
554
555 ex_info->ContextRecord->Rip -= 5;
556
557#endif
558
initial.commitd7cae122008-07-26 21:49:38559 return EXCEPTION_CONTINUE_EXECUTION;
560}
561
[email protected]4d9bdfaf2008-08-26 05:53:57562void RunTest_Crasher(MessageLoop::Type message_loop_type) {
563 MessageLoop loop(message_loop_type);
[email protected]b16ef312008-08-19 18:36:23564
initial.commitd7cae122008-07-26 21:49:38565 if (::IsDebuggerPresent())
566 return;
567
568 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter =
[email protected]b224f792011-04-20 16:02:23569 ::SetUnhandledExceptionFilter(&HandleCrasherException);
initial.commitd7cae122008-07-26 21:49:38570
[email protected]b224f792011-04-20 16:02:23571 MessageLoop::current()->PostTask(
572 FROM_HERE,
573 base::Bind(&Crasher::Run, new Crasher(false)));
initial.commitd7cae122008-07-26 21:49:38574 MessageLoop::current()->set_exception_restoration(true);
575 MessageLoop::current()->Run();
576 MessageLoop::current()->set_exception_restoration(false);
577
578 ::SetUnhandledExceptionFilter(old_SEH_filter);
579}
580
[email protected]4d9bdfaf2008-08-26 05:53:57581void RunTest_CrasherNasty(MessageLoop::Type message_loop_type) {
582 MessageLoop loop(message_loop_type);
[email protected]32cda29d2008-10-09 23:58:43583
initial.commitd7cae122008-07-26 21:49:38584 if (::IsDebuggerPresent())
585 return;
586
587 LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter =
[email protected]b224f792011-04-20 16:02:23588 ::SetUnhandledExceptionFilter(&HandleCrasherException);
initial.commitd7cae122008-07-26 21:49:38589
[email protected]b224f792011-04-20 16:02:23590 MessageLoop::current()->PostTask(
591 FROM_HERE,
592 base::Bind(&Crasher::Run, new Crasher(true)));
initial.commitd7cae122008-07-26 21:49:38593 MessageLoop::current()->set_exception_restoration(true);
594 MessageLoop::current()->Run();
595 MessageLoop::current()->set_exception_restoration(false);
596
597 ::SetUnhandledExceptionFilter(old_SEH_filter);
598}
599
[email protected]295039bd2008-08-15 04:32:57600#endif // defined(OS_WIN)
601
[email protected]4d9bdfaf2008-08-26 05:53:57602void RunTest_Nesting(MessageLoop::Type message_loop_type) {
603 MessageLoop loop(message_loop_type);
[email protected]c88873922008-07-30 13:02:03604
initial.commitd7cae122008-07-26 21:49:38605 int depth = 100;
[email protected]b224f792011-04-20 16:02:23606 MessageLoop::current()->PostTask(FROM_HERE,
607 base::Bind(&NestingFunc, &depth));
initial.commitd7cae122008-07-26 21:49:38608 MessageLoop::current()->Run();
609 EXPECT_EQ(depth, 0);
610}
611
initial.commitd7cae122008-07-26 21:49:38612const wchar_t* const kMessageBoxTitle = L"MessageLoop Unit Test";
613
614enum TaskType {
615 MESSAGEBOX,
616 ENDDIALOG,
617 RECURSIVE,
618 TIMEDMESSAGELOOP,
619 QUITMESSAGELOOP,
620 ORDERERD,
621 PUMPS,
[email protected]c4280a92009-06-25 19:23:11622 SLEEP,
initial.commitd7cae122008-07-26 21:49:38623};
624
625// Saves the order in which the tasks executed.
626struct TaskItem {
627 TaskItem(TaskType t, int c, bool s)
628 : type(t),
629 cookie(c),
630 start(s) {
631 }
632
633 TaskType type;
634 int cookie;
635 bool start;
636
637 bool operator == (const TaskItem& other) const {
638 return type == other.type && cookie == other.cookie && start == other.start;
639 }
640};
641
initial.commitd7cae122008-07-26 21:49:38642std::ostream& operator <<(std::ostream& os, TaskType type) {
643 switch (type) {
644 case MESSAGEBOX: os << "MESSAGEBOX"; break;
645 case ENDDIALOG: os << "ENDDIALOG"; break;
646 case RECURSIVE: os << "RECURSIVE"; break;
647 case TIMEDMESSAGELOOP: os << "TIMEDMESSAGELOOP"; break;
648 case QUITMESSAGELOOP: os << "QUITMESSAGELOOP"; break;
649 case ORDERERD: os << "ORDERERD"; break;
650 case PUMPS: os << "PUMPS"; break;
[email protected]c4280a92009-06-25 19:23:11651 case SLEEP: os << "SLEEP"; break;
initial.commitd7cae122008-07-26 21:49:38652 default:
653 NOTREACHED();
654 os << "Unknown TaskType";
655 break;
656 }
657 return os;
658}
659
660std::ostream& operator <<(std::ostream& os, const TaskItem& item) {
661 if (item.start)
662 return os << item.type << " " << item.cookie << " starts";
663 else
664 return os << item.type << " " << item.cookie << " ends";
665}
666
[email protected]b224f792011-04-20 16:02:23667class TaskList {
initial.commitd7cae122008-07-26 21:49:38668 public:
[email protected]b224f792011-04-20 16:02:23669 void RecordStart(TaskType type, int cookie) {
670 TaskItem item(type, cookie, true);
[email protected]b026e35d2010-10-19 02:31:03671 DVLOG(1) << item;
[email protected]b224f792011-04-20 16:02:23672 task_list_.push_back(item);
initial.commitd7cae122008-07-26 21:49:38673 }
[email protected]b224f792011-04-20 16:02:23674
675 void RecordEnd(TaskType type, int cookie) {
676 TaskItem item(type, cookie, false);
[email protected]b026e35d2010-10-19 02:31:03677 DVLOG(1) << item;
[email protected]b224f792011-04-20 16:02:23678 task_list_.push_back(item);
initial.commitd7cae122008-07-26 21:49:38679 }
680
[email protected]b224f792011-04-20 16:02:23681 size_t Size() {
682 return task_list_.size();
initial.commitd7cae122008-07-26 21:49:38683 }
684
[email protected]b224f792011-04-20 16:02:23685 TaskItem Get(int n) {
686 return task_list_[n];
initial.commitd7cae122008-07-26 21:49:38687 }
688
689 private:
[email protected]b224f792011-04-20 16:02:23690 std::vector<TaskItem> task_list_;
initial.commitd7cae122008-07-26 21:49:38691};
692
[email protected]b224f792011-04-20 16:02:23693// Saves the order the tasks ran.
694void OrderedFunc(TaskList* order, int cookie) {
695 order->RecordStart(ORDERERD, cookie);
696 order->RecordEnd(ORDERERD, cookie);
697}
698
[email protected]b16ef312008-08-19 18:36:23699#if defined(OS_WIN)
700
initial.commitd7cae122008-07-26 21:49:38701// MessageLoop implicitly start a "modal message loop". Modal dialog boxes,
702// common controls (like OpenFile) and StartDoc printing function can cause
703// implicit message loops.
[email protected]b224f792011-04-20 16:02:23704void MessageBoxFunc(TaskList* order, int cookie, bool is_reentrant) {
705 order->RecordStart(MESSAGEBOX, cookie);
706 if (is_reentrant)
707 MessageLoop::current()->SetNestableTasksAllowed(true);
708 MessageBox(NULL, L"Please wait...", kMessageBoxTitle, MB_OK);
709 order->RecordEnd(MESSAGEBOX, cookie);
710}
initial.commitd7cae122008-07-26 21:49:38711
712// Will end the MessageBox.
[email protected]b224f792011-04-20 16:02:23713void EndDialogFunc(TaskList* order, int cookie) {
714 order->RecordStart(ENDDIALOG, cookie);
715 HWND window = GetActiveWindow();
716 if (window != NULL) {
717 EXPECT_NE(EndDialog(window, IDCONTINUE), 0);
718 // Cheap way to signal that the window wasn't found if RunEnd() isn't
719 // called.
720 order->RecordEnd(ENDDIALOG, cookie);
initial.commitd7cae122008-07-26 21:49:38721 }
[email protected]b224f792011-04-20 16:02:23722}
initial.commitd7cae122008-07-26 21:49:38723
[email protected]b16ef312008-08-19 18:36:23724#endif // defined(OS_WIN)
725
[email protected]b224f792011-04-20 16:02:23726void RecursiveFunc(TaskList* order, int cookie, int depth,
727 bool is_reentrant) {
728 order->RecordStart(RECURSIVE, cookie);
729 if (depth > 0) {
730 if (is_reentrant)
731 MessageLoop::current()->SetNestableTasksAllowed(true);
732 MessageLoop::current()->PostTask(
733 FROM_HERE,
734 base::Bind(&RecursiveFunc, order, cookie, depth - 1, is_reentrant));
initial.commitd7cae122008-07-26 21:49:38735 }
[email protected]b224f792011-04-20 16:02:23736 order->RecordEnd(RECURSIVE, cookie);
737}
initial.commitd7cae122008-07-26 21:49:38738
[email protected]b224f792011-04-20 16:02:23739void RecursiveSlowFunc(TaskList* order, int cookie, int depth,
740 bool is_reentrant) {
741 RecursiveFunc(order, cookie, depth, is_reentrant);
742 PlatformThread::Sleep(10); // milliseconds
743}
initial.commitd7cae122008-07-26 21:49:38744
[email protected]b224f792011-04-20 16:02:23745void QuitFunc(TaskList* order, int cookie) {
746 order->RecordStart(QUITMESSAGELOOP, cookie);
747 MessageLoop::current()->Quit();
748 order->RecordEnd(QUITMESSAGELOOP, cookie);
749}
initial.commitd7cae122008-07-26 21:49:38750
[email protected]b224f792011-04-20 16:02:23751void SleepFunc(TaskList* order, int cookie, int ms) {
752 order->RecordStart(SLEEP, cookie);
753 PlatformThread::Sleep(ms);
754 order->RecordEnd(SLEEP, cookie);
755}
[email protected]c4280a92009-06-25 19:23:11756
[email protected]295039bd2008-08-15 04:32:57757#if defined(OS_WIN)
[email protected]b224f792011-04-20 16:02:23758void RecursiveFuncWin(MessageLoop* target,
759 HANDLE event,
760 bool expect_window,
761 TaskList* order,
762 bool is_reentrant) {
763 target->PostTask(FROM_HERE,
764 base::Bind(&RecursiveFunc, order, 1, 2, is_reentrant));
765 target->PostTask(FROM_HERE,
766 base::Bind(&MessageBoxFunc, order, 2, is_reentrant));
767 target->PostTask(FROM_HERE,
768 base::Bind(&RecursiveFunc, order, 3, 2, is_reentrant));
769 // The trick here is that for recursive task processing, this task will be
770 // ran _inside_ the MessageBox message loop, dismissing the MessageBox
771 // without a chance.
772 // For non-recursive task processing, this will be executed _after_ the
773 // MessageBox will have been dismissed by the code below, where
774 // expect_window_ is true.
775 target->PostTask(FROM_HERE,
776 base::Bind(&EndDialogFunc, order, 4));
777 target->PostTask(FROM_HERE,
778 base::Bind(&QuitFunc, order, 5));
[email protected]295039bd2008-08-15 04:32:57779
[email protected]b224f792011-04-20 16:02:23780 // Enforce that every tasks are sent before starting to run the main thread
781 // message loop.
782 ASSERT_TRUE(SetEvent(event));
initial.commitd7cae122008-07-26 21:49:38783
[email protected]b224f792011-04-20 16:02:23784 // Poll for the MessageBox. Don't do this at home! At the speed we do it,
785 // you will never realize one MessageBox was shown.
786 for (; expect_window;) {
787 HWND window = FindWindow(L"#32770", kMessageBoxTitle);
788 if (window) {
789 // Dismiss it.
790 for (;;) {
791 HWND button = FindWindowEx(window, NULL, L"Button", NULL);
792 if (button != NULL) {
793 EXPECT_EQ(0, SendMessage(button, WM_LBUTTONDOWN, 0, 0));
794 EXPECT_EQ(0, SendMessage(button, WM_LBUTTONUP, 0, 0));
795 break;
initial.commitd7cae122008-07-26 21:49:38796 }
initial.commitd7cae122008-07-26 21:49:38797 }
[email protected]b224f792011-04-20 16:02:23798 break;
initial.commitd7cae122008-07-26 21:49:38799 }
800 }
[email protected]b224f792011-04-20 16:02:23801}
initial.commitd7cae122008-07-26 21:49:38802
[email protected]295039bd2008-08-15 04:32:57803#endif // defined(OS_WIN)
804
[email protected]4d9bdfaf2008-08-26 05:53:57805void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) {
806 MessageLoop loop(message_loop_type);
initial.commitd7cae122008-07-26 21:49:38807
initial.commitd7cae122008-07-26 21:49:38808 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
809 TaskList order;
[email protected]b224f792011-04-20 16:02:23810 MessageLoop::current()->PostTask(
811 FROM_HERE,
812 base::Bind(&RecursiveFunc, &order, 1, 2, false));
813 MessageLoop::current()->PostTask(
814 FROM_HERE,
815 base::Bind(&RecursiveFunc, &order, 2, 2, false));
816 MessageLoop::current()->PostTask(
817 FROM_HERE,
818 base::Bind(&QuitFunc, &order, 3));
initial.commitd7cae122008-07-26 21:49:38819
820 MessageLoop::current()->Run();
821
822 // FIFO order.
[email protected]b224f792011-04-20 16:02:23823 ASSERT_EQ(14U, order.Size());
824 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
825 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
826 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true));
827 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false));
828 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true));
829 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false));
830 EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true));
831 EXPECT_EQ(order.Get(7), TaskItem(RECURSIVE, 1, false));
832 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true));
833 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false));
834 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true));
835 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false));
836 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true));
837 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false));
initial.commitd7cae122008-07-26 21:49:38838}
839
[email protected]4554c232011-02-17 19:25:04840void RunTest_RecursiveDenial3(MessageLoop::Type message_loop_type) {
841 MessageLoop loop(message_loop_type);
842
843 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
844 TaskList order;
[email protected]b224f792011-04-20 16:02:23845 MessageLoop::current()->PostTask(
846 FROM_HERE, base::Bind(&RecursiveSlowFunc, &order, 1, 2, false));
847 MessageLoop::current()->PostTask(
848 FROM_HERE, base::Bind(&RecursiveSlowFunc, &order, 2, 2, false));
849 MessageLoop::current()->PostDelayedTask(
850 FROM_HERE, base::Bind(&OrderedFunc, &order, 3), 5);
851 MessageLoop::current()->PostDelayedTask(
852 FROM_HERE, base::Bind(&QuitFunc, &order, 4), 5);
[email protected]4554c232011-02-17 19:25:04853
854 MessageLoop::current()->Run();
855
856 // FIFO order.
[email protected]b224f792011-04-20 16:02:23857 ASSERT_EQ(16U, order.Size());
858 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
859 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
860 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true));
861 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false));
862 EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 1, true));
863 EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 1, false));
864 EXPECT_EQ(order.Get(6), TaskItem(ORDERERD, 3, true));
865 EXPECT_EQ(order.Get(7), TaskItem(ORDERERD, 3, false));
866 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true));
867 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false));
868 EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 4, true));
869 EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 4, false));
870 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 1, true));
871 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, false));
872 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 2, true));
873 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 2, false));
[email protected]4554c232011-02-17 19:25:04874}
875
[email protected]4d9bdfaf2008-08-26 05:53:57876void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type) {
877 MessageLoop loop(message_loop_type);
[email protected]32cda29d2008-10-09 23:58:43878
initial.commitd7cae122008-07-26 21:49:38879 TaskList order;
[email protected]b224f792011-04-20 16:02:23880 MessageLoop::current()->PostTask(
881 FROM_HERE, base::Bind(&RecursiveFunc, &order, 1, 2, true));
882 MessageLoop::current()->PostTask(
883 FROM_HERE, base::Bind(&RecursiveFunc, &order, 2, 2, true));
884 MessageLoop::current()->PostTask(
885 FROM_HERE, base::Bind(&QuitFunc, &order, 3));
initial.commitd7cae122008-07-26 21:49:38886
887 MessageLoop::current()->Run();
888
889 // FIFO order.
[email protected]b224f792011-04-20 16:02:23890 ASSERT_EQ(14U, order.Size());
891 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
892 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
893 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true));
894 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false));
895 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true));
896 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false));
897 EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true));
898 EXPECT_EQ(order.Get(7), TaskItem(RECURSIVE, 1, false));
899 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true));
900 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false));
901 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true));
902 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false));
903 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true));
904 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false));
initial.commitd7cae122008-07-26 21:49:38905}
906
[email protected]295039bd2008-08-15 04:32:57907#if defined(OS_WIN)
908// TODO(darin): These tests need to be ported since they test critical
909// message loop functionality.
910
initial.commitd7cae122008-07-26 21:49:38911// A side effect of this test is the generation a beep. Sorry.
[email protected]4d9bdfaf2008-08-26 05:53:57912void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type) {
913 MessageLoop loop(message_loop_type);
914
initial.commitd7cae122008-07-26 21:49:38915 Thread worker("RecursiveDenial2_worker");
[email protected]4d9bdfaf2008-08-26 05:53:57916 Thread::Options options;
917 options.message_loop_type = message_loop_type;
918 ASSERT_EQ(true, worker.StartWithOptions(options));
initial.commitd7cae122008-07-26 21:49:38919 TaskList order;
[email protected]b90d7e802011-01-09 16:32:20920 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
initial.commitd7cae122008-07-26 21:49:38921 worker.message_loop()->PostTask(FROM_HERE,
[email protected]b224f792011-04-20 16:02:23922 base::Bind(&RecursiveFuncWin,
923 MessageLoop::current(),
924 event.Get(),
925 true,
926 &order,
927 false));
initial.commitd7cae122008-07-26 21:49:38928 // Let the other thread execute.
929 WaitForSingleObject(event, INFINITE);
930 MessageLoop::current()->Run();
931
[email protected]b224f792011-04-20 16:02:23932 ASSERT_EQ(order.Size(), 17);
933 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
934 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
935 EXPECT_EQ(order.Get(2), TaskItem(MESSAGEBOX, 2, true));
936 EXPECT_EQ(order.Get(3), TaskItem(MESSAGEBOX, 2, false));
937 EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 3, true));
938 EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 3, false));
939 // When EndDialogFunc is processed, the window is already dismissed, hence no
initial.commitd7cae122008-07-26 21:49:38940 // "end" entry.
[email protected]b224f792011-04-20 16:02:23941 EXPECT_EQ(order.Get(6), TaskItem(ENDDIALOG, 4, true));
942 EXPECT_EQ(order.Get(7), TaskItem(QUITMESSAGELOOP, 5, true));
943 EXPECT_EQ(order.Get(8), TaskItem(QUITMESSAGELOOP, 5, false));
944 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 1, true));
945 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, false));
946 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 3, true));
947 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 3, false));
948 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, true));
949 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 1, false));
950 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 3, true));
951 EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, false));
initial.commitd7cae122008-07-26 21:49:38952}
953
[email protected]4d9bdfaf2008-08-26 05:53:57954// A side effect of this test is the generation a beep. Sorry. This test also
955// needs to process windows messages on the current thread.
956void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type) {
957 MessageLoop loop(message_loop_type);
958
initial.commitd7cae122008-07-26 21:49:38959 Thread worker("RecursiveSupport2_worker");
[email protected]4d9bdfaf2008-08-26 05:53:57960 Thread::Options options;
961 options.message_loop_type = message_loop_type;
962 ASSERT_EQ(true, worker.StartWithOptions(options));
initial.commitd7cae122008-07-26 21:49:38963 TaskList order;
[email protected]b90d7e802011-01-09 16:32:20964 base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL));
initial.commitd7cae122008-07-26 21:49:38965 worker.message_loop()->PostTask(FROM_HERE,
[email protected]b224f792011-04-20 16:02:23966 base::Bind(&RecursiveFuncWin,
967 MessageLoop::current(),
968 event.Get(),
969 false,
970 &order,
971 true));
initial.commitd7cae122008-07-26 21:49:38972 // Let the other thread execute.
973 WaitForSingleObject(event, INFINITE);
974 MessageLoop::current()->Run();
975
[email protected]b224f792011-04-20 16:02:23976 ASSERT_EQ(order.Size(), 18);
977 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true));
978 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false));
979 EXPECT_EQ(order.Get(2), TaskItem(MESSAGEBOX, 2, true));
initial.commitd7cae122008-07-26 21:49:38980 // Note that this executes in the MessageBox modal loop.
[email protected]b224f792011-04-20 16:02:23981 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 3, true));
982 EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 3, false));
983 EXPECT_EQ(order.Get(5), TaskItem(ENDDIALOG, 4, true));
984 EXPECT_EQ(order.Get(6), TaskItem(ENDDIALOG, 4, false));
985 EXPECT_EQ(order.Get(7), TaskItem(MESSAGEBOX, 2, false));
986 /* The order can subtly change here. The reason is that when RecursiveFunc(1)
initial.commitd7cae122008-07-26 21:49:38987 is called in the main thread, if it is faster than getting to the
[email protected]b224f792011-04-20 16:02:23988 PostTask(FROM_HERE, base::Bind(&QuitFunc) execution, the order of task
989 execution can change. We don't care anyway that the order isn't correct.
990 EXPECT_EQ(order.Get(8), TaskItem(QUITMESSAGELOOP, 5, true));
991 EXPECT_EQ(order.Get(9), TaskItem(QUITMESSAGELOOP, 5, false));
992 EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true));
993 EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false));
initial.commitd7cae122008-07-26 21:49:38994 */
[email protected]b224f792011-04-20 16:02:23995 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 3, true));
996 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 3, false));
997 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 1, true));
998 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 1, false));
999 EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, true));
1000 EXPECT_EQ(order.Get(17), TaskItem(RECURSIVE, 3, false));
initial.commitd7cae122008-07-26 21:49:381001}
1002
[email protected]295039bd2008-08-15 04:32:571003#endif // defined(OS_WIN)
1004
[email protected]b224f792011-04-20 16:02:231005void FuncThatPumps(TaskList* order, int cookie) {
1006 order->RecordStart(PUMPS, cookie);
1007 bool old_state = MessageLoop::current()->NestableTasksAllowed();
1008 MessageLoop::current()->SetNestableTasksAllowed(true);
1009 MessageLoop::current()->RunAllPending();
1010 MessageLoop::current()->SetNestableTasksAllowed(old_state);
1011 order->RecordEnd(PUMPS, cookie);
1012}
initial.commitd7cae122008-07-26 21:49:381013
initial.commitd7cae122008-07-26 21:49:381014// Tests that non nestable tasks run in FIFO if there are no nested loops.
[email protected]b224f792011-04-20 16:02:231015void RunTest_NonNestableWithNoNesting(
1016 MessageLoop::Type message_loop_type) {
[email protected]4d9bdfaf2008-08-26 05:53:571017 MessageLoop loop(message_loop_type);
1018
initial.commitd7cae122008-07-26 21:49:381019 TaskList order;
1020
[email protected]b224f792011-04-20 16:02:231021 MessageLoop::current()->PostNonNestableTask(
1022 FROM_HERE,
1023 base::Bind(&OrderedFunc, &order, 1));
1024 MessageLoop::current()->PostTask(FROM_HERE,
1025 base::Bind(&OrderedFunc, &order, 2));
1026 MessageLoop::current()->PostTask(FROM_HERE,
1027 base::Bind(&QuitFunc, &order, 3));
initial.commitd7cae122008-07-26 21:49:381028 MessageLoop::current()->Run();
1029
1030 // FIFO order.
[email protected]b224f792011-04-20 16:02:231031 ASSERT_EQ(6U, order.Size());
1032 EXPECT_EQ(order.Get(0), TaskItem(ORDERERD, 1, true));
1033 EXPECT_EQ(order.Get(1), TaskItem(ORDERERD, 1, false));
1034 EXPECT_EQ(order.Get(2), TaskItem(ORDERERD, 2, true));
1035 EXPECT_EQ(order.Get(3), TaskItem(ORDERERD, 2, false));
1036 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true));
1037 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false));
initial.commitd7cae122008-07-26 21:49:381038}
1039
1040// Tests that non nestable tasks don't run when there's code in the call stack.
[email protected]c4280a92009-06-25 19:23:111041void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type,
1042 bool use_delayed) {
[email protected]4d9bdfaf2008-08-26 05:53:571043 MessageLoop loop(message_loop_type);
1044
initial.commitd7cae122008-07-26 21:49:381045 TaskList order;
1046
[email protected]b224f792011-04-20 16:02:231047 MessageLoop::current()->PostTask(
1048 FROM_HERE,
1049 base::Bind(&FuncThatPumps, &order, 1));
[email protected]c4280a92009-06-25 19:23:111050 if (use_delayed) {
[email protected]b224f792011-04-20 16:02:231051 MessageLoop::current()->PostNonNestableDelayedTask(
1052 FROM_HERE,
1053 base::Bind(&OrderedFunc, &order, 2),
1054 1);
[email protected]c4280a92009-06-25 19:23:111055 } else {
[email protected]b224f792011-04-20 16:02:231056 MessageLoop::current()->PostNonNestableTask(
1057 FROM_HERE,
1058 base::Bind(&OrderedFunc, &order, 2));
[email protected]c4280a92009-06-25 19:23:111059 }
[email protected]b224f792011-04-20 16:02:231060 MessageLoop::current()->PostTask(FROM_HERE,
1061 base::Bind(&OrderedFunc, &order, 3));
1062 MessageLoop::current()->PostTask(FROM_HERE,
1063 base::Bind(&SleepFunc, &order, 4, 50));
1064 MessageLoop::current()->PostTask(FROM_HERE,
1065 base::Bind(&OrderedFunc, &order, 5));
[email protected]c4280a92009-06-25 19:23:111066 if (use_delayed) {
[email protected]b224f792011-04-20 16:02:231067 MessageLoop::current()->PostNonNestableDelayedTask(
1068 FROM_HERE,
1069 base::Bind(&QuitFunc, &order, 6),
1070 2);
[email protected]c4280a92009-06-25 19:23:111071 } else {
[email protected]b224f792011-04-20 16:02:231072 MessageLoop::current()->PostNonNestableTask(
1073 FROM_HERE,
1074 base::Bind(&QuitFunc, &order, 6));
[email protected]c4280a92009-06-25 19:23:111075 }
initial.commitd7cae122008-07-26 21:49:381076
initial.commitd7cae122008-07-26 21:49:381077 MessageLoop::current()->Run();
1078
1079 // FIFO order.
[email protected]b224f792011-04-20 16:02:231080 ASSERT_EQ(12U, order.Size());
1081 EXPECT_EQ(order.Get(0), TaskItem(PUMPS, 1, true));
1082 EXPECT_EQ(order.Get(1), TaskItem(ORDERERD, 3, true));
1083 EXPECT_EQ(order.Get(2), TaskItem(ORDERERD, 3, false));
1084 EXPECT_EQ(order.Get(3), TaskItem(SLEEP, 4, true));
1085 EXPECT_EQ(order.Get(4), TaskItem(SLEEP, 4, false));
1086 EXPECT_EQ(order.Get(5), TaskItem(ORDERERD, 5, true));
1087 EXPECT_EQ(order.Get(6), TaskItem(ORDERERD, 5, false));
1088 EXPECT_EQ(order.Get(7), TaskItem(PUMPS, 1, false));
1089 EXPECT_EQ(order.Get(8), TaskItem(ORDERERD, 2, true));
1090 EXPECT_EQ(order.Get(9), TaskItem(ORDERERD, 2, false));
1091 EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 6, true));
1092 EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 6, false));
initial.commitd7cae122008-07-26 21:49:381093}
1094
[email protected]295039bd2008-08-15 04:32:571095#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381096
[email protected]4d9bdfaf2008-08-26 05:53:571097class DispatcherImpl : public MessageLoopForUI::Dispatcher {
initial.commitd7cae122008-07-26 21:49:381098 public:
1099 DispatcherImpl() : dispatch_count_(0) {}
1100
1101 virtual bool Dispatch(const MSG& msg) {
1102 ::TranslateMessage(&msg);
1103 ::DispatchMessage(&msg);
[email protected]6aa4a1c02010-01-15 18:49:581104 // Do not count WM_TIMER since it is not what we post and it will cause
1105 // flakiness.
1106 if (msg.message != WM_TIMER)
1107 ++dispatch_count_;
1108 // We treat WM_LBUTTONUP as the last message.
1109 return msg.message != WM_LBUTTONUP;
initial.commitd7cae122008-07-26 21:49:381110 }
1111
1112 int dispatch_count_;
1113};
1114
[email protected]b224f792011-04-20 16:02:231115void MouseDownUp() {
1116 PostMessage(NULL, WM_LBUTTONDOWN, 0, 0);
1117 PostMessage(NULL, WM_LBUTTONUP, 'A', 0);
1118}
1119
[email protected]4d9bdfaf2008-08-26 05:53:571120void RunTest_Dispatcher(MessageLoop::Type message_loop_type) {
1121 MessageLoop loop(message_loop_type);
initial.commitd7cae122008-07-26 21:49:381122
[email protected]b224f792011-04-20 16:02:231123 MessageLoop::current()->PostDelayedTask(FROM_HERE,
1124 base::Bind(&MouseDownUp), 100);
initial.commitd7cae122008-07-26 21:49:381125 DispatcherImpl dispatcher;
[email protected]4d9bdfaf2008-08-26 05:53:571126 MessageLoopForUI::current()->Run(&dispatcher);
initial.commitd7cae122008-07-26 21:49:381127 ASSERT_EQ(2, dispatcher.dispatch_count_);
1128}
[email protected]295039bd2008-08-15 04:32:571129
[email protected]6aa4a1c02010-01-15 18:49:581130LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) {
1131 if (code == base::MessagePumpForUI::kMessageFilterCode) {
1132 MSG* msg = reinterpret_cast<MSG*>(lparam);
1133 if (msg->message == WM_LBUTTONDOWN)
1134 return TRUE;
1135 }
1136 return FALSE;
1137}
1138
1139void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type) {
1140 MessageLoop loop(message_loop_type);
1141
[email protected]b224f792011-04-20 16:02:231142 MessageLoop::current()->PostDelayedTask(FROM_HERE,
1143 base::Bind(&MouseDownUp), 100);
[email protected]6aa4a1c02010-01-15 18:49:581144 HHOOK msg_hook = SetWindowsHookEx(WH_MSGFILTER,
1145 MsgFilterProc,
1146 NULL,
1147 GetCurrentThreadId());
1148 DispatcherImpl dispatcher;
1149 MessageLoopForUI::current()->Run(&dispatcher);
1150 ASSERT_EQ(1, dispatcher.dispatch_count_);
1151 UnhookWindowsHookEx(msg_hook);
1152}
1153
[email protected]32cda29d2008-10-09 23:58:431154class TestIOHandler : public MessageLoopForIO::IOHandler {
1155 public:
[email protected]17b89142008-11-07 21:52:151156 TestIOHandler(const wchar_t* name, HANDLE signal, bool wait);
[email protected]32cda29d2008-10-09 23:58:431157
[email protected]17b89142008-11-07 21:52:151158 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
1159 DWORD bytes_transfered, DWORD error);
[email protected]32cda29d2008-10-09 23:58:431160
[email protected]17b89142008-11-07 21:52:151161 void Init();
1162 void WaitForIO();
1163 OVERLAPPED* context() { return &context_.overlapped; }
[email protected]32cda29d2008-10-09 23:58:431164 DWORD size() { return sizeof(buffer_); }
1165
1166 private:
1167 char buffer_[48];
[email protected]17b89142008-11-07 21:52:151168 MessageLoopForIO::IOContext context_;
[email protected]32cda29d2008-10-09 23:58:431169 HANDLE signal_;
[email protected]b90d7e802011-01-09 16:32:201170 base::win::ScopedHandle file_;
[email protected]17b89142008-11-07 21:52:151171 bool wait_;
[email protected]32cda29d2008-10-09 23:58:431172};
1173
[email protected]17b89142008-11-07 21:52:151174TestIOHandler::TestIOHandler(const wchar_t* name, HANDLE signal, bool wait)
1175 : signal_(signal), wait_(wait) {
[email protected]32cda29d2008-10-09 23:58:431176 memset(buffer_, 0, sizeof(buffer_));
1177 memset(&context_, 0, sizeof(context_));
[email protected]17b89142008-11-07 21:52:151178 context_.handler = this;
[email protected]32cda29d2008-10-09 23:58:431179
1180 file_.Set(CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1181 FILE_FLAG_OVERLAPPED, NULL));
1182 EXPECT_TRUE(file_.IsValid());
1183}
1184
[email protected]17b89142008-11-07 21:52:151185void TestIOHandler::Init() {
1186 MessageLoopForIO::current()->RegisterIOHandler(file_, this);
1187
1188 DWORD read;
1189 EXPECT_FALSE(ReadFile(file_, buffer_, size(), &read, context()));
1190 EXPECT_EQ(ERROR_IO_PENDING, GetLastError());
1191 if (wait_)
1192 WaitForIO();
1193}
1194
1195void TestIOHandler::OnIOCompleted(MessageLoopForIO::IOContext* context,
1196 DWORD bytes_transfered, DWORD error) {
[email protected]32cda29d2008-10-09 23:58:431197 ASSERT_TRUE(context == &context_);
[email protected]32cda29d2008-10-09 23:58:431198 ASSERT_TRUE(SetEvent(signal_));
1199}
1200
[email protected]17b89142008-11-07 21:52:151201void TestIOHandler::WaitForIO() {
1202 EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(300, this));
1203 EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(400, this));
1204}
1205
[email protected]32cda29d2008-10-09 23:58:431206void RunTest_IOHandler() {
[email protected]b90d7e802011-01-09 16:32:201207 base::win::ScopedHandle callback_called(CreateEvent(NULL, TRUE, FALSE, NULL));
[email protected]32cda29d2008-10-09 23:58:431208 ASSERT_TRUE(callback_called.IsValid());
1209
1210 const wchar_t* kPipeName = L"\\\\.\\pipe\\iohandler_pipe";
[email protected]b90d7e802011-01-09 16:32:201211 base::win::ScopedHandle server(
1212 CreateNamedPipe(kPipeName, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL));
[email protected]32cda29d2008-10-09 23:58:431213 ASSERT_TRUE(server.IsValid());
1214
1215 Thread thread("IOHandler test");
1216 Thread::Options options;
1217 options.message_loop_type = MessageLoop::TYPE_IO;
1218 ASSERT_TRUE(thread.StartWithOptions(options));
1219
1220 MessageLoop* thread_loop = thread.message_loop();
1221 ASSERT_TRUE(NULL != thread_loop);
1222
[email protected]17b89142008-11-07 21:52:151223 TestIOHandler handler(kPipeName, callback_called, false);
[email protected]b224f792011-04-20 16:02:231224 thread_loop->PostTask(FROM_HERE, base::Bind(&TestIOHandler::Init,
1225 base::Unretained(&handler)));
[email protected]32cda29d2008-10-09 23:58:431226 Sleep(100); // Make sure the thread runs and sleeps for lack of work.
1227
1228 const char buffer[] = "Hello there!";
1229 DWORD written;
1230 EXPECT_TRUE(WriteFile(server, buffer, sizeof(buffer), &written, NULL));
1231
1232 DWORD result = WaitForSingleObject(callback_called, 1000);
1233 EXPECT_EQ(WAIT_OBJECT_0, result);
1234
1235 thread.Stop();
1236}
1237
[email protected]17b89142008-11-07 21:52:151238void RunTest_WaitForIO() {
[email protected]b90d7e802011-01-09 16:32:201239 base::win::ScopedHandle callback1_called(
1240 CreateEvent(NULL, TRUE, FALSE, NULL));
1241 base::win::ScopedHandle callback2_called(
1242 CreateEvent(NULL, TRUE, FALSE, NULL));
[email protected]17b89142008-11-07 21:52:151243 ASSERT_TRUE(callback1_called.IsValid());
1244 ASSERT_TRUE(callback2_called.IsValid());
1245
1246 const wchar_t* kPipeName1 = L"\\\\.\\pipe\\iohandler_pipe1";
1247 const wchar_t* kPipeName2 = L"\\\\.\\pipe\\iohandler_pipe2";
[email protected]b90d7e802011-01-09 16:32:201248 base::win::ScopedHandle server1(
1249 CreateNamedPipe(kPipeName1, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL));
1250 base::win::ScopedHandle server2(
1251 CreateNamedPipe(kPipeName2, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL));
[email protected]17b89142008-11-07 21:52:151252 ASSERT_TRUE(server1.IsValid());
1253 ASSERT_TRUE(server2.IsValid());
1254
1255 Thread thread("IOHandler test");
1256 Thread::Options options;
1257 options.message_loop_type = MessageLoop::TYPE_IO;
1258 ASSERT_TRUE(thread.StartWithOptions(options));
1259
1260 MessageLoop* thread_loop = thread.message_loop();
1261 ASSERT_TRUE(NULL != thread_loop);
1262
1263 TestIOHandler handler1(kPipeName1, callback1_called, false);
1264 TestIOHandler handler2(kPipeName2, callback2_called, true);
[email protected]b224f792011-04-20 16:02:231265 thread_loop->PostTask(FROM_HERE, base::Bind(&TestIOHandler::Init,
1266 base::Unretained(&handler1)));
1267 // TODO(ajwong): Do we really need such long Sleeps in ths function?
[email protected]17b89142008-11-07 21:52:151268 Sleep(100); // Make sure the thread runs and sleeps for lack of work.
[email protected]b224f792011-04-20 16:02:231269 thread_loop->PostTask(FROM_HERE, base::Bind(&TestIOHandler::Init,
1270 base::Unretained(&handler2)));
[email protected]17b89142008-11-07 21:52:151271 Sleep(100);
1272
1273 // At this time handler1 is waiting to be called, and the thread is waiting
1274 // on the Init method of handler2, filtering only handler2 callbacks.
1275
1276 const char buffer[] = "Hello there!";
1277 DWORD written;
1278 EXPECT_TRUE(WriteFile(server1, buffer, sizeof(buffer), &written, NULL));
1279 Sleep(200);
1280 EXPECT_EQ(WAIT_TIMEOUT, WaitForSingleObject(callback1_called, 0)) <<
1281 "handler1 has not been called";
1282
1283 EXPECT_TRUE(WriteFile(server2, buffer, sizeof(buffer), &written, NULL));
1284
1285 HANDLE objects[2] = { callback1_called.Get(), callback2_called.Get() };
1286 DWORD result = WaitForMultipleObjects(2, objects, TRUE, 1000);
1287 EXPECT_EQ(WAIT_OBJECT_0, result);
1288
1289 thread.Stop();
1290}
1291
[email protected]4d9bdfaf2008-08-26 05:53:571292#endif // defined(OS_WIN)
license.botbf09a502008-08-24 00:55:551293
[email protected]4d9bdfaf2008-08-26 05:53:571294} // namespace
1295
1296//-----------------------------------------------------------------------------
1297// Each test is run against each type of MessageLoop. That way we are sure
1298// that message loops work properly in all configurations. Of course, in some
1299// cases, a unit test may only be for a particular type of loop.
1300
[email protected]b224f792011-04-20 16:02:231301TEST(MessageLoopTest, PostLegacyTask) {
1302 RunTest_PostLegacyTask(MessageLoop::TYPE_DEFAULT);
1303 RunTest_PostLegacyTask(MessageLoop::TYPE_UI);
1304 RunTest_PostLegacyTask(MessageLoop::TYPE_IO);
1305}
1306
[email protected]4d9bdfaf2008-08-26 05:53:571307TEST(MessageLoopTest, PostTask) {
1308 RunTest_PostTask(MessageLoop::TYPE_DEFAULT);
1309 RunTest_PostTask(MessageLoop::TYPE_UI);
1310 RunTest_PostTask(MessageLoop::TYPE_IO);
1311}
1312
1313TEST(MessageLoopTest, PostTask_SEH) {
1314 RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT);
1315 RunTest_PostTask_SEH(MessageLoop::TYPE_UI);
1316 RunTest_PostTask_SEH(MessageLoop::TYPE_IO);
1317}
1318
[email protected]752578562008-09-07 08:08:291319TEST(MessageLoopTest, PostDelayedTask_Basic) {
1320 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT);
1321 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI);
1322 RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO);
1323}
1324
1325TEST(MessageLoopTest, PostDelayedTask_InDelayOrder) {
1326 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_DEFAULT);
1327 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_UI);
1328 RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_IO);
1329}
1330
1331TEST(MessageLoopTest, PostDelayedTask_InPostOrder) {
1332 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_DEFAULT);
1333 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_UI);
1334 RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_IO);
1335}
1336
1337TEST(MessageLoopTest, PostDelayedTask_InPostOrder_2) {
1338 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_DEFAULT);
1339 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI);
1340 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO);
1341}
1342
1343TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) {
1344 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT);
1345 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI);
1346 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO);
1347}
1348
[email protected]72deacd2008-09-23 19:19:201349TEST(MessageLoopTest, PostDelayedTask_SharedTimer) {
1350 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_DEFAULT);
1351 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_UI);
1352 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_IO);
1353}
1354
1355#if defined(OS_WIN)
1356TEST(MessageLoopTest, PostDelayedTask_SharedTimer_SubPump) {
1357 RunTest_PostDelayedTask_SharedTimer_SubPump();
1358}
1359#endif
1360
[email protected]5affb7e2010-05-25 20:13:361361// TODO(darin): MessageLoop does not support deleting all tasks in the
1362// destructor.
[email protected]2f861b32010-07-26 21:23:181363// Fails, http://crbug.com/50272.
[email protected]b224f792011-04-20 16:02:231364TEST(MessageLoopTest, FAILS_EnsureDeletion) {
1365 RunTest_EnsureDeletion(MessageLoop::TYPE_DEFAULT);
1366 RunTest_EnsureDeletion(MessageLoop::TYPE_UI);
1367 RunTest_EnsureDeletion(MessageLoop::TYPE_IO);
[email protected]001747c2008-09-10 00:37:071368}
1369
[email protected]5affb7e2010-05-25 20:13:361370// TODO(darin): MessageLoop does not support deleting all tasks in the
1371// destructor.
[email protected]2f861b32010-07-26 21:23:181372// Fails, http://crbug.com/50272.
[email protected]b224f792011-04-20 16:02:231373TEST(MessageLoopTest, FAILS_EnsureDeletion_Chain) {
1374 RunTest_EnsureDeletion_Chain(MessageLoop::TYPE_DEFAULT);
1375 RunTest_EnsureDeletion_Chain(MessageLoop::TYPE_UI);
1376 RunTest_EnsureDeletion_Chain(MessageLoop::TYPE_IO);
[email protected]001747c2008-09-10 00:37:071377}
1378
[email protected]4d9bdfaf2008-08-26 05:53:571379#if defined(OS_WIN)
1380TEST(MessageLoopTest, Crasher) {
1381 RunTest_Crasher(MessageLoop::TYPE_DEFAULT);
1382 RunTest_Crasher(MessageLoop::TYPE_UI);
1383 RunTest_Crasher(MessageLoop::TYPE_IO);
1384}
1385
1386TEST(MessageLoopTest, CrasherNasty) {
1387 RunTest_CrasherNasty(MessageLoop::TYPE_DEFAULT);
1388 RunTest_CrasherNasty(MessageLoop::TYPE_UI);
1389 RunTest_CrasherNasty(MessageLoop::TYPE_IO);
1390}
1391#endif // defined(OS_WIN)
1392
1393TEST(MessageLoopTest, Nesting) {
1394 RunTest_Nesting(MessageLoop::TYPE_DEFAULT);
1395 RunTest_Nesting(MessageLoop::TYPE_UI);
1396 RunTest_Nesting(MessageLoop::TYPE_IO);
1397}
1398
1399TEST(MessageLoopTest, RecursiveDenial1) {
1400 RunTest_RecursiveDenial1(MessageLoop::TYPE_DEFAULT);
1401 RunTest_RecursiveDenial1(MessageLoop::TYPE_UI);
1402 RunTest_RecursiveDenial1(MessageLoop::TYPE_IO);
1403}
1404
[email protected]4554c232011-02-17 19:25:041405TEST(MessageLoopTest, RecursiveDenial3) {
1406 RunTest_RecursiveDenial3(MessageLoop::TYPE_DEFAULT);
1407 RunTest_RecursiveDenial3(MessageLoop::TYPE_UI);
1408 RunTest_RecursiveDenial3(MessageLoop::TYPE_IO);
1409}
1410
[email protected]4d9bdfaf2008-08-26 05:53:571411TEST(MessageLoopTest, RecursiveSupport1) {
1412 RunTest_RecursiveSupport1(MessageLoop::TYPE_DEFAULT);
1413 RunTest_RecursiveSupport1(MessageLoop::TYPE_UI);
1414 RunTest_RecursiveSupport1(MessageLoop::TYPE_IO);
1415}
1416
1417#if defined(OS_WIN)
[email protected]85f39f82010-05-19 14:33:441418// This test occasionally hangs http://crbug.com/44567
1419TEST(MessageLoopTest, DISABLED_RecursiveDenial2) {
[email protected]4d9bdfaf2008-08-26 05:53:571420 RunTest_RecursiveDenial2(MessageLoop::TYPE_DEFAULT);
1421 RunTest_RecursiveDenial2(MessageLoop::TYPE_UI);
1422 RunTest_RecursiveDenial2(MessageLoop::TYPE_IO);
1423}
1424
1425TEST(MessageLoopTest, RecursiveSupport2) {
1426 // This test requires a UI loop
1427 RunTest_RecursiveSupport2(MessageLoop::TYPE_UI);
1428}
1429#endif // defined(OS_WIN)
1430
1431TEST(MessageLoopTest, NonNestableWithNoNesting) {
1432 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_DEFAULT);
1433 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_UI);
1434 RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_IO);
1435}
1436
1437TEST(MessageLoopTest, NonNestableInNestedLoop) {
[email protected]c4280a92009-06-25 19:23:111438 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, false);
1439 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, false);
1440 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, false);
1441}
1442
1443TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) {
1444 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true);
1445 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true);
1446 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true);
[email protected]4d9bdfaf2008-08-26 05:53:571447}
1448
[email protected]b224f792011-04-20 16:02:231449void PostNTasksThenQuit(int posts_remaining) {
1450 if (posts_remaining > 1) {
1451 MessageLoop::current()->PostTask(
1452 FROM_HERE,
1453 base::Bind(&PostNTasksThenQuit, posts_remaining - 1));
1454 } else {
1455 MessageLoop::current()->Quit();
[email protected]9cfb89a2010-06-09 21:20:411456 }
[email protected]b224f792011-04-20 16:02:231457}
[email protected]9cfb89a2010-06-09 21:20:411458
1459class DummyTaskObserver : public MessageLoop::TaskObserver {
1460 public:
[email protected]b7243c42010-07-23 05:23:131461 explicit DummyTaskObserver(int num_tasks)
[email protected]9cfb89a2010-06-09 21:20:411462 : num_tasks_started_(0),
1463 num_tasks_processed_(0),
1464 num_tasks_(num_tasks) {}
1465
1466 virtual ~DummyTaskObserver() {}
1467
[email protected]b224f792011-04-20 16:02:231468 virtual void WillProcessTask(TimeTicks time_posted) OVERRIDE {
[email protected]9cfb89a2010-06-09 21:20:411469 num_tasks_started_++;
[email protected]b224f792011-04-20 16:02:231470 EXPECT_TRUE(time_posted != TimeTicks());
[email protected]9cfb89a2010-06-09 21:20:411471 EXPECT_LE(num_tasks_started_, num_tasks_);
1472 EXPECT_EQ(num_tasks_started_, num_tasks_processed_ + 1);
1473 }
1474
[email protected]b224f792011-04-20 16:02:231475 virtual void DidProcessTask(TimeTicks time_posted) OVERRIDE {
[email protected]9cfb89a2010-06-09 21:20:411476 num_tasks_processed_++;
[email protected]b224f792011-04-20 16:02:231477 EXPECT_TRUE(time_posted != TimeTicks());
[email protected]9cfb89a2010-06-09 21:20:411478 EXPECT_LE(num_tasks_started_, num_tasks_);
1479 EXPECT_EQ(num_tasks_started_, num_tasks_processed_);
1480 }
1481
1482 int num_tasks_started() const { return num_tasks_started_; }
1483 int num_tasks_processed() const { return num_tasks_processed_; }
1484
1485 private:
1486 int num_tasks_started_;
1487 int num_tasks_processed_;
1488 const int num_tasks_;
1489
1490 DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver);
1491};
1492
1493TEST(MessageLoopTest, TaskObserver) {
[email protected]b224f792011-04-20 16:02:231494 const int kNumPosts = 6;
1495 DummyTaskObserver observer(kNumPosts);
[email protected]9cfb89a2010-06-09 21:20:411496
1497 MessageLoop loop;
1498 loop.AddTaskObserver(&observer);
[email protected]b224f792011-04-20 16:02:231499 loop.PostTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, kNumPosts));
[email protected]9cfb89a2010-06-09 21:20:411500 loop.Run();
1501 loop.RemoveTaskObserver(&observer);
1502
[email protected]b224f792011-04-20 16:02:231503 EXPECT_EQ(kNumPosts, observer.num_tasks_started());
1504 EXPECT_EQ(kNumPosts, observer.num_tasks_processed());
[email protected]9cfb89a2010-06-09 21:20:411505}
1506
[email protected]4d9bdfaf2008-08-26 05:53:571507#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:571508TEST(MessageLoopTest, Dispatcher) {
1509 // This test requires a UI loop
1510 RunTest_Dispatcher(MessageLoop::TYPE_UI);
1511}
[email protected]32cda29d2008-10-09 23:58:431512
[email protected]6aa4a1c02010-01-15 18:49:581513TEST(MessageLoopTest, DispatcherWithMessageHook) {
1514 // This test requires a UI loop
1515 RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI);
1516}
1517
[email protected]32cda29d2008-10-09 23:58:431518TEST(MessageLoopTest, IOHandler) {
1519 RunTest_IOHandler();
1520}
[email protected]17b89142008-11-07 21:52:151521
1522TEST(MessageLoopTest, WaitForIO) {
1523 RunTest_WaitForIO();
1524}
[email protected]57f030a2010-06-29 04:58:151525
1526TEST(MessageLoopTest, HighResolutionTimer) {
1527 MessageLoop loop;
1528
1529 const int kFastTimerMs = 5;
1530 const int kSlowTimerMs = 100;
1531
[email protected]515f2492011-01-14 10:36:281532 EXPECT_FALSE(loop.high_resolution_timers_enabled());
[email protected]57f030a2010-06-29 04:58:151533
1534 // Post a fast task to enable the high resolution timers.
[email protected]b224f792011-04-20 16:02:231535 loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1),
1536 kFastTimerMs);
[email protected]57f030a2010-06-29 04:58:151537 loop.Run();
[email protected]515f2492011-01-14 10:36:281538 EXPECT_TRUE(loop.high_resolution_timers_enabled());
[email protected]57f030a2010-06-29 04:58:151539
1540 // Post a slow task and verify high resolution timers
1541 // are still enabled.
[email protected]b224f792011-04-20 16:02:231542 loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1),
1543 kSlowTimerMs);
[email protected]57f030a2010-06-29 04:58:151544 loop.Run();
[email protected]515f2492011-01-14 10:36:281545 EXPECT_TRUE(loop.high_resolution_timers_enabled());
[email protected]57f030a2010-06-29 04:58:151546
1547 // Wait for a while so that high-resolution mode elapses.
1548 Sleep(MessageLoop::kHighResolutionTimerModeLeaseTimeMs);
1549
1550 // Post a slow task to disable the high resolution timers.
[email protected]b224f792011-04-20 16:02:231551 loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1),
1552 kSlowTimerMs);
[email protected]57f030a2010-06-29 04:58:151553 loop.Run();
[email protected]515f2492011-01-14 10:36:281554 EXPECT_FALSE(loop.high_resolution_timers_enabled());
[email protected]57f030a2010-06-29 04:58:151555}
1556
[email protected]4d9bdfaf2008-08-26 05:53:571557#endif // defined(OS_WIN)
[email protected]f74c8962009-04-22 20:01:361558
[email protected]5cffdfd2010-12-01 08:45:511559#if defined(OS_POSIX) && !defined(OS_NACL)
[email protected]f74c8962009-04-22 20:01:361560
1561namespace {
1562
[email protected]9cfb89a2010-06-09 21:20:411563class QuitDelegate : public base::MessagePumpLibevent::Watcher {
[email protected]f74c8962009-04-22 20:01:361564 public:
1565 virtual void OnFileCanWriteWithoutBlocking(int fd) {
1566 MessageLoop::current()->Quit();
1567 }
1568 virtual void OnFileCanReadWithoutBlocking(int fd) {
1569 MessageLoop::current()->Quit();
1570 }
1571};
1572
[email protected]f45213772010-06-11 00:06:191573TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) {
[email protected]f74c8962009-04-22 20:01:361574 // Simulate a MessageLoop that dies before an FileDescriptorWatcher.
1575 // This could happen when people use the Singleton pattern or atexit.
[email protected]f74c8962009-04-22 20:01:361576
1577 // Create a file descriptor. Doesn't need to be readable or writable,
1578 // as we don't need to actually get any notifications.
1579 // pipe() is just the easiest way to do it.
1580 int pipefds[2];
1581 int err = pipe(pipefds);
[email protected]b7243c42010-07-23 05:23:131582 ASSERT_EQ(0, err);
[email protected]f74c8962009-04-22 20:01:361583 int fd = pipefds[1];
1584 {
1585 // Arrange for controller to live longer than message loop.
1586 base::MessagePumpLibevent::FileDescriptorWatcher controller;
1587 {
1588 MessageLoopForIO message_loop;
1589
1590 QuitDelegate delegate;
1591 message_loop.WatchFileDescriptor(fd,
1592 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
1593 // and don't run the message loop, just destroy it.
1594 }
1595 }
[email protected]70eb6572010-06-23 00:37:461596 if (HANDLE_EINTR(close(pipefds[0])) < 0)
1597 PLOG(ERROR) << "close";
1598 if (HANDLE_EINTR(close(pipefds[1])) < 0)
1599 PLOG(ERROR) << "close";
[email protected]f74c8962009-04-22 20:01:361600}
1601
1602TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) {
1603 // Verify that it's ok to call StopWatchingFileDescriptor().
1604 // (Errors only showed up in valgrind.)
1605 int pipefds[2];
1606 int err = pipe(pipefds);
[email protected]b7243c42010-07-23 05:23:131607 ASSERT_EQ(0, err);
[email protected]f74c8962009-04-22 20:01:361608 int fd = pipefds[1];
1609 {
1610 // Arrange for message loop to live longer than controller.
1611 MessageLoopForIO message_loop;
1612 {
1613 base::MessagePumpLibevent::FileDescriptorWatcher controller;
1614
1615 QuitDelegate delegate;
1616 message_loop.WatchFileDescriptor(fd,
1617 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
1618 controller.StopWatchingFileDescriptor();
1619 }
1620 }
[email protected]70eb6572010-06-23 00:37:461621 if (HANDLE_EINTR(close(pipefds[0])) < 0)
1622 PLOG(ERROR) << "close";
1623 if (HANDLE_EINTR(close(pipefds[1])) < 0)
1624 PLOG(ERROR) << "close";
[email protected]f74c8962009-04-22 20:01:361625}
1626
[email protected]9cfb89a2010-06-09 21:20:411627} // namespace
1628
[email protected]5cffdfd2010-12-01 08:45:511629#endif // defined(OS_POSIX) && !defined(OS_NACL)
[email protected]582384772010-11-30 00:25:291630
1631namespace {
[email protected]b224f792011-04-20 16:02:231632// Inject a test point for recording the destructor calls for Closure objects
1633// send to MessageLoop::PostTask(). It is awkward usage since we are trying to
1634// hook the actual destruction, which is not a common operation.
1635class DestructionObserverProbe :
1636 public base::RefCounted<DestructionObserverProbe> {
[email protected]582384772010-11-30 00:25:291637 public:
[email protected]b224f792011-04-20 16:02:231638 DestructionObserverProbe(bool* task_destroyed,
1639 bool* destruction_observer_called)
[email protected]582384772010-11-30 00:25:291640 : task_destroyed_(task_destroyed),
1641 destruction_observer_called_(destruction_observer_called) {
1642 }
[email protected]b224f792011-04-20 16:02:231643 virtual ~DestructionObserverProbe() {
[email protected]582384772010-11-30 00:25:291644 EXPECT_FALSE(*destruction_observer_called_);
1645 *task_destroyed_ = true;
1646 }
1647 virtual void Run() {
1648 // This task should never run.
1649 ADD_FAILURE();
1650 }
1651 private:
1652 bool* task_destroyed_;
1653 bool* destruction_observer_called_;
1654};
1655
1656class MLDestructionObserver : public MessageLoop::DestructionObserver {
1657 public:
1658 MLDestructionObserver(bool* task_destroyed, bool* destruction_observer_called)
1659 : task_destroyed_(task_destroyed),
1660 destruction_observer_called_(destruction_observer_called),
1661 task_destroyed_before_message_loop_(false) {
1662 }
1663 virtual void WillDestroyCurrentMessageLoop() {
1664 task_destroyed_before_message_loop_ = *task_destroyed_;
1665 *destruction_observer_called_ = true;
1666 }
1667 bool task_destroyed_before_message_loop() const {
1668 return task_destroyed_before_message_loop_;
1669 }
1670 private:
1671 bool* task_destroyed_;
1672 bool* destruction_observer_called_;
1673 bool task_destroyed_before_message_loop_;
1674};
1675
1676} // namespace
1677
1678TEST(MessageLoopTest, DestructionObserverTest) {
1679 // Verify that the destruction observer gets called at the very end (after
1680 // all the pending tasks have been destroyed).
1681 MessageLoop* loop = new MessageLoop;
1682 const int kDelayMS = 100;
1683
1684 bool task_destroyed = false;
1685 bool destruction_observer_called = false;
1686
1687 MLDestructionObserver observer(&task_destroyed, &destruction_observer_called);
1688 loop->AddDestructionObserver(&observer);
1689 loop->PostDelayedTask(
1690 FROM_HERE,
[email protected]b224f792011-04-20 16:02:231691 base::Bind(&DestructionObserverProbe::Run,
1692 new DestructionObserverProbe(&task_destroyed,
1693 &destruction_observer_called)),
[email protected]582384772010-11-30 00:25:291694 kDelayMS);
1695 delete loop;
1696 EXPECT_TRUE(observer.task_destroyed_before_message_loop());
1697 // The task should have been destroyed when we deleted the loop.
1698 EXPECT_TRUE(task_destroyed);
1699 EXPECT_TRUE(destruction_observer_called);
1700}