| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop/message_loop_test.h" | 5 #include "base/message_loop/message_loop_test.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/single_thread_task_runner.h" |
| 15 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 namespace test { | 20 namespace test { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 class Foo : public RefCounted<Foo> { | 24 class Foo : public RefCounted<Foo> { |
| 24 public: | 25 public: |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 } | 90 } |
| 90 | 91 |
| 91 } // namespace | 92 } // namespace |
| 92 | 93 |
| 93 void RunTest_PostTask(MessagePumpFactory factory) { | 94 void RunTest_PostTask(MessagePumpFactory factory) { |
| 94 std::unique_ptr<MessagePump> pump(factory()); | 95 std::unique_ptr<MessagePump> pump(factory()); |
| 95 MessageLoop loop(std::move(pump)); | 96 MessageLoop loop(std::move(pump)); |
| 96 // Add tests to message loop | 97 // Add tests to message loop |
| 97 scoped_refptr<Foo> foo(new Foo()); | 98 scoped_refptr<Foo> foo(new Foo()); |
| 98 std::string a("a"), b("b"), c("c"), d("d"); | 99 std::string a("a"), b("b"), c("c"), d("d"); |
| 99 MessageLoop::current()->PostTask(FROM_HERE, Bind( | 100 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 100 &Foo::Test0, foo.get())); | 101 Bind(&Foo::Test0, foo.get())); |
| 101 MessageLoop::current()->PostTask(FROM_HERE, Bind( | 102 MessageLoop::current()->task_runner()->PostTask( |
| 102 &Foo::Test1ConstRef, foo.get(), a)); | 103 FROM_HERE, Bind(&Foo::Test1ConstRef, foo.get(), a)); |
| 103 MessageLoop::current()->PostTask(FROM_HERE, Bind( | 104 MessageLoop::current()->task_runner()->PostTask( |
| 104 &Foo::Test1Ptr, foo.get(), &b)); | 105 FROM_HERE, Bind(&Foo::Test1Ptr, foo.get(), &b)); |
| 105 MessageLoop::current()->PostTask(FROM_HERE, Bind( | 106 MessageLoop::current()->task_runner()->PostTask( |
| 106 &Foo::Test1Int, foo.get(), 100)); | 107 FROM_HERE, Bind(&Foo::Test1Int, foo.get(), 100)); |
| 107 MessageLoop::current()->PostTask(FROM_HERE, Bind( | 108 MessageLoop::current()->task_runner()->PostTask( |
| 108 &Foo::Test2Ptr, foo.get(), &a, &c)); | 109 FROM_HERE, Bind(&Foo::Test2Ptr, foo.get(), &a, &c)); |
| 109 MessageLoop::current()->PostTask(FROM_HERE, Bind( | 110 MessageLoop::current()->task_runner()->PostTask( |
| 110 &Foo::Test2Mixed, foo.get(), a, &d)); | 111 FROM_HERE, Bind(&Foo::Test2Mixed, foo.get(), a, &d)); |
| 111 // After all tests, post a message that will shut down the message loop | 112 // After all tests, post a message that will shut down the message loop |
| 112 MessageLoop::current()->PostTask( | 113 MessageLoop::current()->task_runner()->PostTask( |
| 113 FROM_HERE, | 114 FROM_HERE, |
| 114 Bind(&MessageLoop::QuitWhenIdle, Unretained(MessageLoop::current()))); | 115 Bind(&MessageLoop::QuitWhenIdle, Unretained(MessageLoop::current()))); |
| 115 | 116 |
| 116 // Now kick things off | 117 // Now kick things off |
| 117 MessageLoop::current()->Run(); | 118 RunLoop().Run(); |
| 118 | 119 |
| 119 EXPECT_EQ(foo->test_count(), 105); | 120 EXPECT_EQ(foo->test_count(), 105); |
| 120 EXPECT_EQ(foo->result(), "abacad"); | 121 EXPECT_EQ(foo->result(), "abacad"); |
| 121 } | 122 } |
| 122 | 123 |
| 123 void RunTest_PostDelayedTask_Basic(MessagePumpFactory factory) { | 124 void RunTest_PostDelayedTask_Basic(MessagePumpFactory factory) { |
| 124 std::unique_ptr<MessagePump> pump(factory()); | 125 std::unique_ptr<MessagePump> pump(factory()); |
| 125 MessageLoop loop(std::move(pump)); | 126 MessageLoop loop(std::move(pump)); |
| 126 | 127 |
| 127 // Test that PostDelayedTask results in a delayed task. | 128 // Test that PostDelayedTask results in a delayed task. |
| 128 | 129 |
| 129 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); | 130 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); |
| 130 | 131 |
| 131 int num_tasks = 1; | 132 int num_tasks = 1; |
| 132 Time run_time; | 133 Time run_time; |
| 133 | 134 |
| 134 loop.PostDelayedTask( | 135 loop.task_runner()->PostDelayedTask( |
| 135 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time, &num_tasks), | 136 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time, &num_tasks), kDelay); |
| 136 kDelay); | |
| 137 | 137 |
| 138 Time time_before_run = Time::Now(); | 138 Time time_before_run = Time::Now(); |
| 139 loop.Run(); | 139 RunLoop().Run(); |
| 140 Time time_after_run = Time::Now(); | 140 Time time_after_run = Time::Now(); |
| 141 | 141 |
| 142 EXPECT_EQ(0, num_tasks); | 142 EXPECT_EQ(0, num_tasks); |
| 143 EXPECT_LT(kDelay, time_after_run - time_before_run); | 143 EXPECT_LT(kDelay, time_after_run - time_before_run); |
| 144 } | 144 } |
| 145 | 145 |
| 146 void RunTest_PostDelayedTask_InDelayOrder(MessagePumpFactory factory) { | 146 void RunTest_PostDelayedTask_InDelayOrder(MessagePumpFactory factory) { |
| 147 std::unique_ptr<MessagePump> pump(factory()); | 147 std::unique_ptr<MessagePump> pump(factory()); |
| 148 MessageLoop loop(std::move(pump)); | 148 MessageLoop loop(std::move(pump)); |
| 149 | 149 |
| 150 // Test that two tasks with different delays run in the right order. | 150 // Test that two tasks with different delays run in the right order. |
| 151 int num_tasks = 2; | 151 int num_tasks = 2; |
| 152 Time run_time1, run_time2; | 152 Time run_time1, run_time2; |
| 153 | 153 |
| 154 loop.PostDelayedTask( | 154 loop.task_runner()->PostDelayedTask( |
| 155 FROM_HERE, | 155 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), |
| 156 Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), | |
| 157 TimeDelta::FromMilliseconds(200)); | 156 TimeDelta::FromMilliseconds(200)); |
| 158 // If we get a large pause in execution (due to a context switch) here, this | 157 // If we get a large pause in execution (due to a context switch) here, this |
| 159 // test could fail. | 158 // test could fail. |
| 160 loop.PostDelayedTask( | 159 loop.task_runner()->PostDelayedTask( |
| 161 FROM_HERE, | 160 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), |
| 162 Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), | |
| 163 TimeDelta::FromMilliseconds(10)); | 161 TimeDelta::FromMilliseconds(10)); |
| 164 | 162 |
| 165 loop.Run(); | 163 RunLoop().Run(); |
| 166 EXPECT_EQ(0, num_tasks); | 164 EXPECT_EQ(0, num_tasks); |
| 167 | 165 |
| 168 EXPECT_TRUE(run_time2 < run_time1); | 166 EXPECT_TRUE(run_time2 < run_time1); |
| 169 } | 167 } |
| 170 | 168 |
| 171 void RunTest_PostDelayedTask_InPostOrder(MessagePumpFactory factory) { | 169 void RunTest_PostDelayedTask_InPostOrder(MessagePumpFactory factory) { |
| 172 std::unique_ptr<MessagePump> pump(factory()); | 170 std::unique_ptr<MessagePump> pump(factory()); |
| 173 MessageLoop loop(std::move(pump)); | 171 MessageLoop loop(std::move(pump)); |
| 174 | 172 |
| 175 // Test that two tasks with the same delay run in the order in which they | 173 // Test that two tasks with the same delay run in the order in which they |
| 176 // were posted. | 174 // were posted. |
| 177 // | 175 // |
| 178 // NOTE: This is actually an approximate test since the API only takes a | 176 // NOTE: This is actually an approximate test since the API only takes a |
| 179 // "delay" parameter, so we are not exactly simulating two tasks that get | 177 // "delay" parameter, so we are not exactly simulating two tasks that get |
| 180 // posted at the exact same time. It would be nice if the API allowed us to | 178 // posted at the exact same time. It would be nice if the API allowed us to |
| 181 // specify the desired run time. | 179 // specify the desired run time. |
| 182 | 180 |
| 183 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); | 181 const TimeDelta kDelay = TimeDelta::FromMilliseconds(100); |
| 184 | 182 |
| 185 int num_tasks = 2; | 183 int num_tasks = 2; |
| 186 Time run_time1, run_time2; | 184 Time run_time1, run_time2; |
| 187 | 185 |
| 188 loop.PostDelayedTask( | 186 loop.task_runner()->PostDelayedTask( |
| 189 FROM_HERE, | 187 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), kDelay); |
| 190 Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), kDelay); | 188 loop.task_runner()->PostDelayedTask( |
| 191 loop.PostDelayedTask( | 189 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), kDelay); |
| 192 FROM_HERE, | |
| 193 Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), kDelay); | |
| 194 | 190 |
| 195 loop.Run(); | 191 RunLoop().Run(); |
| 196 EXPECT_EQ(0, num_tasks); | 192 EXPECT_EQ(0, num_tasks); |
| 197 | 193 |
| 198 EXPECT_TRUE(run_time1 < run_time2); | 194 EXPECT_TRUE(run_time1 < run_time2); |
| 199 } | 195 } |
| 200 | 196 |
| 201 void RunTest_PostDelayedTask_InPostOrder_2(MessagePumpFactory factory) { | 197 void RunTest_PostDelayedTask_InPostOrder_2(MessagePumpFactory factory) { |
| 202 std::unique_ptr<MessagePump> pump(factory()); | 198 std::unique_ptr<MessagePump> pump(factory()); |
| 203 MessageLoop loop(std::move(pump)); | 199 MessageLoop loop(std::move(pump)); |
| 204 | 200 |
| 205 // Test that a delayed task still runs after a normal tasks even if the | 201 // Test that a delayed task still runs after a normal tasks even if the |
| 206 // normal tasks take a long time to run. | 202 // normal tasks take a long time to run. |
| 207 | 203 |
| 208 const TimeDelta kPause = TimeDelta::FromMilliseconds(50); | 204 const TimeDelta kPause = TimeDelta::FromMilliseconds(50); |
| 209 | 205 |
| 210 int num_tasks = 2; | 206 int num_tasks = 2; |
| 211 Time run_time; | 207 Time run_time; |
| 212 | 208 |
| 213 loop.PostTask(FROM_HERE, Bind(&SlowFunc, kPause, &num_tasks)); | 209 loop.task_runner()->PostTask(FROM_HERE, Bind(&SlowFunc, kPause, &num_tasks)); |
| 214 loop.PostDelayedTask( | 210 loop.task_runner()->PostDelayedTask( |
| 215 FROM_HERE, | 211 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time, &num_tasks), |
| 216 Bind(&RecordRunTimeFunc, &run_time, &num_tasks), | |
| 217 TimeDelta::FromMilliseconds(10)); | 212 TimeDelta::FromMilliseconds(10)); |
| 218 | 213 |
| 219 Time time_before_run = Time::Now(); | 214 Time time_before_run = Time::Now(); |
| 220 loop.Run(); | 215 RunLoop().Run(); |
| 221 Time time_after_run = Time::Now(); | 216 Time time_after_run = Time::Now(); |
| 222 | 217 |
| 223 EXPECT_EQ(0, num_tasks); | 218 EXPECT_EQ(0, num_tasks); |
| 224 | 219 |
| 225 EXPECT_LT(kPause, time_after_run - time_before_run); | 220 EXPECT_LT(kPause, time_after_run - time_before_run); |
| 226 } | 221 } |
| 227 | 222 |
| 228 void RunTest_PostDelayedTask_InPostOrder_3(MessagePumpFactory factory) { | 223 void RunTest_PostDelayedTask_InPostOrder_3(MessagePumpFactory factory) { |
| 229 std::unique_ptr<MessagePump> pump(factory()); | 224 std::unique_ptr<MessagePump> pump(factory()); |
| 230 MessageLoop loop(std::move(pump)); | 225 MessageLoop loop(std::move(pump)); |
| 231 | 226 |
| 232 // Test that a delayed task still runs after a pile of normal tasks. The key | 227 // Test that a delayed task still runs after a pile of normal tasks. The key |
| 233 // difference between this test and the previous one is that here we return | 228 // difference between this test and the previous one is that here we return |
| 234 // the MessageLoop a lot so we give the MessageLoop plenty of opportunities | 229 // the MessageLoop a lot so we give the MessageLoop plenty of opportunities |
| 235 // to maybe run the delayed task. It should know not to do so until the | 230 // to maybe run the delayed task. It should know not to do so until the |
| 236 // delayed task's delay has passed. | 231 // delayed task's delay has passed. |
| 237 | 232 |
| 238 int num_tasks = 11; | 233 int num_tasks = 11; |
| 239 Time run_time1, run_time2; | 234 Time run_time1, run_time2; |
| 240 | 235 |
| 241 // Clutter the ML with tasks. | 236 // Clutter the ML with tasks. |
| 242 for (int i = 1; i < num_tasks; ++i) | 237 for (int i = 1; i < num_tasks; ++i) |
| 243 loop.PostTask(FROM_HERE, | 238 loop.task_runner()->PostTask( |
| 244 Bind(&RecordRunTimeFunc, &run_time1, &num_tasks)); | 239 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time1, &num_tasks)); |
| 245 | 240 |
| 246 loop.PostDelayedTask( | 241 loop.task_runner()->PostDelayedTask( |
| 247 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), | 242 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), |
| 248 TimeDelta::FromMilliseconds(1)); | 243 TimeDelta::FromMilliseconds(1)); |
| 249 | 244 |
| 250 loop.Run(); | 245 RunLoop().Run(); |
| 251 EXPECT_EQ(0, num_tasks); | 246 EXPECT_EQ(0, num_tasks); |
| 252 | 247 |
| 253 EXPECT_TRUE(run_time2 > run_time1); | 248 EXPECT_TRUE(run_time2 > run_time1); |
| 254 } | 249 } |
| 255 | 250 |
| 256 void RunTest_PostDelayedTask_SharedTimer(MessagePumpFactory factory) { | 251 void RunTest_PostDelayedTask_SharedTimer(MessagePumpFactory factory) { |
| 257 std::unique_ptr<MessagePump> pump(factory()); | 252 std::unique_ptr<MessagePump> pump(factory()); |
| 258 MessageLoop loop(std::move(pump)); | 253 MessageLoop loop(std::move(pump)); |
| 259 | 254 |
| 260 // Test that the interval of the timer, used to run the next delayed task, is | 255 // Test that the interval of the timer, used to run the next delayed task, is |
| 261 // set to a value corresponding to when the next delayed task should run. | 256 // set to a value corresponding to when the next delayed task should run. |
| 262 | 257 |
| 263 // By setting num_tasks to 1, we ensure that the first task to run causes the | 258 // By setting num_tasks to 1, we ensure that the first task to run causes the |
| 264 // run loop to exit. | 259 // run loop to exit. |
| 265 int num_tasks = 1; | 260 int num_tasks = 1; |
| 266 Time run_time1, run_time2; | 261 Time run_time1, run_time2; |
| 267 | 262 |
| 268 loop.PostDelayedTask( | 263 loop.task_runner()->PostDelayedTask( |
| 269 FROM_HERE, | 264 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), |
| 270 Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), | |
| 271 TimeDelta::FromSeconds(1000)); | 265 TimeDelta::FromSeconds(1000)); |
| 272 loop.PostDelayedTask( | 266 loop.task_runner()->PostDelayedTask( |
| 273 FROM_HERE, | 267 FROM_HERE, Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), |
| 274 Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), | |
| 275 TimeDelta::FromMilliseconds(10)); | 268 TimeDelta::FromMilliseconds(10)); |
| 276 | 269 |
| 277 Time start_time = Time::Now(); | 270 Time start_time = Time::Now(); |
| 278 | 271 |
| 279 loop.Run(); | 272 RunLoop().Run(); |
| 280 EXPECT_EQ(0, num_tasks); | 273 EXPECT_EQ(0, num_tasks); |
| 281 | 274 |
| 282 // Ensure that we ran in far less time than the slower timer. | 275 // Ensure that we ran in far less time than the slower timer. |
| 283 TimeDelta total_time = Time::Now() - start_time; | 276 TimeDelta total_time = Time::Now() - start_time; |
| 284 EXPECT_GT(5000, total_time.InMilliseconds()); | 277 EXPECT_GT(5000, total_time.InMilliseconds()); |
| 285 | 278 |
| 286 // In case both timers somehow run at nearly the same time, sleep a little | 279 // In case both timers somehow run at nearly the same time, sleep a little |
| 287 // and then run all pending to force them both to have run. This is just | 280 // and then run all pending to force them both to have run. This is just |
| 288 // encouraging flakiness if there is any. | 281 // encouraging flakiness if there is any. |
| 289 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); | 282 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 302 : post_on_delete_(post_on_delete), was_deleted_(was_deleted) { | 295 : post_on_delete_(post_on_delete), was_deleted_(was_deleted) { |
| 303 } | 296 } |
| 304 void Run() {} | 297 void Run() {} |
| 305 | 298 |
| 306 private: | 299 private: |
| 307 friend class RefCounted<RecordDeletionProbe>; | 300 friend class RefCounted<RecordDeletionProbe>; |
| 308 | 301 |
| 309 ~RecordDeletionProbe() { | 302 ~RecordDeletionProbe() { |
| 310 *was_deleted_ = true; | 303 *was_deleted_ = true; |
| 311 if (post_on_delete_.get()) | 304 if (post_on_delete_.get()) |
| 312 MessageLoop::current()->PostTask( | 305 MessageLoop::current()->task_runner()->PostTask( |
| 313 FROM_HERE, Bind(&RecordDeletionProbe::Run, post_on_delete_.get())); | 306 FROM_HERE, Bind(&RecordDeletionProbe::Run, post_on_delete_.get())); |
| 314 } | 307 } |
| 315 | 308 |
| 316 scoped_refptr<RecordDeletionProbe> post_on_delete_; | 309 scoped_refptr<RecordDeletionProbe> post_on_delete_; |
| 317 bool* was_deleted_; | 310 bool* was_deleted_; |
| 318 }; | 311 }; |
| 319 | 312 |
| 320 void RunTest_EnsureDeletion(MessagePumpFactory factory) { | 313 void RunTest_EnsureDeletion(MessagePumpFactory factory) { |
| 321 bool a_was_deleted = false; | 314 bool a_was_deleted = false; |
| 322 bool b_was_deleted = false; | 315 bool b_was_deleted = false; |
| 323 { | 316 { |
| 324 std::unique_ptr<MessagePump> pump(factory()); | 317 std::unique_ptr<MessagePump> pump(factory()); |
| 325 MessageLoop loop(std::move(pump)); | 318 MessageLoop loop(std::move(pump)); |
| 326 loop.PostTask( | 319 loop.task_runner()->PostTask( |
| 327 FROM_HERE, Bind(&RecordDeletionProbe::Run, | 320 FROM_HERE, Bind(&RecordDeletionProbe::Run, |
| 328 new RecordDeletionProbe(NULL, &a_was_deleted))); | 321 new RecordDeletionProbe(NULL, &a_was_deleted))); |
| 329 // TODO(ajwong): Do we really need 1000ms here? | 322 // TODO(ajwong): Do we really need 1000ms here? |
| 330 loop.PostDelayedTask( | 323 loop.task_runner()->PostDelayedTask( |
| 331 FROM_HERE, Bind(&RecordDeletionProbe::Run, | 324 FROM_HERE, Bind(&RecordDeletionProbe::Run, |
| 332 new RecordDeletionProbe(NULL, &b_was_deleted)), | 325 new RecordDeletionProbe(NULL, &b_was_deleted)), |
| 333 TimeDelta::FromMilliseconds(1000)); | 326 TimeDelta::FromMilliseconds(1000)); |
| 334 } | 327 } |
| 335 EXPECT_TRUE(a_was_deleted); | 328 EXPECT_TRUE(a_was_deleted); |
| 336 EXPECT_TRUE(b_was_deleted); | 329 EXPECT_TRUE(b_was_deleted); |
| 337 } | 330 } |
| 338 | 331 |
| 339 void RunTest_EnsureDeletion_Chain(MessagePumpFactory factory) { | 332 void RunTest_EnsureDeletion_Chain(MessagePumpFactory factory) { |
| 340 bool a_was_deleted = false; | 333 bool a_was_deleted = false; |
| 341 bool b_was_deleted = false; | 334 bool b_was_deleted = false; |
| 342 bool c_was_deleted = false; | 335 bool c_was_deleted = false; |
| 343 { | 336 { |
| 344 std::unique_ptr<MessagePump> pump(factory()); | 337 std::unique_ptr<MessagePump> pump(factory()); |
| 345 MessageLoop loop(std::move(pump)); | 338 MessageLoop loop(std::move(pump)); |
| 346 // The scoped_refptr for each of the below is held either by the chained | 339 // The scoped_refptr for each of the below is held either by the chained |
| 347 // RecordDeletionProbe, or the bound RecordDeletionProbe::Run() callback. | 340 // RecordDeletionProbe, or the bound RecordDeletionProbe::Run() callback. |
| 348 RecordDeletionProbe* a = new RecordDeletionProbe(NULL, &a_was_deleted); | 341 RecordDeletionProbe* a = new RecordDeletionProbe(NULL, &a_was_deleted); |
| 349 RecordDeletionProbe* b = new RecordDeletionProbe(a, &b_was_deleted); | 342 RecordDeletionProbe* b = new RecordDeletionProbe(a, &b_was_deleted); |
| 350 RecordDeletionProbe* c = new RecordDeletionProbe(b, &c_was_deleted); | 343 RecordDeletionProbe* c = new RecordDeletionProbe(b, &c_was_deleted); |
| 351 loop.PostTask(FROM_HERE, Bind(&RecordDeletionProbe::Run, c)); | 344 loop.task_runner()->PostTask(FROM_HERE, Bind(&RecordDeletionProbe::Run, c)); |
| 352 } | 345 } |
| 353 EXPECT_TRUE(a_was_deleted); | 346 EXPECT_TRUE(a_was_deleted); |
| 354 EXPECT_TRUE(b_was_deleted); | 347 EXPECT_TRUE(b_was_deleted); |
| 355 EXPECT_TRUE(c_was_deleted); | 348 EXPECT_TRUE(c_was_deleted); |
| 356 } | 349 } |
| 357 | 350 |
| 358 void NestingFunc(int* depth) { | 351 void NestingFunc(int* depth) { |
| 359 if (*depth > 0) { | 352 if (*depth > 0) { |
| 360 *depth -= 1; | 353 *depth -= 1; |
| 361 MessageLoop::current()->PostTask(FROM_HERE, | 354 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 362 Bind(&NestingFunc, depth)); | 355 Bind(&NestingFunc, depth)); |
| 363 | 356 |
| 364 MessageLoop::current()->SetNestableTasksAllowed(true); | 357 MessageLoop::current()->SetNestableTasksAllowed(true); |
| 365 MessageLoop::current()->Run(); | 358 RunLoop().Run(); |
| 366 } | 359 } |
| 367 MessageLoop::current()->QuitWhenIdle(); | 360 MessageLoop::current()->QuitWhenIdle(); |
| 368 } | 361 } |
| 369 | 362 |
| 370 void RunTest_Nesting(MessagePumpFactory factory) { | 363 void RunTest_Nesting(MessagePumpFactory factory) { |
| 371 std::unique_ptr<MessagePump> pump(factory()); | 364 std::unique_ptr<MessagePump> pump(factory()); |
| 372 MessageLoop loop(std::move(pump)); | 365 MessageLoop loop(std::move(pump)); |
| 373 | 366 |
| 374 int depth = 100; | 367 int depth = 100; |
| 375 MessageLoop::current()->PostTask(FROM_HERE, | 368 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 376 Bind(&NestingFunc, &depth)); | 369 Bind(&NestingFunc, &depth)); |
| 377 MessageLoop::current()->Run(); | 370 RunLoop().Run(); |
| 378 EXPECT_EQ(depth, 0); | 371 EXPECT_EQ(depth, 0); |
| 379 } | 372 } |
| 380 | 373 |
| 381 // A NestingObserver that tracks the number of nested message loop starts it | 374 // A NestingObserver that tracks the number of nested message loop starts it |
| 382 // has seen. | 375 // has seen. |
| 383 class TestNestingObserver : public MessageLoop::NestingObserver { | 376 class TestNestingObserver : public MessageLoop::NestingObserver { |
| 384 public: | 377 public: |
| 385 TestNestingObserver() {} | 378 TestNestingObserver() {} |
| 386 ~TestNestingObserver() override {} | 379 ~TestNestingObserver() override {} |
| 387 | 380 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 403 // Starts a nested message loop. | 396 // Starts a nested message loop. |
| 404 void RunNestedLoop(TestNestingObserver* observer, | 397 void RunNestedLoop(TestNestingObserver* observer, |
| 405 const Closure& quit_outer_loop) { | 398 const Closure& quit_outer_loop) { |
| 406 // The nested loop hasn't started yet. | 399 // The nested loop hasn't started yet. |
| 407 EXPECT_EQ(0, observer->begin_nested_loop_count()); | 400 EXPECT_EQ(0, observer->begin_nested_loop_count()); |
| 408 | 401 |
| 409 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 402 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 410 RunLoop nested_loop; | 403 RunLoop nested_loop; |
| 411 // Verify that by the time the first task is run the observer has seen the | 404 // Verify that by the time the first task is run the observer has seen the |
| 412 // message loop begin. | 405 // message loop begin. |
| 413 MessageLoop::current()->PostTask(FROM_HERE, | 406 MessageLoop::current()->task_runner()->PostTask( |
| 414 Bind(&ExpectOneBeginNestedLoop, observer)); | 407 FROM_HERE, Bind(&ExpectOneBeginNestedLoop, observer)); |
| 415 MessageLoop::current()->PostTask(FROM_HERE, nested_loop.QuitClosure()); | 408 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 409 nested_loop.QuitClosure()); |
| 416 nested_loop.Run(); | 410 nested_loop.Run(); |
| 417 | 411 |
| 418 // Quitting message loops doesn't change the begin count. | 412 // Quitting message loops doesn't change the begin count. |
| 419 EXPECT_EQ(1, observer->begin_nested_loop_count()); | 413 EXPECT_EQ(1, observer->begin_nested_loop_count()); |
| 420 | 414 |
| 421 quit_outer_loop.Run(); | 415 quit_outer_loop.Run(); |
| 422 } | 416 } |
| 423 | 417 |
| 424 // Tests that a NestingObserver is notified when a nested message loop begins. | 418 // Tests that a NestingObserver is notified when a nested message loop begins. |
| 425 void RunTest_NestingObserver(MessagePumpFactory factory) { | 419 void RunTest_NestingObserver(MessagePumpFactory factory) { |
| 426 std::unique_ptr<MessagePump> pump(factory()); | 420 std::unique_ptr<MessagePump> pump(factory()); |
| 427 MessageLoop outer_loop(std::move(pump)); | 421 MessageLoop outer_loop(std::move(pump)); |
| 428 | 422 |
| 429 // Observe the outer loop for nested message loops beginning. | 423 // Observe the outer loop for nested message loops beginning. |
| 430 TestNestingObserver nesting_observer; | 424 TestNestingObserver nesting_observer; |
| 431 outer_loop.AddNestingObserver(&nesting_observer); | 425 outer_loop.AddNestingObserver(&nesting_observer); |
| 432 | 426 |
| 433 // Post a task that runs a nested message loop. | 427 // Post a task that runs a nested message loop. |
| 434 outer_loop.PostTask(FROM_HERE, Bind(&RunNestedLoop, &nesting_observer, | 428 outer_loop.task_runner()->PostTask(FROM_HERE, |
| 435 outer_loop.QuitWhenIdleClosure())); | 429 Bind(&RunNestedLoop, &nesting_observer, |
| 436 outer_loop.Run(); | 430 outer_loop.QuitWhenIdleClosure())); |
| 431 RunLoop().Run(); |
| 437 | 432 |
| 438 outer_loop.RemoveNestingObserver(&nesting_observer); | 433 outer_loop.RemoveNestingObserver(&nesting_observer); |
| 439 } | 434 } |
| 440 | 435 |
| 441 enum TaskType { | 436 enum TaskType { |
| 442 MESSAGEBOX, | 437 MESSAGEBOX, |
| 443 ENDDIALOG, | 438 ENDDIALOG, |
| 444 RECURSIVE, | 439 RECURSIVE, |
| 445 TIMEDMESSAGELOOP, | 440 TIMEDMESSAGELOOP, |
| 446 QUITMESSAGELOOP, | 441 QUITMESSAGELOOP, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 private: | 511 private: |
| 517 std::vector<TaskItem> task_list_; | 512 std::vector<TaskItem> task_list_; |
| 518 }; | 513 }; |
| 519 | 514 |
| 520 void RecursiveFunc(TaskList* order, int cookie, int depth, | 515 void RecursiveFunc(TaskList* order, int cookie, int depth, |
| 521 bool is_reentrant) { | 516 bool is_reentrant) { |
| 522 order->RecordStart(RECURSIVE, cookie); | 517 order->RecordStart(RECURSIVE, cookie); |
| 523 if (depth > 0) { | 518 if (depth > 0) { |
| 524 if (is_reentrant) | 519 if (is_reentrant) |
| 525 MessageLoop::current()->SetNestableTasksAllowed(true); | 520 MessageLoop::current()->SetNestableTasksAllowed(true); |
| 526 MessageLoop::current()->PostTask( | 521 MessageLoop::current()->task_runner()->PostTask( |
| 527 FROM_HERE, | 522 FROM_HERE, |
| 528 Bind(&RecursiveFunc, order, cookie, depth - 1, is_reentrant)); | 523 Bind(&RecursiveFunc, order, cookie, depth - 1, is_reentrant)); |
| 529 } | 524 } |
| 530 order->RecordEnd(RECURSIVE, cookie); | 525 order->RecordEnd(RECURSIVE, cookie); |
| 531 } | 526 } |
| 532 | 527 |
| 533 void QuitFunc(TaskList* order, int cookie) { | 528 void QuitFunc(TaskList* order, int cookie) { |
| 534 order->RecordStart(QUITMESSAGELOOP, cookie); | 529 order->RecordStart(QUITMESSAGELOOP, cookie); |
| 535 MessageLoop::current()->QuitWhenIdle(); | 530 MessageLoop::current()->QuitWhenIdle(); |
| 536 order->RecordEnd(QUITMESSAGELOOP, cookie); | 531 order->RecordEnd(QUITMESSAGELOOP, cookie); |
| 537 } | 532 } |
| 538 void RunTest_RecursiveDenial1(MessagePumpFactory factory) { | 533 void RunTest_RecursiveDenial1(MessagePumpFactory factory) { |
| 539 std::unique_ptr<MessagePump> pump(factory()); | 534 std::unique_ptr<MessagePump> pump(factory()); |
| 540 MessageLoop loop(std::move(pump)); | 535 MessageLoop loop(std::move(pump)); |
| 541 | 536 |
| 542 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); | 537 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); |
| 543 TaskList order; | 538 TaskList order; |
| 544 MessageLoop::current()->PostTask( | 539 MessageLoop::current()->task_runner()->PostTask( |
| 545 FROM_HERE, | 540 FROM_HERE, Bind(&RecursiveFunc, &order, 1, 2, false)); |
| 546 Bind(&RecursiveFunc, &order, 1, 2, false)); | 541 MessageLoop::current()->task_runner()->PostTask( |
| 547 MessageLoop::current()->PostTask( | 542 FROM_HERE, Bind(&RecursiveFunc, &order, 2, 2, false)); |
| 548 FROM_HERE, | 543 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 549 Bind(&RecursiveFunc, &order, 2, 2, false)); | 544 Bind(&QuitFunc, &order, 3)); |
| 550 MessageLoop::current()->PostTask( | |
| 551 FROM_HERE, | |
| 552 Bind(&QuitFunc, &order, 3)); | |
| 553 | 545 |
| 554 MessageLoop::current()->Run(); | 546 RunLoop().Run(); |
| 555 | 547 |
| 556 // FIFO order. | 548 // FIFO order. |
| 557 ASSERT_EQ(14U, order.Size()); | 549 ASSERT_EQ(14U, order.Size()); |
| 558 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); | 550 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); |
| 559 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); | 551 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); |
| 560 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); | 552 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); |
| 561 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); | 553 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); |
| 562 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); | 554 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); |
| 563 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); | 555 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); |
| 564 EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true)); | 556 EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 581 order->RecordStart(ORDERED, cookie); | 573 order->RecordStart(ORDERED, cookie); |
| 582 order->RecordEnd(ORDERED, cookie); | 574 order->RecordEnd(ORDERED, cookie); |
| 583 } | 575 } |
| 584 | 576 |
| 585 void RunTest_RecursiveDenial3(MessagePumpFactory factory) { | 577 void RunTest_RecursiveDenial3(MessagePumpFactory factory) { |
| 586 std::unique_ptr<MessagePump> pump(factory()); | 578 std::unique_ptr<MessagePump> pump(factory()); |
| 587 MessageLoop loop(std::move(pump)); | 579 MessageLoop loop(std::move(pump)); |
| 588 | 580 |
| 589 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); | 581 EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed()); |
| 590 TaskList order; | 582 TaskList order; |
| 591 MessageLoop::current()->PostTask( | 583 MessageLoop::current()->task_runner()->PostTask( |
| 592 FROM_HERE, Bind(&RecursiveSlowFunc, &order, 1, 2, false)); | 584 FROM_HERE, Bind(&RecursiveSlowFunc, &order, 1, 2, false)); |
| 593 MessageLoop::current()->PostTask( | 585 MessageLoop::current()->task_runner()->PostTask( |
| 594 FROM_HERE, Bind(&RecursiveSlowFunc, &order, 2, 2, false)); | 586 FROM_HERE, Bind(&RecursiveSlowFunc, &order, 2, 2, false)); |
| 595 MessageLoop::current()->PostDelayedTask( | 587 MessageLoop::current()->task_runner()->PostDelayedTask( |
| 596 FROM_HERE, | 588 FROM_HERE, Bind(&OrderedFunc, &order, 3), TimeDelta::FromMilliseconds(5)); |
| 597 Bind(&OrderedFunc, &order, 3), | 589 MessageLoop::current()->task_runner()->PostDelayedTask( |
| 598 TimeDelta::FromMilliseconds(5)); | 590 FROM_HERE, Bind(&QuitFunc, &order, 4), TimeDelta::FromMilliseconds(5)); |
| 599 MessageLoop::current()->PostDelayedTask( | |
| 600 FROM_HERE, | |
| 601 Bind(&QuitFunc, &order, 4), | |
| 602 TimeDelta::FromMilliseconds(5)); | |
| 603 | 591 |
| 604 MessageLoop::current()->Run(); | 592 RunLoop().Run(); |
| 605 | 593 |
| 606 // FIFO order. | 594 // FIFO order. |
| 607 ASSERT_EQ(16U, order.Size()); | 595 ASSERT_EQ(16U, order.Size()); |
| 608 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); | 596 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); |
| 609 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); | 597 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); |
| 610 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); | 598 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); |
| 611 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); | 599 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); |
| 612 EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 1, true)); | 600 EXPECT_EQ(order.Get(4), TaskItem(RECURSIVE, 1, true)); |
| 613 EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 1, false)); | 601 EXPECT_EQ(order.Get(5), TaskItem(RECURSIVE, 1, false)); |
| 614 EXPECT_EQ(order.Get(6), TaskItem(ORDERED, 3, true)); | 602 EXPECT_EQ(order.Get(6), TaskItem(ORDERED, 3, true)); |
| 615 EXPECT_EQ(order.Get(7), TaskItem(ORDERED, 3, false)); | 603 EXPECT_EQ(order.Get(7), TaskItem(ORDERED, 3, false)); |
| 616 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true)); | 604 EXPECT_EQ(order.Get(8), TaskItem(RECURSIVE, 2, true)); |
| 617 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false)); | 605 EXPECT_EQ(order.Get(9), TaskItem(RECURSIVE, 2, false)); |
| 618 EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 4, true)); | 606 EXPECT_EQ(order.Get(10), TaskItem(QUITMESSAGELOOP, 4, true)); |
| 619 EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 4, false)); | 607 EXPECT_EQ(order.Get(11), TaskItem(QUITMESSAGELOOP, 4, false)); |
| 620 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 1, true)); | 608 EXPECT_EQ(order.Get(12), TaskItem(RECURSIVE, 1, true)); |
| 621 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, false)); | 609 EXPECT_EQ(order.Get(13), TaskItem(RECURSIVE, 1, false)); |
| 622 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 2, true)); | 610 EXPECT_EQ(order.Get(14), TaskItem(RECURSIVE, 2, true)); |
| 623 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 2, false)); | 611 EXPECT_EQ(order.Get(15), TaskItem(RECURSIVE, 2, false)); |
| 624 } | 612 } |
| 625 | 613 |
| 626 void RunTest_RecursiveSupport1(MessagePumpFactory factory) { | 614 void RunTest_RecursiveSupport1(MessagePumpFactory factory) { |
| 627 std::unique_ptr<MessagePump> pump(factory()); | 615 std::unique_ptr<MessagePump> pump(factory()); |
| 628 MessageLoop loop(std::move(pump)); | 616 MessageLoop loop(std::move(pump)); |
| 629 | 617 |
| 630 TaskList order; | 618 TaskList order; |
| 631 MessageLoop::current()->PostTask( | 619 MessageLoop::current()->task_runner()->PostTask( |
| 632 FROM_HERE, Bind(&RecursiveFunc, &order, 1, 2, true)); | 620 FROM_HERE, Bind(&RecursiveFunc, &order, 1, 2, true)); |
| 633 MessageLoop::current()->PostTask( | 621 MessageLoop::current()->task_runner()->PostTask( |
| 634 FROM_HERE, Bind(&RecursiveFunc, &order, 2, 2, true)); | 622 FROM_HERE, Bind(&RecursiveFunc, &order, 2, 2, true)); |
| 635 MessageLoop::current()->PostTask( | 623 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 636 FROM_HERE, Bind(&QuitFunc, &order, 3)); | 624 Bind(&QuitFunc, &order, 3)); |
| 637 | 625 |
| 638 MessageLoop::current()->Run(); | 626 RunLoop().Run(); |
| 639 | 627 |
| 640 // FIFO order. | 628 // FIFO order. |
| 641 ASSERT_EQ(14U, order.Size()); | 629 ASSERT_EQ(14U, order.Size()); |
| 642 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); | 630 EXPECT_EQ(order.Get(0), TaskItem(RECURSIVE, 1, true)); |
| 643 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); | 631 EXPECT_EQ(order.Get(1), TaskItem(RECURSIVE, 1, false)); |
| 644 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); | 632 EXPECT_EQ(order.Get(2), TaskItem(RECURSIVE, 2, true)); |
| 645 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); | 633 EXPECT_EQ(order.Get(3), TaskItem(RECURSIVE, 2, false)); |
| 646 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); | 634 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); |
| 647 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); | 635 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); |
| 648 EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true)); | 636 EXPECT_EQ(order.Get(6), TaskItem(RECURSIVE, 1, true)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 663 TaskList order; | 651 TaskList order; |
| 664 | 652 |
| 665 MessageLoop::current()->task_runner()->PostNonNestableTask( | 653 MessageLoop::current()->task_runner()->PostNonNestableTask( |
| 666 FROM_HERE, | 654 FROM_HERE, |
| 667 Bind(&OrderedFunc, &order, 1)); | 655 Bind(&OrderedFunc, &order, 1)); |
| 668 MessageLoop::current()->task_runner()->PostTask( | 656 MessageLoop::current()->task_runner()->PostTask( |
| 669 FROM_HERE, | 657 FROM_HERE, |
| 670 Bind(&OrderedFunc, &order, 2)); | 658 Bind(&OrderedFunc, &order, 2)); |
| 671 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, | 659 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 672 Bind(&QuitFunc, &order, 3)); | 660 Bind(&QuitFunc, &order, 3)); |
| 673 MessageLoop::current()->Run(); | 661 RunLoop().Run(); |
| 674 | 662 |
| 675 // FIFO order. | 663 // FIFO order. |
| 676 ASSERT_EQ(6U, order.Size()); | 664 ASSERT_EQ(6U, order.Size()); |
| 677 EXPECT_EQ(order.Get(0), TaskItem(ORDERED, 1, true)); | 665 EXPECT_EQ(order.Get(0), TaskItem(ORDERED, 1, true)); |
| 678 EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 1, false)); | 666 EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 1, false)); |
| 679 EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 2, true)); | 667 EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 2, true)); |
| 680 EXPECT_EQ(order.Get(3), TaskItem(ORDERED, 2, false)); | 668 EXPECT_EQ(order.Get(3), TaskItem(ORDERED, 2, false)); |
| 681 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); | 669 EXPECT_EQ(order.Get(4), TaskItem(QUITMESSAGELOOP, 3, true)); |
| 682 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); | 670 EXPECT_EQ(order.Get(5), TaskItem(QUITMESSAGELOOP, 3, false)); |
| 683 } | 671 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 MessageLoop::current()->task_runner()->PostTask( | 704 MessageLoop::current()->task_runner()->PostTask( |
| 717 FROM_HERE, | 705 FROM_HERE, |
| 718 Bind(&SleepFunc, &order, 4, TimeDelta::FromMilliseconds(50))); | 706 Bind(&SleepFunc, &order, 4, TimeDelta::FromMilliseconds(50))); |
| 719 MessageLoop::current()->task_runner()->PostTask( | 707 MessageLoop::current()->task_runner()->PostTask( |
| 720 FROM_HERE, | 708 FROM_HERE, |
| 721 Bind(&OrderedFunc, &order, 5)); | 709 Bind(&OrderedFunc, &order, 5)); |
| 722 MessageLoop::current()->task_runner()->PostNonNestableTask( | 710 MessageLoop::current()->task_runner()->PostNonNestableTask( |
| 723 FROM_HERE, | 711 FROM_HERE, |
| 724 Bind(&QuitFunc, &order, 6)); | 712 Bind(&QuitFunc, &order, 6)); |
| 725 | 713 |
| 726 MessageLoop::current()->Run(); | 714 RunLoop().Run(); |
| 727 | 715 |
| 728 // FIFO order. | 716 // FIFO order. |
| 729 ASSERT_EQ(12U, order.Size()); | 717 ASSERT_EQ(12U, order.Size()); |
| 730 EXPECT_EQ(order.Get(0), TaskItem(PUMPS, 1, true)); | 718 EXPECT_EQ(order.Get(0), TaskItem(PUMPS, 1, true)); |
| 731 EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 3, true)); | 719 EXPECT_EQ(order.Get(1), TaskItem(ORDERED, 3, true)); |
| 732 EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 3, false)); | 720 EXPECT_EQ(order.Get(2), TaskItem(ORDERED, 3, false)); |
| 733 EXPECT_EQ(order.Get(3), TaskItem(SLEEP, 4, true)); | 721 EXPECT_EQ(order.Get(3), TaskItem(SLEEP, 4, true)); |
| 734 EXPECT_EQ(order.Get(4), TaskItem(SLEEP, 4, false)); | 722 EXPECT_EQ(order.Get(4), TaskItem(SLEEP, 4, false)); |
| 735 EXPECT_EQ(order.Get(5), TaskItem(ORDERED, 5, true)); | 723 EXPECT_EQ(order.Get(5), TaskItem(ORDERED, 5, true)); |
| 736 EXPECT_EQ(order.Get(6), TaskItem(ORDERED, 5, false)); | 724 EXPECT_EQ(order.Get(6), TaskItem(ORDERED, 5, false)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 755 } | 743 } |
| 756 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. | 744 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. |
| 757 void RunTest_QuitNow(MessagePumpFactory factory) { | 745 void RunTest_QuitNow(MessagePumpFactory factory) { |
| 758 std::unique_ptr<MessagePump> pump(factory()); | 746 std::unique_ptr<MessagePump> pump(factory()); |
| 759 MessageLoop loop(std::move(pump)); | 747 MessageLoop loop(std::move(pump)); |
| 760 | 748 |
| 761 TaskList order; | 749 TaskList order; |
| 762 | 750 |
| 763 RunLoop run_loop; | 751 RunLoop run_loop; |
| 764 | 752 |
| 765 MessageLoop::current()->PostTask(FROM_HERE, | 753 MessageLoop::current()->task_runner()->PostTask( |
| 766 Bind(&FuncThatRuns, &order, 1, Unretained(&run_loop))); | 754 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&run_loop))); |
| 767 MessageLoop::current()->PostTask( | 755 MessageLoop::current()->task_runner()->PostTask( |
| 768 FROM_HERE, Bind(&OrderedFunc, &order, 2)); | 756 FROM_HERE, Bind(&OrderedFunc, &order, 2)); |
| 769 MessageLoop::current()->PostTask( | 757 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 770 FROM_HERE, Bind(&FuncThatQuitsNow)); | 758 Bind(&FuncThatQuitsNow)); |
| 771 MessageLoop::current()->PostTask( | 759 MessageLoop::current()->task_runner()->PostTask( |
| 772 FROM_HERE, Bind(&OrderedFunc, &order, 3)); | 760 FROM_HERE, Bind(&OrderedFunc, &order, 3)); |
| 773 MessageLoop::current()->PostTask( | 761 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 774 FROM_HERE, Bind(&FuncThatQuitsNow)); | 762 Bind(&FuncThatQuitsNow)); |
| 775 MessageLoop::current()->PostTask( | 763 MessageLoop::current()->task_runner()->PostTask( |
| 776 FROM_HERE, Bind(&OrderedFunc, &order, 4)); // never runs | 764 FROM_HERE, Bind(&OrderedFunc, &order, 4)); // never runs |
| 777 | 765 |
| 778 MessageLoop::current()->Run(); | 766 RunLoop().Run(); |
| 779 | 767 |
| 780 ASSERT_EQ(6U, order.Size()); | 768 ASSERT_EQ(6U, order.Size()); |
| 781 int task_index = 0; | 769 int task_index = 0; |
| 782 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); | 770 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 783 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); | 771 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); |
| 784 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); | 772 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); |
| 785 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); | 773 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 786 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true)); | 774 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true)); |
| 787 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false)); | 775 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false)); |
| 788 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); | 776 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 789 } | 777 } |
| 790 | 778 |
| 791 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. | 779 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. |
| 792 void RunTest_RunLoopQuitTop(MessagePumpFactory factory) { | 780 void RunTest_RunLoopQuitTop(MessagePumpFactory factory) { |
| 793 std::unique_ptr<MessagePump> pump(factory()); | 781 std::unique_ptr<MessagePump> pump(factory()); |
| 794 MessageLoop loop(std::move(pump)); | 782 MessageLoop loop(std::move(pump)); |
| 795 | 783 |
| 796 TaskList order; | 784 TaskList order; |
| 797 | 785 |
| 798 RunLoop outer_run_loop; | 786 RunLoop outer_run_loop; |
| 799 RunLoop nested_run_loop; | 787 RunLoop nested_run_loop; |
| 800 | 788 |
| 801 MessageLoop::current()->PostTask(FROM_HERE, | 789 MessageLoop::current()->task_runner()->PostTask( |
| 802 Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop))); | 790 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop))); |
| 803 MessageLoop::current()->PostTask( | 791 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 804 FROM_HERE, outer_run_loop.QuitClosure()); | 792 outer_run_loop.QuitClosure()); |
| 805 MessageLoop::current()->PostTask( | 793 MessageLoop::current()->task_runner()->PostTask( |
| 806 FROM_HERE, Bind(&OrderedFunc, &order, 2)); | 794 FROM_HERE, Bind(&OrderedFunc, &order, 2)); |
| 807 MessageLoop::current()->PostTask( | 795 MessageLoop::current()->task_runner()->PostTask( |
| 808 FROM_HERE, nested_run_loop.QuitClosure()); | 796 FROM_HERE, nested_run_loop.QuitClosure()); |
| 809 | 797 |
| 810 outer_run_loop.Run(); | 798 outer_run_loop.Run(); |
| 811 | 799 |
| 812 ASSERT_EQ(4U, order.Size()); | 800 ASSERT_EQ(4U, order.Size()); |
| 813 int task_index = 0; | 801 int task_index = 0; |
| 814 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); | 802 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 815 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); | 803 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); |
| 816 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); | 804 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); |
| 817 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); | 805 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 818 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); | 806 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 819 } | 807 } |
| 820 | 808 |
| 821 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. | 809 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. |
| 822 void RunTest_RunLoopQuitNested(MessagePumpFactory factory) { | 810 void RunTest_RunLoopQuitNested(MessagePumpFactory factory) { |
| 823 std::unique_ptr<MessagePump> pump(factory()); | 811 std::unique_ptr<MessagePump> pump(factory()); |
| 824 MessageLoop loop(std::move(pump)); | 812 MessageLoop loop(std::move(pump)); |
| 825 | 813 |
| 826 TaskList order; | 814 TaskList order; |
| 827 | 815 |
| 828 RunLoop outer_run_loop; | 816 RunLoop outer_run_loop; |
| 829 RunLoop nested_run_loop; | 817 RunLoop nested_run_loop; |
| 830 | 818 |
| 831 MessageLoop::current()->PostTask(FROM_HERE, | 819 MessageLoop::current()->task_runner()->PostTask( |
| 832 Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop))); | 820 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop))); |
| 833 MessageLoop::current()->PostTask( | 821 MessageLoop::current()->task_runner()->PostTask( |
| 834 FROM_HERE, nested_run_loop.QuitClosure()); | 822 FROM_HERE, nested_run_loop.QuitClosure()); |
| 835 MessageLoop::current()->PostTask( | 823 MessageLoop::current()->task_runner()->PostTask( |
| 836 FROM_HERE, Bind(&OrderedFunc, &order, 2)); | 824 FROM_HERE, Bind(&OrderedFunc, &order, 2)); |
| 837 MessageLoop::current()->PostTask( | 825 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 838 FROM_HERE, outer_run_loop.QuitClosure()); | 826 outer_run_loop.QuitClosure()); |
| 839 | 827 |
| 840 outer_run_loop.Run(); | 828 outer_run_loop.Run(); |
| 841 | 829 |
| 842 ASSERT_EQ(4U, order.Size()); | 830 ASSERT_EQ(4U, order.Size()); |
| 843 int task_index = 0; | 831 int task_index = 0; |
| 844 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); | 832 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 845 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); | 833 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 846 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); | 834 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); |
| 847 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); | 835 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); |
| 848 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); | 836 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 849 } | 837 } |
| 850 | 838 |
| 851 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. | 839 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. |
| 852 void RunTest_RunLoopQuitBogus(MessagePumpFactory factory) { | 840 void RunTest_RunLoopQuitBogus(MessagePumpFactory factory) { |
| 853 std::unique_ptr<MessagePump> pump(factory()); | 841 std::unique_ptr<MessagePump> pump(factory()); |
| 854 MessageLoop loop(std::move(pump)); | 842 MessageLoop loop(std::move(pump)); |
| 855 | 843 |
| 856 TaskList order; | 844 TaskList order; |
| 857 | 845 |
| 858 RunLoop outer_run_loop; | 846 RunLoop outer_run_loop; |
| 859 RunLoop nested_run_loop; | 847 RunLoop nested_run_loop; |
| 860 RunLoop bogus_run_loop; | 848 RunLoop bogus_run_loop; |
| 861 | 849 |
| 862 MessageLoop::current()->PostTask(FROM_HERE, | 850 MessageLoop::current()->task_runner()->PostTask( |
| 863 Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop))); | 851 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop))); |
| 864 MessageLoop::current()->PostTask( | 852 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 865 FROM_HERE, bogus_run_loop.QuitClosure()); | 853 bogus_run_loop.QuitClosure()); |
| 866 MessageLoop::current()->PostTask( | 854 MessageLoop::current()->task_runner()->PostTask( |
| 867 FROM_HERE, Bind(&OrderedFunc, &order, 2)); | 855 FROM_HERE, Bind(&OrderedFunc, &order, 2)); |
| 868 MessageLoop::current()->PostTask( | 856 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 869 FROM_HERE, outer_run_loop.QuitClosure()); | 857 outer_run_loop.QuitClosure()); |
| 870 MessageLoop::current()->PostTask( | 858 MessageLoop::current()->task_runner()->PostTask( |
| 871 FROM_HERE, nested_run_loop.QuitClosure()); | 859 FROM_HERE, nested_run_loop.QuitClosure()); |
| 872 | 860 |
| 873 outer_run_loop.Run(); | 861 outer_run_loop.Run(); |
| 874 | 862 |
| 875 ASSERT_EQ(4U, order.Size()); | 863 ASSERT_EQ(4U, order.Size()); |
| 876 int task_index = 0; | 864 int task_index = 0; |
| 877 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); | 865 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 878 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); | 866 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); |
| 879 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); | 867 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); |
| 880 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); | 868 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 881 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); | 869 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 882 } | 870 } |
| 883 | 871 |
| 884 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. | 872 // Tests RunLoopQuit only quits the corresponding MessageLoop::Run. |
| 885 void RunTest_RunLoopQuitDeep(MessagePumpFactory factory) { | 873 void RunTest_RunLoopQuitDeep(MessagePumpFactory factory) { |
| 886 std::unique_ptr<MessagePump> pump(factory()); | 874 std::unique_ptr<MessagePump> pump(factory()); |
| 887 MessageLoop loop(std::move(pump)); | 875 MessageLoop loop(std::move(pump)); |
| 888 | 876 |
| 889 TaskList order; | 877 TaskList order; |
| 890 | 878 |
| 891 RunLoop outer_run_loop; | 879 RunLoop outer_run_loop; |
| 892 RunLoop nested_loop1; | 880 RunLoop nested_loop1; |
| 893 RunLoop nested_loop2; | 881 RunLoop nested_loop2; |
| 894 RunLoop nested_loop3; | 882 RunLoop nested_loop3; |
| 895 RunLoop nested_loop4; | 883 RunLoop nested_loop4; |
| 896 | 884 |
| 897 MessageLoop::current()->PostTask(FROM_HERE, | 885 MessageLoop::current()->task_runner()->PostTask( |
| 898 Bind(&FuncThatRuns, &order, 1, Unretained(&nested_loop1))); | 886 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&nested_loop1))); |
| 899 MessageLoop::current()->PostTask(FROM_HERE, | 887 MessageLoop::current()->task_runner()->PostTask( |
| 900 Bind(&FuncThatRuns, &order, 2, Unretained(&nested_loop2))); | 888 FROM_HERE, Bind(&FuncThatRuns, &order, 2, Unretained(&nested_loop2))); |
| 901 MessageLoop::current()->PostTask(FROM_HERE, | 889 MessageLoop::current()->task_runner()->PostTask( |
| 902 Bind(&FuncThatRuns, &order, 3, Unretained(&nested_loop3))); | 890 FROM_HERE, Bind(&FuncThatRuns, &order, 3, Unretained(&nested_loop3))); |
| 903 MessageLoop::current()->PostTask(FROM_HERE, | 891 MessageLoop::current()->task_runner()->PostTask( |
| 904 Bind(&FuncThatRuns, &order, 4, Unretained(&nested_loop4))); | 892 FROM_HERE, Bind(&FuncThatRuns, &order, 4, Unretained(&nested_loop4))); |
| 905 MessageLoop::current()->PostTask( | 893 MessageLoop::current()->task_runner()->PostTask( |
| 906 FROM_HERE, Bind(&OrderedFunc, &order, 5)); | 894 FROM_HERE, Bind(&OrderedFunc, &order, 5)); |
| 907 MessageLoop::current()->PostTask( | 895 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 908 FROM_HERE, outer_run_loop.QuitClosure()); | 896 outer_run_loop.QuitClosure()); |
| 909 MessageLoop::current()->PostTask( | 897 MessageLoop::current()->task_runner()->PostTask( |
| 910 FROM_HERE, Bind(&OrderedFunc, &order, 6)); | 898 FROM_HERE, Bind(&OrderedFunc, &order, 6)); |
| 911 MessageLoop::current()->PostTask( | 899 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 912 FROM_HERE, nested_loop1.QuitClosure()); | 900 nested_loop1.QuitClosure()); |
| 913 MessageLoop::current()->PostTask( | 901 MessageLoop::current()->task_runner()->PostTask( |
| 914 FROM_HERE, Bind(&OrderedFunc, &order, 7)); | 902 FROM_HERE, Bind(&OrderedFunc, &order, 7)); |
| 915 MessageLoop::current()->PostTask( | 903 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 916 FROM_HERE, nested_loop2.QuitClosure()); | 904 nested_loop2.QuitClosure()); |
| 917 MessageLoop::current()->PostTask( | 905 MessageLoop::current()->task_runner()->PostTask( |
| 918 FROM_HERE, Bind(&OrderedFunc, &order, 8)); | 906 FROM_HERE, Bind(&OrderedFunc, &order, 8)); |
| 919 MessageLoop::current()->PostTask( | 907 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 920 FROM_HERE, nested_loop3.QuitClosure()); | 908 nested_loop3.QuitClosure()); |
| 921 MessageLoop::current()->PostTask( | 909 MessageLoop::current()->task_runner()->PostTask( |
| 922 FROM_HERE, Bind(&OrderedFunc, &order, 9)); | 910 FROM_HERE, Bind(&OrderedFunc, &order, 9)); |
| 923 MessageLoop::current()->PostTask( | 911 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 924 FROM_HERE, nested_loop4.QuitClosure()); | 912 nested_loop4.QuitClosure()); |
| 925 MessageLoop::current()->PostTask( | 913 MessageLoop::current()->task_runner()->PostTask( |
| 926 FROM_HERE, Bind(&OrderedFunc, &order, 10)); | 914 FROM_HERE, Bind(&OrderedFunc, &order, 10)); |
| 927 | 915 |
| 928 outer_run_loop.Run(); | 916 outer_run_loop.Run(); |
| 929 | 917 |
| 930 ASSERT_EQ(18U, order.Size()); | 918 ASSERT_EQ(18U, order.Size()); |
| 931 int task_index = 0; | 919 int task_index = 0; |
| 932 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); | 920 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 933 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 2, true)); | 921 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 2, true)); |
| 934 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 3, true)); | 922 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 3, true)); |
| 935 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 4, true)); | 923 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 4, true)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 954 void RunTest_RunLoopQuitOrderBefore(MessagePumpFactory factory) { | 942 void RunTest_RunLoopQuitOrderBefore(MessagePumpFactory factory) { |
| 955 std::unique_ptr<MessagePump> pump(factory()); | 943 std::unique_ptr<MessagePump> pump(factory()); |
| 956 MessageLoop loop(std::move(pump)); | 944 MessageLoop loop(std::move(pump)); |
| 957 | 945 |
| 958 TaskList order; | 946 TaskList order; |
| 959 | 947 |
| 960 RunLoop run_loop; | 948 RunLoop run_loop; |
| 961 | 949 |
| 962 run_loop.Quit(); | 950 run_loop.Quit(); |
| 963 | 951 |
| 964 MessageLoop::current()->PostTask( | 952 MessageLoop::current()->task_runner()->PostTask( |
| 965 FROM_HERE, Bind(&OrderedFunc, &order, 1)); // never runs | 953 FROM_HERE, Bind(&OrderedFunc, &order, 1)); // never runs |
| 966 MessageLoop::current()->PostTask( | 954 MessageLoop::current()->task_runner()->PostTask( |
| 967 FROM_HERE, Bind(&FuncThatQuitsNow)); // never runs | 955 FROM_HERE, Bind(&FuncThatQuitsNow)); // never runs |
| 968 | 956 |
| 969 run_loop.Run(); | 957 run_loop.Run(); |
| 970 | 958 |
| 971 ASSERT_EQ(0U, order.Size()); | 959 ASSERT_EQ(0U, order.Size()); |
| 972 } | 960 } |
| 973 | 961 |
| 974 // Tests RunLoopQuit works during RunWithID. | 962 // Tests RunLoopQuit works during RunWithID. |
| 975 void RunTest_RunLoopQuitOrderDuring(MessagePumpFactory factory) { | 963 void RunTest_RunLoopQuitOrderDuring(MessagePumpFactory factory) { |
| 976 std::unique_ptr<MessagePump> pump(factory()); | 964 std::unique_ptr<MessagePump> pump(factory()); |
| 977 MessageLoop loop(std::move(pump)); | 965 MessageLoop loop(std::move(pump)); |
| 978 | 966 |
| 979 TaskList order; | 967 TaskList order; |
| 980 | 968 |
| 981 RunLoop run_loop; | 969 RunLoop run_loop; |
| 982 | 970 |
| 983 MessageLoop::current()->PostTask( | 971 MessageLoop::current()->task_runner()->PostTask( |
| 984 FROM_HERE, Bind(&OrderedFunc, &order, 1)); | 972 FROM_HERE, Bind(&OrderedFunc, &order, 1)); |
| 985 MessageLoop::current()->PostTask( | 973 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 986 FROM_HERE, run_loop.QuitClosure()); | 974 run_loop.QuitClosure()); |
| 987 MessageLoop::current()->PostTask( | 975 MessageLoop::current()->task_runner()->PostTask( |
| 988 FROM_HERE, Bind(&OrderedFunc, &order, 2)); // never runs | 976 FROM_HERE, Bind(&OrderedFunc, &order, 2)); // never runs |
| 989 MessageLoop::current()->PostTask( | 977 MessageLoop::current()->task_runner()->PostTask( |
| 990 FROM_HERE, Bind(&FuncThatQuitsNow)); // never runs | 978 FROM_HERE, Bind(&FuncThatQuitsNow)); // never runs |
| 991 | 979 |
| 992 run_loop.Run(); | 980 run_loop.Run(); |
| 993 | 981 |
| 994 ASSERT_EQ(2U, order.Size()); | 982 ASSERT_EQ(2U, order.Size()); |
| 995 int task_index = 0; | 983 int task_index = 0; |
| 996 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, true)); | 984 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, true)); |
| 997 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, false)); | 985 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 1, false)); |
| 998 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); | 986 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 999 } | 987 } |
| 1000 | 988 |
| 1001 // Tests RunLoopQuit works after RunWithID. | 989 // Tests RunLoopQuit works after RunWithID. |
| 1002 void RunTest_RunLoopQuitOrderAfter(MessagePumpFactory factory) { | 990 void RunTest_RunLoopQuitOrderAfter(MessagePumpFactory factory) { |
| 1003 std::unique_ptr<MessagePump> pump(factory()); | 991 std::unique_ptr<MessagePump> pump(factory()); |
| 1004 MessageLoop loop(std::move(pump)); | 992 MessageLoop loop(std::move(pump)); |
| 1005 | 993 |
| 1006 TaskList order; | 994 TaskList order; |
| 1007 | 995 |
| 1008 RunLoop run_loop; | 996 RunLoop run_loop; |
| 1009 | 997 |
| 1010 MessageLoop::current()->PostTask(FROM_HERE, | 998 MessageLoop::current()->task_runner()->PostTask( |
| 1011 Bind(&FuncThatRuns, &order, 1, Unretained(&run_loop))); | 999 FROM_HERE, Bind(&FuncThatRuns, &order, 1, Unretained(&run_loop))); |
| 1012 MessageLoop::current()->PostTask( | 1000 MessageLoop::current()->task_runner()->PostTask( |
| 1013 FROM_HERE, Bind(&OrderedFunc, &order, 2)); | 1001 FROM_HERE, Bind(&OrderedFunc, &order, 2)); |
| 1014 MessageLoop::current()->PostTask( | 1002 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 1015 FROM_HERE, Bind(&FuncThatQuitsNow)); | 1003 Bind(&FuncThatQuitsNow)); |
| 1016 MessageLoop::current()->PostTask( | 1004 MessageLoop::current()->task_runner()->PostTask( |
| 1017 FROM_HERE, Bind(&OrderedFunc, &order, 3)); | 1005 FROM_HERE, Bind(&OrderedFunc, &order, 3)); |
| 1018 MessageLoop::current()->PostTask( | 1006 MessageLoop::current()->task_runner()->PostTask( |
| 1019 FROM_HERE, run_loop.QuitClosure()); // has no affect | 1007 FROM_HERE, run_loop.QuitClosure()); // has no affect |
| 1020 MessageLoop::current()->PostTask( | 1008 MessageLoop::current()->task_runner()->PostTask( |
| 1021 FROM_HERE, Bind(&OrderedFunc, &order, 4)); | 1009 FROM_HERE, Bind(&OrderedFunc, &order, 4)); |
| 1022 MessageLoop::current()->PostTask( | 1010 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 1023 FROM_HERE, Bind(&FuncThatQuitsNow)); | 1011 Bind(&FuncThatQuitsNow)); |
| 1024 | 1012 |
| 1025 RunLoop outer_run_loop; | 1013 RunLoop outer_run_loop; |
| 1026 outer_run_loop.Run(); | 1014 outer_run_loop.Run(); |
| 1027 | 1015 |
| 1028 ASSERT_EQ(8U, order.Size()); | 1016 ASSERT_EQ(8U, order.Size()); |
| 1029 int task_index = 0; | 1017 int task_index = 0; |
| 1030 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); | 1018 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, true)); |
| 1031 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); | 1019 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, true)); |
| 1032 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); | 1020 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 2, false)); |
| 1033 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); | 1021 EXPECT_EQ(order.Get(task_index++), TaskItem(RUNS, 1, false)); |
| 1034 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true)); | 1022 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, true)); |
| 1035 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false)); | 1023 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 3, false)); |
| 1036 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, true)); | 1024 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, true)); |
| 1037 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, false)); | 1025 EXPECT_EQ(order.Get(task_index++), TaskItem(ORDERED, 4, false)); |
| 1038 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); | 1026 EXPECT_EQ(static_cast<size_t>(task_index), order.Size()); |
| 1039 } | 1027 } |
| 1040 | 1028 |
| 1041 void PostNTasksThenQuit(int posts_remaining) { | 1029 void PostNTasksThenQuit(int posts_remaining) { |
| 1042 if (posts_remaining > 1) { | 1030 if (posts_remaining > 1) { |
| 1043 MessageLoop::current()->PostTask( | 1031 MessageLoop::current()->task_runner()->PostTask( |
| 1044 FROM_HERE, | 1032 FROM_HERE, Bind(&PostNTasksThenQuit, posts_remaining - 1)); |
| 1045 Bind(&PostNTasksThenQuit, posts_remaining - 1)); | |
| 1046 } else { | 1033 } else { |
| 1047 MessageLoop::current()->QuitWhenIdle(); | 1034 MessageLoop::current()->QuitWhenIdle(); |
| 1048 } | 1035 } |
| 1049 } | 1036 } |
| 1050 | 1037 |
| 1051 // There was a bug in the MessagePumpGLib where posting tasks recursively | 1038 // There was a bug in the MessagePumpGLib where posting tasks recursively |
| 1052 // caused the message loop to hang, due to the buffer of the internal pipe | 1039 // caused the message loop to hang, due to the buffer of the internal pipe |
| 1053 // becoming full. Test all MessageLoop types to ensure this issue does not | 1040 // becoming full. Test all MessageLoop types to ensure this issue does not |
| 1054 // exist in other MessagePumps. | 1041 // exist in other MessagePumps. |
| 1055 // | 1042 // |
| 1056 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one | 1043 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one |
| 1057 // byte accumulated in the pipe per two posts, so we should repeat 128K | 1044 // byte accumulated in the pipe per two posts, so we should repeat 128K |
| 1058 // times to reproduce the bug. | 1045 // times to reproduce the bug. |
| 1059 void RunTest_RecursivePosts(MessagePumpFactory factory) { | 1046 void RunTest_RecursivePosts(MessagePumpFactory factory) { |
| 1060 const int kNumTimes = 1 << 17; | 1047 const int kNumTimes = 1 << 17; |
| 1061 std::unique_ptr<MessagePump> pump(factory()); | 1048 std::unique_ptr<MessagePump> pump(factory()); |
| 1062 MessageLoop loop(std::move(pump)); | 1049 MessageLoop loop(std::move(pump)); |
| 1063 loop.PostTask(FROM_HERE, Bind(&PostNTasksThenQuit, kNumTimes)); | 1050 loop.task_runner()->PostTask(FROM_HERE, Bind(&PostNTasksThenQuit, kNumTimes)); |
| 1064 loop.Run(); | 1051 RunLoop().Run(); |
| 1065 } | 1052 } |
| 1066 | 1053 |
| 1067 } // namespace test | 1054 } // namespace test |
| 1068 } // namespace base | 1055 } // namespace base |
| OLD | NEW |