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