OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 SocketAddress address() const { return socket_->GetLocalAddress(); } | 66 SocketAddress address() const { return socket_->GetLocalAddress(); } |
67 | 67 |
68 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t size, | 68 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t size, |
69 const SocketAddress& remote_addr, | 69 const SocketAddress& remote_addr, |
70 const PacketTime& packet_time) { | 70 const PacketTime& packet_time) { |
71 EXPECT_EQ(size, sizeof(uint32_t)); | 71 EXPECT_EQ(size, sizeof(uint32_t)); |
72 uint32_t prev = reinterpret_cast<const uint32_t*>(buf)[0]; | 72 uint32_t prev = reinterpret_cast<const uint32_t*>(buf)[0]; |
73 uint32_t result = Next(prev); | 73 uint32_t result = Next(prev); |
74 | 74 |
75 post_thread_->PostDelayed(200, post_handler_, 0, new TestMessage(result)); | 75 post_thread_->PostDelayed(RTC_FROM_HERE, 200, post_handler_, 0, |
| 76 new TestMessage(result)); |
76 } | 77 } |
77 | 78 |
78 private: | 79 private: |
79 AsyncUDPSocket* socket_; | 80 AsyncUDPSocket* socket_; |
80 Thread* post_thread_; | 81 Thread* post_thread_; |
81 MessageHandler* post_handler_; | 82 MessageHandler* post_handler_; |
82 }; | 83 }; |
83 | 84 |
84 // Receives messages and sends on a socket. | 85 // Receives messages and sends on a socket. |
85 class MessageClient : public MessageHandler, public TestGenerator { | 86 class MessageClient : public MessageHandler, public TestGenerator { |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 AsyncSocket* asocket = | 205 AsyncSocket* asocket = |
205 th2.socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM); | 206 th2.socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM); |
206 SocketClient sock_client(asocket, addr, &th1, &msg_client); | 207 SocketClient sock_client(asocket, addr, &th1, &msg_client); |
207 | 208 |
208 socket->Connect(sock_client.address()); | 209 socket->Connect(sock_client.address()); |
209 | 210 |
210 th1.Start(); | 211 th1.Start(); |
211 th2.Start(); | 212 th2.Start(); |
212 | 213 |
213 // Get the messages started. | 214 // Get the messages started. |
214 th1.PostDelayed(100, &msg_client, 0, new TestMessage(1)); | 215 th1.PostDelayed(RTC_FROM_HERE, 100, &msg_client, 0, new TestMessage(1)); |
215 | 216 |
216 // Give the clients a little while to run. | 217 // Give the clients a little while to run. |
217 // Messages will be processed at 100, 300, 500, 700, 900. | 218 // Messages will be processed at 100, 300, 500, 700, 900. |
218 Thread* th_main = Thread::Current(); | 219 Thread* th_main = Thread::Current(); |
219 th_main->ProcessMessages(1000); | 220 th_main->ProcessMessages(1000); |
220 | 221 |
221 // Stop the sending client. Give the receiver a bit longer to run, in case | 222 // Stop the sending client. Give the receiver a bit longer to run, in case |
222 // it is running on a machine that is under load (e.g. the build machine). | 223 // it is running on a machine that is under load (e.g. the build machine). |
223 th1.Stop(); | 224 th1.Stop(); |
224 th_main->ProcessMessages(200); | 225 th_main->ProcessMessages(200); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 EXPECT_FALSE(cthread->RunningForTest()); | 266 EXPECT_FALSE(cthread->RunningForTest()); |
266 delete cthread; | 267 delete cthread; |
267 current_thread->WrapCurrent(); | 268 current_thread->WrapCurrent(); |
268 } | 269 } |
269 | 270 |
270 TEST(ThreadTest, Invoke) { | 271 TEST(ThreadTest, Invoke) { |
271 // Create and start the thread. | 272 // Create and start the thread. |
272 Thread thread; | 273 Thread thread; |
273 thread.Start(); | 274 thread.Start(); |
274 // Try calling functors. | 275 // Try calling functors. |
275 EXPECT_EQ(42, thread.Invoke<int>(FunctorA())); | 276 EXPECT_EQ(42, thread.Invoke<int>(RTC_FROM_HERE, FunctorA())); |
276 AtomicBool called; | 277 AtomicBool called; |
277 FunctorB f2(&called); | 278 FunctorB f2(&called); |
278 thread.Invoke<void>(f2); | 279 thread.Invoke<void>(RTC_FROM_HERE, f2); |
279 EXPECT_TRUE(called.get()); | 280 EXPECT_TRUE(called.get()); |
280 // Try calling bare functions. | 281 // Try calling bare functions. |
281 struct LocalFuncs { | 282 struct LocalFuncs { |
282 static int Func1() { return 999; } | 283 static int Func1() { return 999; } |
283 static void Func2() {} | 284 static void Func2() {} |
284 }; | 285 }; |
285 EXPECT_EQ(999, thread.Invoke<int>(&LocalFuncs::Func1)); | 286 EXPECT_EQ(999, thread.Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1)); |
286 thread.Invoke<void>(&LocalFuncs::Func2); | 287 thread.Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2); |
287 } | 288 } |
288 | 289 |
289 // Verifies that two threads calling Invoke on each other at the same time does | 290 // Verifies that two threads calling Invoke on each other at the same time does |
290 // not deadlock. | 291 // not deadlock. |
291 TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) { | 292 TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) { |
292 AutoThread thread; | 293 AutoThread thread; |
293 Thread* current_thread = Thread::Current(); | 294 Thread* current_thread = Thread::Current(); |
294 ASSERT_TRUE(current_thread != NULL); | 295 ASSERT_TRUE(current_thread != NULL); |
295 | 296 |
296 Thread other_thread; | 297 Thread other_thread; |
297 other_thread.Start(); | 298 other_thread.Start(); |
298 | 299 |
299 struct LocalFuncs { | 300 struct LocalFuncs { |
300 static void Set(bool* out) { *out = true; } | 301 static void Set(bool* out) { *out = true; } |
301 static void InvokeSet(Thread* thread, bool* out) { | 302 static void InvokeSet(Thread* thread, bool* out) { |
302 thread->Invoke<void>(Bind(&Set, out)); | 303 thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out)); |
303 } | 304 } |
304 }; | 305 }; |
305 | 306 |
306 bool called = false; | 307 bool called = false; |
307 other_thread.Invoke<void>( | 308 other_thread.Invoke<void>( |
308 Bind(&LocalFuncs::InvokeSet, current_thread, &called)); | 309 RTC_FROM_HERE, Bind(&LocalFuncs::InvokeSet, current_thread, &called)); |
309 | 310 |
310 EXPECT_TRUE(called); | 311 EXPECT_TRUE(called); |
311 } | 312 } |
312 | 313 |
313 // Verifies that if thread A invokes a call on thread B and thread C is trying | 314 // Verifies that if thread A invokes a call on thread B and thread C is trying |
314 // to invoke A at the same time, thread A does not handle C's invoke while | 315 // to invoke A at the same time, thread A does not handle C's invoke while |
315 // invoking B. | 316 // invoking B. |
316 TEST(ThreadTest, ThreeThreadsInvoke) { | 317 TEST(ThreadTest, ThreeThreadsInvoke) { |
317 AutoThread thread; | 318 AutoThread thread; |
318 Thread* thread_a = Thread::Current(); | 319 Thread* thread_a = Thread::Current(); |
(...skipping 16 matching lines...) Expand all Loading... |
335 } | 336 } |
336 | 337 |
337 private: | 338 private: |
338 CriticalSection crit_; | 339 CriticalSection crit_; |
339 bool value_ GUARDED_BY(crit_); | 340 bool value_ GUARDED_BY(crit_); |
340 }; | 341 }; |
341 | 342 |
342 struct LocalFuncs { | 343 struct LocalFuncs { |
343 static void Set(LockedBool* out) { out->Set(true); } | 344 static void Set(LockedBool* out) { out->Set(true); } |
344 static void InvokeSet(Thread* thread, LockedBool* out) { | 345 static void InvokeSet(Thread* thread, LockedBool* out) { |
345 thread->Invoke<void>(Bind(&Set, out)); | 346 thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out)); |
346 } | 347 } |
347 | 348 |
348 // Set |out| true and call InvokeSet on |thread|. | 349 // Set |out| true and call InvokeSet on |thread|. |
349 static void SetAndInvokeSet(LockedBool* out, | 350 static void SetAndInvokeSet(LockedBool* out, |
350 Thread* thread, | 351 Thread* thread, |
351 LockedBool* out_inner) { | 352 LockedBool* out_inner) { |
352 out->Set(true); | 353 out->Set(true); |
353 InvokeSet(thread, out_inner); | 354 InvokeSet(thread, out_inner); |
354 } | 355 } |
355 | 356 |
356 // Asynchronously invoke SetAndInvokeSet on |thread1| and wait until | 357 // Asynchronously invoke SetAndInvokeSet on |thread1| and wait until |
357 // |thread1| starts the call. | 358 // |thread1| starts the call. |
358 static void AsyncInvokeSetAndWait( | 359 static void AsyncInvokeSetAndWait( |
359 Thread* thread1, Thread* thread2, LockedBool* out) { | 360 Thread* thread1, Thread* thread2, LockedBool* out) { |
360 CriticalSection crit; | 361 CriticalSection crit; |
361 LockedBool async_invoked(false); | 362 LockedBool async_invoked(false); |
362 | 363 |
363 AsyncInvoker invoker; | 364 AsyncInvoker invoker; |
364 invoker.AsyncInvoke<void>( | 365 invoker.AsyncInvoke<void>( |
365 thread1, Bind(&SetAndInvokeSet, &async_invoked, thread2, out)); | 366 RTC_FROM_HERE, thread1, |
| 367 Bind(&SetAndInvokeSet, &async_invoked, thread2, out)); |
366 | 368 |
367 EXPECT_TRUE_WAIT(async_invoked.Get(), 2000); | 369 EXPECT_TRUE_WAIT(async_invoked.Get(), 2000); |
368 } | 370 } |
369 }; | 371 }; |
370 | 372 |
371 LockedBool thread_a_called(false); | 373 LockedBool thread_a_called(false); |
372 | 374 |
373 // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A. | 375 // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A. |
374 // Thread B returns when C receives the call and C should be blocked until A | 376 // Thread B returns when C receives the call and C should be blocked until A |
375 // starts to process messages. | 377 // starts to process messages. |
376 thread_b.Invoke<void>(Bind(&LocalFuncs::AsyncInvokeSetAndWait, | 378 thread_b.Invoke<void>(RTC_FROM_HERE, |
377 &thread_c, thread_a, &thread_a_called)); | 379 Bind(&LocalFuncs::AsyncInvokeSetAndWait, &thread_c, |
| 380 thread_a, &thread_a_called)); |
378 EXPECT_FALSE(thread_a_called.Get()); | 381 EXPECT_FALSE(thread_a_called.Get()); |
379 | 382 |
380 EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000); | 383 EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000); |
381 } | 384 } |
382 | 385 |
383 // Set the name on a thread when the underlying QueueDestroyed signal is | 386 // Set the name on a thread when the underlying QueueDestroyed signal is |
384 // triggered. This causes an error if the object is already partially | 387 // triggered. This causes an error if the object is already partially |
385 // destroyed. | 388 // destroyed. |
386 class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> { | 389 class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> { |
387 public: | 390 public: |
(...skipping 29 matching lines...) Expand all Loading... |
417 } | 420 } |
418 | 421 |
419 class AsyncInvokeTest : public testing::Test { | 422 class AsyncInvokeTest : public testing::Test { |
420 public: | 423 public: |
421 void IntCallback(int value) { | 424 void IntCallback(int value) { |
422 EXPECT_EQ(expected_thread_, Thread::Current()); | 425 EXPECT_EQ(expected_thread_, Thread::Current()); |
423 int_value_ = value; | 426 int_value_ = value; |
424 } | 427 } |
425 void AsyncInvokeIntCallback(AsyncInvoker* invoker, Thread* thread) { | 428 void AsyncInvokeIntCallback(AsyncInvoker* invoker, Thread* thread) { |
426 expected_thread_ = thread; | 429 expected_thread_ = thread; |
427 invoker->AsyncInvoke(thread, FunctorC(), | 430 invoker->AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, thread, FunctorC(), |
428 &AsyncInvokeTest::IntCallback, | 431 &AsyncInvokeTest::IntCallback, |
429 static_cast<AsyncInvokeTest*>(this)); | 432 static_cast<AsyncInvokeTest*>(this)); |
430 invoke_started_.Set(); | 433 invoke_started_.Set(); |
431 } | 434 } |
432 void SetExpectedThreadForIntCallback(Thread* thread) { | 435 void SetExpectedThreadForIntCallback(Thread* thread) { |
433 expected_thread_ = thread; | 436 expected_thread_ = thread; |
434 } | 437 } |
435 | 438 |
436 protected: | 439 protected: |
437 enum { kWaitTimeout = 1000 }; | 440 enum { kWaitTimeout = 1000 }; |
438 AsyncInvokeTest() | 441 AsyncInvokeTest() |
439 : int_value_(0), | 442 : int_value_(0), |
440 invoke_started_(true, false), | 443 invoke_started_(true, false), |
441 expected_thread_(NULL) {} | 444 expected_thread_(NULL) {} |
442 | 445 |
443 int int_value_; | 446 int int_value_; |
444 Event invoke_started_; | 447 Event invoke_started_; |
445 Thread* expected_thread_; | 448 Thread* expected_thread_; |
446 }; | 449 }; |
447 | 450 |
448 TEST_F(AsyncInvokeTest, FireAndForget) { | 451 TEST_F(AsyncInvokeTest, FireAndForget) { |
449 AsyncInvoker invoker; | 452 AsyncInvoker invoker; |
450 // Create and start the thread. | 453 // Create and start the thread. |
451 Thread thread; | 454 Thread thread; |
452 thread.Start(); | 455 thread.Start(); |
453 // Try calling functor. | 456 // Try calling functor. |
454 AtomicBool called; | 457 AtomicBool called; |
455 invoker.AsyncInvoke<void>(&thread, FunctorB(&called)); | 458 invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread, FunctorB(&called)); |
456 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); | 459 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
457 } | 460 } |
458 | 461 |
459 TEST_F(AsyncInvokeTest, WithCallback) { | 462 TEST_F(AsyncInvokeTest, WithCallback) { |
460 AsyncInvoker invoker; | 463 AsyncInvoker invoker; |
461 // Create and start the thread. | 464 // Create and start the thread. |
462 Thread thread; | 465 Thread thread; |
463 thread.Start(); | 466 thread.Start(); |
464 // Try calling functor. | 467 // Try calling functor. |
465 SetExpectedThreadForIntCallback(Thread::Current()); | 468 SetExpectedThreadForIntCallback(Thread::Current()); |
466 invoker.AsyncInvoke(&thread, FunctorA(), | 469 invoker.AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, &thread, FunctorA(), |
467 &AsyncInvokeTest::IntCallback, | 470 &AsyncInvokeTest::IntCallback, |
468 static_cast<AsyncInvokeTest*>(this)); | 471 static_cast<AsyncInvokeTest*>(this)); |
469 EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout); | 472 EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout); |
470 } | 473 } |
471 | 474 |
472 TEST_F(AsyncInvokeTest, CancelInvoker) { | 475 TEST_F(AsyncInvokeTest, CancelInvoker) { |
473 // Create and start the thread. | 476 // Create and start the thread. |
474 Thread thread; | 477 Thread thread; |
475 thread.Start(); | 478 thread.Start(); |
476 // Try destroying invoker during call. | 479 // Try destroying invoker during call. |
477 { | 480 { |
478 AsyncInvoker invoker; | 481 AsyncInvoker invoker; |
479 invoker.AsyncInvoke(&thread, FunctorC(), | 482 invoker.AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, &thread, FunctorC(), |
480 &AsyncInvokeTest::IntCallback, | 483 &AsyncInvokeTest::IntCallback, |
481 static_cast<AsyncInvokeTest*>(this)); | 484 static_cast<AsyncInvokeTest*>(this)); |
482 } | 485 } |
483 // With invoker gone, callback should be cancelled. | 486 // With invoker gone, callback should be cancelled. |
484 Thread::Current()->ProcessMessages(kWaitTimeout); | 487 Thread::Current()->ProcessMessages(kWaitTimeout); |
485 EXPECT_EQ(0, int_value_); | 488 EXPECT_EQ(0, int_value_); |
486 } | 489 } |
487 | 490 |
488 TEST_F(AsyncInvokeTest, CancelCallingThread) { | 491 TEST_F(AsyncInvokeTest, CancelCallingThread) { |
489 AsyncInvoker invoker; | 492 AsyncInvoker invoker; |
490 { // Create and start the thread. | 493 { // Create and start the thread. |
491 Thread thread; | 494 Thread thread; |
492 thread.Start(); | 495 thread.Start(); |
493 // Try calling functor. | 496 // Try calling functor. |
494 thread.Invoke<void>(Bind(&AsyncInvokeTest::AsyncInvokeIntCallback, | 497 thread.Invoke<void>( |
495 static_cast<AsyncInvokeTest*>(this), | 498 RTC_FROM_HERE, |
496 &invoker, Thread::Current())); | 499 Bind(&AsyncInvokeTest::AsyncInvokeIntCallback, |
| 500 static_cast<AsyncInvokeTest*>(this), &invoker, Thread::Current())); |
497 // Wait for the call to begin. | 501 // Wait for the call to begin. |
498 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); | 502 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); |
499 } | 503 } |
500 // Calling thread is gone. Return message shouldn't happen. | 504 // Calling thread is gone. Return message shouldn't happen. |
501 Thread::Current()->ProcessMessages(kWaitTimeout); | 505 Thread::Current()->ProcessMessages(kWaitTimeout); |
502 EXPECT_EQ(0, int_value_); | 506 EXPECT_EQ(0, int_value_); |
503 } | 507 } |
504 | 508 |
505 TEST_F(AsyncInvokeTest, KillInvokerBeforeExecute) { | 509 TEST_F(AsyncInvokeTest, KillInvokerBeforeExecute) { |
506 Thread thread; | 510 Thread thread; |
507 thread.Start(); | 511 thread.Start(); |
508 { | 512 { |
509 AsyncInvoker invoker; | 513 AsyncInvoker invoker; |
510 // Try calling functor. | 514 // Try calling functor. |
511 thread.Invoke<void>(Bind(&AsyncInvokeTest::AsyncInvokeIntCallback, | 515 thread.Invoke<void>( |
512 static_cast<AsyncInvokeTest*>(this), | 516 RTC_FROM_HERE, |
513 &invoker, Thread::Current())); | 517 Bind(&AsyncInvokeTest::AsyncInvokeIntCallback, |
| 518 static_cast<AsyncInvokeTest*>(this), &invoker, Thread::Current())); |
514 // Wait for the call to begin. | 519 // Wait for the call to begin. |
515 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); | 520 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); |
516 } | 521 } |
517 // Invoker is destroyed. Function should not execute. | 522 // Invoker is destroyed. Function should not execute. |
518 Thread::Current()->ProcessMessages(kWaitTimeout); | 523 Thread::Current()->ProcessMessages(kWaitTimeout); |
519 EXPECT_EQ(0, int_value_); | 524 EXPECT_EQ(0, int_value_); |
520 } | 525 } |
521 | 526 |
522 TEST_F(AsyncInvokeTest, Flush) { | 527 TEST_F(AsyncInvokeTest, Flush) { |
523 AsyncInvoker invoker; | 528 AsyncInvoker invoker; |
524 AtomicBool flag1; | 529 AtomicBool flag1; |
525 AtomicBool flag2; | 530 AtomicBool flag2; |
526 // Queue two async calls to the current thread. | 531 // Queue two async calls to the current thread. |
527 invoker.AsyncInvoke<void>(Thread::Current(), | 532 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1)); |
528 FunctorB(&flag1)); | 533 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2)); |
529 invoker.AsyncInvoke<void>(Thread::Current(), | |
530 FunctorB(&flag2)); | |
531 // Because we haven't pumped messages, these should not have run yet. | 534 // Because we haven't pumped messages, these should not have run yet. |
532 EXPECT_FALSE(flag1.get()); | 535 EXPECT_FALSE(flag1.get()); |
533 EXPECT_FALSE(flag2.get()); | 536 EXPECT_FALSE(flag2.get()); |
534 // Force them to run now. | 537 // Force them to run now. |
535 invoker.Flush(Thread::Current()); | 538 invoker.Flush(Thread::Current()); |
536 EXPECT_TRUE(flag1.get()); | 539 EXPECT_TRUE(flag1.get()); |
537 EXPECT_TRUE(flag2.get()); | 540 EXPECT_TRUE(flag2.get()); |
538 } | 541 } |
539 | 542 |
540 TEST_F(AsyncInvokeTest, FlushWithIds) { | 543 TEST_F(AsyncInvokeTest, FlushWithIds) { |
541 AsyncInvoker invoker; | 544 AsyncInvoker invoker; |
542 AtomicBool flag1; | 545 AtomicBool flag1; |
543 AtomicBool flag2; | 546 AtomicBool flag2; |
544 // Queue two async calls to the current thread, one with a message id. | 547 // Queue two async calls to the current thread, one with a message id. |
545 invoker.AsyncInvoke<void>(Thread::Current(), | 548 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1), |
546 FunctorB(&flag1), | |
547 5); | 549 5); |
548 invoker.AsyncInvoke<void>(Thread::Current(), | 550 invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2)); |
549 FunctorB(&flag2)); | |
550 // Because we haven't pumped messages, these should not have run yet. | 551 // Because we haven't pumped messages, these should not have run yet. |
551 EXPECT_FALSE(flag1.get()); | 552 EXPECT_FALSE(flag1.get()); |
552 EXPECT_FALSE(flag2.get()); | 553 EXPECT_FALSE(flag2.get()); |
553 // Execute pending calls with id == 5. | 554 // Execute pending calls with id == 5. |
554 invoker.Flush(Thread::Current(), 5); | 555 invoker.Flush(Thread::Current(), 5); |
555 EXPECT_TRUE(flag1.get()); | 556 EXPECT_TRUE(flag1.get()); |
556 EXPECT_FALSE(flag2.get()); | 557 EXPECT_FALSE(flag2.get()); |
557 flag1 = false; | 558 flag1 = false; |
558 // Execute all pending calls. The id == 5 call should not execute again. | 559 // Execute all pending calls. The id == 5 call should not execute again. |
559 invoker.Flush(Thread::Current()); | 560 invoker.Flush(Thread::Current()); |
560 EXPECT_FALSE(flag1.get()); | 561 EXPECT_FALSE(flag1.get()); |
561 EXPECT_TRUE(flag2.get()); | 562 EXPECT_TRUE(flag2.get()); |
562 } | 563 } |
563 | 564 |
564 class GuardedAsyncInvokeTest : public testing::Test { | 565 class GuardedAsyncInvokeTest : public testing::Test { |
565 public: | 566 public: |
566 void IntCallback(int value) { | 567 void IntCallback(int value) { |
567 EXPECT_EQ(expected_thread_, Thread::Current()); | 568 EXPECT_EQ(expected_thread_, Thread::Current()); |
568 int_value_ = value; | 569 int_value_ = value; |
569 } | 570 } |
570 void AsyncInvokeIntCallback(GuardedAsyncInvoker* invoker, Thread* thread) { | 571 void AsyncInvokeIntCallback(GuardedAsyncInvoker* invoker, Thread* thread) { |
571 expected_thread_ = thread; | 572 expected_thread_ = thread; |
572 invoker->AsyncInvoke(FunctorC(), &GuardedAsyncInvokeTest::IntCallback, | 573 invoker->AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, FunctorC(), |
| 574 &GuardedAsyncInvokeTest::IntCallback, |
573 static_cast<GuardedAsyncInvokeTest*>(this)); | 575 static_cast<GuardedAsyncInvokeTest*>(this)); |
574 invoke_started_.Set(); | 576 invoke_started_.Set(); |
575 } | 577 } |
576 void SetExpectedThreadForIntCallback(Thread* thread) { | 578 void SetExpectedThreadForIntCallback(Thread* thread) { |
577 expected_thread_ = thread; | 579 expected_thread_ = thread; |
578 } | 580 } |
579 | 581 |
580 protected: | 582 protected: |
581 const static int kWaitTimeout = 1000; | 583 const static int kWaitTimeout = 1000; |
582 GuardedAsyncInvokeTest() | 584 GuardedAsyncInvokeTest() |
(...skipping 14 matching lines...) Expand all Loading... |
597 std::unique_ptr<GuardedAsyncInvoker>* invoker_; | 599 std::unique_ptr<GuardedAsyncInvoker>* invoker_; |
598 }; | 600 }; |
599 | 601 |
600 // Test that we can call AsyncInvoke<void>() after the thread died. | 602 // Test that we can call AsyncInvoke<void>() after the thread died. |
601 TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) { | 603 TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) { |
602 // Create and start the thread. | 604 // Create and start the thread. |
603 std::unique_ptr<Thread> thread(new Thread()); | 605 std::unique_ptr<Thread> thread(new Thread()); |
604 thread->Start(); | 606 thread->Start(); |
605 std::unique_ptr<GuardedAsyncInvoker> invoker; | 607 std::unique_ptr<GuardedAsyncInvoker> invoker; |
606 // Create the invoker on |thread|. | 608 // Create the invoker on |thread|. |
607 thread->Invoke<void>(CreateInvoker(&invoker)); | 609 thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker)); |
608 // Kill |thread|. | 610 // Kill |thread|. |
609 thread = nullptr; | 611 thread = nullptr; |
610 // Try calling functor. | 612 // Try calling functor. |
611 AtomicBool called; | 613 AtomicBool called; |
612 EXPECT_FALSE(invoker->AsyncInvoke<void>(FunctorB(&called))); | 614 EXPECT_FALSE(invoker->AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called))); |
613 // With thread gone, nothing should happen. | 615 // With thread gone, nothing should happen. |
614 WAIT(called.get(), kWaitTimeout); | 616 WAIT(called.get(), kWaitTimeout); |
615 EXPECT_FALSE(called.get()); | 617 EXPECT_FALSE(called.get()); |
616 } | 618 } |
617 | 619 |
618 // Test that we can call AsyncInvoke with callback after the thread died. | 620 // Test that we can call AsyncInvoke with callback after the thread died. |
619 TEST_F(GuardedAsyncInvokeTest, KillThreadWithCallback) { | 621 TEST_F(GuardedAsyncInvokeTest, KillThreadWithCallback) { |
620 // Create and start the thread. | 622 // Create and start the thread. |
621 std::unique_ptr<Thread> thread(new Thread()); | 623 std::unique_ptr<Thread> thread(new Thread()); |
622 thread->Start(); | 624 thread->Start(); |
623 std::unique_ptr<GuardedAsyncInvoker> invoker; | 625 std::unique_ptr<GuardedAsyncInvoker> invoker; |
624 // Create the invoker on |thread|. | 626 // Create the invoker on |thread|. |
625 thread->Invoke<void>(CreateInvoker(&invoker)); | 627 thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker)); |
626 // Kill |thread|. | 628 // Kill |thread|. |
627 thread = nullptr; | 629 thread = nullptr; |
628 // Try calling functor. | 630 // Try calling functor. |
629 EXPECT_FALSE( | 631 EXPECT_FALSE( |
630 invoker->AsyncInvoke(FunctorC(), &GuardedAsyncInvokeTest::IntCallback, | 632 invoker->AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, FunctorC(), |
| 633 &GuardedAsyncInvokeTest::IntCallback, |
631 static_cast<GuardedAsyncInvokeTest*>(this))); | 634 static_cast<GuardedAsyncInvokeTest*>(this))); |
632 // With thread gone, callback should be cancelled. | 635 // With thread gone, callback should be cancelled. |
633 Thread::Current()->ProcessMessages(kWaitTimeout); | 636 Thread::Current()->ProcessMessages(kWaitTimeout); |
634 EXPECT_EQ(0, int_value_); | 637 EXPECT_EQ(0, int_value_); |
635 } | 638 } |
636 | 639 |
637 // The remaining tests check that GuardedAsyncInvoker behaves as AsyncInvoker | 640 // The remaining tests check that GuardedAsyncInvoker behaves as AsyncInvoker |
638 // when Thread is still alive. | 641 // when Thread is still alive. |
639 TEST_F(GuardedAsyncInvokeTest, FireAndForget) { | 642 TEST_F(GuardedAsyncInvokeTest, FireAndForget) { |
640 GuardedAsyncInvoker invoker; | 643 GuardedAsyncInvoker invoker; |
641 // Try calling functor. | 644 // Try calling functor. |
642 AtomicBool called; | 645 AtomicBool called; |
643 EXPECT_TRUE(invoker.AsyncInvoke<void>(FunctorB(&called))); | 646 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called))); |
644 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); | 647 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
645 } | 648 } |
646 | 649 |
647 TEST_F(GuardedAsyncInvokeTest, WithCallback) { | 650 TEST_F(GuardedAsyncInvokeTest, WithCallback) { |
648 GuardedAsyncInvoker invoker; | 651 GuardedAsyncInvoker invoker; |
649 // Try calling functor. | 652 // Try calling functor. |
650 SetExpectedThreadForIntCallback(Thread::Current()); | 653 SetExpectedThreadForIntCallback(Thread::Current()); |
651 EXPECT_TRUE(invoker.AsyncInvoke(FunctorA(), | 654 EXPECT_TRUE(invoker.AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, FunctorA(), |
652 &GuardedAsyncInvokeTest::IntCallback, | 655 &GuardedAsyncInvokeTest::IntCallback, |
653 static_cast<GuardedAsyncInvokeTest*>(this))); | 656 static_cast<GuardedAsyncInvokeTest*>(this))); |
654 EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout); | 657 EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout); |
655 } | 658 } |
656 | 659 |
657 TEST_F(GuardedAsyncInvokeTest, CancelInvoker) { | 660 TEST_F(GuardedAsyncInvokeTest, CancelInvoker) { |
658 // Try destroying invoker during call. | 661 // Try destroying invoker during call. |
659 { | 662 { |
660 GuardedAsyncInvoker invoker; | 663 GuardedAsyncInvoker invoker; |
661 EXPECT_TRUE( | 664 EXPECT_TRUE( |
662 invoker.AsyncInvoke(FunctorC(), &GuardedAsyncInvokeTest::IntCallback, | 665 invoker.AsyncInvoke(RTC_FROM_HERE, RTC_FROM_HERE, FunctorC(), |
| 666 &GuardedAsyncInvokeTest::IntCallback, |
663 static_cast<GuardedAsyncInvokeTest*>(this))); | 667 static_cast<GuardedAsyncInvokeTest*>(this))); |
664 } | 668 } |
665 // With invoker gone, callback should be cancelled. | 669 // With invoker gone, callback should be cancelled. |
666 Thread::Current()->ProcessMessages(kWaitTimeout); | 670 Thread::Current()->ProcessMessages(kWaitTimeout); |
667 EXPECT_EQ(0, int_value_); | 671 EXPECT_EQ(0, int_value_); |
668 } | 672 } |
669 | 673 |
670 TEST_F(GuardedAsyncInvokeTest, CancelCallingThread) { | 674 TEST_F(GuardedAsyncInvokeTest, CancelCallingThread) { |
671 GuardedAsyncInvoker invoker; | 675 GuardedAsyncInvoker invoker; |
672 // Try destroying calling thread during call. | 676 // Try destroying calling thread during call. |
673 { | 677 { |
674 Thread thread; | 678 Thread thread; |
675 thread.Start(); | 679 thread.Start(); |
676 // Try calling functor. | 680 // Try calling functor. |
677 thread.Invoke<void>(Bind(&GuardedAsyncInvokeTest::AsyncInvokeIntCallback, | 681 thread.Invoke<void>(RTC_FROM_HERE, |
| 682 Bind(&GuardedAsyncInvokeTest::AsyncInvokeIntCallback, |
678 static_cast<GuardedAsyncInvokeTest*>(this), | 683 static_cast<GuardedAsyncInvokeTest*>(this), |
679 &invoker, Thread::Current())); | 684 &invoker, Thread::Current())); |
680 // Wait for the call to begin. | 685 // Wait for the call to begin. |
681 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); | 686 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); |
682 } | 687 } |
683 // Calling thread is gone. Return message shouldn't happen. | 688 // Calling thread is gone. Return message shouldn't happen. |
684 Thread::Current()->ProcessMessages(kWaitTimeout); | 689 Thread::Current()->ProcessMessages(kWaitTimeout); |
685 EXPECT_EQ(0, int_value_); | 690 EXPECT_EQ(0, int_value_); |
686 } | 691 } |
687 | 692 |
688 TEST_F(GuardedAsyncInvokeTest, KillInvokerBeforeExecute) { | 693 TEST_F(GuardedAsyncInvokeTest, KillInvokerBeforeExecute) { |
689 Thread thread; | 694 Thread thread; |
690 thread.Start(); | 695 thread.Start(); |
691 { | 696 { |
692 GuardedAsyncInvoker invoker; | 697 GuardedAsyncInvoker invoker; |
693 // Try calling functor. | 698 // Try calling functor. |
694 thread.Invoke<void>(Bind(&GuardedAsyncInvokeTest::AsyncInvokeIntCallback, | 699 thread.Invoke<void>(RTC_FROM_HERE, |
| 700 Bind(&GuardedAsyncInvokeTest::AsyncInvokeIntCallback, |
695 static_cast<GuardedAsyncInvokeTest*>(this), | 701 static_cast<GuardedAsyncInvokeTest*>(this), |
696 &invoker, Thread::Current())); | 702 &invoker, Thread::Current())); |
697 // Wait for the call to begin. | 703 // Wait for the call to begin. |
698 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); | 704 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); |
699 } | 705 } |
700 // Invoker is destroyed. Function should not execute. | 706 // Invoker is destroyed. Function should not execute. |
701 Thread::Current()->ProcessMessages(kWaitTimeout); | 707 Thread::Current()->ProcessMessages(kWaitTimeout); |
702 EXPECT_EQ(0, int_value_); | 708 EXPECT_EQ(0, int_value_); |
703 } | 709 } |
704 | 710 |
705 TEST_F(GuardedAsyncInvokeTest, Flush) { | 711 TEST_F(GuardedAsyncInvokeTest, Flush) { |
706 GuardedAsyncInvoker invoker; | 712 GuardedAsyncInvoker invoker; |
707 AtomicBool flag1; | 713 AtomicBool flag1; |
708 AtomicBool flag2; | 714 AtomicBool flag2; |
709 // Queue two async calls to the current thread. | 715 // Queue two async calls to the current thread. |
710 EXPECT_TRUE(invoker.AsyncInvoke<void>(FunctorB(&flag1))); | 716 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1))); |
711 EXPECT_TRUE(invoker.AsyncInvoke<void>(FunctorB(&flag2))); | 717 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2))); |
712 // Because we haven't pumped messages, these should not have run yet. | 718 // Because we haven't pumped messages, these should not have run yet. |
713 EXPECT_FALSE(flag1.get()); | 719 EXPECT_FALSE(flag1.get()); |
714 EXPECT_FALSE(flag2.get()); | 720 EXPECT_FALSE(flag2.get()); |
715 // Force them to run now. | 721 // Force them to run now. |
716 EXPECT_TRUE(invoker.Flush()); | 722 EXPECT_TRUE(invoker.Flush()); |
717 EXPECT_TRUE(flag1.get()); | 723 EXPECT_TRUE(flag1.get()); |
718 EXPECT_TRUE(flag2.get()); | 724 EXPECT_TRUE(flag2.get()); |
719 } | 725 } |
720 | 726 |
721 TEST_F(GuardedAsyncInvokeTest, FlushWithIds) { | 727 TEST_F(GuardedAsyncInvokeTest, FlushWithIds) { |
722 GuardedAsyncInvoker invoker; | 728 GuardedAsyncInvoker invoker; |
723 AtomicBool flag1; | 729 AtomicBool flag1; |
724 AtomicBool flag2; | 730 AtomicBool flag2; |
725 // Queue two async calls to the current thread, one with a message id. | 731 // Queue two async calls to the current thread, one with a message id. |
726 EXPECT_TRUE(invoker.AsyncInvoke<void>(FunctorB(&flag1), 5)); | 732 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1), 5)); |
727 EXPECT_TRUE(invoker.AsyncInvoke<void>(FunctorB(&flag2))); | 733 EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2))); |
728 // Because we haven't pumped messages, these should not have run yet. | 734 // Because we haven't pumped messages, these should not have run yet. |
729 EXPECT_FALSE(flag1.get()); | 735 EXPECT_FALSE(flag1.get()); |
730 EXPECT_FALSE(flag2.get()); | 736 EXPECT_FALSE(flag2.get()); |
731 // Execute pending calls with id == 5. | 737 // Execute pending calls with id == 5. |
732 EXPECT_TRUE(invoker.Flush(5)); | 738 EXPECT_TRUE(invoker.Flush(5)); |
733 EXPECT_TRUE(flag1.get()); | 739 EXPECT_TRUE(flag1.get()); |
734 EXPECT_FALSE(flag2.get()); | 740 EXPECT_FALSE(flag2.get()); |
735 flag1 = false; | 741 flag1 = false; |
736 // Execute all pending calls. The id == 5 call should not execute again. | 742 // Execute all pending calls. The id == 5 call should not execute again. |
737 EXPECT_TRUE(invoker.Flush()); | 743 EXPECT_TRUE(invoker.Flush()); |
(...skipping 14 matching lines...) Expand all Loading... |
752 CoUninitialize(); | 758 CoUninitialize(); |
753 } | 759 } |
754 done_ = true; | 760 done_ = true; |
755 } | 761 } |
756 bool done_; | 762 bool done_; |
757 }; | 763 }; |
758 | 764 |
759 TEST_F(ComThreadTest, ComInited) { | 765 TEST_F(ComThreadTest, ComInited) { |
760 Thread* thread = new ComThread(); | 766 Thread* thread = new ComThread(); |
761 EXPECT_TRUE(thread->Start()); | 767 EXPECT_TRUE(thread->Start()); |
762 thread->Post(this, 0); | 768 thread->Post(RTC_FROM_HERE, this, 0); |
763 EXPECT_TRUE_WAIT(done_, 1000); | 769 EXPECT_TRUE_WAIT(done_, 1000); |
764 delete thread; | 770 delete thread; |
765 } | 771 } |
766 #endif | 772 #endif |
OLD | NEW |