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