[email protected] | ac4c668 | 2012-01-04 00:57:39 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 5 | #include <vector> |
| 6 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
| 9 | #include "base/compiler_specific.h" |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 10 | #include "base/eintr_wrapper.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | #include "base/message_loop.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 14 | #include "base/run_loop.h" |
[email protected] | 4fc20d1 | 2012-05-09 20:16:14 | [diff] [blame] | 15 | #include "base/thread_task_runner_handle.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 16 | #include "base/threading/platform_thread.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 20 | #if defined(OS_WIN) |
| 21 | #include "base/message_pump_win.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 22 | #include "base/win/scoped_handle.h" |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 23 | #endif |
| 24 | |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 25 | using base::PlatformThread; |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 26 | using base::Thread; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 27 | using base::Time; |
| 28 | using base::TimeDelta; |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 29 | using base::TimeTicks; |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 30 | |
| 31 | // TODO(darin): Platform-specific MessageLoop tests should be grouped together |
| 32 | // to avoid chopping this file up with so many #ifdefs. |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 33 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 34 | namespace { |
| 35 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 36 | class MessageLoopTest : public testing::Test {}; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 37 | |
| 38 | class Foo : public base::RefCounted<Foo> { |
| 39 | public: |
| 40 | Foo() : test_count_(0) { |
| 41 | } |
| 42 | |
| 43 | void Test0() { |
| 44 | ++test_count_; |
| 45 | } |
| 46 | |
| 47 | void Test1ConstRef(const std::string& a) { |
| 48 | ++test_count_; |
| 49 | result_.append(a); |
| 50 | } |
| 51 | |
| 52 | void Test1Ptr(std::string* a) { |
| 53 | ++test_count_; |
| 54 | result_.append(*a); |
| 55 | } |
| 56 | |
| 57 | void Test1Int(int a) { |
| 58 | test_count_ += a; |
| 59 | } |
| 60 | |
| 61 | void Test2Ptr(std::string* a, std::string* b) { |
| 62 | ++test_count_; |
| 63 | result_.append(*a); |
| 64 | result_.append(*b); |
| 65 | } |
| 66 | |
| 67 | void Test2Mixed(const std::string& a, std::string* b) { |
| 68 | ++test_count_; |
| 69 | result_.append(a); |
| 70 | result_.append(*b); |
| 71 | } |
| 72 | |
| 73 | int test_count() const { return test_count_; } |
| 74 | const std::string& result() const { return result_; } |
| 75 | |
| 76 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 77 | friend class base::RefCounted<Foo>; |
| 78 | |
| 79 | ~Foo() {} |
| 80 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 81 | int test_count_; |
| 82 | std::string result_; |
| 83 | }; |
| 84 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 85 | void RunTest_PostTask(MessageLoop::Type message_loop_type) { |
| 86 | MessageLoop loop(message_loop_type); |
| 87 | |
| 88 | // Add tests to message loop |
| 89 | scoped_refptr<Foo> foo(new Foo()); |
| 90 | std::string a("a"), b("b"), c("c"), d("d"); |
| 91 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 92 | &Foo::Test0, foo.get())); |
| 93 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 94 | &Foo::Test1ConstRef, foo.get(), a)); |
| 95 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 96 | &Foo::Test1Ptr, foo.get(), &b)); |
| 97 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 98 | &Foo::Test1Int, foo.get(), 100)); |
| 99 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 100 | &Foo::Test2Ptr, foo.get(), &a, &c)); |
| 101 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 102 | &Foo::Test2Mixed, foo.get(), a, &d)); |
| 103 | |
| 104 | // After all tests, post a message that will shut down the message loop |
| 105 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 106 | &MessageLoop::Quit, base::Unretained(MessageLoop::current()))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 107 | |
| 108 | // Now kick things off |
| 109 | MessageLoop::current()->Run(); |
| 110 | |
| 111 | EXPECT_EQ(foo->test_count(), 105); |
| 112 | EXPECT_EQ(foo->result(), "abacad"); |
| 113 | } |
| 114 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 115 | void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) { |
| 116 | MessageLoop loop(message_loop_type); |
| 117 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 118 | // Add tests to message loop |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 119 | scoped_refptr<Foo> foo(new Foo()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 120 | std::string a("a"), b("b"), c("c"), d("d"); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 121 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 122 | &Foo::Test0, foo.get())); |
| 123 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 124 | &Foo::Test1ConstRef, foo.get(), a)); |
| 125 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 126 | &Foo::Test1Ptr, foo.get(), &b)); |
| 127 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 128 | &Foo::Test1Int, foo.get(), 100)); |
| 129 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 130 | &Foo::Test2Ptr, foo.get(), &a, &c)); |
| 131 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 132 | &Foo::Test2Mixed, foo.get(), a, &d)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 133 | |
| 134 | // After all tests, post a message that will shut down the message loop |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 135 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 136 | &MessageLoop::Quit, base::Unretained(MessageLoop::current()))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 137 | |
| 138 | // Now kick things off with the SEH block active. |
| 139 | MessageLoop::current()->set_exception_restoration(true); |
| 140 | MessageLoop::current()->Run(); |
| 141 | MessageLoop::current()->set_exception_restoration(false); |
| 142 | |
| 143 | EXPECT_EQ(foo->test_count(), 105); |
| 144 | EXPECT_EQ(foo->result(), "abacad"); |
| 145 | } |
| 146 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 147 | // This function runs slowly to simulate a large amount of work being done. |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 148 | static void SlowFunc(TimeDelta pause, int* quit_counter) { |
| 149 | PlatformThread::Sleep(pause); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 150 | if (--(*quit_counter) == 0) |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 151 | MessageLoop::current()->Quit(); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 152 | } |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 153 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 154 | // This function records the time when Run was called in a Time object, which is |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 155 | // useful for building a variety of MessageLoop tests. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 156 | static void RecordRunTimeFunc(Time* run_time, int* quit_counter) { |
| 157 | *run_time = Time::Now(); |
| 158 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 159 | // Cause our Run function to take some time to execute. As a result we can |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 160 | // count on subsequent RecordRunTimeFunc()s running at a future time, |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 161 | // without worry about the resolution of our system clock being an issue. |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 162 | SlowFunc(TimeDelta::FromMilliseconds(10), quit_counter); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 163 | } |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 164 | |
| 165 | void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) { |
| 166 | MessageLoop loop(message_loop_type); |
| 167 | |
| 168 | // Test that PostDelayedTask results in a delayed task. |
| 169 | |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 170 | const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 171 | |
| 172 | int num_tasks = 1; |
| 173 | Time run_time; |
| 174 | |
| 175 | loop.PostDelayedTask( |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 176 | FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 177 | kDelay); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 178 | |
| 179 | Time time_before_run = Time::Now(); |
| 180 | loop.Run(); |
| 181 | Time time_after_run = Time::Now(); |
| 182 | |
| 183 | EXPECT_EQ(0, num_tasks); |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 184 | EXPECT_LT(kDelay, time_after_run - time_before_run); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 187 | void RunTest_PostDelayedTask_InDelayOrder( |
| 188 | MessageLoop::Type message_loop_type) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 189 | MessageLoop loop(message_loop_type); |
| 190 | |
| 191 | // Test that two tasks with different delays run in the right order. |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 192 | int num_tasks = 2; |
| 193 | Time run_time1, run_time2; |
| 194 | |
| 195 | loop.PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 196 | FROM_HERE, |
| 197 | base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), |
| 198 | TimeDelta::FromMilliseconds(200)); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 199 | // If we get a large pause in execution (due to a context switch) here, this |
| 200 | // test could fail. |
| 201 | loop.PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 202 | FROM_HERE, |
| 203 | base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), |
| 204 | TimeDelta::FromMilliseconds(10)); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 205 | |
| 206 | loop.Run(); |
| 207 | EXPECT_EQ(0, num_tasks); |
| 208 | |
| 209 | EXPECT_TRUE(run_time2 < run_time1); |
| 210 | } |
| 211 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 212 | void RunTest_PostDelayedTask_InPostOrder( |
| 213 | MessageLoop::Type message_loop_type) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 214 | MessageLoop loop(message_loop_type); |
| 215 | |
| 216 | // Test that two tasks with the same delay run in the order in which they |
| 217 | // were posted. |
| 218 | // |
| 219 | // NOTE: This is actually an approximate test since the API only takes a |
| 220 | // "delay" parameter, so we are not exactly simulating two tasks that get |
| 221 | // posted at the exact same time. It would be nice if the API allowed us to |
| 222 | // specify the desired run time. |
| 223 | |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 224 | const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 225 | |
| 226 | int num_tasks = 2; |
| 227 | Time run_time1, run_time2; |
| 228 | |
| 229 | loop.PostDelayedTask( |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 230 | FROM_HERE, |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 231 | base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), kDelay); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 232 | loop.PostDelayedTask( |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 233 | FROM_HERE, |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 234 | base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), kDelay); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 235 | |
| 236 | loop.Run(); |
| 237 | EXPECT_EQ(0, num_tasks); |
| 238 | |
| 239 | EXPECT_TRUE(run_time1 < run_time2); |
| 240 | } |
| 241 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 242 | void RunTest_PostDelayedTask_InPostOrder_2( |
| 243 | MessageLoop::Type message_loop_type) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 244 | MessageLoop loop(message_loop_type); |
| 245 | |
| 246 | // Test that a delayed task still runs after a normal tasks even if the |
| 247 | // normal tasks take a long time to run. |
| 248 | |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 249 | const TimeDelta kPause = TimeDelta::FromMilliseconds(50); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 250 | |
| 251 | int num_tasks = 2; |
| 252 | Time run_time; |
| 253 | |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 254 | loop.PostTask(FROM_HERE, base::Bind(&SlowFunc, kPause, &num_tasks)); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 255 | loop.PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 256 | FROM_HERE, |
| 257 | base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), |
| 258 | TimeDelta::FromMilliseconds(10)); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 259 | |
| 260 | Time time_before_run = Time::Now(); |
| 261 | loop.Run(); |
| 262 | Time time_after_run = Time::Now(); |
| 263 | |
| 264 | EXPECT_EQ(0, num_tasks); |
| 265 | |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 266 | EXPECT_LT(kPause, time_after_run - time_before_run); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 267 | } |
| 268 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 269 | void RunTest_PostDelayedTask_InPostOrder_3( |
| 270 | MessageLoop::Type message_loop_type) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 271 | MessageLoop loop(message_loop_type); |
| 272 | |
| 273 | // Test that a delayed task still runs after a pile of normal tasks. The key |
| 274 | // difference between this test and the previous one is that here we return |
| 275 | // the MessageLoop a lot so we give the MessageLoop plenty of opportunities |
| 276 | // to maybe run the delayed task. It should know not to do so until the |
| 277 | // delayed task's delay has passed. |
| 278 | |
| 279 | int num_tasks = 11; |
| 280 | Time run_time1, run_time2; |
| 281 | |
| 282 | // Clutter the ML with tasks. |
| 283 | for (int i = 1; i < num_tasks; ++i) |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 284 | loop.PostTask(FROM_HERE, |
| 285 | base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks)); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 286 | |
| 287 | loop.PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 288 | FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), |
| 289 | TimeDelta::FromMilliseconds(1)); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 290 | |
| 291 | loop.Run(); |
| 292 | EXPECT_EQ(0, num_tasks); |
| 293 | |
| 294 | EXPECT_TRUE(run_time2 > run_time1); |
| 295 | } |
| 296 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 297 | void RunTest_PostDelayedTask_SharedTimer( |
| 298 | MessageLoop::Type message_loop_type) { |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 299 | MessageLoop loop(message_loop_type); |
| 300 | |
| 301 | // Test that the interval of the timer, used to run the next delayed task, is |
| 302 | // set to a value corresponding to when the next delayed task should run. |
| 303 | |
| 304 | // By setting num_tasks to 1, we ensure that the first task to run causes the |
| 305 | // run loop to exit. |
| 306 | int num_tasks = 1; |
| 307 | Time run_time1, run_time2; |
| 308 | |
| 309 | loop.PostDelayedTask( |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 310 | FROM_HERE, |
| 311 | base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 312 | TimeDelta::FromSeconds(1000)); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 313 | loop.PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 314 | FROM_HERE, |
| 315 | base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), |
| 316 | TimeDelta::FromMilliseconds(10)); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 317 | |
| 318 | Time start_time = Time::Now(); |
| 319 | |
| 320 | loop.Run(); |
| 321 | EXPECT_EQ(0, num_tasks); |
| 322 | |
| 323 | // Ensure that we ran in far less time than the slower timer. |
[email protected] | 4615f198 | 2008-09-23 19:22:33 | [diff] [blame] | 324 | TimeDelta total_time = Time::Now() - start_time; |
| 325 | EXPECT_GT(5000, total_time.InMilliseconds()); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 326 | |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 327 | // In case both timers somehow run at nearly the same time, sleep a little |
| 328 | // and then run all pending to force them both to have run. This is just |
| 329 | // encouraging flakiness if there is any. |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 330 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 331 | loop.RunAllPending(); |
| 332 | |
| 333 | EXPECT_TRUE(run_time1.is_null()); |
| 334 | EXPECT_FALSE(run_time2.is_null()); |
| 335 | } |
| 336 | |
| 337 | #if defined(OS_WIN) |
| 338 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 339 | void SubPumpFunc() { |
| 340 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 341 | MSG msg; |
| 342 | while (GetMessage(&msg, NULL, 0, 0)) { |
| 343 | TranslateMessage(&msg); |
| 344 | DispatchMessage(&msg); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 345 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 346 | MessageLoop::current()->Quit(); |
| 347 | } |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 348 | |
| 349 | void RunTest_PostDelayedTask_SharedTimer_SubPump() { |
| 350 | MessageLoop loop(MessageLoop::TYPE_UI); |
| 351 | |
| 352 | // Test that the interval of the timer, used to run the next delayed task, is |
| 353 | // set to a value corresponding to when the next delayed task should run. |
| 354 | |
| 355 | // By setting num_tasks to 1, we ensure that the first task to run causes the |
| 356 | // run loop to exit. |
| 357 | int num_tasks = 1; |
| 358 | Time run_time; |
| 359 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 360 | loop.PostTask(FROM_HERE, base::Bind(&SubPumpFunc)); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 361 | |
| 362 | // This very delayed task should never run. |
| 363 | loop.PostDelayedTask( |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 364 | FROM_HERE, |
| 365 | base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 366 | TimeDelta::FromSeconds(1000)); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 367 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 368 | // This slightly delayed task should run from within SubPumpFunc). |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 369 | loop.PostDelayedTask( |
| 370 | FROM_HERE, |
| 371 | base::Bind(&PostQuitMessage, 0), |
| 372 | TimeDelta::FromMilliseconds(10)); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 373 | |
| 374 | Time start_time = Time::Now(); |
| 375 | |
| 376 | loop.Run(); |
| 377 | EXPECT_EQ(1, num_tasks); |
| 378 | |
| 379 | // Ensure that we ran in far less time than the slower timer. |
[email protected] | 4615f198 | 2008-09-23 19:22:33 | [diff] [blame] | 380 | TimeDelta total_time = Time::Now() - start_time; |
| 381 | EXPECT_GT(5000, total_time.InMilliseconds()); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 382 | |
| 383 | // In case both timers somehow run at nearly the same time, sleep a little |
| 384 | // and then run all pending to force them both to have run. This is just |
| 385 | // encouraging flakiness if there is any. |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 386 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 387 | loop.RunAllPending(); |
| 388 | |
| 389 | EXPECT_TRUE(run_time.is_null()); |
| 390 | } |
| 391 | |
| 392 | #endif // defined(OS_WIN) |
| 393 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 394 | // This is used to inject a test point for recording the destructor calls for |
| 395 | // Closure objects send to MessageLoop::PostTask(). It is awkward usage since we |
| 396 | // are trying to hook the actual destruction, which is not a common operation. |
| 397 | class RecordDeletionProbe : public base::RefCounted<RecordDeletionProbe> { |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 398 | public: |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 399 | RecordDeletionProbe(RecordDeletionProbe* post_on_delete, bool* was_deleted) |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 400 | : post_on_delete_(post_on_delete), was_deleted_(was_deleted) { |
| 401 | } |
[email protected] | a9aaa9d1 | 2012-04-25 00:42:51 | [diff] [blame] | 402 | void Run() {} |
| 403 | |
| 404 | private: |
| 405 | friend class base::RefCounted<RecordDeletionProbe>; |
| 406 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 407 | ~RecordDeletionProbe() { |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 408 | *was_deleted_ = true; |
| 409 | if (post_on_delete_) |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 410 | MessageLoop::current()->PostTask( |
| 411 | FROM_HERE, |
| 412 | base::Bind(&RecordDeletionProbe::Run, post_on_delete_.get())); |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 413 | } |
[email protected] | a9aaa9d1 | 2012-04-25 00:42:51 | [diff] [blame] | 414 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 415 | scoped_refptr<RecordDeletionProbe> post_on_delete_; |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 416 | bool* was_deleted_; |
| 417 | }; |
| 418 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 419 | void RunTest_EnsureDeletion(MessageLoop::Type message_loop_type) { |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 420 | bool a_was_deleted = false; |
| 421 | bool b_was_deleted = false; |
| 422 | { |
| 423 | MessageLoop loop(message_loop_type); |
| 424 | loop.PostTask( |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 425 | FROM_HERE, base::Bind(&RecordDeletionProbe::Run, |
| 426 | new RecordDeletionProbe(NULL, &a_was_deleted))); |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 427 | // TODO(ajwong): Do we really need 1000ms here? |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 428 | loop.PostDelayedTask( |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 429 | FROM_HERE, base::Bind(&RecordDeletionProbe::Run, |
| 430 | new RecordDeletionProbe(NULL, &b_was_deleted)), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 431 | TimeDelta::FromMilliseconds(1000)); |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 432 | } |
| 433 | EXPECT_TRUE(a_was_deleted); |
| 434 | EXPECT_TRUE(b_was_deleted); |
| 435 | } |
| 436 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 437 | void RunTest_EnsureDeletion_Chain(MessageLoop::Type message_loop_type) { |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 438 | bool a_was_deleted = false; |
| 439 | bool b_was_deleted = false; |
| 440 | bool c_was_deleted = false; |
| 441 | { |
| 442 | MessageLoop loop(message_loop_type); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 443 | // The scoped_refptr for each of the below is held either by the chained |
| 444 | // RecordDeletionProbe, or the bound RecordDeletionProbe::Run() callback. |
| 445 | RecordDeletionProbe* a = new RecordDeletionProbe(NULL, &a_was_deleted); |
| 446 | RecordDeletionProbe* b = new RecordDeletionProbe(a, &b_was_deleted); |
| 447 | RecordDeletionProbe* c = new RecordDeletionProbe(b, &c_was_deleted); |
| 448 | loop.PostTask(FROM_HERE, base::Bind(&RecordDeletionProbe::Run, c)); |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 449 | } |
| 450 | EXPECT_TRUE(a_was_deleted); |
| 451 | EXPECT_TRUE(b_was_deleted); |
| 452 | EXPECT_TRUE(c_was_deleted); |
| 453 | } |
| 454 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 455 | void NestingFunc(int* depth) { |
| 456 | if (*depth > 0) { |
| 457 | *depth -= 1; |
| 458 | MessageLoop::current()->PostTask(FROM_HERE, |
| 459 | base::Bind(&NestingFunc, depth)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 460 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 461 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 462 | MessageLoop::current()->Run(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 463 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 464 | MessageLoop::current()->Quit(); |
| 465 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 466 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 467 | #if defined(OS_WIN) |
| 468 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 469 | LONG WINAPI BadExceptionHandler(EXCEPTION_POINTERS *ex_info) { |
| 470 | ADD_FAILURE() << "bad exception handler"; |
| 471 | ::ExitProcess(ex_info->ExceptionRecord->ExceptionCode); |
| 472 | return EXCEPTION_EXECUTE_HANDLER; |
| 473 | } |
| 474 | |
| 475 | // This task throws an SEH exception: initially write to an invalid address. |
| 476 | // If the right SEH filter is installed, it will fix the error. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 477 | class Crasher : public base::RefCounted<Crasher> { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 478 | public: |
| 479 | // Ctor. If trash_SEH_handler is true, the task will override the unhandled |
| 480 | // exception handler with one sure to crash this test. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 481 | explicit Crasher(bool trash_SEH_handler) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 482 | : trash_SEH_handler_(trash_SEH_handler) { |
| 483 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 484 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 485 | void Run() { |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 486 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(1)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 487 | if (trash_SEH_handler_) |
| 488 | ::SetUnhandledExceptionFilter(&BadExceptionHandler); |
| 489 | // Generate a SEH fault. We do it in asm to make sure we know how to undo |
| 490 | // the damage. |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 491 | |
| 492 | #if defined(_M_IX86) |
| 493 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 494 | __asm { |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 495 | mov eax, dword ptr [Crasher::bad_array_] |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 496 | mov byte ptr [eax], 66 |
| 497 | } |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 498 | |
| 499 | #elif defined(_M_X64) |
| 500 | |
| 501 | bad_array_[0] = 66; |
| 502 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 503 | #else |
| 504 | #error "needs architecture support" |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 505 | #endif |
| 506 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 507 | MessageLoop::current()->Quit(); |
| 508 | } |
| 509 | // Points the bad array to a valid memory location. |
| 510 | static void FixError() { |
| 511 | bad_array_ = &valid_store_; |
| 512 | } |
| 513 | |
| 514 | private: |
| 515 | bool trash_SEH_handler_; |
| 516 | static volatile char* bad_array_; |
| 517 | static char valid_store_; |
| 518 | }; |
| 519 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 520 | volatile char* Crasher::bad_array_ = 0; |
| 521 | char Crasher::valid_store_ = 0; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 522 | |
| 523 | // This SEH filter fixes the problem and retries execution. Fixing requires |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 524 | // that the last instruction: mov eax, [Crasher::bad_array_] to be retried |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 525 | // so we move the instruction pointer 5 bytes back. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 526 | LONG WINAPI HandleCrasherException(EXCEPTION_POINTERS *ex_info) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 527 | if (ex_info->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION) |
| 528 | return EXCEPTION_EXECUTE_HANDLER; |
| 529 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 530 | Crasher::FixError(); |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 531 | |
| 532 | #if defined(_M_IX86) |
| 533 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 534 | ex_info->ContextRecord->Eip -= 5; |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 535 | |
| 536 | #elif defined(_M_X64) |
| 537 | |
| 538 | ex_info->ContextRecord->Rip -= 5; |
| 539 | |
| 540 | #endif |
| 541 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 542 | return EXCEPTION_CONTINUE_EXECUTION; |
| 543 | } |
| 544 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 545 | void RunTest_Crasher(MessageLoop::Type message_loop_type) { |
| 546 | MessageLoop loop(message_loop_type); |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 547 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 548 | if (::IsDebuggerPresent()) |
| 549 | return; |
| 550 | |
| 551 | LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter = |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 552 | ::SetUnhandledExceptionFilter(&HandleCrasherException); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 553 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 554 | MessageLoop::current()->PostTask( |
| 555 | FROM_HERE, |
| 556 | base::Bind(&Crasher::Run, new Crasher(false))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 557 | MessageLoop::current()->set_exception_restoration(true); |
| 558 | MessageLoop::current()->Run(); |
| 559 | MessageLoop::current()->set_exception_restoration(false); |
| 560 | |
| 561 | ::SetUnhandledExceptionFilter(old_SEH_filter); |
| 562 | } |
| 563 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 564 | void RunTest_CrasherNasty(MessageLoop::Type message_loop_type) { |
| 565 | MessageLoop loop(message_loop_type); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 566 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 567 | if (::IsDebuggerPresent()) |
| 568 | return; |
| 569 | |
| 570 | LPTOP_LEVEL_EXCEPTION_FILTER old_SEH_filter = |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 571 | ::SetUnhandledExceptionFilter(&HandleCrasherException); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 572 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 573 | MessageLoop::current()->PostTask( |
| 574 | FROM_HERE, |
| 575 | base::Bind(&Crasher::Run, new Crasher(true))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 576 | MessageLoop::current()->set_exception_restoration(true); |
| 577 | MessageLoop::current()->Run(); |
| 578 | MessageLoop::current()->set_exception_restoration(false); |
| 579 | |
| 580 | ::SetUnhandledExceptionFilter(old_SEH_filter); |
| 581 | } |
| 582 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 583 | #endif // defined(OS_WIN) |
| 584 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 585 | void RunTest_Nesting(MessageLoop::Type message_loop_type) { |
| 586 | MessageLoop loop(message_loop_type); |
[email protected] | c8887392 | 2008-07-30 13:02:03 | [diff] [blame] | 587 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 588 | int depth = 100; |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 589 | MessageLoop::current()->PostTask(FROM_HERE, |
| 590 | base::Bind(&NestingFunc, &depth)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 591 | MessageLoop::current()->Run(); |
| 592 | EXPECT_EQ(depth, 0); |
| 593 | } |
| 594 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 595 | const wchar_t* const kMessageBoxTitle = L"MessageLoop Unit Test"; |
| 596 | |
| 597 | enum TaskType { |
| 598 | MESSAGEBOX, |
| 599 | ENDDIALOG, |
| 600 | RECURSIVE, |
| 601 | TIMEDMESSAGELOOP, |
| 602 | QUITMESSAGELOOP, |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 603 | ORDERED, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 604 | PUMPS, |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 605 | SLEEP, |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 606 | RUNS, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 607 | }; |
| 608 | |
| 609 | // Saves the order in which the tasks executed. |
| 610 | struct TaskItem { |
| 611 | TaskItem(TaskType t, int c, bool s) |
| 612 | : type(t), |
| 613 | cookie(c), |
| 614 | start(s) { |
| 615 | } |
| 616 | |
| 617 | TaskType type; |
| 618 | int cookie; |
| 619 | bool start; |
| 620 | |
| 621 | bool operator == (const TaskItem& other) const { |
| 622 | return type == other.type && cookie == other.cookie && start == other.start; |
| 623 | } |
| 624 | }; |
| 625 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 626 | std::ostream& operator <<(std::ostream& os, TaskType type) { |
| 627 | switch (type) { |
| 628 | case MESSAGEBOX: os << "MESSAGEBOX"; break; |
| 629 | case ENDDIALOG: os << "ENDDIALOG"; break; |
| 630 | case RECURSIVE: os << "RECURSIVE"; break; |
| 631 | case TIMEDMESSAGELOOP: os << "TIMEDMESSAGELOOP"; break; |
| 632 | case QUITMESSAGELOOP: os << "QUITMESSAGELOOP"; break; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 633 | case ORDERED: os << "ORDERED"; break; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 634 | case PUMPS: os << "PUMPS"; break; |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 635 | case SLEEP: os << "SLEEP"; break; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 636 | default: |
| 637 | NOTREACHED(); |
| 638 | os << "Unknown TaskType"; |
| 639 | break; |
| 640 | } |
| 641 | return os; |
| 642 | } |
| 643 | |
| 644 | std::ostream& operator <<(std::ostream& os, const TaskItem& item) { |
| 645 | if (item.start) |
| 646 | return os << item.type << " " << item.cookie << " starts"; |
| 647 | else |
| 648 | return os << item.type << " " << item.cookie << " ends"; |
| 649 | } |
| 650 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 651 | class TaskList { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 652 | public: |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 653 | void RecordStart(TaskType type, int cookie) { |
| 654 | TaskItem item(type, cookie, true); |
[email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 655 | DVLOG(1) << item; |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 656 | task_list_.push_back(item); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 657 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 658 | |
| 659 | void RecordEnd(TaskType type, int cookie) { |
| 660 | TaskItem item(type, cookie, false); |
[email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 661 | DVLOG(1) << item; |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 662 | task_list_.push_back(item); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 663 | } |
| 664 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 665 | size_t Size() { |
| 666 | return task_list_.size(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 667 | } |
| 668 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 669 | TaskItem Get(int n) { |
| 670 | return task_list_[n]; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | private: |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 674 | std::vector<TaskItem> task_list_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 675 | }; |
| 676 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 677 | // Saves the order the tasks ran. |
| 678 | void OrderedFunc(TaskList* order, int cookie) { |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 679 | order->RecordStart(ORDERED, cookie); |
| 680 | order->RecordEnd(ORDERED, cookie); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 681 | } |
| 682 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 683 | #if defined(OS_WIN) |
| 684 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 685 | // MessageLoop implicitly start a "modal message loop". Modal dialog boxes, |
| 686 | // common controls (like OpenFile) and StartDoc printing function can cause |
| 687 | // implicit message loops. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 688 | void MessageBoxFunc(TaskList* order, int cookie, bool is_reentrant) { |
| 689 | order->RecordStart(MESSAGEBOX, cookie); |
| 690 | if (is_reentrant) |
| 691 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 692 | MessageBox(NULL, L"Please wait...", kMessageBoxTitle, MB_OK); |
| 693 | order->RecordEnd(MESSAGEBOX, cookie); |
| 694 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 695 | |
| 696 | // Will end the MessageBox. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 697 | void EndDialogFunc(TaskList* order, int cookie) { |
| 698 | order->RecordStart(ENDDIALOG, cookie); |
| 699 | HWND window = GetActiveWindow(); |
| 700 | if (window != NULL) { |
| 701 | EXPECT_NE(EndDialog(window, IDCONTINUE), 0); |
| 702 | // Cheap way to signal that the window wasn't found if RunEnd() isn't |
| 703 | // called. |
| 704 | order->RecordEnd(ENDDIALOG, cookie); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 705 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 706 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 707 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 708 | #endif // defined(OS_WIN) |
| 709 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 710 | void RecursiveFunc(TaskList* order, int cookie, int depth, |
| 711 | bool is_reentrant) { |
| 712 | order->RecordStart(RECURSIVE, cookie); |
| 713 | if (depth > 0) { |
| 714 | if (is_reentrant) |
| 715 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 716 | MessageLoop::current()->PostTask( |
| 717 | FROM_HERE, |
| 718 | base::Bind(&RecursiveFunc, order, cookie, depth - 1, is_reentrant)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 719 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 720 | order->RecordEnd(RECURSIVE, cookie); |
| 721 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 722 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 723 | void RecursiveSlowFunc(TaskList* order, int cookie, int depth, |
| 724 | bool is_reentrant) { |
| 725 | RecursiveFunc(order, cookie, depth, is_reentrant); |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 726 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(10)); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 727 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 728 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 729 | void QuitFunc(TaskList* order, int cookie) { |
| 730 | order->RecordStart(QUITMESSAGELOOP, cookie); |
| 731 | MessageLoop::current()->Quit(); |
| 732 | order->RecordEnd(QUITMESSAGELOOP, cookie); |
| 733 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 734 | |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 735 | void SleepFunc(TaskList* order, int cookie, TimeDelta delay) { |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 736 | order->RecordStart(SLEEP, cookie); |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 737 | PlatformThread::Sleep(delay); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 738 | order->RecordEnd(SLEEP, cookie); |
| 739 | } |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 740 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 741 | #if defined(OS_WIN) |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 742 | void RecursiveFuncWin(MessageLoop* target, |
| 743 | HANDLE event, |
| 744 | bool expect_window, |
| 745 | TaskList* order, |
| 746 | bool is_reentrant) { |
| 747 | target->PostTask(FROM_HERE, |
| 748 | base::Bind(&RecursiveFunc, order, 1, 2, is_reentrant)); |
| 749 | target->PostTask(FROM_HERE, |
| 750 | base::Bind(&MessageBoxFunc, order, 2, is_reentrant)); |
| 751 | target->PostTask(FROM_HERE, |
| 752 | base::Bind(&RecursiveFunc, order, 3, 2, is_reentrant)); |
| 753 | // The trick here is that for recursive task processing, this task will be |
| 754 | // ran _inside_ the MessageBox message loop, dismissing the MessageBox |
| 755 | // without a chance. |
| 756 | // For non-recursive task processing, this will be executed _after_ the |
| 757 | // MessageBox will have been dismissed by the code below, where |
| 758 | // expect_window_ is true. |
| 759 | target->PostTask(FROM_HERE, |
| 760 | base::Bind(&EndDialogFunc, order, 4)); |
| 761 | target->PostTask(FROM_HERE, |
| 762 | base::Bind(&QuitFunc, order, 5)); |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 763 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 764 | // Enforce that every tasks are sent before starting to run the main thread |
| 765 | // message loop. |
| 766 | ASSERT_TRUE(SetEvent(event)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 767 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 768 | // Poll for the MessageBox. Don't do this at home! At the speed we do it, |
| 769 | // you will never realize one MessageBox was shown. |
| 770 | for (; expect_window;) { |
| 771 | HWND window = FindWindow(L"#32770", kMessageBoxTitle); |
| 772 | if (window) { |
| 773 | // Dismiss it. |
| 774 | for (;;) { |
| 775 | HWND button = FindWindowEx(window, NULL, L"Button", NULL); |
| 776 | if (button != NULL) { |
| 777 | EXPECT_EQ(0, SendMessage(button, WM_LBUTTONDOWN, 0, 0)); |
| 778 | EXPECT_EQ(0, SendMessage(button, WM_LBUTTONUP, 0, 0)); |
| 779 | break; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 780 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 781 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 782 | break; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 783 | } |
| 784 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 785 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 786 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 787 | #endif // defined(OS_WIN) |
| 788 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 789 | void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) { |
| 790 | MessageLoop loop(message_loop_type); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 791 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 792 | EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); |
| 793 | TaskList order; |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 794 | MessageLoop::current()->PostTask( |
| 795 | FROM_HERE, |
| 796 | base::Bind(&RecursiveFunc, &order, 1, 2, false)); |
| 797 | MessageLoop::current()->PostTask( |
| 798 | FROM_HERE, |
| 799 | base::Bind(&RecursiveFunc, &order, 2, 2, false)); |
| 800 | MessageLoop::current()->PostTask( |
| 801 | FROM_HERE, |
| 802 | base::Bind(&QuitFunc, &order, 3)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 803 | |
| 804 | MessageLoop::current()->Run(); |
| 805 | |
| 806 | // FIFO order. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 807 | ASSERT_EQ(14U, order.Size()); |
| 808 | EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); |
| 809 | EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); |
| 810 | EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); |
| 811 | EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); |
| 812 | EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); |
| 813 | EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); |
| 814 | EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true)); |
| 815 | EXPECT_EQ(order.Get(7), TaskItem(RECURSIVE, 1, false)); |
| 816 | EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true)); |
| 817 | EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false)); |
| 818 | EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true)); |
| 819 | EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false)); |
| 820 | EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true)); |
| 821 | EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 822 | } |
| 823 | |
[email protected] | 4554c23 | 2011-02-17 19:25:04 | [diff] [blame] | 824 | void RunTest_RecursiveDenial3(MessageLoop::Type message_loop_type) { |
| 825 | MessageLoop loop(message_loop_type); |
| 826 | |
| 827 | EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); |
| 828 | TaskList order; |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 829 | MessageLoop::current()->PostTask( |
| 830 | FROM_HERE, base::Bind(&RecursiveSlowFunc, &order, 1, 2, false)); |
| 831 | MessageLoop::current()->PostTask( |
| 832 | FROM_HERE, base::Bind(&RecursiveSlowFunc, &order, 2, 2, false)); |
| 833 | MessageLoop::current()->PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 834 | FROM_HERE, |
| 835 | base::Bind(&OrderedFunc, &order, 3), |
| 836 | TimeDelta::FromMilliseconds(5)); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 837 | MessageLoop::current()->PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 838 | FROM_HERE, |
| 839 | base::Bind(&QuitFunc, &order, 4), |
| 840 | TimeDelta::FromMilliseconds(5)); |
[email protected] | 4554c23 | 2011-02-17 19:25:04 | [diff] [blame] | 841 | |
| 842 | MessageLoop::current()->Run(); |
| 843 | |
| 844 | // FIFO order. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 845 | ASSERT_EQ(16U, order.Size()); |
| 846 | EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); |
| 847 | EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); |
| 848 | EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); |
| 849 | EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); |
| 850 | EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 1, true)); |
| 851 | EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 1, false)); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 852 | EXPECT_EQ(order.Get(6), TaskItem(ORDERED, 3, true)); |
| 853 | EXPECT_EQ(order.Get(7), TaskItem(ORDERED, 3, false)); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 854 | EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true)); |
| 855 | EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false)); |
| 856 | EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 4, true)); |
| 857 | EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 4, false)); |
| 858 | EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 1, true)); |
| 859 | EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, false)); |
| 860 | EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 2, true)); |
| 861 | EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 2, false)); |
[email protected] | 4554c23 | 2011-02-17 19:25:04 | [diff] [blame] | 862 | } |
| 863 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 864 | void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type) { |
| 865 | MessageLoop loop(message_loop_type); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 866 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 867 | TaskList order; |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 868 | MessageLoop::current()->PostTask( |
| 869 | FROM_HERE, base::Bind(&RecursiveFunc, &order, 1, 2, true)); |
| 870 | MessageLoop::current()->PostTask( |
| 871 | FROM_HERE, base::Bind(&RecursiveFunc, &order, 2, 2, true)); |
| 872 | MessageLoop::current()->PostTask( |
| 873 | FROM_HERE, base::Bind(&QuitFunc, &order, 3)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 874 | |
| 875 | MessageLoop::current()->Run(); |
| 876 | |
| 877 | // FIFO order. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 878 | ASSERT_EQ(14U, order.Size()); |
| 879 | EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); |
| 880 | EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); |
| 881 | EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); |
| 882 | EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); |
| 883 | EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); |
| 884 | EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); |
| 885 | EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true)); |
| 886 | EXPECT_EQ(order.Get(7), TaskItem(RECURSIVE, 1, false)); |
| 887 | EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true)); |
| 888 | EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false)); |
| 889 | EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true)); |
| 890 | EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false)); |
| 891 | EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 2, true)); |
| 892 | EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 2, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 893 | } |
| 894 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 895 | #if defined(OS_WIN) |
| 896 | // TODO(darin): These tests need to be ported since they test critical |
| 897 | // message loop functionality. |
| 898 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 899 | // A side effect of this test is the generation a beep. Sorry. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 900 | void RunTest_RecursiveDenial2(MessageLoop::Type message_loop_type) { |
| 901 | MessageLoop loop(message_loop_type); |
| 902 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 903 | Thread worker("RecursiveDenial2_worker"); |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 904 | Thread::Options options; |
| 905 | options.message_loop_type = message_loop_type; |
| 906 | ASSERT_EQ(true, worker.StartWithOptions(options)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 907 | TaskList order; |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 908 | base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 909 | worker.message_loop()->PostTask(FROM_HERE, |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 910 | base::Bind(&RecursiveFuncWin, |
| 911 | MessageLoop::current(), |
| 912 | event.Get(), |
| 913 | true, |
| 914 | &order, |
| 915 | false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 916 | // Let the other thread execute. |
| 917 | WaitForSingleObject(event, INFINITE); |
| 918 | MessageLoop::current()->Run(); |
| 919 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 920 | ASSERT_EQ(order.Size(), 17); |
| 921 | EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); |
| 922 | EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); |
| 923 | EXPECT_EQ(order.Get(2), TaskItem(MESSAGEBOX, 2, true)); |
| 924 | EXPECT_EQ(order.Get(3), TaskItem(MESSAGEBOX, 2, false)); |
| 925 | EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 3, true)); |
| 926 | EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 3, false)); |
| 927 | // When EndDialogFunc is processed, the window is already dismissed, hence no |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 928 | // "end" entry. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 929 | EXPECT_EQ(order.Get(6), TaskItem(ENDDIALOG, 4, true)); |
| 930 | EXPECT_EQ(order.Get(7), TaskItem(QUITMESSAGELOOP, 5, true)); |
| 931 | EXPECT_EQ(order.Get(8), TaskItem(QUITMESSAGELOOP, 5, false)); |
| 932 | EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 1, true)); |
| 933 | EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, false)); |
| 934 | EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 3, true)); |
| 935 | EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 3, false)); |
| 936 | EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, true)); |
| 937 | EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 1, false)); |
| 938 | EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 3, true)); |
| 939 | EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 940 | } |
| 941 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 942 | // A side effect of this test is the generation a beep. Sorry. This test also |
| 943 | // needs to process windows messages on the current thread. |
| 944 | void RunTest_RecursiveSupport2(MessageLoop::Type message_loop_type) { |
| 945 | MessageLoop loop(message_loop_type); |
| 946 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 947 | Thread worker("RecursiveSupport2_worker"); |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 948 | Thread::Options options; |
| 949 | options.message_loop_type = message_loop_type; |
| 950 | ASSERT_EQ(true, worker.StartWithOptions(options)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 951 | TaskList order; |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 952 | base::win::ScopedHandle event(CreateEvent(NULL, FALSE, FALSE, NULL)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 953 | worker.message_loop()->PostTask(FROM_HERE, |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 954 | base::Bind(&RecursiveFuncWin, |
| 955 | MessageLoop::current(), |
| 956 | event.Get(), |
| 957 | false, |
| 958 | &order, |
| 959 | true)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 960 | // Let the other thread execute. |
| 961 | WaitForSingleObject(event, INFINITE); |
| 962 | MessageLoop::current()->Run(); |
| 963 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 964 | ASSERT_EQ(order.Size(), 18); |
| 965 | EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); |
| 966 | EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); |
| 967 | EXPECT_EQ(order.Get(2), TaskItem(MESSAGEBOX, 2, true)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 968 | // Note that this executes in the MessageBox modal loop. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 969 | EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 3, true)); |
| 970 | EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 3, false)); |
| 971 | EXPECT_EQ(order.Get(5), TaskItem(ENDDIALOG, 4, true)); |
| 972 | EXPECT_EQ(order.Get(6), TaskItem(ENDDIALOG, 4, false)); |
| 973 | EXPECT_EQ(order.Get(7), TaskItem(MESSAGEBOX, 2, false)); |
| 974 | /* The order can subtly change here. The reason is that when RecursiveFunc(1) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 975 | is called in the main thread, if it is faster than getting to the |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 976 | PostTask(FROM_HERE, base::Bind(&QuitFunc) execution, the order of task |
| 977 | execution can change. We don't care anyway that the order isn't correct. |
| 978 | EXPECT_EQ(order.Get(8), TaskItem(QUITMESSAGELOOP, 5, true)); |
| 979 | EXPECT_EQ(order.Get(9), TaskItem(QUITMESSAGELOOP, 5, false)); |
| 980 | EXPECT_EQ(order.Get(10), TaskItem(RECURSIVE, 1, true)); |
| 981 | EXPECT_EQ(order.Get(11), TaskItem(RECURSIVE, 1, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 982 | */ |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 983 | EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 3, true)); |
| 984 | EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 3, false)); |
| 985 | EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 1, true)); |
| 986 | EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 1, false)); |
| 987 | EXPECT_EQ(order.Get(16), TaskItem(RECURSIVE, 3, true)); |
| 988 | EXPECT_EQ(order.Get(17), TaskItem(RECURSIVE, 3, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 989 | } |
| 990 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 991 | #endif // defined(OS_WIN) |
| 992 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 993 | void FuncThatPumps(TaskList* order, int cookie) { |
| 994 | order->RecordStart(PUMPS, cookie); |
[email protected] | b5717a4 | 2012-02-14 19:33:52 | [diff] [blame] | 995 | { |
| 996 | MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 997 | MessageLoop::current()->RunAllPending(); |
| 998 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 999 | order->RecordEnd(PUMPS, cookie); |
| 1000 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1001 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1002 | void FuncThatRuns(TaskList* order, int cookie, base::RunLoop* run_loop) { |
| 1003 | order->RecordStart(RUNS, cookie); |
| 1004 | { |
| 1005 | MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 1006 | run_loop->Run(); |
| 1007 | } |
| 1008 | order->RecordEnd(RUNS, cookie); |
| 1009 | } |
| 1010 | |
| 1011 | void FuncThatQuitsNow() { |
| 1012 | MessageLoop::current()->QuitNow(); |
| 1013 | } |
| 1014 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1015 | // Tests that non nestable tasks run in FIFO if there are no nested loops. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1016 | void RunTest_NonNestableWithNoNesting( |
| 1017 | MessageLoop::Type message_loop_type) { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1018 | MessageLoop loop(message_loop_type); |
| 1019 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1020 | TaskList order; |
| 1021 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1022 | MessageLoop::current()->PostNonNestableTask( |
| 1023 | FROM_HERE, |
| 1024 | base::Bind(&OrderedFunc, &order, 1)); |
| 1025 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1026 | base::Bind(&OrderedFunc, &order, 2)); |
| 1027 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1028 | base::Bind(&QuitFunc, &order, 3)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1029 | MessageLoop::current()->Run(); |
| 1030 | |
| 1031 | // FIFO order. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1032 | ASSERT_EQ(6U, order.Size()); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1033 | EXPECT_EQ(order.Get(0), TaskItem(ORDERED, 1, true)); |
| 1034 | EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 1, false)); |
| 1035 | EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 2, true)); |
| 1036 | EXPECT_EQ(order.Get(3), TaskItem(ORDERED, 2, false)); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1037 | EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); |
| 1038 | EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | // 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] | 1042 | void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type, |
| 1043 | bool use_delayed) { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1044 | MessageLoop loop(message_loop_type); |
| 1045 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1046 | TaskList order; |
| 1047 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1048 | MessageLoop::current()->PostTask( |
| 1049 | FROM_HERE, |
| 1050 | base::Bind(&FuncThatPumps, &order, 1)); |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1051 | if (use_delayed) { |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1052 | MessageLoop::current()->PostNonNestableDelayedTask( |
| 1053 | FROM_HERE, |
| 1054 | base::Bind(&OrderedFunc, &order, 2), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 1055 | TimeDelta::FromMilliseconds(1)); |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1056 | } else { |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1057 | MessageLoop::current()->PostNonNestableTask( |
| 1058 | FROM_HERE, |
| 1059 | base::Bind(&OrderedFunc, &order, 2)); |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1060 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1061 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1062 | base::Bind(&OrderedFunc, &order, 3)); |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 1063 | MessageLoop::current()->PostTask( |
| 1064 | FROM_HERE, |
| 1065 | base::Bind(&SleepFunc, &order, 4, TimeDelta::FromMilliseconds(50))); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1066 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1067 | base::Bind(&OrderedFunc, &order, 5)); |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1068 | if (use_delayed) { |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1069 | MessageLoop::current()->PostNonNestableDelayedTask( |
| 1070 | FROM_HERE, |
| 1071 | base::Bind(&QuitFunc, &order, 6), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 1072 | TimeDelta::FromMilliseconds(2)); |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1073 | } else { |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1074 | MessageLoop::current()->PostNonNestableTask( |
| 1075 | FROM_HERE, |
| 1076 | base::Bind(&QuitFunc, &order, 6)); |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1077 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1078 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1079 | MessageLoop::current()->Run(); |
| 1080 | |
| 1081 | // FIFO order. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1082 | ASSERT_EQ(12U, order.Size()); |
| 1083 | EXPECT_EQ(order.Get(0), TaskItem(PUMPS, 1, true)); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1084 | EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 3, true)); |
| 1085 | EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 3, false)); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1086 | EXPECT_EQ(order.Get(3), TaskItem(SLEEP, 4, true)); |
| 1087 | EXPECT_EQ(order.Get(4), TaskItem(SLEEP, 4, false)); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1088 | EXPECT_EQ(order.Get(5), TaskItem(ORDERED, 5, true)); |
| 1089 | EXPECT_EQ(order.Get(6), TaskItem(ORDERED, 5, false)); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1090 | EXPECT_EQ(order.Get(7), TaskItem(PUMPS, 1, false)); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1091 | EXPECT_EQ(order.Get(8), TaskItem(ORDERED, 2, true)); |
| 1092 | EXPECT_EQ(order.Get(9), TaskItem(ORDERED, 2, false)); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1093 | EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 6, true)); |
| 1094 | EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 6, false)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1095 | } |
| 1096 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1097 | // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. |
| 1098 | void RunTest_QuitNow(MessageLoop::Type message_loop_type) { |
| 1099 | MessageLoop loop(message_loop_type); |
| 1100 | |
| 1101 | TaskList order; |
| 1102 | |
| 1103 | base::RunLoop run_loop; |
| 1104 | |
| 1105 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1106 | base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&run_loop))); |
| 1107 | MessageLoop::current()->PostTask( |
| 1108 | FROM_HERE, base::Bind(&OrderedFunc, &order, 2)); |
| 1109 | MessageLoop::current()->PostTask( |
| 1110 | FROM_HERE, base::Bind(&FuncThatQuitsNow)); |
| 1111 | MessageLoop::current()->PostTask( |
| 1112 | FROM_HERE, base::Bind(&OrderedFunc, &order, 3)); |
| 1113 | MessageLoop::current()->PostTask( |
| 1114 | FROM_HERE, base::Bind(&FuncThatQuitsNow)); |
| 1115 | MessageLoop::current()->PostTask( |
| 1116 | FROM_HERE, base::Bind(&OrderedFunc, &order, 4)); // never runs |
| 1117 | |
| 1118 | MessageLoop::current()->Run(); |
| 1119 | |
| 1120 | ASSERT_EQ(6U, order.Size()); |
| 1121 | int task_index = 0; |
| 1122 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 1123 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); |
| 1124 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); |
| 1125 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 1126 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true)); |
| 1127 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false)); |
| 1128 | EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 1129 | } |
| 1130 | |
| 1131 | // Tests RunLoopQuit works before RunWithID. |
| 1132 | void RunTest_RunLoopQuitOrderBefore(MessageLoop::Type message_loop_type) { |
| 1133 | MessageLoop loop(message_loop_type); |
| 1134 | |
| 1135 | TaskList order; |
| 1136 | |
| 1137 | base::RunLoop run_loop; |
| 1138 | |
| 1139 | run_loop.Quit(); |
| 1140 | |
| 1141 | MessageLoop::current()->PostTask( |
| 1142 | FROM_HERE, base::Bind(&OrderedFunc, &order, 1)); // never runs |
| 1143 | MessageLoop::current()->PostTask( |
| 1144 | FROM_HERE, base::Bind(&FuncThatQuitsNow)); // never runs |
| 1145 | |
| 1146 | run_loop.Run(); |
| 1147 | |
| 1148 | ASSERT_EQ(0U, order.Size()); |
| 1149 | } |
| 1150 | |
| 1151 | // Tests RunLoopQuit works during RunWithID. |
| 1152 | void RunTest_RunLoopQuitOrderDuring(MessageLoop::Type message_loop_type) { |
| 1153 | MessageLoop loop(message_loop_type); |
| 1154 | |
| 1155 | TaskList order; |
| 1156 | |
| 1157 | base::RunLoop run_loop; |
| 1158 | |
| 1159 | MessageLoop::current()->PostTask( |
| 1160 | FROM_HERE, base::Bind(&OrderedFunc, &order, 1)); |
| 1161 | MessageLoop::current()->PostTask( |
| 1162 | FROM_HERE, run_loop.QuitClosure()); |
| 1163 | MessageLoop::current()->PostTask( |
| 1164 | FROM_HERE, base::Bind(&OrderedFunc, &order, 2)); // never runs |
| 1165 | MessageLoop::current()->PostTask( |
| 1166 | FROM_HERE, base::Bind(&FuncThatQuitsNow)); // never runs |
| 1167 | |
| 1168 | run_loop.Run(); |
| 1169 | |
| 1170 | ASSERT_EQ(2U, order.Size()); |
| 1171 | int task_index = 0; |
| 1172 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, true)); |
| 1173 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, false)); |
| 1174 | EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 1175 | } |
| 1176 | |
| 1177 | // Tests RunLoopQuit works after RunWithID. |
| 1178 | void RunTest_RunLoopQuitOrderAfter(MessageLoop::Type message_loop_type) { |
| 1179 | MessageLoop loop(message_loop_type); |
| 1180 | |
| 1181 | TaskList order; |
| 1182 | |
| 1183 | base::RunLoop run_loop; |
| 1184 | |
| 1185 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1186 | base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&run_loop))); |
| 1187 | MessageLoop::current()->PostTask( |
| 1188 | FROM_HERE, base::Bind(&OrderedFunc, &order, 2)); |
| 1189 | MessageLoop::current()->PostTask( |
| 1190 | FROM_HERE, base::Bind(&FuncThatQuitsNow)); |
| 1191 | MessageLoop::current()->PostTask( |
| 1192 | FROM_HERE, base::Bind(&OrderedFunc, &order, 3)); |
| 1193 | MessageLoop::current()->PostTask( |
| 1194 | FROM_HERE, run_loop.QuitClosure()); // has no affect |
| 1195 | MessageLoop::current()->PostTask( |
| 1196 | FROM_HERE, base::Bind(&OrderedFunc, &order, 4)); |
| 1197 | MessageLoop::current()->PostTask( |
| 1198 | FROM_HERE, base::Bind(&FuncThatQuitsNow)); |
| 1199 | |
| 1200 | base::RunLoop outer_run_loop; |
| 1201 | outer_run_loop.Run(); |
| 1202 | |
| 1203 | ASSERT_EQ(8U, order.Size()); |
| 1204 | int task_index = 0; |
| 1205 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 1206 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); |
| 1207 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); |
| 1208 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 1209 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true)); |
| 1210 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false)); |
| 1211 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, true)); |
| 1212 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, false)); |
| 1213 | EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 1214 | } |
| 1215 | |
| 1216 | // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. |
| 1217 | void RunTest_RunLoopQuitTop(MessageLoop::Type message_loop_type) { |
| 1218 | MessageLoop loop(message_loop_type); |
| 1219 | |
| 1220 | TaskList order; |
| 1221 | |
| 1222 | base::RunLoop outer_run_loop; |
| 1223 | base::RunLoop nested_run_loop; |
| 1224 | |
| 1225 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1226 | base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&nested_run_loop))); |
| 1227 | MessageLoop::current()->PostTask( |
| 1228 | FROM_HERE, outer_run_loop.QuitClosure()); |
| 1229 | MessageLoop::current()->PostTask( |
| 1230 | FROM_HERE, base::Bind(&OrderedFunc, &order, 2)); |
| 1231 | MessageLoop::current()->PostTask( |
| 1232 | FROM_HERE, nested_run_loop.QuitClosure()); |
| 1233 | |
| 1234 | outer_run_loop.Run(); |
| 1235 | |
| 1236 | ASSERT_EQ(4U, order.Size()); |
| 1237 | int task_index = 0; |
| 1238 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 1239 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); |
| 1240 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); |
| 1241 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 1242 | EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 1243 | } |
| 1244 | |
| 1245 | // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. |
| 1246 | void RunTest_RunLoopQuitNested(MessageLoop::Type message_loop_type) { |
| 1247 | MessageLoop loop(message_loop_type); |
| 1248 | |
| 1249 | TaskList order; |
| 1250 | |
| 1251 | base::RunLoop outer_run_loop; |
| 1252 | base::RunLoop nested_run_loop; |
| 1253 | |
| 1254 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1255 | base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&nested_run_loop))); |
| 1256 | MessageLoop::current()->PostTask( |
| 1257 | FROM_HERE, nested_run_loop.QuitClosure()); |
| 1258 | MessageLoop::current()->PostTask( |
| 1259 | FROM_HERE, base::Bind(&OrderedFunc, &order, 2)); |
| 1260 | MessageLoop::current()->PostTask( |
| 1261 | FROM_HERE, outer_run_loop.QuitClosure()); |
| 1262 | |
| 1263 | outer_run_loop.Run(); |
| 1264 | |
| 1265 | ASSERT_EQ(4U, order.Size()); |
| 1266 | int task_index = 0; |
| 1267 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 1268 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 1269 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); |
| 1270 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); |
| 1271 | EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 1272 | } |
| 1273 | |
| 1274 | // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. |
| 1275 | void RunTest_RunLoopQuitBogus(MessageLoop::Type message_loop_type) { |
| 1276 | MessageLoop loop(message_loop_type); |
| 1277 | |
| 1278 | TaskList order; |
| 1279 | |
| 1280 | base::RunLoop outer_run_loop; |
| 1281 | base::RunLoop nested_run_loop; |
| 1282 | base::RunLoop bogus_run_loop; |
| 1283 | |
| 1284 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1285 | base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&nested_run_loop))); |
| 1286 | MessageLoop::current()->PostTask( |
| 1287 | FROM_HERE, bogus_run_loop.QuitClosure()); |
| 1288 | MessageLoop::current()->PostTask( |
| 1289 | FROM_HERE, base::Bind(&OrderedFunc, &order, 2)); |
| 1290 | MessageLoop::current()->PostTask( |
| 1291 | FROM_HERE, outer_run_loop.QuitClosure()); |
| 1292 | MessageLoop::current()->PostTask( |
| 1293 | FROM_HERE, nested_run_loop.QuitClosure()); |
| 1294 | |
| 1295 | outer_run_loop.Run(); |
| 1296 | |
| 1297 | ASSERT_EQ(4U, order.Size()); |
| 1298 | int task_index = 0; |
| 1299 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 1300 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); |
| 1301 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); |
| 1302 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 1303 | EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 1304 | } |
| 1305 | |
| 1306 | // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. |
| 1307 | void RunTest_RunLoopQuitDeep(MessageLoop::Type message_loop_type) { |
| 1308 | MessageLoop loop(message_loop_type); |
| 1309 | |
| 1310 | TaskList order; |
| 1311 | |
| 1312 | base::RunLoop outer_run_loop; |
| 1313 | base::RunLoop nested_loop1; |
| 1314 | base::RunLoop nested_loop2; |
| 1315 | base::RunLoop nested_loop3; |
| 1316 | base::RunLoop nested_loop4; |
| 1317 | |
| 1318 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1319 | base::Bind(&FuncThatRuns, &order, 1, base::Unretained(&nested_loop1))); |
| 1320 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1321 | base::Bind(&FuncThatRuns, &order, 2, base::Unretained(&nested_loop2))); |
| 1322 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1323 | base::Bind(&FuncThatRuns, &order, 3, base::Unretained(&nested_loop3))); |
| 1324 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1325 | base::Bind(&FuncThatRuns, &order, 4, base::Unretained(&nested_loop4))); |
| 1326 | MessageLoop::current()->PostTask( |
| 1327 | FROM_HERE, base::Bind(&OrderedFunc, &order, 5)); |
| 1328 | MessageLoop::current()->PostTask( |
| 1329 | FROM_HERE, outer_run_loop.QuitClosure()); |
| 1330 | MessageLoop::current()->PostTask( |
| 1331 | FROM_HERE, base::Bind(&OrderedFunc, &order, 6)); |
| 1332 | MessageLoop::current()->PostTask( |
| 1333 | FROM_HERE, nested_loop1.QuitClosure()); |
| 1334 | MessageLoop::current()->PostTask( |
| 1335 | FROM_HERE, base::Bind(&OrderedFunc, &order, 7)); |
| 1336 | MessageLoop::current()->PostTask( |
| 1337 | FROM_HERE, nested_loop2.QuitClosure()); |
| 1338 | MessageLoop::current()->PostTask( |
| 1339 | FROM_HERE, base::Bind(&OrderedFunc, &order, 8)); |
| 1340 | MessageLoop::current()->PostTask( |
| 1341 | FROM_HERE, nested_loop3.QuitClosure()); |
| 1342 | MessageLoop::current()->PostTask( |
| 1343 | FROM_HERE, base::Bind(&OrderedFunc, &order, 9)); |
| 1344 | MessageLoop::current()->PostTask( |
| 1345 | FROM_HERE, nested_loop4.QuitClosure()); |
| 1346 | MessageLoop::current()->PostTask( |
| 1347 | FROM_HERE, base::Bind(&OrderedFunc, &order, 10)); |
| 1348 | |
| 1349 | outer_run_loop.Run(); |
| 1350 | |
| 1351 | ASSERT_EQ(18U, order.Size()); |
| 1352 | int task_index = 0; |
| 1353 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 1354 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 2, true)); |
| 1355 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 3, true)); |
| 1356 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 4, true)); |
| 1357 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 5, true)); |
| 1358 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 5, false)); |
| 1359 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 6, true)); |
| 1360 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 6, false)); |
| 1361 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 7, true)); |
| 1362 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 7, false)); |
| 1363 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 8, true)); |
| 1364 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 8, false)); |
| 1365 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 9, true)); |
| 1366 | EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 9, false)); |
| 1367 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 4, false)); |
| 1368 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 3, false)); |
| 1369 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 2, false)); |
| 1370 | EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 1371 | EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 1372 | } |
| 1373 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 1374 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1375 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1376 | class DispatcherImpl : public MessageLoopForUI::Dispatcher { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1377 | public: |
| 1378 | DispatcherImpl() : dispatch_count_(0) {} |
| 1379 | |
[email protected] | 046460d | 2012-04-09 19:50:06 | [diff] [blame] | 1380 | virtual bool Dispatch(const base::NativeEvent& msg) OVERRIDE { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1381 | ::TranslateMessage(&msg); |
| 1382 | ::DispatchMessage(&msg); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1383 | // Do not count WM_TIMER since it is not what we post and it will cause |
| 1384 | // flakiness. |
| 1385 | if (msg.message != WM_TIMER) |
| 1386 | ++dispatch_count_; |
| 1387 | // We treat WM_LBUTTONUP as the last message. |
| 1388 | return msg.message != WM_LBUTTONUP; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | int dispatch_count_; |
| 1392 | }; |
| 1393 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1394 | void MouseDownUp() { |
| 1395 | PostMessage(NULL, WM_LBUTTONDOWN, 0, 0); |
| 1396 | PostMessage(NULL, WM_LBUTTONUP, 'A', 0); |
| 1397 | } |
| 1398 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1399 | void RunTest_Dispatcher(MessageLoop::Type message_loop_type) { |
| 1400 | MessageLoop loop(message_loop_type); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1401 | |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 1402 | MessageLoop::current()->PostDelayedTask( |
| 1403 | FROM_HERE, |
| 1404 | base::Bind(&MouseDownUp), |
| 1405 | TimeDelta::FromMilliseconds(100)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1406 | DispatcherImpl dispatcher; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1407 | base::RunLoop run_loop(&dispatcher); |
| 1408 | run_loop.Run(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1409 | ASSERT_EQ(2, dispatcher.dispatch_count_); |
| 1410 | } |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 1411 | |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1412 | LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) { |
| 1413 | if (code == base::MessagePumpForUI::kMessageFilterCode) { |
| 1414 | MSG* msg = reinterpret_cast<MSG*>(lparam); |
| 1415 | if (msg->message == WM_LBUTTONDOWN) |
| 1416 | return TRUE; |
| 1417 | } |
| 1418 | return FALSE; |
| 1419 | } |
| 1420 | |
| 1421 | void RunTest_DispatcherWithMessageHook(MessageLoop::Type message_loop_type) { |
| 1422 | MessageLoop loop(message_loop_type); |
| 1423 | |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 1424 | MessageLoop::current()->PostDelayedTask( |
| 1425 | FROM_HERE, |
| 1426 | base::Bind(&MouseDownUp), |
| 1427 | TimeDelta::FromMilliseconds(100)); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1428 | HHOOK msg_hook = SetWindowsHookEx(WH_MSGFILTER, |
| 1429 | MsgFilterProc, |
| 1430 | NULL, |
| 1431 | GetCurrentThreadId()); |
| 1432 | DispatcherImpl dispatcher; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1433 | base::RunLoop run_loop(&dispatcher); |
| 1434 | run_loop.Run(); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1435 | ASSERT_EQ(1, dispatcher.dispatch_count_); |
| 1436 | UnhookWindowsHookEx(msg_hook); |
| 1437 | } |
| 1438 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1439 | class TestIOHandler : public MessageLoopForIO::IOHandler { |
| 1440 | public: |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1441 | TestIOHandler(const wchar_t* name, HANDLE signal, bool wait); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1442 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1443 | virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 1444 | DWORD bytes_transfered, DWORD error); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1445 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1446 | void Init(); |
| 1447 | void WaitForIO(); |
| 1448 | OVERLAPPED* context() { return &context_.overlapped; } |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1449 | DWORD size() { return sizeof(buffer_); } |
| 1450 | |
| 1451 | private: |
| 1452 | char buffer_[48]; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1453 | MessageLoopForIO::IOContext context_; |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1454 | HANDLE signal_; |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 1455 | base::win::ScopedHandle file_; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1456 | bool wait_; |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1457 | }; |
| 1458 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1459 | TestIOHandler::TestIOHandler(const wchar_t* name, HANDLE signal, bool wait) |
| 1460 | : signal_(signal), wait_(wait) { |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1461 | memset(buffer_, 0, sizeof(buffer_)); |
| 1462 | memset(&context_, 0, sizeof(context_)); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1463 | context_.handler = this; |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1464 | |
| 1465 | file_.Set(CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, |
| 1466 | FILE_FLAG_OVERLAPPED, NULL)); |
| 1467 | EXPECT_TRUE(file_.IsValid()); |
| 1468 | } |
| 1469 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1470 | void TestIOHandler::Init() { |
| 1471 | MessageLoopForIO::current()->RegisterIOHandler(file_, this); |
| 1472 | |
| 1473 | DWORD read; |
| 1474 | EXPECT_FALSE(ReadFile(file_, buffer_, size(), &read, context())); |
| 1475 | EXPECT_EQ(ERROR_IO_PENDING, GetLastError()); |
| 1476 | if (wait_) |
| 1477 | WaitForIO(); |
| 1478 | } |
| 1479 | |
| 1480 | void TestIOHandler::OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 1481 | DWORD bytes_transfered, DWORD error) { |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1482 | ASSERT_TRUE(context == &context_); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1483 | ASSERT_TRUE(SetEvent(signal_)); |
| 1484 | } |
| 1485 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1486 | void TestIOHandler::WaitForIO() { |
| 1487 | EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(300, this)); |
| 1488 | EXPECT_TRUE(MessageLoopForIO::current()->WaitForIOCompletion(400, this)); |
| 1489 | } |
| 1490 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1491 | void RunTest_IOHandler() { |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 1492 | base::win::ScopedHandle callback_called(CreateEvent(NULL, TRUE, FALSE, NULL)); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1493 | ASSERT_TRUE(callback_called.IsValid()); |
| 1494 | |
| 1495 | const wchar_t* kPipeName = L"\\\\.\\pipe\\iohandler_pipe"; |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 1496 | base::win::ScopedHandle server( |
| 1497 | CreateNamedPipe(kPipeName, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL)); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1498 | ASSERT_TRUE(server.IsValid()); |
| 1499 | |
| 1500 | Thread thread("IOHandler test"); |
| 1501 | Thread::Options options; |
| 1502 | options.message_loop_type = MessageLoop::TYPE_IO; |
| 1503 | ASSERT_TRUE(thread.StartWithOptions(options)); |
| 1504 | |
| 1505 | MessageLoop* thread_loop = thread.message_loop(); |
| 1506 | ASSERT_TRUE(NULL != thread_loop); |
| 1507 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1508 | TestIOHandler handler(kPipeName, callback_called, false); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1509 | thread_loop->PostTask(FROM_HERE, base::Bind(&TestIOHandler::Init, |
| 1510 | base::Unretained(&handler))); |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 1511 | // Make sure the thread runs and sleeps for lack of work. |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 1512 | base::PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1513 | |
| 1514 | const char buffer[] = "Hello there!"; |
| 1515 | DWORD written; |
| 1516 | EXPECT_TRUE(WriteFile(server, buffer, sizeof(buffer), &written, NULL)); |
| 1517 | |
| 1518 | DWORD result = WaitForSingleObject(callback_called, 1000); |
| 1519 | EXPECT_EQ(WAIT_OBJECT_0, result); |
| 1520 | |
| 1521 | thread.Stop(); |
| 1522 | } |
| 1523 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1524 | void RunTest_WaitForIO() { |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 1525 | base::win::ScopedHandle callback1_called( |
| 1526 | CreateEvent(NULL, TRUE, FALSE, NULL)); |
| 1527 | base::win::ScopedHandle callback2_called( |
| 1528 | CreateEvent(NULL, TRUE, FALSE, NULL)); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1529 | ASSERT_TRUE(callback1_called.IsValid()); |
| 1530 | ASSERT_TRUE(callback2_called.IsValid()); |
| 1531 | |
| 1532 | const wchar_t* kPipeName1 = L"\\\\.\\pipe\\iohandler_pipe1"; |
| 1533 | const wchar_t* kPipeName2 = L"\\\\.\\pipe\\iohandler_pipe2"; |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 1534 | base::win::ScopedHandle server1( |
| 1535 | CreateNamedPipe(kPipeName1, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL)); |
| 1536 | base::win::ScopedHandle server2( |
| 1537 | CreateNamedPipe(kPipeName2, PIPE_ACCESS_OUTBOUND, 0, 1, 0, 0, 0, NULL)); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1538 | ASSERT_TRUE(server1.IsValid()); |
| 1539 | ASSERT_TRUE(server2.IsValid()); |
| 1540 | |
| 1541 | Thread thread("IOHandler test"); |
| 1542 | Thread::Options options; |
| 1543 | options.message_loop_type = MessageLoop::TYPE_IO; |
| 1544 | ASSERT_TRUE(thread.StartWithOptions(options)); |
| 1545 | |
| 1546 | MessageLoop* thread_loop = thread.message_loop(); |
| 1547 | ASSERT_TRUE(NULL != thread_loop); |
| 1548 | |
| 1549 | TestIOHandler handler1(kPipeName1, callback1_called, false); |
| 1550 | TestIOHandler handler2(kPipeName2, callback2_called, true); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1551 | thread_loop->PostTask(FROM_HERE, base::Bind(&TestIOHandler::Init, |
| 1552 | base::Unretained(&handler1))); |
| 1553 | // TODO(ajwong): Do we really need such long Sleeps in ths function? |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 1554 | // Make sure the thread runs and sleeps for lack of work. |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 1555 | TimeDelta delay = TimeDelta::FromMilliseconds(100); |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 1556 | base::PlatformThread::Sleep(delay); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1557 | thread_loop->PostTask(FROM_HERE, base::Bind(&TestIOHandler::Init, |
| 1558 | base::Unretained(&handler2))); |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 1559 | base::PlatformThread::Sleep(delay); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1560 | |
| 1561 | // At this time handler1 is waiting to be called, and the thread is waiting |
| 1562 | // on the Init method of handler2, filtering only handler2 callbacks. |
| 1563 | |
| 1564 | const char buffer[] = "Hello there!"; |
| 1565 | DWORD written; |
| 1566 | EXPECT_TRUE(WriteFile(server1, buffer, sizeof(buffer), &written, NULL)); |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 1567 | base::PlatformThread::Sleep(2 * delay); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1568 | EXPECT_EQ(WAIT_TIMEOUT, WaitForSingleObject(callback1_called, 0)) << |
| 1569 | "handler1 has not been called"; |
| 1570 | |
| 1571 | EXPECT_TRUE(WriteFile(server2, buffer, sizeof(buffer), &written, NULL)); |
| 1572 | |
| 1573 | HANDLE objects[2] = { callback1_called.Get(), callback2_called.Get() }; |
| 1574 | DWORD result = WaitForMultipleObjects(2, objects, TRUE, 1000); |
| 1575 | EXPECT_EQ(WAIT_OBJECT_0, result); |
| 1576 | |
| 1577 | thread.Stop(); |
| 1578 | } |
| 1579 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1580 | #endif // defined(OS_WIN) |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1581 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1582 | } // namespace |
| 1583 | |
| 1584 | //----------------------------------------------------------------------------- |
| 1585 | // Each test is run against each type of MessageLoop. That way we are sure |
| 1586 | // that message loops work properly in all configurations. Of course, in some |
| 1587 | // cases, a unit test may only be for a particular type of loop. |
| 1588 | |
| 1589 | TEST(MessageLoopTest, PostTask) { |
| 1590 | RunTest_PostTask(MessageLoop::TYPE_DEFAULT); |
| 1591 | RunTest_PostTask(MessageLoop::TYPE_UI); |
| 1592 | RunTest_PostTask(MessageLoop::TYPE_IO); |
| 1593 | } |
| 1594 | |
| 1595 | TEST(MessageLoopTest, PostTask_SEH) { |
| 1596 | RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT); |
| 1597 | RunTest_PostTask_SEH(MessageLoop::TYPE_UI); |
| 1598 | RunTest_PostTask_SEH(MessageLoop::TYPE_IO); |
| 1599 | } |
| 1600 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 1601 | TEST(MessageLoopTest, PostDelayedTask_Basic) { |
| 1602 | RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT); |
| 1603 | RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI); |
| 1604 | RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO); |
| 1605 | } |
| 1606 | |
| 1607 | TEST(MessageLoopTest, PostDelayedTask_InDelayOrder) { |
| 1608 | RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_DEFAULT); |
| 1609 | RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_UI); |
| 1610 | RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_IO); |
| 1611 | } |
| 1612 | |
| 1613 | TEST(MessageLoopTest, PostDelayedTask_InPostOrder) { |
| 1614 | RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_DEFAULT); |
| 1615 | RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_UI); |
| 1616 | RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_IO); |
| 1617 | } |
| 1618 | |
| 1619 | TEST(MessageLoopTest, PostDelayedTask_InPostOrder_2) { |
| 1620 | RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_DEFAULT); |
| 1621 | RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI); |
| 1622 | RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO); |
| 1623 | } |
| 1624 | |
| 1625 | TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) { |
| 1626 | RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT); |
| 1627 | RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI); |
| 1628 | RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO); |
| 1629 | } |
| 1630 | |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 1631 | TEST(MessageLoopTest, PostDelayedTask_SharedTimer) { |
| 1632 | RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_DEFAULT); |
| 1633 | RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_UI); |
| 1634 | RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_IO); |
| 1635 | } |
| 1636 | |
| 1637 | #if defined(OS_WIN) |
| 1638 | TEST(MessageLoopTest, PostDelayedTask_SharedTimer_SubPump) { |
| 1639 | RunTest_PostDelayedTask_SharedTimer_SubPump(); |
| 1640 | } |
| 1641 | #endif |
| 1642 | |
[email protected] | 5affb7e | 2010-05-25 20:13:36 | [diff] [blame] | 1643 | // TODO(darin): MessageLoop does not support deleting all tasks in the |
| 1644 | // destructor. |
[email protected] | 2f861b3 | 2010-07-26 21:23:18 | [diff] [blame] | 1645 | // Fails, http://crbug.com/50272. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1646 | TEST(MessageLoopTest, FAILS_EnsureDeletion) { |
| 1647 | RunTest_EnsureDeletion(MessageLoop::TYPE_DEFAULT); |
| 1648 | RunTest_EnsureDeletion(MessageLoop::TYPE_UI); |
| 1649 | RunTest_EnsureDeletion(MessageLoop::TYPE_IO); |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 1650 | } |
| 1651 | |
[email protected] | 5affb7e | 2010-05-25 20:13:36 | [diff] [blame] | 1652 | // TODO(darin): MessageLoop does not support deleting all tasks in the |
| 1653 | // destructor. |
[email protected] | 2f861b3 | 2010-07-26 21:23:18 | [diff] [blame] | 1654 | // Fails, http://crbug.com/50272. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1655 | TEST(MessageLoopTest, FAILS_EnsureDeletion_Chain) { |
| 1656 | RunTest_EnsureDeletion_Chain(MessageLoop::TYPE_DEFAULT); |
| 1657 | RunTest_EnsureDeletion_Chain(MessageLoop::TYPE_UI); |
| 1658 | RunTest_EnsureDeletion_Chain(MessageLoop::TYPE_IO); |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 1659 | } |
| 1660 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1661 | #if defined(OS_WIN) |
| 1662 | TEST(MessageLoopTest, Crasher) { |
| 1663 | RunTest_Crasher(MessageLoop::TYPE_DEFAULT); |
| 1664 | RunTest_Crasher(MessageLoop::TYPE_UI); |
| 1665 | RunTest_Crasher(MessageLoop::TYPE_IO); |
| 1666 | } |
| 1667 | |
| 1668 | TEST(MessageLoopTest, CrasherNasty) { |
| 1669 | RunTest_CrasherNasty(MessageLoop::TYPE_DEFAULT); |
| 1670 | RunTest_CrasherNasty(MessageLoop::TYPE_UI); |
| 1671 | RunTest_CrasherNasty(MessageLoop::TYPE_IO); |
| 1672 | } |
| 1673 | #endif // defined(OS_WIN) |
| 1674 | |
| 1675 | TEST(MessageLoopTest, Nesting) { |
| 1676 | RunTest_Nesting(MessageLoop::TYPE_DEFAULT); |
| 1677 | RunTest_Nesting(MessageLoop::TYPE_UI); |
| 1678 | RunTest_Nesting(MessageLoop::TYPE_IO); |
| 1679 | } |
| 1680 | |
| 1681 | TEST(MessageLoopTest, RecursiveDenial1) { |
| 1682 | RunTest_RecursiveDenial1(MessageLoop::TYPE_DEFAULT); |
| 1683 | RunTest_RecursiveDenial1(MessageLoop::TYPE_UI); |
| 1684 | RunTest_RecursiveDenial1(MessageLoop::TYPE_IO); |
| 1685 | } |
| 1686 | |
[email protected] | 4554c23 | 2011-02-17 19:25:04 | [diff] [blame] | 1687 | TEST(MessageLoopTest, RecursiveDenial3) { |
| 1688 | RunTest_RecursiveDenial3(MessageLoop::TYPE_DEFAULT); |
| 1689 | RunTest_RecursiveDenial3(MessageLoop::TYPE_UI); |
| 1690 | RunTest_RecursiveDenial3(MessageLoop::TYPE_IO); |
| 1691 | } |
| 1692 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1693 | TEST(MessageLoopTest, RecursiveSupport1) { |
| 1694 | RunTest_RecursiveSupport1(MessageLoop::TYPE_DEFAULT); |
| 1695 | RunTest_RecursiveSupport1(MessageLoop::TYPE_UI); |
| 1696 | RunTest_RecursiveSupport1(MessageLoop::TYPE_IO); |
| 1697 | } |
| 1698 | |
| 1699 | #if defined(OS_WIN) |
[email protected] | 85f39f8 | 2010-05-19 14:33:44 | [diff] [blame] | 1700 | // This test occasionally hangs http://crbug.com/44567 |
| 1701 | TEST(MessageLoopTest, DISABLED_RecursiveDenial2) { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1702 | RunTest_RecursiveDenial2(MessageLoop::TYPE_DEFAULT); |
| 1703 | RunTest_RecursiveDenial2(MessageLoop::TYPE_UI); |
| 1704 | RunTest_RecursiveDenial2(MessageLoop::TYPE_IO); |
| 1705 | } |
| 1706 | |
| 1707 | TEST(MessageLoopTest, RecursiveSupport2) { |
| 1708 | // This test requires a UI loop |
| 1709 | RunTest_RecursiveSupport2(MessageLoop::TYPE_UI); |
| 1710 | } |
| 1711 | #endif // defined(OS_WIN) |
| 1712 | |
| 1713 | TEST(MessageLoopTest, NonNestableWithNoNesting) { |
| 1714 | RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_DEFAULT); |
| 1715 | RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_UI); |
| 1716 | RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_IO); |
| 1717 | } |
| 1718 | |
| 1719 | TEST(MessageLoopTest, NonNestableInNestedLoop) { |
[email protected] | c4280a9 | 2009-06-25 19:23:11 | [diff] [blame] | 1720 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, false); |
| 1721 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, false); |
| 1722 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, false); |
| 1723 | } |
| 1724 | |
| 1725 | TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) { |
| 1726 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true); |
| 1727 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true); |
| 1728 | RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true); |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1729 | } |
| 1730 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1731 | TEST(MessageLoopTest, QuitNow) { |
| 1732 | RunTest_QuitNow(MessageLoop::TYPE_DEFAULT); |
| 1733 | RunTest_QuitNow(MessageLoop::TYPE_UI); |
| 1734 | RunTest_QuitNow(MessageLoop::TYPE_IO); |
| 1735 | } |
| 1736 | |
| 1737 | TEST(MessageLoopTest, RunLoopQuitTop) { |
| 1738 | RunTest_RunLoopQuitTop(MessageLoop::TYPE_DEFAULT); |
| 1739 | RunTest_RunLoopQuitTop(MessageLoop::TYPE_UI); |
| 1740 | RunTest_RunLoopQuitTop(MessageLoop::TYPE_IO); |
| 1741 | } |
| 1742 | |
| 1743 | TEST(MessageLoopTest, RunLoopQuitNested) { |
| 1744 | RunTest_RunLoopQuitNested(MessageLoop::TYPE_DEFAULT); |
| 1745 | RunTest_RunLoopQuitNested(MessageLoop::TYPE_UI); |
| 1746 | RunTest_RunLoopQuitNested(MessageLoop::TYPE_IO); |
| 1747 | } |
| 1748 | |
| 1749 | TEST(MessageLoopTest, RunLoopQuitBogus) { |
| 1750 | RunTest_RunLoopQuitBogus(MessageLoop::TYPE_DEFAULT); |
| 1751 | RunTest_RunLoopQuitBogus(MessageLoop::TYPE_UI); |
| 1752 | RunTest_RunLoopQuitBogus(MessageLoop::TYPE_IO); |
| 1753 | } |
| 1754 | |
| 1755 | TEST(MessageLoopTest, RunLoopQuitDeep) { |
| 1756 | RunTest_RunLoopQuitDeep(MessageLoop::TYPE_DEFAULT); |
| 1757 | RunTest_RunLoopQuitDeep(MessageLoop::TYPE_UI); |
| 1758 | RunTest_RunLoopQuitDeep(MessageLoop::TYPE_IO); |
| 1759 | } |
| 1760 | |
| 1761 | TEST(MessageLoopTest, RunLoopQuitOrderBefore) { |
| 1762 | RunTest_RunLoopQuitOrderBefore(MessageLoop::TYPE_DEFAULT); |
| 1763 | RunTest_RunLoopQuitOrderBefore(MessageLoop::TYPE_UI); |
| 1764 | RunTest_RunLoopQuitOrderBefore(MessageLoop::TYPE_IO); |
| 1765 | } |
| 1766 | |
| 1767 | TEST(MessageLoopTest, RunLoopQuitOrderDuring) { |
| 1768 | RunTest_RunLoopQuitOrderDuring(MessageLoop::TYPE_DEFAULT); |
| 1769 | RunTest_RunLoopQuitOrderDuring(MessageLoop::TYPE_UI); |
| 1770 | RunTest_RunLoopQuitOrderDuring(MessageLoop::TYPE_IO); |
| 1771 | } |
| 1772 | |
| 1773 | TEST(MessageLoopTest, RunLoopQuitOrderAfter) { |
| 1774 | RunTest_RunLoopQuitOrderAfter(MessageLoop::TYPE_DEFAULT); |
| 1775 | RunTest_RunLoopQuitOrderAfter(MessageLoop::TYPE_UI); |
| 1776 | RunTest_RunLoopQuitOrderAfter(MessageLoop::TYPE_IO); |
| 1777 | } |
| 1778 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1779 | void PostNTasksThenQuit(int posts_remaining) { |
| 1780 | if (posts_remaining > 1) { |
| 1781 | MessageLoop::current()->PostTask( |
| 1782 | FROM_HERE, |
| 1783 | base::Bind(&PostNTasksThenQuit, posts_remaining - 1)); |
| 1784 | } else { |
| 1785 | MessageLoop::current()->Quit(); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1786 | } |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1787 | } |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1788 | |
| 1789 | class DummyTaskObserver : public MessageLoop::TaskObserver { |
| 1790 | public: |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 1791 | explicit DummyTaskObserver(int num_tasks) |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1792 | : num_tasks_started_(0), |
| 1793 | num_tasks_processed_(0), |
| 1794 | num_tasks_(num_tasks) {} |
| 1795 | |
| 1796 | virtual ~DummyTaskObserver() {} |
| 1797 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1798 | virtual void WillProcessTask(TimeTicks time_posted) OVERRIDE { |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1799 | num_tasks_started_++; |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1800 | EXPECT_TRUE(time_posted != TimeTicks()); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1801 | EXPECT_LE(num_tasks_started_, num_tasks_); |
| 1802 | EXPECT_EQ(num_tasks_started_, num_tasks_processed_ + 1); |
| 1803 | } |
| 1804 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1805 | virtual void DidProcessTask(TimeTicks time_posted) OVERRIDE { |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1806 | num_tasks_processed_++; |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1807 | EXPECT_TRUE(time_posted != TimeTicks()); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1808 | EXPECT_LE(num_tasks_started_, num_tasks_); |
| 1809 | EXPECT_EQ(num_tasks_started_, num_tasks_processed_); |
| 1810 | } |
| 1811 | |
| 1812 | int num_tasks_started() const { return num_tasks_started_; } |
| 1813 | int num_tasks_processed() const { return num_tasks_processed_; } |
| 1814 | |
| 1815 | private: |
| 1816 | int num_tasks_started_; |
| 1817 | int num_tasks_processed_; |
| 1818 | const int num_tasks_; |
| 1819 | |
| 1820 | DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver); |
| 1821 | }; |
| 1822 | |
| 1823 | TEST(MessageLoopTest, TaskObserver) { |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1824 | const int kNumPosts = 6; |
| 1825 | DummyTaskObserver observer(kNumPosts); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1826 | |
| 1827 | MessageLoop loop; |
| 1828 | loop.AddTaskObserver(&observer); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1829 | loop.PostTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, kNumPosts)); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1830 | loop.Run(); |
| 1831 | loop.RemoveTaskObserver(&observer); |
| 1832 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1833 | EXPECT_EQ(kNumPosts, observer.num_tasks_started()); |
| 1834 | EXPECT_EQ(kNumPosts, observer.num_tasks_processed()); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1835 | } |
| 1836 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1837 | #if defined(OS_WIN) |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1838 | TEST(MessageLoopTest, Dispatcher) { |
| 1839 | // This test requires a UI loop |
| 1840 | RunTest_Dispatcher(MessageLoop::TYPE_UI); |
| 1841 | } |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1842 | |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1843 | TEST(MessageLoopTest, DispatcherWithMessageHook) { |
| 1844 | // This test requires a UI loop |
| 1845 | RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI); |
| 1846 | } |
| 1847 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 1848 | TEST(MessageLoopTest, IOHandler) { |
| 1849 | RunTest_IOHandler(); |
| 1850 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 1851 | |
| 1852 | TEST(MessageLoopTest, WaitForIO) { |
| 1853 | RunTest_WaitForIO(); |
| 1854 | } |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 1855 | |
| 1856 | TEST(MessageLoopTest, HighResolutionTimer) { |
| 1857 | MessageLoop loop; |
| 1858 | |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 1859 | const TimeDelta kFastTimer = TimeDelta::FromMilliseconds(5); |
| 1860 | const TimeDelta kSlowTimer = TimeDelta::FromMilliseconds(100); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 1861 | |
[email protected] | 515f249 | 2011-01-14 10:36:28 | [diff] [blame] | 1862 | EXPECT_FALSE(loop.high_resolution_timers_enabled()); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 1863 | |
| 1864 | // Post a fast task to enable the high resolution timers. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1865 | loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 1866 | kFastTimer); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 1867 | loop.Run(); |
[email protected] | 515f249 | 2011-01-14 10:36:28 | [diff] [blame] | 1868 | EXPECT_TRUE(loop.high_resolution_timers_enabled()); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 1869 | |
| 1870 | // Post a slow task and verify high resolution timers |
| 1871 | // are still enabled. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1872 | loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 1873 | kSlowTimer); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 1874 | loop.Run(); |
[email protected] | 515f249 | 2011-01-14 10:36:28 | [diff] [blame] | 1875 | EXPECT_TRUE(loop.high_resolution_timers_enabled()); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 1876 | |
| 1877 | // Wait for a while so that high-resolution mode elapses. |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 1878 | base::PlatformThread::Sleep(TimeDelta::FromMilliseconds( |
| 1879 | MessageLoop::kHighResolutionTimerModeLeaseTimeMs)); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 1880 | |
| 1881 | // Post a slow task to disable the high resolution timers. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1882 | loop.PostDelayedTask(FROM_HERE, base::Bind(&PostNTasksThenQuit, 1), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 1883 | kSlowTimer); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 1884 | loop.Run(); |
[email protected] | 515f249 | 2011-01-14 10:36:28 | [diff] [blame] | 1885 | EXPECT_FALSE(loop.high_resolution_timers_enabled()); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 1886 | } |
| 1887 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 1888 | #endif // defined(OS_WIN) |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1889 | |
[email protected] | 5cffdfd | 2010-12-01 08:45:51 | [diff] [blame] | 1890 | #if defined(OS_POSIX) && !defined(OS_NACL) |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1891 | |
| 1892 | namespace { |
| 1893 | |
[email protected] | a27de26 | 2011-06-22 06:33:05 | [diff] [blame] | 1894 | class QuitDelegate : public MessageLoopForIO::Watcher { |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1895 | public: |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 1896 | virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE { |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1897 | MessageLoop::current()->Quit(); |
| 1898 | } |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 1899 | virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE { |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1900 | MessageLoop::current()->Quit(); |
| 1901 | } |
| 1902 | }; |
| 1903 | |
[email protected] | f4521377 | 2010-06-11 00:06:19 | [diff] [blame] | 1904 | TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) { |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1905 | // Simulate a MessageLoop that dies before an FileDescriptorWatcher. |
| 1906 | // This could happen when people use the Singleton pattern or atexit. |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1907 | |
| 1908 | // Create a file descriptor. Doesn't need to be readable or writable, |
| 1909 | // as we don't need to actually get any notifications. |
| 1910 | // pipe() is just the easiest way to do it. |
| 1911 | int pipefds[2]; |
| 1912 | int err = pipe(pipefds); |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 1913 | ASSERT_EQ(0, err); |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1914 | int fd = pipefds[1]; |
| 1915 | { |
| 1916 | // Arrange for controller to live longer than message loop. |
[email protected] | a27de26 | 2011-06-22 06:33:05 | [diff] [blame] | 1917 | MessageLoopForIO::FileDescriptorWatcher controller; |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1918 | { |
| 1919 | MessageLoopForIO message_loop; |
| 1920 | |
| 1921 | QuitDelegate delegate; |
| 1922 | message_loop.WatchFileDescriptor(fd, |
| 1923 | true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); |
| 1924 | // and don't run the message loop, just destroy it. |
| 1925 | } |
| 1926 | } |
[email protected] | 70eb657 | 2010-06-23 00:37:46 | [diff] [blame] | 1927 | if (HANDLE_EINTR(close(pipefds[0])) < 0) |
| 1928 | PLOG(ERROR) << "close"; |
| 1929 | if (HANDLE_EINTR(close(pipefds[1])) < 0) |
| 1930 | PLOG(ERROR) << "close"; |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) { |
| 1934 | // Verify that it's ok to call StopWatchingFileDescriptor(). |
| 1935 | // (Errors only showed up in valgrind.) |
| 1936 | int pipefds[2]; |
| 1937 | int err = pipe(pipefds); |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 1938 | ASSERT_EQ(0, err); |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1939 | int fd = pipefds[1]; |
| 1940 | { |
| 1941 | // Arrange for message loop to live longer than controller. |
| 1942 | MessageLoopForIO message_loop; |
| 1943 | { |
[email protected] | a27de26 | 2011-06-22 06:33:05 | [diff] [blame] | 1944 | MessageLoopForIO::FileDescriptorWatcher controller; |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1945 | |
| 1946 | QuitDelegate delegate; |
| 1947 | message_loop.WatchFileDescriptor(fd, |
| 1948 | true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); |
| 1949 | controller.StopWatchingFileDescriptor(); |
| 1950 | } |
| 1951 | } |
[email protected] | 70eb657 | 2010-06-23 00:37:46 | [diff] [blame] | 1952 | if (HANDLE_EINTR(close(pipefds[0])) < 0) |
| 1953 | PLOG(ERROR) << "close"; |
| 1954 | if (HANDLE_EINTR(close(pipefds[1])) < 0) |
| 1955 | PLOG(ERROR) << "close"; |
[email protected] | f74c896 | 2009-04-22 20:01:36 | [diff] [blame] | 1956 | } |
| 1957 | |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 1958 | } // namespace |
| 1959 | |
[email protected] | 5cffdfd | 2010-12-01 08:45:51 | [diff] [blame] | 1960 | #endif // defined(OS_POSIX) && !defined(OS_NACL) |
[email protected] | 58238477 | 2010-11-30 00:25:29 | [diff] [blame] | 1961 | |
| 1962 | namespace { |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1963 | // Inject a test point for recording the destructor calls for Closure objects |
| 1964 | // send to MessageLoop::PostTask(). It is awkward usage since we are trying to |
| 1965 | // hook the actual destruction, which is not a common operation. |
| 1966 | class DestructionObserverProbe : |
| 1967 | public base::RefCounted<DestructionObserverProbe> { |
[email protected] | 58238477 | 2010-11-30 00:25:29 | [diff] [blame] | 1968 | public: |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 1969 | DestructionObserverProbe(bool* task_destroyed, |
| 1970 | bool* destruction_observer_called) |
[email protected] | 58238477 | 2010-11-30 00:25:29 | [diff] [blame] | 1971 | : task_destroyed_(task_destroyed), |
| 1972 | destruction_observer_called_(destruction_observer_called) { |
| 1973 | } |
[email protected] | 58238477 | 2010-11-30 00:25:29 | [diff] [blame] | 1974 | virtual void Run() { |
| 1975 | // This task should never run. |
| 1976 | ADD_FAILURE(); |
| 1977 | } |
| 1978 | private: |
[email protected] | a9aaa9d1 | 2012-04-25 00:42:51 | [diff] [blame] | 1979 | friend class base::RefCounted<DestructionObserverProbe>; |
| 1980 | |
| 1981 | virtual ~DestructionObserverProbe() { |
| 1982 | EXPECT_FALSE(*destruction_observer_called_); |
| 1983 | *task_destroyed_ = true; |
| 1984 | } |
| 1985 | |
[email protected] | 58238477 | 2010-11-30 00:25:29 | [diff] [blame] | 1986 | bool* task_destroyed_; |
| 1987 | bool* destruction_observer_called_; |
| 1988 | }; |
| 1989 | |
| 1990 | class MLDestructionObserver : public MessageLoop::DestructionObserver { |
| 1991 | public: |
| 1992 | MLDestructionObserver(bool* task_destroyed, bool* destruction_observer_called) |
| 1993 | : task_destroyed_(task_destroyed), |
| 1994 | destruction_observer_called_(destruction_observer_called), |
| 1995 | task_destroyed_before_message_loop_(false) { |
| 1996 | } |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 1997 | virtual void WillDestroyCurrentMessageLoop() OVERRIDE { |
[email protected] | 58238477 | 2010-11-30 00:25:29 | [diff] [blame] | 1998 | task_destroyed_before_message_loop_ = *task_destroyed_; |
| 1999 | *destruction_observer_called_ = true; |
| 2000 | } |
| 2001 | bool task_destroyed_before_message_loop() const { |
| 2002 | return task_destroyed_before_message_loop_; |
| 2003 | } |
| 2004 | private: |
| 2005 | bool* task_destroyed_; |
| 2006 | bool* destruction_observer_called_; |
| 2007 | bool task_destroyed_before_message_loop_; |
| 2008 | }; |
| 2009 | |
| 2010 | } // namespace |
| 2011 | |
| 2012 | TEST(MessageLoopTest, DestructionObserverTest) { |
| 2013 | // Verify that the destruction observer gets called at the very end (after |
| 2014 | // all the pending tasks have been destroyed). |
| 2015 | MessageLoop* loop = new MessageLoop; |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 2016 | const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); |
[email protected] | 58238477 | 2010-11-30 00:25:29 | [diff] [blame] | 2017 | |
| 2018 | bool task_destroyed = false; |
| 2019 | bool destruction_observer_called = false; |
| 2020 | |
| 2021 | MLDestructionObserver observer(&task_destroyed, &destruction_observer_called); |
| 2022 | loop->AddDestructionObserver(&observer); |
| 2023 | loop->PostDelayedTask( |
| 2024 | FROM_HERE, |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 2025 | base::Bind(&DestructionObserverProbe::Run, |
| 2026 | new DestructionObserverProbe(&task_destroyed, |
| 2027 | &destruction_observer_called)), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 2028 | kDelay); |
[email protected] | 58238477 | 2010-11-30 00:25:29 | [diff] [blame] | 2029 | delete loop; |
| 2030 | EXPECT_TRUE(observer.task_destroyed_before_message_loop()); |
| 2031 | // The task should have been destroyed when we deleted the loop. |
| 2032 | EXPECT_TRUE(task_destroyed); |
| 2033 | EXPECT_TRUE(destruction_observer_called); |
| 2034 | } |
[email protected] | 4fc20d1 | 2012-05-09 20:16:14 | [diff] [blame] | 2035 | |
| 2036 | |
| 2037 | // Verify that MessageLoop sets ThreadMainTaskRunner::current() and it |
| 2038 | // posts tasks on that message loop. |
| 2039 | TEST(MessageLoopTest, ThreadMainTaskRunner) { |
| 2040 | MessageLoop loop; |
| 2041 | |
| 2042 | scoped_refptr<Foo> foo(new Foo()); |
| 2043 | std::string a("a"); |
| 2044 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::Bind( |
| 2045 | &Foo::Test1ConstRef, foo.get(), a)); |
| 2046 | |
| 2047 | // Post quit task; |
| 2048 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 2049 | &MessageLoop::Quit, base::Unretained(MessageLoop::current()))); |
| 2050 | |
| 2051 | // Now kick things off |
| 2052 | MessageLoop::current()->Run(); |
| 2053 | |
| 2054 | EXPECT_EQ(foo->test_count(), 1); |
| 2055 | EXPECT_EQ(foo->result(), "a"); |
| 2056 | } |
[email protected] | ab563ff | 2012-07-21 00:26:32 | [diff] [blame] | 2057 | |
| 2058 | TEST(MessageLoopTest, IsType) { |
| 2059 | MessageLoop loop(MessageLoop::TYPE_UI); |
| 2060 | EXPECT_TRUE(loop.IsType(MessageLoop::TYPE_UI)); |
| 2061 | EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_IO)); |
| 2062 | EXPECT_FALSE(loop.IsType(MessageLoop::TYPE_DEFAULT)); |
| 2063 | } |