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