license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 5 | #include "base/eintr_wrapper.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | #include "base/logging.h" |
| 7 | #include "base/message_loop.h" |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 8 | #include "base/platform_thread.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include "base/ref_counted.h" |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 10 | #include "base/task.h" |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 11 | #include "base/thread.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 14 | #if defined(OS_WIN) |
| 15 | #include "base/message_pump_win.h" |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 16 | #include "base/scoped_handle.h" |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 17 | #endif |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 18 | #if defined(OS_POSIX) |
| 19 | #include "base/message_pump_libevent.h" |
| 20 | #endif |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 21 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 22 | using base::Thread; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 23 | using base::Time; |
| 24 | using base::TimeDelta; |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 25 | |
| 26 | // TODO(darin): Platform-specific MessageLoop tests should be grouped together |
| 27 | // to avoid chopping this file up with so many #ifdefs. |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 28 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 29 | namespace { |
| 30 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 31 | class MessageLoopTest : public testing::Test {}; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | |
| 33 | class Foo : public base::RefCounted<Foo> { |
| 34 | public: |
| 35 | Foo() : test_count_(0) { |
| 36 | } |
| 37 | |
| 38 | void Test0() { |
| 39 | ++test_count_; |
| 40 | } |
| 41 | |
| 42 | void Test1ConstRef(const std::string& a) { |
| 43 | ++test_count_; |
| 44 | result_.append(a); |
| 45 | } |
| 46 | |
| 47 | void Test1Ptr(std::string* a) { |
| 48 | ++test_count_; |
| 49 | result_.append(*a); |
| 50 | } |
| 51 | |
| 52 | void Test1Int(int a) { |
| 53 | test_count_ += a; |
| 54 | } |
| 55 | |
| 56 | void Test2Ptr(std::string* a, std::string* b) { |
| 57 | ++test_count_; |
| 58 | result_.append(*a); |
| 59 | result_.append(*b); |
| 60 | } |
| 61 | |
| 62 | void Test2Mixed(const std::string& a, std::string* b) { |
| 63 | ++test_count_; |
| 64 | result_.append(a); |
| 65 | result_.append(*b); |
| 66 | } |
| 67 | |
| 68 | int test_count() const { return test_count_; } |
| 69 | const std::string& result() const { return result_; } |
| 70 | |
| 71 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 72 | friend class base::RefCounted<Foo>; |
| 73 | |
| 74 | ~Foo() {} |
| 75 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 76 | int test_count_; |
| 77 | std::string result_; |
| 78 | }; |
| 79 | |
| 80 | class QuitMsgLoop : public base::RefCounted<QuitMsgLoop> { |
| 81 | public: |
| 82 | void QuitNow() { |
| 83 | MessageLoop::current()->Quit(); |
| 84 | } |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | friend class base::RefCounted<QuitMsgLoop>; |
| 88 | |
| 89 | ~QuitMsgLoop() {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 90 | }; |
| 91 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 92 | void RunTest_PostTask(MessageLoop::Type message_loop_type) { |
| 93 | MessageLoop loop(message_loop_type); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 94 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 95 | // Add tests to message loop |
| 96 | scoped_refptr<Foo> foo = new Foo(); |
| 97 | std::string a("a"), b("b"), c("c"), d("d"); |
| 98 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 99 | foo.get(), &Foo::Test0)); |
| 100 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 101 | foo.get(), &Foo::Test1ConstRef, a)); |
| 102 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 103 | foo.get(), &Foo::Test1Ptr, &b)); |
| 104 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 105 | foo.get(), &Foo::Test1Int, 100)); |
| 106 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 107 | foo.get(), &Foo::Test2Ptr, &a, &c)); |
| 108 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 109 | foo.get(), &Foo::Test2Mixed, a, &d)); |
| 110 | |
| 111 | // After all tests, post a message that will shut down the message loop |
| 112 | scoped_refptr<QuitMsgLoop> quit = new QuitMsgLoop(); |
| 113 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 114 | quit.get(), &QuitMsgLoop::QuitNow)); |
| 115 | |
| 116 | // Now kick things off |
| 117 | MessageLoop::current()->Run(); |
| 118 | |
| 119 | EXPECT_EQ(foo->test_count(), 105); |
| 120 | EXPECT_EQ(foo->result(), "abacad"); |
| 121 | } |
| 122 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 123 | void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) { |
| 124 | MessageLoop loop(message_loop_type); |
| 125 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 126 | // Add tests to message loop |
| 127 | scoped_refptr<Foo> foo = new Foo(); |
| 128 | std::string a("a"), b("b"), c("c"), d("d"); |
| 129 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 130 | foo.get(), &Foo::Test0)); |
| 131 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 132 | foo.get(), &Foo::Test1ConstRef, a)); |
| 133 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 134 | foo.get(), &Foo::Test1Ptr, &b)); |
| 135 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 136 | foo.get(), &Foo::Test1Int, 100)); |
| 137 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 138 | foo.get(), &Foo::Test2Ptr, &a, &c)); |
| 139 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 140 | foo.get(), &Foo::Test2Mixed, a, &d)); |
| 141 | |
| 142 | // After all tests, post a message that will shut down the message loop |
| 143 | scoped_refptr<QuitMsgLoop> quit = new QuitMsgLoop(); |
| 144 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 145 | quit.get(), &QuitMsgLoop::QuitNow)); |
| 146 | |
| 147 | // Now kick things off with the SEH block active. |
| 148 | MessageLoop::current()->set_exception_restoration(true); |
| 149 | MessageLoop::current()->Run(); |
| 150 | MessageLoop::current()->set_exception_restoration(false); |
| 151 | |
| 152 | EXPECT_EQ(foo->test_count(), 105); |
| 153 | EXPECT_EQ(foo->result(), "abacad"); |
| 154 | } |
| 155 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 156 | // This class runs slowly to simulate a large amount of work being done. |
| 157 | class SlowTask : public Task { |
| 158 | public: |
| 159 | SlowTask(int pause_ms, int* quit_counter) |
| 160 | : pause_ms_(pause_ms), quit_counter_(quit_counter) { |
| 161 | } |
| 162 | virtual void Run() { |
| 163 | PlatformThread::Sleep(pause_ms_); |
| 164 | if (--(*quit_counter_) == 0) |
| 165 | MessageLoop::current()->Quit(); |
| 166 | } |
| 167 | private: |
| 168 | int pause_ms_; |
| 169 | int* quit_counter_; |
| 170 | }; |
| 171 | |
| 172 | // This class records the time when Run was called in a Time object, which is |
| 173 | // useful for building a variety of MessageLoop tests. |
| 174 | class RecordRunTimeTask : public SlowTask { |
| 175 | public: |
| 176 | RecordRunTimeTask(Time* run_time, int* quit_counter) |
| 177 | : SlowTask(10, quit_counter), run_time_(run_time) { |
| 178 | } |
| 179 | virtual void Run() { |
| 180 | *run_time_ = Time::Now(); |
| 181 | // Cause our Run function to take some time to execute. As a result we can |
| 182 | // count on subsequent RecordRunTimeTask objects running at a future time, |
| 183 | // without worry about the resolution of our system clock being an issue. |
| 184 | SlowTask::Run(); |
| 185 | } |
| 186 | private: |
| 187 | Time* run_time_; |
| 188 | }; |
| 189 | |
| 190 | void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) { |
| 191 | MessageLoop loop(message_loop_type); |
| 192 | |
| 193 | // Test that PostDelayedTask results in a delayed task. |
| 194 | |
| 195 | const int kDelayMS = 100; |
| 196 | |
| 197 | int num_tasks = 1; |
| 198 | Time run_time; |
| 199 | |
| 200 | loop.PostDelayedTask( |
| 201 | FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), kDelayMS); |
| 202 | |
| 203 | Time time_before_run = Time::Now(); |
| 204 | loop.Run(); |
| 205 | Time time_after_run = Time::Now(); |
| 206 | |
| 207 | EXPECT_EQ(0, num_tasks); |
| 208 | EXPECT_LT(kDelayMS, (time_after_run - time_before_run).InMilliseconds()); |
| 209 | } |
| 210 | |
| 211 | void RunTest_PostDelayedTask_InDelayOrder(MessageLoop::Type message_loop_type) { |
| 212 | MessageLoop loop(message_loop_type); |
| 213 | |
| 214 | // Test that two tasks with different delays run in the right order. |
| 215 | |
| 216 | int num_tasks = 2; |
| 217 | Time run_time1, run_time2; |
| 218 | |
| 219 | loop.PostDelayedTask( |
| 220 | FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks), 200); |
| 221 | // If we get a large pause in execution (due to a context switch) here, this |
| 222 | // test could fail. |
| 223 | loop.PostDelayedTask( |
| 224 | FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 10); |
| 225 | |
| 226 | loop.Run(); |
| 227 | EXPECT_EQ(0, num_tasks); |
| 228 | |
| 229 | EXPECT_TRUE(run_time2 < run_time1); |
| 230 | } |
| 231 | |
| 232 | void RunTest_PostDelayedTask_InPostOrder(MessageLoop::Type message_loop_type) { |
| 233 | MessageLoop loop(message_loop_type); |
| 234 | |
| 235 | // Test that two tasks with the same delay run in the order in which they |
| 236 | // were posted. |
| 237 | // |
| 238 | // NOTE: This is actually an approximate test since the API only takes a |
| 239 | // "delay" parameter, so we are not exactly simulating two tasks that get |
| 240 | // posted at the exact same time. It would be nice if the API allowed us to |
| 241 | // specify the desired run time. |
| 242 | |
| 243 | const int kDelayMS = 100; |
| 244 | |
| 245 | int num_tasks = 2; |
| 246 | Time run_time1, run_time2; |
| 247 | |
| 248 | loop.PostDelayedTask( |
| 249 | FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks), kDelayMS); |
| 250 | loop.PostDelayedTask( |
| 251 | FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), kDelayMS); |
| 252 | |
| 253 | loop.Run(); |
| 254 | EXPECT_EQ(0, num_tasks); |
| 255 | |
| 256 | EXPECT_TRUE(run_time1 < run_time2); |
| 257 | } |
| 258 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 259 | void RunTest_PostDelayedTask_InPostOrder_2( |
| 260 | MessageLoop::Type message_loop_type) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 261 | MessageLoop loop(message_loop_type); |
| 262 | |
| 263 | // Test that a delayed task still runs after a normal tasks even if the |
| 264 | // normal tasks take a long time to run. |
| 265 | |
| 266 | const int kPauseMS = 50; |
| 267 | |
| 268 | int num_tasks = 2; |
| 269 | Time run_time; |
| 270 | |
| 271 | loop.PostTask( |
| 272 | FROM_HERE, new SlowTask(kPauseMS, &num_tasks)); |
| 273 | loop.PostDelayedTask( |
| 274 | FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), 10); |
| 275 | |
| 276 | Time time_before_run = Time::Now(); |
| 277 | loop.Run(); |
| 278 | Time time_after_run = Time::Now(); |
| 279 | |
| 280 | EXPECT_EQ(0, num_tasks); |
| 281 | |
| 282 | EXPECT_LT(kPauseMS, (time_after_run - time_before_run).InMilliseconds()); |
| 283 | } |
| 284 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 285 | void RunTest_PostDelayedTask_InPostOrder_3( |
| 286 | MessageLoop::Type message_loop_type) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 287 | MessageLoop loop(message_loop_type); |
| 288 | |
| 289 | // Test that a delayed task still runs after a pile of normal tasks. The key |
| 290 | // difference between this test and the previous one is that here we return |
| 291 | // the MessageLoop a lot so we give the MessageLoop plenty of opportunities |
| 292 | // to maybe run the delayed task. It should know not to do so until the |
| 293 | // delayed task's delay has passed. |
| 294 | |
| 295 | int num_tasks = 11; |
| 296 | Time run_time1, run_time2; |
| 297 | |
| 298 | // Clutter the ML with tasks. |
| 299 | for (int i = 1; i < num_tasks; ++i) |
| 300 | loop.PostTask(FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks)); |
| 301 | |
| 302 | loop.PostDelayedTask( |
| 303 | FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 1); |
| 304 | |
| 305 | loop.Run(); |
| 306 | EXPECT_EQ(0, num_tasks); |
| 307 | |
| 308 | EXPECT_TRUE(run_time2 > run_time1); |
| 309 | } |
| 310 | |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 311 | void RunTest_PostDelayedTask_SharedTimer(MessageLoop::Type message_loop_type) { |
| 312 | MessageLoop loop(message_loop_type); |
| 313 | |
| 314 | // Test that the interval of the timer, used to run the next delayed task, is |
| 315 | // set to a value corresponding to when the next delayed task should run. |
| 316 | |
| 317 | // By setting num_tasks to 1, we ensure that the first task to run causes the |
| 318 | // run loop to exit. |
| 319 | int num_tasks = 1; |
| 320 | Time run_time1, run_time2; |
| 321 | |
| 322 | loop.PostDelayedTask( |
| 323 | FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks), 1000000); |
| 324 | loop.PostDelayedTask( |
| 325 | FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 10); |
| 326 | |
| 327 | Time start_time = Time::Now(); |
| 328 | |
| 329 | loop.Run(); |
| 330 | EXPECT_EQ(0, num_tasks); |
| 331 | |
| 332 | // Ensure that we ran in far less time than the slower timer. |
[email protected] | 4615f198 | 2008-09-23 19:22:33 | [diff] [blame] | 333 | TimeDelta total_time = Time::Now() - start_time; |
| 334 | EXPECT_GT(5000, total_time.InMilliseconds()); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 335 | |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 336 | // In case both timers somehow run at nearly the same time, sleep a little |
| 337 | // and then run all pending to force them both to have run. This is just |
| 338 | // encouraging flakiness if there is any. |
| 339 | PlatformThread::Sleep(100); |
| 340 | loop.RunAllPending(); |
| 341 | |
| 342 | EXPECT_TRUE(run_time1.is_null()); |
| 343 | EXPECT_FALSE(run_time2.is_null()); |
| 344 | } |
| 345 | |
| 346 | #if defined(OS_WIN) |
| 347 | |
| 348 | class SubPumpTask : public Task { |
| 349 | public: |
| 350 | virtual void Run() { |
| 351 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 352 | MSG msg; |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 353 | while (GetMessage(&msg, NULL, 0, 0)) { |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 354 | TranslateMessage(&msg); |
| 355 | DispatchMessage(&msg); |
| 356 | } |
| 357 | MessageLoop::current()->Quit(); |
| 358 | } |
| 359 | }; |
| 360 | |
| 361 | class SubPumpQuitTask : public Task { |
| 362 | public: |
| 363 | SubPumpQuitTask() { |
| 364 | } |
| 365 | virtual void Run() { |
| 366 | PostQuitMessage(0); |
| 367 | } |
| 368 | }; |
| 369 | |
| 370 | void RunTest_PostDelayedTask_SharedTimer_SubPump() { |
| 371 | MessageLoop loop(MessageLoop::TYPE_UI); |
| 372 | |
| 373 | // Test that the interval of the timer, used to run the next delayed task, is |
| 374 | // set to a value corresponding to when the next delayed task should run. |
| 375 | |
| 376 | // By setting num_tasks to 1, we ensure that the first task to run causes the |
| 377 | // run loop to exit. |
| 378 | int num_tasks = 1; |
| 379 | Time run_time; |
| 380 | |
| 381 | loop.PostTask(FROM_HERE, new SubPumpTask()); |
| 382 | |
| 383 | // This very delayed task should never run. |
| 384 | loop.PostDelayedTask( |
| 385 | FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), 1000000); |
| 386 | |
| 387 | // This slightly delayed task should run from within SubPumpTask::Run(). |
| 388 | loop.PostDelayedTask( |
| 389 | FROM_HERE, new SubPumpQuitTask(), 10); |
| 390 | |
| 391 | Time start_time = Time::Now(); |
| 392 | |
| 393 | loop.Run(); |
| 394 | EXPECT_EQ(1, num_tasks); |
| 395 | |
| 396 | // Ensure that we ran in far less time than the slower timer. |
[email protected] | 4615f198 | 2008-09-23 19:22:33 | [diff] [blame] | 397 | TimeDelta total_time = Time::Now() - start_time; |
| 398 | EXPECT_GT(5000, total_time.InMilliseconds()); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 399 | |
| 400 | // In case both timers somehow run at nearly the same time, sleep a little |
| 401 | // and then run all pending to force them both to have run. This is just |
| 402 | // encouraging flakiness if there is any. |
| 403 | PlatformThread::Sleep(100); |
| 404 | loop.RunAllPending(); |
| 405 | |
| 406 | EXPECT_TRUE(run_time.is_null()); |
| 407 | } |
| 408 | |
| 409 | #endif // defined(OS_WIN) |
| 410 | |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 411 | class RecordDeletionTask : public Task { |
| 412 | public: |
| 413 | RecordDeletionTask(Task* post_on_delete, bool* was_deleted) |
| 414 | : post_on_delete_(post_on_delete), was_deleted_(was_deleted) { |
| 415 | } |
| 416 | ~RecordDeletionTask() { |
| 417 | *was_deleted_ = true; |
| 418 | if (post_on_delete_) |
| 419 | MessageLoop::current()->PostTask(FROM_HERE, post_on_delete_); |
| 420 | } |
| 421 | virtual void Run() {} |
| 422 | private: |
| 423 | Task* post_on_delete_; |
| 424 | bool* was_deleted_; |
| 425 | }; |
| 426 | |
| 427 | void RunTest_EnsureTaskDeletion(MessageLoop::Type message_loop_type) { |
| 428 | bool a_was_deleted = false; |
| 429 | bool b_was_deleted = false; |
| 430 | { |
| 431 | MessageLoop loop(message_loop_type); |
| 432 | loop.PostTask( |
| 433 | FROM_HERE, new RecordDeletionTask(NULL, &a_was_deleted)); |
| 434 | loop.PostDelayedTask( |
| 435 | FROM_HERE, new RecordDeletionTask(NULL, &b_was_deleted), 1000); |
| 436 | } |
| 437 | EXPECT_TRUE(a_was_deleted); |
| 438 | EXPECT_TRUE(b_was_deleted); |
| 439 | } |
| 440 | |
| 441 | void RunTest_EnsureTaskDeletion_Chain(MessageLoop::Type message_loop_type) { |
| 442 | bool a_was_deleted = false; |
| 443 | bool b_was_deleted = false; |
| 444 | bool c_was_deleted = false; |
| 445 | { |
| 446 | MessageLoop loop(message_loop_type); |
| 447 | RecordDeletionTask* a = new RecordDeletionTask(NULL, &a_was_deleted); |
| 448 | RecordDeletionTask* b = new RecordDeletionTask(a, &b_was_deleted); |
| 449 | RecordDeletionTask* c = new RecordDeletionTask(b, &c_was_deleted); |
| 450 | loop.PostTask(FROM_HERE, c); |
| 451 | } |
| 452 | EXPECT_TRUE(a_was_deleted); |
| 453 | EXPECT_TRUE(b_was_deleted); |
| 454 | EXPECT_TRUE(c_was_deleted); |
| 455 | } |
| 456 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 457 | class NestingTest : public Task { |
| 458 | public: |
| 459 | explicit NestingTest(int* depth) : depth_(depth) { |
| 460 | } |
| 461 | void Run() { |
| 462 | if (*depth_ > 0) { |
| 463 | *depth_ -= 1; |
| 464 | MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(depth_)); |
| 465 | |
| 466 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 467 | MessageLoop::current()->Run(); |
| 468 | } |
| 469 | MessageLoop::current()->Quit(); |
| 470 | } |
| 471 | private: |
| 472 | int* depth_; |
| 473 | }; |
| 474 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 475 | #if defined(OS_WIN) |
| 476 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 477 | LONG WINAPI BadExceptionHandler(EXCEPTION_POINTERS *ex_info) { |
| 478 | ADD_FAILURE() << "bad exception handler"; |
| 479 | ::ExitProcess(ex_info->ExceptionRecord->ExceptionCode); |
| 480 | return EXCEPTION_EXECUTE_HANDLER; |
| 481 | } |
| 482 | |
| 483 | // This task throws an SEH exception: initially write to an invalid address. |
| 484 | // If the right SEH filter is installed, it will fix the error. |
| 485 | class CrasherTask : public Task { |
| 486 | public: |
| 487 | // Ctor. If trash_SEH_handler is true, the task will override the unhandled |
| 488 | // exception handler with one sure to crash this test. |
| 489 | explicit CrasherTask(bool trash_SEH_handler) |
| 490 | : trash_SEH_handler_(trash_SEH_handler) { |
| 491 | } |
| 492 | void Run() { |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 493 | PlatformThread::Sleep(1); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 494 | if (trash_SEH_handler_) |
| 495 | ::SetUnhandledExceptionFilter(&BadExceptionHandler); |
| 496 | // Generate a SEH fault. We do it in asm to make sure we know how to undo |
| 497 | // the damage. |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 498 | |
| 499 | #if defined(_M_IX86) |
| 500 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 501 | __asm { |
| 502 | mov eax, dword ptr [CrasherTask::bad_array_] |
| 503 | mov byte ptr [eax], 66 |
| 504 | } |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 505 | |
| 506 | #elif defined(_M_X64) |
| 507 | |
| 508 | bad_array_[0] = 66; |
| 509 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 510 | #else |
| 511 | #error "needs architecture support" |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 512 | #endif |
| 513 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 514 | MessageLoop::current()->Quit(); |
| 515 | } |
| 516 | // Points the bad array to a valid memory location. |
| 517 | static void FixError() { |
| 518 | bad_array_ = &valid_store_; |
| 519 | } |
| 520 | |
| 521 | private: |
| 522 | bool trash_SEH_handler_; |
| 523 | static volatile char* bad_array_; |
| 524 | static char valid_store_; |
| 525 | }; |
| 526 | |
| 527 | volatile char* CrasherTask::bad_array_ = 0; |
| 528 | char CrasherTask::valid_store_ = 0; |
| 529 | |
| 530 | // This SEH filter fixes the problem and retries execution. Fixing requires |
| 531 | // that the last instruction: mov eax, [CrasherTask::bad_array_] to be retried |
| 532 | // so we move the instruction pointer 5 bytes back. |
| 533 | LONG WINAPI HandleCrasherTaskException(EXCEPTION_POINTERS *ex_info) { |
| 534 | if (ex_info->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION) |
| 535 | return EXCEPTION_EXECUTE_HANDLER; |
| 536 | |
| 537 | CrasherTask::FixError(); |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 538 | |
| 539 | #if defined(_M_IX86) |
| 540 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 541 | ex_info->ContextRecord->Eip -= 5; |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 542 | |
| 543 | #elif defined(_M_X64) |
| 544 | |
| 545 | ex_info->ContextRecord->Rip -= 5; |
| 546 | |
| 547 | #endif |
| 548 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 549 | return EXCEPTION_CONTINUE_EXECUTION; |
| 550 | } |
| 551 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 552 | void RunTest_Crasher(MessageLoop::Type message_loop_type) { |
| 553 | MessageLoop loop(message_loop_type); |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 554 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 555 | if (::IsDebuggerPresent()) |
| 556 | return; |
| 557 | |
| 558 | LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter = |
| 559 | ::SetUnhandledExceptionFilter(&HandleCrasherTaskException); |
| 560 | |
| 561 | MessageLoop::current()->PostTask(FROM_HERE, new CrasherTask(false)); |
| 562 | MessageLoop::current()->set_exception_restoration(true); |
| 563 | MessageLoop::current()->Run(); |
| 564 | MessageLoop::current()->set_exception_restoration(false); |
| 565 | |
| 566 | ::SetUnhandledExceptionFilter(old_SEH_filter); |
| 567 | } |
| 568 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 569 | void RunTest_CrasherNasty(MessageLoop::Type message_loop_type) { |
| 570 | MessageLoop loop(message_loop_type); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 571 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 572 | if (::IsDebuggerPresent()) |
| 573 | return; |
| 574 | |
| 575 | LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter = |
| 576 | ::SetUnhandledExceptionFilter(&HandleCrasherTaskException); |
| 577 | |
| 578 | MessageLoop::current()->PostTask(FROM_HERE, new CrasherTask(true)); |
| 579 | MessageLoop::current()->set_exception_restoration(true); |
| 580 | MessageLoop::current()->Run(); |
| 581 | MessageLoop::current()->set_exception_restoration(false); |
| 582 | |
| 583 | ::SetUnhandledExceptionFilter(old_SEH_filter); |
| 584 | } |
| 585 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 586 | #endif // defined(OS_WIN) |
| 587 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 588 | void RunTest_Nesting(MessageLoop::Type message_loop_type) { |
| 589 | MessageLoop loop(message_loop_type); |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 590 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 591 | int depth = 100; |
| 592 | MessageLoop::current()->PostTask(FROM_HERE, new NestingTest(&depth)); |
| 593 | MessageLoop::current()->Run(); |
| 594 | EXPECT_EQ(depth, 0); |
| 595 | } |
| 596 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 597 | const wchar_t* const kMessageBoxTitle = L"MessageLoop Unit Test"; |
| 598 | |
| 599 | enum TaskType { |
| 600 | MESSAGEBOX, |
| 601 | ENDDIALOG, |
| 602 | RECURSIVE, |
| 603 | TIMEDMESSAGELOOP, |
| 604 | QUITMESSAGELOOP, |
| 605 | ORDERERD, |
| 606 | PUMPS, |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 607 | SLEEP, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 608 | }; |
| 609 | |
| 610 | // Saves the order in which the tasks executed. |
| 611 | struct TaskItem { |
| 612 | TaskItem(TaskType t, int c, bool s) |
| 613 | : type(t), |
| 614 | cookie(c), |
| 615 | start(s) { |
| 616 | } |
| 617 | |
| 618 | TaskType type; |
| 619 | int cookie; |
| 620 | bool start; |
| 621 | |
| 622 | bool operator == (const TaskItem& other) const { |
| 623 | return type == other.type && cookie == other.cookie && start == other.start; |
| 624 | } |
| 625 | }; |
| 626 | |
| 627 | typedef std::vector<TaskItem> TaskList; |
| 628 | |
| 629 | std::ostream& operator <<(std::ostream& os, TaskType type) { |
| 630 | switch (type) { |
| 631 | case MESSAGEBOX: os << "MESSAGEBOX"; break; |
| 632 | case ENDDIALOG: os << "ENDDIALOG"; break; |
| 633 | case RECURSIVE: os << "RECURSIVE"; break; |
| 634 | case TIMEDMESSAGELOOP: os << "TIMEDMESSAGELOOP"; break; |
| 635 | case QUITMESSAGELOOP: os << "QUITMESSAGELOOP"; break; |
| 636 | case ORDERERD: os << "ORDERERD"; break; |
| 637 | case PUMPS: os << "PUMPS"; break; |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 638 | case SLEEP: os << "SLEEP"; break; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 639 | default: |
| 640 | NOTREACHED(); |
| 641 | os << "Unknown TaskType"; |
| 642 | break; |
| 643 | } |
| 644 | return os; |
| 645 | } |
| 646 | |
| 647 | std::ostream& operator <<(std::ostream& os, const TaskItem& item) { |
| 648 | if (item.start) |
| 649 | return os << item.type << " " << item.cookie << " starts"; |
| 650 | else |
| 651 | return os << item.type << " " << item.cookie << " ends"; |
| 652 | } |
| 653 | |
| 654 | // Saves the order the tasks ran. |
| 655 | class OrderedTasks : public Task { |
| 656 | public: |
| 657 | OrderedTasks(TaskList* order, int cookie) |
| 658 | : order_(order), |
| 659 | type_(ORDERERD), |
| 660 | cookie_(cookie) { |
| 661 | } |
| 662 | OrderedTasks(TaskList* order, TaskType type, int cookie) |
| 663 | : order_(order), |
| 664 | type_(type), |
| 665 | cookie_(cookie) { |
| 666 | } |
| 667 | |
| 668 | void RunStart() { |
| 669 | TaskItem item(type_, cookie_, true); |
| 670 | DLOG(INFO) << item; |
| 671 | order_->push_back(item); |
| 672 | } |
| 673 | void RunEnd() { |
| 674 | TaskItem item(type_, cookie_, false); |
| 675 | DLOG(INFO) << item; |
| 676 | order_->push_back(item); |
| 677 | } |
| 678 | |
| 679 | virtual void Run() { |
| 680 | RunStart(); |
| 681 | RunEnd(); |
| 682 | } |
| 683 | |
| 684 | protected: |
| 685 | TaskList* order() const { |
| 686 | return order_; |
| 687 | } |
| 688 | |
| 689 | int cookie() const { |
| 690 | return cookie_; |
| 691 | } |
| 692 | |
| 693 | private: |
| 694 | TaskList* order_; |
| 695 | TaskType type_; |
| 696 | int cookie_; |
| 697 | }; |
| 698 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 699 | #if defined(OS_WIN) |
| 700 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 701 | // 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. |
| 704 | class MessageBoxTask : public OrderedTasks { |
| 705 | public: |
| 706 | MessageBoxTask(TaskList* order, int cookie, bool is_reentrant) |
| 707 | : OrderedTasks(order, MESSAGEBOX, cookie), |
| 708 | is_reentrant_(is_reentrant) { |
| 709 | } |
| 710 | |
| 711 | virtual void Run() { |
| 712 | RunStart(); |
| 713 | if (is_reentrant_) |
| 714 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 715 | MessageBox(NULL, L"Please wait...", kMessageBoxTitle, MB_OK); |
| 716 | RunEnd(); |
| 717 | } |
| 718 | |
| 719 | private: |
| 720 | bool is_reentrant_; |
| 721 | }; |
| 722 | |
| 723 | // Will end the MessageBox. |
| 724 | class EndDialogTask : public OrderedTasks { |
| 725 | public: |
| 726 | EndDialogTask(TaskList* order, int cookie) |
| 727 | : OrderedTasks(order, ENDDIALOG, cookie) { |
| 728 | } |
| 729 | |
| 730 | virtual void Run() { |
| 731 | RunStart(); |
| 732 | HWND window = GetActiveWindow(); |
| 733 | if (window != NULL) { |
| 734 | EXPECT_NE(EndDialog(window, IDCONTINUE), 0); |
| 735 | // Cheap way to signal that the window wasn't found if RunEnd() isn't |
| 736 | // called. |
| 737 | RunEnd(); |
| 738 | } |
| 739 | } |
| 740 | }; |
| 741 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 742 | #endif // defined(OS_WIN) |
| 743 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 744 | class RecursiveTask : public OrderedTasks { |
| 745 | public: |
| 746 | RecursiveTask(int depth, TaskList* order, int cookie, bool is_reentrant) |
| 747 | : OrderedTasks(order, RECURSIVE, cookie), |
| 748 | depth_(depth), |
| 749 | is_reentrant_(is_reentrant) { |
| 750 | } |
| 751 | |
| 752 | virtual void Run() { |
| 753 | RunStart(); |
| 754 | if (depth_ > 0) { |
| 755 | if (is_reentrant_) |
| 756 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 757 | MessageLoop::current()->PostTask(FROM_HERE, |
| 758 | new RecursiveTask(depth_ - 1, order(), cookie(), is_reentrant_)); |
| 759 | } |
| 760 | RunEnd(); |
| 761 | } |
| 762 | |
| 763 | private: |
| 764 | int depth_; |
| 765 | bool is_reentrant_; |
| 766 | }; |
| 767 | |
| 768 | class QuitTask : public OrderedTasks { |
| 769 | public: |
| 770 | QuitTask(TaskList* order, int cookie) |
| 771 | : OrderedTasks(order, QUITMESSAGELOOP, cookie) { |
| 772 | } |
| 773 | |
| 774 | virtual void Run() { |
| 775 | RunStart(); |
| 776 | MessageLoop::current()->Quit(); |
| 777 | RunEnd(); |
| 778 | } |
| 779 | }; |
| 780 | |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 781 | class SleepTask : public OrderedTasks { |
| 782 | public: |
| 783 | SleepTask(TaskList* order, int cookie, int ms) |
| 784 | : OrderedTasks(order, SLEEP, cookie), ms_(ms) { |
| 785 | } |
| 786 | |
| 787 | virtual void Run() { |
| 788 | RunStart(); |
| 789 | PlatformThread::Sleep(ms_); |
| 790 | RunEnd(); |
| 791 | } |
| 792 | |
| 793 | private: |
| 794 | int ms_; |
| 795 | }; |
| 796 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 797 | #if defined(OS_WIN) |
| 798 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 799 | class Recursive2Tasks : public Task { |
| 800 | public: |
| 801 | Recursive2Tasks(MessageLoop* target, |
| 802 | HANDLE event, |
| 803 | bool expect_window, |
| 804 | TaskList* order, |
| 805 | bool is_reentrant) |
| 806 | : target_(target), |
| 807 | event_(event), |
| 808 | expect_window_(expect_window), |
| 809 | order_(order), |
| 810 | is_reentrant_(is_reentrant) { |
| 811 | } |
| 812 | |
| 813 | virtual void Run() { |
| 814 | target_->PostTask(FROM_HERE, |
| 815 | new RecursiveTask(2, order_, 1, is_reentrant_)); |
| 816 | target_->PostTask(FROM_HERE, |
| 817 | new MessageBoxTask(order_, 2, is_reentrant_)); |
| 818 | target_->PostTask(FROM_HERE, |
| 819 | new RecursiveTask(2, order_, 3, is_reentrant_)); |
| 820 | // The trick here is that for recursive task processing, this task will be |
| 821 | // ran _inside_ the MessageBox message loop, dismissing the MessageBox |
| 822 | // without a chance. |
| 823 | // For non-recursive task processing, this will be executed _after_ the |
| 824 | // MessageBox will have been dismissed by the code below, where |
| 825 | // expect_window_ is true. |
| 826 | target_->PostTask(FROM_HERE, new EndDialogTask(order_, 4)); |
| 827 | target_->PostTask(FROM_HERE, new QuitTask(order_, 5)); |
| 828 | |
| 829 | // Enforce that every tasks are sent before starting to run the main thread |
| 830 | // message loop. |
| 831 | ASSERT_TRUE(SetEvent(event_)); |
| 832 | |
| 833 | // Poll for the MessageBox. Don't do this at home! At the speed we do it, |
| 834 | // you will never realize one MessageBox was shown. |
| 835 | for (; expect_window_;) { |
| 836 | HWND window = FindWindow(L"#32770", kMessageBoxTitle); |
| 837 | if (window) { |
| 838 | // Dismiss it. |
| 839 | for (;;) { |
| 840 | HWND button = FindWindowEx(window, NULL, L"Button", NULL); |
| 841 | if (button != NULL) { |
| 842 | EXPECT_TRUE(0 == SendMessage(button, WM_LBUTTONDOWN, 0, 0)); |
| 843 | EXPECT_TRUE(0 == SendMessage(button, WM_LBUTTONUP, 0, 0)); |
| 844 | break; |
| 845 | } |
| 846 | } |
| 847 | break; |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | private: |
| 853 | MessageLoop* target_; |
| 854 | HANDLE event_; |
| 855 | TaskList* order_; |
| 856 | bool expect_window_; |
| 857 | bool is_reentrant_; |
| 858 | }; |
| 859 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 860 | #endif // defined(OS_WIN) |
| 861 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 862 | void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) { |
| 863 | MessageLoop loop(message_loop_type); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 864 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 865 | EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); |
| 866 | TaskList order; |
| 867 | MessageLoop::current()->PostTask(FROM_HERE, |
| 868 | new RecursiveTask(2, &order, 1, false)); |
| 869 | MessageLoop::current()->PostTask(FROM_HERE, |
| 870 | new RecursiveTask(2, &order, 2, false)); |
| 871 | MessageLoop::current()->PostTask(FROM_HERE, new QuitTask(&order, 3)); |
| 872 | |
| 873 | MessageLoop::current()->Run(); |
| 874 | |
| 875 | // FIFO order. |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 876 | ASSERT_EQ(14U, order.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 877 | EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true)); |
| 878 | EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false)); |
| 879 | EXPECT_EQ(order[ 2], TaskItem(RECURSIVE, 2, true)); |
| 880 | EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 2, false)); |
| 881 | EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true)); |
| 882 | EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false)); |
| 883 | EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true)); |
| 884 | EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false)); |
| 885 | EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true)); |
| 886 | EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false)); |
| 887 | EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true)); |
| 888 | EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false)); |
| 889 | EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true)); |
| 890 | EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false)); |
| 891 | } |
| 892 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 893 | void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type) { |
| 894 | MessageLoop loop(message_loop_type); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 895 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 896 | TaskList order; |
| 897 | MessageLoop::current()->PostTask(FROM_HERE, |
| 898 | new RecursiveTask(2, &order, 1, true)); |
| 899 | MessageLoop::current()->PostTask(FROM_HERE, |
| 900 | new RecursiveTask(2, &order, 2, true)); |
| 901 | MessageLoop::current()->PostTask(FROM_HERE, |
| 902 | new QuitTask(&order, 3)); |
| 903 | |
| 904 | MessageLoop::current()->Run(); |
| 905 | |
| 906 | // FIFO order. |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 907 | ASSERT_EQ(14U, order.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 908 | EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true)); |
| 909 | EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false)); |
| 910 | EXPECT_EQ(order[ 2], TaskItem(RECURSIVE, 2, true)); |
| 911 | EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 2, false)); |
| 912 | EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true)); |
| 913 | EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false)); |
| 914 | EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true)); |
| 915 | EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false)); |
| 916 | EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true)); |
| 917 | EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false)); |
| 918 | EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true)); |
| 919 | EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false)); |
| 920 | EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true)); |
| 921 | EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false)); |
| 922 | } |
| 923 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 924 | #if defined(OS_WIN) |
| 925 | // TODO(darin): These tests need to be ported since they test critical |
| 926 | // message loop functionality. |
| 927 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 928 | // A side effect of this test is the generation a beep. Sorry. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 929 | void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type) { |
| 930 | MessageLoop loop(message_loop_type); |
| 931 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 932 | Thread worker("RecursiveDenial2_worker"); |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 933 | Thread::Options options; |
| 934 | options.message_loop_type = message_loop_type; |
| 935 | ASSERT_EQ(true, worker.StartWithOptions(options)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 936 | TaskList order; |
| 937 | ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL)); |
| 938 | worker.message_loop()->PostTask(FROM_HERE, |
| 939 | new Recursive2Tasks(MessageLoop::current(), |
| 940 | event, |
| 941 | true, |
| 942 | &order, |
| 943 | false)); |
| 944 | // Let the other thread execute. |
| 945 | WaitForSingleObject(event, INFINITE); |
| 946 | MessageLoop::current()->Run(); |
| 947 | |
| 948 | ASSERT_EQ(order.size(), 17); |
| 949 | EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true)); |
| 950 | EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false)); |
| 951 | EXPECT_EQ(order[ 2], TaskItem(MESSAGEBOX, 2, true)); |
| 952 | EXPECT_EQ(order[ 3], TaskItem(MESSAGEBOX, 2, false)); |
| 953 | EXPECT_EQ(order[ 4], TaskItem(RECURSIVE, 3, true)); |
| 954 | EXPECT_EQ(order[ 5], TaskItem(RECURSIVE, 3, false)); |
| 955 | // When EndDialogTask is processed, the window is already dismissed, hence no |
| 956 | // "end" entry. |
| 957 | EXPECT_EQ(order[ 6], TaskItem(ENDDIALOG, 4, true)); |
| 958 | EXPECT_EQ(order[ 7], TaskItem(QUITMESSAGELOOP, 5, true)); |
| 959 | EXPECT_EQ(order[ 8], TaskItem(QUITMESSAGELOOP, 5, false)); |
| 960 | EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 1, true)); |
| 961 | EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, false)); |
| 962 | EXPECT_EQ(order[11], TaskItem(RECURSIVE, 3, true)); |
| 963 | EXPECT_EQ(order[12], TaskItem(RECURSIVE, 3, false)); |
| 964 | EXPECT_EQ(order[13], TaskItem(RECURSIVE, 1, true)); |
| 965 | EXPECT_EQ(order[14], TaskItem(RECURSIVE, 1, false)); |
| 966 | EXPECT_EQ(order[15], TaskItem(RECURSIVE, 3, true)); |
| 967 | EXPECT_EQ(order[16], TaskItem(RECURSIVE, 3, false)); |
| 968 | } |
| 969 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 970 | // A side effect of this test is the generation a beep. Sorry. This test also |
| 971 | // needs to process windows messages on the current thread. |
| 972 | void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type) { |
| 973 | MessageLoop loop(message_loop_type); |
| 974 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 975 | Thread worker("RecursiveSupport2_worker"); |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 976 | Thread::Options options; |
| 977 | options.message_loop_type = message_loop_type; |
| 978 | ASSERT_EQ(true, worker.StartWithOptions(options)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 979 | TaskList order; |
| 980 | ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL)); |
| 981 | worker.message_loop()->PostTask(FROM_HERE, |
| 982 | new Recursive2Tasks(MessageLoop::current(), |
| 983 | event, |
| 984 | false, |
| 985 | &order, |
| 986 | true)); |
| 987 | // Let the other thread execute. |
| 988 | WaitForSingleObject(event, INFINITE); |
| 989 | MessageLoop::current()->Run(); |
| 990 | |
| 991 | ASSERT_EQ(order.size(), 18); |
| 992 | EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true)); |
| 993 | EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false)); |
| 994 | EXPECT_EQ(order[ 2], TaskItem(MESSAGEBOX, 2, true)); |
| 995 | // Note that this executes in the MessageBox modal loop. |
| 996 | EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 3, true)); |
| 997 | EXPECT_EQ(order[ 4], TaskItem(RECURSIVE, 3, false)); |
| 998 | EXPECT_EQ(order[ 5], TaskItem(ENDDIALOG, 4, true)); |
| 999 | EXPECT_EQ(order[ 6], TaskItem(ENDDIALOG, 4, false)); |
| 1000 | EXPECT_EQ(order[ 7], TaskItem(MESSAGEBOX, 2, false)); |
| 1001 | /* The order can subtly change here. The reason is that when RecursiveTask(1) |
| 1002 | is called in the main thread, if it is faster than getting to the |
| 1003 | PostTask(FROM_HERE, QuitTask) execution, the order of task execution can |
| 1004 | change. We don't care anyway that the order isn't correct. |
| 1005 | EXPECT_EQ(order[ 8], TaskItem(QUITMESSAGELOOP, 5, true)); |
| 1006 | EXPECT_EQ(order[ 9], TaskItem(QUITMESSAGELOOP, 5, false)); |
| 1007 | EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true)); |
| 1008 | EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false)); |
| 1009 | */ |
| 1010 | EXPECT_EQ(order[12], TaskItem(RECURSIVE, 3, true)); |
| 1011 | EXPECT_EQ(order[13], TaskItem(RECURSIVE, 3, false)); |
| 1012 | EXPECT_EQ(order[14], TaskItem(RECURSIVE, 1, true)); |
| 1013 | EXPECT_EQ(order[15], TaskItem(RECURSIVE, 1, false)); |
| 1014 | EXPECT_EQ(order[16], TaskItem(RECURSIVE, 3, true)); |
| 1015 | EXPECT_EQ(order[17], TaskItem(RECURSIVE, 3, false)); |
| 1016 | } |
| 1017 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 1018 | #endif // defined(OS_WIN) |
| 1019 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1020 | class TaskThatPumps : public OrderedTasks { |
| 1021 | public: |
| 1022 | TaskThatPumps(TaskList* order, int cookie) |
| 1023 | : OrderedTasks(order, PUMPS, cookie) { |
| 1024 | } |
| 1025 | |
| 1026 | virtual void Run() { |
| 1027 | RunStart(); |
| 1028 | bool old_state = MessageLoop::current()->NestableTasksAllowed(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1029 | MessageLoop::current()->SetNestableTasksAllowed(true); |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 1030 | MessageLoop::current()->RunAllPending(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1031 | MessageLoop::current()->SetNestableTasksAllowed(old_state); |
| 1032 | RunEnd(); |
| 1033 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1034 | }; |
| 1035 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1036 | // Tests that non nestable tasks run in FIFO if there are no nested loops. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1037 | void RunTest_NonNestableWithNoNesting(MessageLoop::Type message_loop_type) { |
| 1038 | MessageLoop loop(message_loop_type); |
| 1039 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1040 | TaskList order; |
| 1041 | |
| 1042 | Task* task = new OrderedTasks(&order, 1); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 1043 | MessageLoop::current()->PostNonNestableTask(FROM_HERE, task); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1044 | MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 2)); |
| 1045 | MessageLoop::current()->PostTask(FROM_HERE, new QuitTask(&order, 3)); |
| 1046 | MessageLoop::current()->Run(); |
| 1047 | |
| 1048 | // FIFO order. |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 1049 | ASSERT_EQ(6U, order.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1050 | EXPECT_EQ(order[ 0], TaskItem(ORDERERD, 1, true)); |
| 1051 | EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 1, false)); |
| 1052 | EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 2, true)); |
| 1053 | EXPECT_EQ(order[ 3], TaskItem(ORDERERD, 2, false)); |
| 1054 | EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true)); |
| 1055 | EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false)); |
| 1056 | } |
| 1057 | |
| 1058 | // Tests that non nestable tasks don't run when there's code in the call stack. |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1059 | void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type, |
| 1060 | bool use_delayed) { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1061 | MessageLoop loop(message_loop_type); |
| 1062 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1063 | TaskList order; |
| 1064 | |
| 1065 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1066 | new TaskThatPumps(&order, 1)); |
| 1067 | Task* task = new OrderedTasks(&order, 2); |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1068 | if (use_delayed) { |
| 1069 | MessageLoop::current()->PostNonNestableDelayedTask(FROM_HERE, task, 1); |
| 1070 | } else { |
| 1071 | MessageLoop::current()->PostNonNestableTask(FROM_HERE, task); |
| 1072 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1073 | MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 3)); |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1074 | MessageLoop::current()->PostTask(FROM_HERE, new SleepTask(&order, 4, 50)); |
| 1075 | MessageLoop::current()->PostTask(FROM_HERE, new OrderedTasks(&order, 5)); |
| 1076 | Task* non_nestable_quit = new QuitTask(&order, 6); |
| 1077 | if (use_delayed) { |
| 1078 | MessageLoop::current()->PostNonNestableDelayedTask(FROM_HERE, |
| 1079 | non_nestable_quit, |
| 1080 | 2); |
| 1081 | } else { |
| 1082 | MessageLoop::current()->PostNonNestableTask(FROM_HERE, non_nestable_quit); |
| 1083 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1084 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1085 | MessageLoop::current()->Run(); |
| 1086 | |
| 1087 | // FIFO order. |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1088 | ASSERT_EQ(12U, order.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1089 | EXPECT_EQ(order[ 0], TaskItem(PUMPS, 1, true)); |
| 1090 | EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 3, true)); |
| 1091 | EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 3, false)); |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1092 | EXPECT_EQ(order[ 3], TaskItem(SLEEP, 4, true)); |
| 1093 | EXPECT_EQ(order[ 4], TaskItem(SLEEP, 4, false)); |
| 1094 | EXPECT_EQ(order[ 5], TaskItem(ORDERERD, 5, true)); |
| 1095 | EXPECT_EQ(order[ 6], TaskItem(ORDERERD, 5, false)); |
| 1096 | EXPECT_EQ(order[ 7], TaskItem(PUMPS, 1, false)); |
| 1097 | EXPECT_EQ(order[ 8], TaskItem(ORDERERD, 2, true)); |
| 1098 | EXPECT_EQ(order[ 9], TaskItem(ORDERERD, 2, false)); |
| 1099 | EXPECT_EQ(order[10], TaskItem(QUITMESSAGELOOP, 6, true)); |
| 1100 | EXPECT_EQ(order[11], TaskItem(QUITMESSAGELOOP, 6, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1101 | } |
| 1102 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 1103 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1104 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1105 | class DispatcherImpl : public MessageLoopForUI::Dispatcher { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1106 | public: |
| 1107 | DispatcherImpl() : dispatch_count_(0) {} |
| 1108 | |
| 1109 | virtual bool Dispatch(const MSG& msg) { |
| 1110 | ::TranslateMessage(&msg); |
| 1111 | ::DispatchMessage(&msg); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1112 | // Do not count WM_TIMER since it is not what we post and it will cause |
| 1113 | // flakiness. |
| 1114 | if (msg.message != WM_TIMER) |
| 1115 | ++dispatch_count_; |
| 1116 | // We treat WM_LBUTTONUP as the last message. |
| 1117 | return msg.message != WM_LBUTTONUP; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | int dispatch_count_; |
| 1121 | }; |
| 1122 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1123 | void RunTest_Dispatcher(MessageLoop::Type message_loop_type) { |
| 1124 | MessageLoop loop(message_loop_type); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1125 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1126 | class MyTask : public Task { |
| 1127 | public: |
| 1128 | virtual void Run() { |
| 1129 | PostMessage(NULL, WM_LBUTTONDOWN, 0, 0); |
| 1130 | PostMessage(NULL, WM_LBUTTONUP, 'A', 0); |
| 1131 | } |
| 1132 | }; |
| 1133 | Task* task = new MyTask(); |
| 1134 | MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 100); |
| 1135 | DispatcherImpl dispatcher; |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1136 | MessageLoopForUI::current()->Run(&dispatcher); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1137 | ASSERT_EQ(2, dispatcher.dispatch_count_); |
| 1138 | } |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 1139 | |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1140 | LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) { |
| 1141 | if (code == base::MessagePumpForUI::kMessageFilterCode) { |
| 1142 | MSG* msg = reinterpret_cast<MSG*>(lparam); |
| 1143 | if (msg->message == WM_LBUTTONDOWN) |
| 1144 | return TRUE; |
| 1145 | } |
| 1146 | return FALSE; |
| 1147 | } |
| 1148 | |
| 1149 | void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type) { |
| 1150 | MessageLoop loop(message_loop_type); |
| 1151 | |
| 1152 | class MyTask : public Task { |
| 1153 | public: |
| 1154 | virtual void Run() { |
| 1155 | PostMessage(NULL, WM_LBUTTONDOWN, 0, 0); |
| 1156 | PostMessage(NULL, WM_LBUTTONUP, 'A', 0); |
| 1157 | } |
| 1158 | }; |
| 1159 | Task* task = new MyTask(); |
| 1160 | MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 100); |
| 1161 | HHOOK msg_hook = SetWindowsHookEx(WH_MSGFILTER, |
| 1162 | MsgFilterProc, |
| 1163 | NULL, |
| 1164 | GetCurrentThreadId()); |
| 1165 | DispatcherImpl dispatcher; |
| 1166 | MessageLoopForUI::current()->Run(&dispatcher); |
| 1167 | ASSERT_EQ(1, dispatcher.dispatch_count_); |
| 1168 | UnhookWindowsHookEx(msg_hook); |
| 1169 | } |
| 1170 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1171 | class TestIOHandler : public MessageLoopForIO::IOHandler { |
| 1172 | public: |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1173 | TestIOHandler(const wchar_t* name, HANDLE signal, bool wait); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1174 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1175 | virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 1176 | DWORD bytes_transfered, DWORD error); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1177 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1178 | void Init(); |
| 1179 | void WaitForIO(); |
| 1180 | OVERLAPPED* context() { return &context_.overlapped; } |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1181 | DWORD size() { return sizeof(buffer_); } |
| 1182 | |
| 1183 | private: |
| 1184 | char buffer_[48]; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1185 | MessageLoopForIO::IOContext context_; |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1186 | HANDLE signal_; |
| 1187 | ScopedHandle file_; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1188 | bool wait_; |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1189 | }; |
| 1190 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1191 | TestIOHandler::TestIOHandler(const wchar_t* name, HANDLE signal, bool wait) |
| 1192 | : signal_(signal), wait_(wait) { |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1193 | memset(buffer_, 0, sizeof(buffer_)); |
| 1194 | memset(&context_, 0, sizeof(context_)); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1195 | context_.handler = this; |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1196 | |
| 1197 | file_.Set(CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, |
| 1198 | FILE_FLAG_OVERLAPPED, NULL)); |
| 1199 | EXPECT_TRUE(file_.IsValid()); |
| 1200 | } |
| 1201 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1202 | void TestIOHandler::Init() { |
| 1203 | MessageLoopForIO::current()->RegisterIOHandler(file_, this); |
| 1204 | |
| 1205 | DWORD read; |
| 1206 | EXPECT_FALSE(ReadFile(file_, buffer_, size(), &read, context())); |
| 1207 | EXPECT_EQ(ERROR_IO_PENDING, GetLastError()); |
| 1208 | if (wait_) |
| 1209 | WaitForIO(); |
| 1210 | } |
| 1211 | |
| 1212 | void TestIOHandler::OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 1213 | DWORD bytes_transfered, DWORD error) { |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1214 | ASSERT_TRUE(context == &context_); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1215 | ASSERT_TRUE(SetEvent(signal_)); |
| 1216 | } |
| 1217 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1218 | void TestIOHandler::WaitForIO() { |
| 1219 | EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(300, this)); |
| 1220 | EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(400, this)); |
| 1221 | } |
| 1222 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1223 | class IOHandlerTask : public Task { |
| 1224 | public: |
| 1225 | explicit IOHandlerTask(TestIOHandler* handler) : handler_(handler) {} |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1226 | virtual void Run() { |
| 1227 | handler_->Init(); |
| 1228 | } |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1229 | |
| 1230 | private: |
| 1231 | TestIOHandler* handler_; |
| 1232 | }; |
| 1233 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1234 | void RunTest_IOHandler() { |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1235 | ScopedHandle callback_called(CreateEvent(NULL, TRUE, FALSE, NULL)); |
| 1236 | ASSERT_TRUE(callback_called.IsValid()); |
| 1237 | |
| 1238 | const wchar_t* kPipeName = L"\\\\.\\pipe\\iohandler_pipe"; |
| 1239 | ScopedHandle server(CreateNamedPipe(kPipeName, PIPE_ACCESS_OUTBOUND, 0, 1, |
| 1240 | 0, 0, 0, NULL)); |
| 1241 | ASSERT_TRUE(server.IsValid()); |
| 1242 | |
| 1243 | Thread thread("IOHandler test"); |
| 1244 | Thread::Options options; |
| 1245 | options.message_loop_type = MessageLoop::TYPE_IO; |
| 1246 | ASSERT_TRUE(thread.StartWithOptions(options)); |
| 1247 | |
| 1248 | MessageLoop* thread_loop = thread.message_loop(); |
| 1249 | ASSERT_TRUE(NULL != thread_loop); |
| 1250 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1251 | TestIOHandler handler(kPipeName, callback_called, false); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1252 | IOHandlerTask* task = new IOHandlerTask(&handler); |
| 1253 | thread_loop->PostTask(FROM_HERE, task); |
| 1254 | Sleep(100); // Make sure the thread runs and sleeps for lack of work. |
| 1255 | |
| 1256 | const char buffer[] = "Hello there!"; |
| 1257 | DWORD written; |
| 1258 | EXPECT_TRUE(WriteFile(server, buffer, sizeof(buffer), &written, NULL)); |
| 1259 | |
| 1260 | DWORD result = WaitForSingleObject(callback_called, 1000); |
| 1261 | EXPECT_EQ(WAIT_OBJECT_0, result); |
| 1262 | |
| 1263 | thread.Stop(); |
| 1264 | } |
| 1265 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1266 | void RunTest_WaitForIO() { |
| 1267 | ScopedHandle callback1_called(CreateEvent(NULL, TRUE, FALSE, NULL)); |
| 1268 | ScopedHandle callback2_called(CreateEvent(NULL, TRUE, FALSE, NULL)); |
| 1269 | ASSERT_TRUE(callback1_called.IsValid()); |
| 1270 | ASSERT_TRUE(callback2_called.IsValid()); |
| 1271 | |
| 1272 | const wchar_t* kPipeName1 = L"\\\\.\\pipe\\iohandler_pipe1"; |
| 1273 | const wchar_t* kPipeName2 = L"\\\\.\\pipe\\iohandler_pipe2"; |
| 1274 | ScopedHandle server1(CreateNamedPipe(kPipeName1, PIPE_ACCESS_OUTBOUND, 0, 1, |
| 1275 | 0, 0, 0, NULL)); |
| 1276 | ScopedHandle server2(CreateNamedPipe(kPipeName2, PIPE_ACCESS_OUTBOUND, 0, 1, |
| 1277 | 0, 0, 0, NULL)); |
| 1278 | ASSERT_TRUE(server1.IsValid()); |
| 1279 | ASSERT_TRUE(server2.IsValid()); |
| 1280 | |
| 1281 | Thread thread("IOHandler test"); |
| 1282 | Thread::Options options; |
| 1283 | options.message_loop_type = MessageLoop::TYPE_IO; |
| 1284 | ASSERT_TRUE(thread.StartWithOptions(options)); |
| 1285 | |
| 1286 | MessageLoop* thread_loop = thread.message_loop(); |
| 1287 | ASSERT_TRUE(NULL != thread_loop); |
| 1288 | |
| 1289 | TestIOHandler handler1(kPipeName1, callback1_called, false); |
| 1290 | TestIOHandler handler2(kPipeName2, callback2_called, true); |
| 1291 | IOHandlerTask* task1 = new IOHandlerTask(&handler1); |
| 1292 | IOHandlerTask* task2 = new IOHandlerTask(&handler2); |
| 1293 | thread_loop->PostTask(FROM_HERE, task1); |
| 1294 | Sleep(100); // Make sure the thread runs and sleeps for lack of work. |
| 1295 | thread_loop->PostTask(FROM_HERE, task2); |
| 1296 | Sleep(100); |
| 1297 | |
| 1298 | // At this time handler1 is waiting to be called, and the thread is waiting |
| 1299 | // on the Init method of handler2, filtering only handler2 callbacks. |
| 1300 | |
| 1301 | const char buffer[] = "Hello there!"; |
| 1302 | DWORD written; |
| 1303 | EXPECT_TRUE(WriteFile(server1, buffer, sizeof(buffer), &written, NULL)); |
| 1304 | Sleep(200); |
| 1305 | EXPECT_EQ(WAIT_TIMEOUT, WaitForSingleObject(callback1_called, 0)) << |
| 1306 | "handler1 has not been called"; |
| 1307 | |
| 1308 | EXPECT_TRUE(WriteFile(server2, buffer, sizeof(buffer), &written, NULL)); |
| 1309 | |
| 1310 | HANDLE objects[2] = { callback1_called.Get(), callback2_called.Get() }; |
| 1311 | DWORD result = WaitForMultipleObjects(2, objects, TRUE, 1000); |
| 1312 | EXPECT_EQ(WAIT_OBJECT_0, result); |
| 1313 | |
| 1314 | thread.Stop(); |
| 1315 | } |
| 1316 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1317 | #endif // defined(OS_WIN) |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1318 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1319 | } // namespace |
| 1320 | |
| 1321 | //----------------------------------------------------------------------------- |
| 1322 | // Each test is run against each type of MessageLoop. That way we are sure |
| 1323 | // that message loops work properly in all configurations. Of course, in some |
| 1324 | // cases, a unit test may only be for a particular type of loop. |
| 1325 | |
| 1326 | TEST(MessageLoopTest, PostTask) { |
| 1327 | RunTest_PostTask(MessageLoop::TYPE_DEFAULT); |
| 1328 | RunTest_PostTask(MessageLoop::TYPE_UI); |
| 1329 | RunTest_PostTask(MessageLoop::TYPE_IO); |
| 1330 | } |
| 1331 | |
| 1332 | TEST(MessageLoopTest, PostTask_SEH) { |
| 1333 | RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT); |
| 1334 | RunTest_PostTask_SEH(MessageLoop::TYPE_UI); |
| 1335 | RunTest_PostTask_SEH(MessageLoop::TYPE_IO); |
| 1336 | } |
| 1337 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 1338 | TEST(MessageLoopTest, PostDelayedTask_Basic) { |
| 1339 | RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT); |
| 1340 | RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI); |
| 1341 | RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO); |
| 1342 | } |
| 1343 | |
| 1344 | TEST(MessageLoopTest, PostDelayedTask_InDelayOrder) { |
| 1345 | RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_DEFAULT); |
| 1346 | RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_UI); |
| 1347 | RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_IO); |
| 1348 | } |
| 1349 | |
| 1350 | TEST(MessageLoopTest, PostDelayedTask_InPostOrder) { |
| 1351 | RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_DEFAULT); |
| 1352 | RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_UI); |
| 1353 | RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_IO); |
| 1354 | } |
| 1355 | |
| 1356 | TEST(MessageLoopTest, PostDelayedTask_InPostOrder_2) { |
| 1357 | RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_DEFAULT); |
| 1358 | RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI); |
| 1359 | RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO); |
| 1360 | } |
| 1361 | |
| 1362 | TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) { |
| 1363 | RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT); |
| 1364 | RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI); |
| 1365 | RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO); |
| 1366 | } |
| 1367 | |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 1368 | TEST(MessageLoopTest, PostDelayedTask_SharedTimer) { |
| 1369 | RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_DEFAULT); |
| 1370 | RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_UI); |
| 1371 | RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_IO); |
| 1372 | } |
| 1373 | |
| 1374 | #if defined(OS_WIN) |
| 1375 | TEST(MessageLoopTest, PostDelayedTask_SharedTimer_SubPump) { |
| 1376 | RunTest_PostDelayedTask_SharedTimer_SubPump(); |
| 1377 | } |
| 1378 | #endif |
| 1379 | |
[email protected] | 5affb7e | 2010-05-25 20:13:36 | [diff] [blame] | 1380 | // TODO(darin): MessageLoop does not support deleting all tasks in the |
| 1381 | // destructor. |
| 1382 | TEST(MessageLoopTest, FAILS_EnsureTaskDeletion) { |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 1383 | RunTest_EnsureTaskDeletion(MessageLoop::TYPE_DEFAULT); |
| 1384 | RunTest_EnsureTaskDeletion(MessageLoop::TYPE_UI); |
| 1385 | RunTest_EnsureTaskDeletion(MessageLoop::TYPE_IO); |
| 1386 | } |
| 1387 | |
[email protected] | 5affb7e | 2010-05-25 20:13:36 | [diff] [blame] | 1388 | // TODO(darin): MessageLoop does not support deleting all tasks in the |
| 1389 | // destructor. |
| 1390 | TEST(MessageLoopTest, FAILS_EnsureTaskDeletion_Chain) { |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 1391 | RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_DEFAULT); |
| 1392 | RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_UI); |
| 1393 | RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_IO); |
| 1394 | } |
| 1395 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1396 | #if defined(OS_WIN) |
| 1397 | TEST(MessageLoopTest, Crasher) { |
| 1398 | RunTest_Crasher(MessageLoop::TYPE_DEFAULT); |
| 1399 | RunTest_Crasher(MessageLoop::TYPE_UI); |
| 1400 | RunTest_Crasher(MessageLoop::TYPE_IO); |
| 1401 | } |
| 1402 | |
| 1403 | TEST(MessageLoopTest, CrasherNasty) { |
| 1404 | RunTest_CrasherNasty(MessageLoop::TYPE_DEFAULT); |
| 1405 | RunTest_CrasherNasty(MessageLoop::TYPE_UI); |
| 1406 | RunTest_CrasherNasty(MessageLoop::TYPE_IO); |
| 1407 | } |
| 1408 | #endif // defined(OS_WIN) |
| 1409 | |
| 1410 | TEST(MessageLoopTest, Nesting) { |
| 1411 | RunTest_Nesting(MessageLoop::TYPE_DEFAULT); |
| 1412 | RunTest_Nesting(MessageLoop::TYPE_UI); |
| 1413 | RunTest_Nesting(MessageLoop::TYPE_IO); |
| 1414 | } |
| 1415 | |
| 1416 | TEST(MessageLoopTest, RecursiveDenial1) { |
| 1417 | RunTest_RecursiveDenial1(MessageLoop::TYPE_DEFAULT); |
| 1418 | RunTest_RecursiveDenial1(MessageLoop::TYPE_UI); |
| 1419 | RunTest_RecursiveDenial1(MessageLoop::TYPE_IO); |
| 1420 | } |
| 1421 | |
| 1422 | TEST(MessageLoopTest, RecursiveSupport1) { |
| 1423 | RunTest_RecursiveSupport1(MessageLoop::TYPE_DEFAULT); |
| 1424 | RunTest_RecursiveSupport1(MessageLoop::TYPE_UI); |
| 1425 | RunTest_RecursiveSupport1(MessageLoop::TYPE_IO); |
| 1426 | } |
| 1427 | |
| 1428 | #if defined(OS_WIN) |
[email protected] | 85f39f8 | 2010-05-19 14:33:44 | [diff] [blame] | 1429 | // This test occasionally hangs http://crbug.com/44567 |
| 1430 | TEST(MessageLoopTest, DISABLED_RecursiveDenial2) { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1431 | RunTest_RecursiveDenial2(MessageLoop::TYPE_DEFAULT); |
| 1432 | RunTest_RecursiveDenial2(MessageLoop::TYPE_UI); |
| 1433 | RunTest_RecursiveDenial2(MessageLoop::TYPE_IO); |
| 1434 | } |
| 1435 | |
| 1436 | TEST(MessageLoopTest, RecursiveSupport2) { |
| 1437 | // This test requires a UI loop |
| 1438 | RunTest_RecursiveSupport2(MessageLoop::TYPE_UI); |
| 1439 | } |
| 1440 | #endif // defined(OS_WIN) |
| 1441 | |
| 1442 | TEST(MessageLoopTest, NonNestableWithNoNesting) { |
| 1443 | RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_DEFAULT); |
| 1444 | RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_UI); |
| 1445 | RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_IO); |
| 1446 | } |
| 1447 | |
| 1448 | TEST(MessageLoopTest, NonNestableInNestedLoop) { |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1449 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, false); |
| 1450 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, false); |
| 1451 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, false); |
| 1452 | } |
| 1453 | |
| 1454 | TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) { |
| 1455 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true); |
| 1456 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true); |
| 1457 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true); |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1458 | } |
| 1459 | |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1460 | class DummyTask : public Task { |
| 1461 | public: |
| 1462 | DummyTask(int num_tasks) : num_tasks_(num_tasks) {} |
| 1463 | |
| 1464 | virtual void Run() { |
| 1465 | if (num_tasks_ > 1) { |
| 1466 | MessageLoop::current()->PostTask( |
| 1467 | FROM_HERE, |
| 1468 | new DummyTask(num_tasks_ - 1)); |
| 1469 | } else { |
| 1470 | MessageLoop::current()->Quit(); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | private: |
| 1475 | const int num_tasks_; |
| 1476 | }; |
| 1477 | |
| 1478 | class DummyTaskObserver : public MessageLoop::TaskObserver { |
| 1479 | public: |
| 1480 | DummyTaskObserver(int num_tasks) |
| 1481 | : num_tasks_started_(0), |
| 1482 | num_tasks_processed_(0), |
| 1483 | num_tasks_(num_tasks) {} |
| 1484 | |
| 1485 | virtual ~DummyTaskObserver() {} |
| 1486 | |
| 1487 | virtual void WillProcessTask(base::TimeTicks /* birth_time */) { |
| 1488 | num_tasks_started_++; |
| 1489 | EXPECT_LE(num_tasks_started_, num_tasks_); |
| 1490 | EXPECT_EQ(num_tasks_started_, num_tasks_processed_ + 1); |
| 1491 | } |
| 1492 | |
| 1493 | virtual void DidProcessTask() { |
| 1494 | num_tasks_processed_++; |
| 1495 | EXPECT_LE(num_tasks_started_, num_tasks_); |
| 1496 | EXPECT_EQ(num_tasks_started_, num_tasks_processed_); |
| 1497 | } |
| 1498 | |
| 1499 | int num_tasks_started() const { return num_tasks_started_; } |
| 1500 | int num_tasks_processed() const { return num_tasks_processed_; } |
| 1501 | |
| 1502 | private: |
| 1503 | int num_tasks_started_; |
| 1504 | int num_tasks_processed_; |
| 1505 | const int num_tasks_; |
| 1506 | |
| 1507 | DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver); |
| 1508 | }; |
| 1509 | |
| 1510 | TEST(MessageLoopTest, TaskObserver) { |
| 1511 | const int kNumTasks = 6; |
| 1512 | DummyTaskObserver observer(kNumTasks); |
| 1513 | |
| 1514 | MessageLoop loop; |
| 1515 | loop.AddTaskObserver(&observer); |
| 1516 | loop.PostTask(FROM_HERE, new DummyTask(kNumTasks)); |
| 1517 | loop.Run(); |
| 1518 | loop.RemoveTaskObserver(&observer); |
| 1519 | |
| 1520 | EXPECT_EQ(kNumTasks, observer.num_tasks_started()); |
| 1521 | EXPECT_EQ(kNumTasks, observer.num_tasks_processed()); |
| 1522 | } |
| 1523 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1524 | #if defined(OS_WIN) |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1525 | TEST(MessageLoopTest, Dispatcher) { |
| 1526 | // This test requires a UI loop |
| 1527 | RunTest_Dispatcher(MessageLoop::TYPE_UI); |
| 1528 | } |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1529 | |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1530 | TEST(MessageLoopTest, DispatcherWithMessageHook) { |
| 1531 | // This test requires a UI loop |
| 1532 | RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI); |
| 1533 | } |
| 1534 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1535 | TEST(MessageLoopTest, IOHandler) { |
| 1536 | RunTest_IOHandler(); |
| 1537 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1538 | |
| 1539 | TEST(MessageLoopTest, WaitForIO) { |
| 1540 | RunTest_WaitForIO(); |
| 1541 | } |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1542 | #endif // defined(OS_WIN) |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1543 | |
| 1544 | #if defined(OS_POSIX) |
| 1545 | |
| 1546 | namespace { |
| 1547 | |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1548 | class QuitDelegate : public base::MessagePumpLibevent::Watcher { |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1549 | public: |
| 1550 | virtual void OnFileCanWriteWithoutBlocking(int fd) { |
| 1551 | MessageLoop::current()->Quit(); |
| 1552 | } |
| 1553 | virtual void OnFileCanReadWithoutBlocking(int fd) { |
| 1554 | MessageLoop::current()->Quit(); |
| 1555 | } |
| 1556 | }; |
| 1557 | |
[email protected] | f4521377 | 2010-06-11 00:06:19 | [diff] [blame] | 1558 | TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) { |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1559 | // Simulate a MessageLoop that dies before an FileDescriptorWatcher. |
| 1560 | // This could happen when people use the Singleton pattern or atexit. |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1561 | |
| 1562 | // Create a file descriptor. Doesn't need to be readable or writable, |
| 1563 | // as we don't need to actually get any notifications. |
| 1564 | // pipe() is just the easiest way to do it. |
| 1565 | int pipefds[2]; |
| 1566 | int err = pipe(pipefds); |
| 1567 | ASSERT_TRUE(err == 0); |
| 1568 | int fd = pipefds[1]; |
| 1569 | { |
| 1570 | // Arrange for controller to live longer than message loop. |
| 1571 | base::MessagePumpLibevent::FileDescriptorWatcher controller; |
| 1572 | { |
| 1573 | MessageLoopForIO message_loop; |
| 1574 | |
| 1575 | QuitDelegate delegate; |
| 1576 | message_loop.WatchFileDescriptor(fd, |
| 1577 | true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); |
| 1578 | // and don't run the message loop, just destroy it. |
| 1579 | } |
| 1580 | } |
[email protected] | 70eb657 | 2010-06-23 00:37:46 | [diff] [blame^] | 1581 | if (HANDLE_EINTR(close(pipefds[0])) < 0) |
| 1582 | PLOG(ERROR) << "close"; |
| 1583 | if (HANDLE_EINTR(close(pipefds[1])) < 0) |
| 1584 | PLOG(ERROR) << "close"; |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) { |
| 1588 | // Verify that it's ok to call StopWatchingFileDescriptor(). |
| 1589 | // (Errors only showed up in valgrind.) |
| 1590 | int pipefds[2]; |
| 1591 | int err = pipe(pipefds); |
| 1592 | ASSERT_TRUE(err == 0); |
| 1593 | int fd = pipefds[1]; |
| 1594 | { |
| 1595 | // Arrange for message loop to live longer than controller. |
| 1596 | MessageLoopForIO message_loop; |
| 1597 | { |
| 1598 | base::MessagePumpLibevent::FileDescriptorWatcher controller; |
| 1599 | |
| 1600 | QuitDelegate delegate; |
| 1601 | message_loop.WatchFileDescriptor(fd, |
| 1602 | true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); |
| 1603 | controller.StopWatchingFileDescriptor(); |
| 1604 | } |
| 1605 | } |
[email protected] | 70eb657 | 2010-06-23 00:37:46 | [diff] [blame^] | 1606 | if (HANDLE_EINTR(close(pipefds[0])) < 0) |
| 1607 | PLOG(ERROR) << "close"; |
| 1608 | if (HANDLE_EINTR(close(pipefds[1])) < 0) |
| 1609 | PLOG(ERROR) << "close"; |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1610 | } |
| 1611 | |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1612 | } // namespace |
| 1613 | |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 1614 | #endif // defined(OS_POSIX) |