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