Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(742)

Side by Side Diff: webrtc/base/thread_unittest.cc

Issue 2019423006: Adding more detail to MessageQueue::Dispatch logging. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(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
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(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
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>(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>(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>(FROM_HERE, &LocalFuncs::Func1));
286 thread.Invoke<void>(&LocalFuncs::Func2); 287 thread.Invoke<void>(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>(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 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
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>(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 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>(FROM_HERE, Bind(&LocalFuncs::AsyncInvokeSetAndWait,
377 &thread_c, thread_a, &thread_a_called)); 379 &thread_c, thread_a, &thread_a_called));
378 EXPECT_FALSE(thread_a_called.Get()); 380 EXPECT_FALSE(thread_a_called.Get());
379 381
380 EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000); 382 EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000);
381 } 383 }
382 384
383 // Set the name on a thread when the underlying QueueDestroyed signal is 385 // Set the name on a thread when the underlying QueueDestroyed signal is
384 // triggered. This causes an error if the object is already partially 386 // triggered. This causes an error if the object is already partially
385 // destroyed. 387 // destroyed.
386 class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> { 388 class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> {
387 public: 389 public:
(...skipping 29 matching lines...) Expand all
417 } 419 }
418 420
419 class AsyncInvokeTest : public testing::Test { 421 class AsyncInvokeTest : public testing::Test {
420 public: 422 public:
421 void IntCallback(int value) { 423 void IntCallback(int value) {
422 EXPECT_EQ(expected_thread_, Thread::Current()); 424 EXPECT_EQ(expected_thread_, Thread::Current());
423 int_value_ = value; 425 int_value_ = value;
424 } 426 }
425 void AsyncInvokeIntCallback(AsyncInvoker* invoker, Thread* thread) { 427 void AsyncInvokeIntCallback(AsyncInvoker* invoker, Thread* thread) {
426 expected_thread_ = thread; 428 expected_thread_ = thread;
427 invoker->AsyncInvoke(thread, FunctorC(), 429 invoker->AsyncInvoke(FROM_HERE, FROM_HERE, thread, FunctorC(),
428 &AsyncInvokeTest::IntCallback, 430 &AsyncInvokeTest::IntCallback,
429 static_cast<AsyncInvokeTest*>(this)); 431 static_cast<AsyncInvokeTest*>(this));
430 invoke_started_.Set(); 432 invoke_started_.Set();
431 } 433 }
432 void SetExpectedThreadForIntCallback(Thread* thread) { 434 void SetExpectedThreadForIntCallback(Thread* thread) {
433 expected_thread_ = thread; 435 expected_thread_ = thread;
434 } 436 }
435 437
436 protected: 438 protected:
437 enum { kWaitTimeout = 1000 }; 439 enum { kWaitTimeout = 1000 };
438 AsyncInvokeTest() 440 AsyncInvokeTest()
439 : int_value_(0), 441 : int_value_(0),
440 invoke_started_(true, false), 442 invoke_started_(true, false),
441 expected_thread_(NULL) {} 443 expected_thread_(NULL) {}
442 444
443 int int_value_; 445 int int_value_;
444 Event invoke_started_; 446 Event invoke_started_;
445 Thread* expected_thread_; 447 Thread* expected_thread_;
446 }; 448 };
447 449
448 TEST_F(AsyncInvokeTest, FireAndForget) { 450 TEST_F(AsyncInvokeTest, FireAndForget) {
449 AsyncInvoker invoker; 451 AsyncInvoker invoker;
450 // Create and start the thread. 452 // Create and start the thread.
451 Thread thread; 453 Thread thread;
452 thread.Start(); 454 thread.Start();
453 // Try calling functor. 455 // Try calling functor.
454 AtomicBool called; 456 AtomicBool called;
455 invoker.AsyncInvoke<void>(&thread, FunctorB(&called)); 457 invoker.AsyncInvoke<void>(FROM_HERE, &thread, FunctorB(&called));
456 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); 458 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
457 } 459 }
458 460
459 TEST_F(AsyncInvokeTest, WithCallback) { 461 TEST_F(AsyncInvokeTest, WithCallback) {
460 AsyncInvoker invoker; 462 AsyncInvoker invoker;
461 // Create and start the thread. 463 // Create and start the thread.
462 Thread thread; 464 Thread thread;
463 thread.Start(); 465 thread.Start();
464 // Try calling functor. 466 // Try calling functor.
465 SetExpectedThreadForIntCallback(Thread::Current()); 467 SetExpectedThreadForIntCallback(Thread::Current());
466 invoker.AsyncInvoke(&thread, FunctorA(), 468 invoker.AsyncInvoke(FROM_HERE, FROM_HERE, &thread, FunctorA(),
467 &AsyncInvokeTest::IntCallback, 469 &AsyncInvokeTest::IntCallback,
468 static_cast<AsyncInvokeTest*>(this)); 470 static_cast<AsyncInvokeTest*>(this));
469 EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout); 471 EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout);
470 } 472 }
471 473
472 TEST_F(AsyncInvokeTest, CancelInvoker) { 474 TEST_F(AsyncInvokeTest, CancelInvoker) {
473 // Create and start the thread. 475 // Create and start the thread.
474 Thread thread; 476 Thread thread;
475 thread.Start(); 477 thread.Start();
476 // Try destroying invoker during call. 478 // Try destroying invoker during call.
477 { 479 {
478 AsyncInvoker invoker; 480 AsyncInvoker invoker;
479 invoker.AsyncInvoke(&thread, FunctorC(), 481 invoker.AsyncInvoke(FROM_HERE, FROM_HERE, &thread, FunctorC(),
480 &AsyncInvokeTest::IntCallback, 482 &AsyncInvokeTest::IntCallback,
481 static_cast<AsyncInvokeTest*>(this)); 483 static_cast<AsyncInvokeTest*>(this));
482 } 484 }
483 // With invoker gone, callback should be cancelled. 485 // With invoker gone, callback should be cancelled.
484 Thread::Current()->ProcessMessages(kWaitTimeout); 486 Thread::Current()->ProcessMessages(kWaitTimeout);
485 EXPECT_EQ(0, int_value_); 487 EXPECT_EQ(0, int_value_);
486 } 488 }
487 489
488 TEST_F(AsyncInvokeTest, CancelCallingThread) { 490 TEST_F(AsyncInvokeTest, CancelCallingThread) {
489 AsyncInvoker invoker; 491 AsyncInvoker invoker;
490 { // Create and start the thread. 492 { // Create and start the thread.
491 Thread thread; 493 Thread thread;
492 thread.Start(); 494 thread.Start();
493 // Try calling functor. 495 // Try calling functor.
494 thread.Invoke<void>(Bind(&AsyncInvokeTest::AsyncInvokeIntCallback, 496 thread.Invoke<void>(
495 static_cast<AsyncInvokeTest*>(this), 497 FROM_HERE,
496 &invoker, Thread::Current())); 498 Bind(&AsyncInvokeTest::AsyncInvokeIntCallback,
499 static_cast<AsyncInvokeTest*>(this), &invoker, Thread::Current()));
497 // Wait for the call to begin. 500 // Wait for the call to begin.
498 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); 501 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout));
499 } 502 }
500 // Calling thread is gone. Return message shouldn't happen. 503 // Calling thread is gone. Return message shouldn't happen.
501 Thread::Current()->ProcessMessages(kWaitTimeout); 504 Thread::Current()->ProcessMessages(kWaitTimeout);
502 EXPECT_EQ(0, int_value_); 505 EXPECT_EQ(0, int_value_);
503 } 506 }
504 507
505 TEST_F(AsyncInvokeTest, KillInvokerBeforeExecute) { 508 TEST_F(AsyncInvokeTest, KillInvokerBeforeExecute) {
506 Thread thread; 509 Thread thread;
507 thread.Start(); 510 thread.Start();
508 { 511 {
509 AsyncInvoker invoker; 512 AsyncInvoker invoker;
510 // Try calling functor. 513 // Try calling functor.
511 thread.Invoke<void>(Bind(&AsyncInvokeTest::AsyncInvokeIntCallback, 514 thread.Invoke<void>(
512 static_cast<AsyncInvokeTest*>(this), 515 FROM_HERE,
513 &invoker, Thread::Current())); 516 Bind(&AsyncInvokeTest::AsyncInvokeIntCallback,
517 static_cast<AsyncInvokeTest*>(this), &invoker, Thread::Current()));
514 // Wait for the call to begin. 518 // Wait for the call to begin.
515 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); 519 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout));
516 } 520 }
517 // Invoker is destroyed. Function should not execute. 521 // Invoker is destroyed. Function should not execute.
518 Thread::Current()->ProcessMessages(kWaitTimeout); 522 Thread::Current()->ProcessMessages(kWaitTimeout);
519 EXPECT_EQ(0, int_value_); 523 EXPECT_EQ(0, int_value_);
520 } 524 }
521 525
522 TEST_F(AsyncInvokeTest, Flush) { 526 TEST_F(AsyncInvokeTest, Flush) {
523 AsyncInvoker invoker; 527 AsyncInvoker invoker;
524 AtomicBool flag1; 528 AtomicBool flag1;
525 AtomicBool flag2; 529 AtomicBool flag2;
526 // Queue two async calls to the current thread. 530 // Queue two async calls to the current thread.
527 invoker.AsyncInvoke<void>(Thread::Current(), 531 invoker.AsyncInvoke<void>(FROM_HERE, Thread::Current(), FunctorB(&flag1));
528 FunctorB(&flag1)); 532 invoker.AsyncInvoke<void>(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. 533 // Because we haven't pumped messages, these should not have run yet.
532 EXPECT_FALSE(flag1.get()); 534 EXPECT_FALSE(flag1.get());
533 EXPECT_FALSE(flag2.get()); 535 EXPECT_FALSE(flag2.get());
534 // Force them to run now. 536 // Force them to run now.
535 invoker.Flush(Thread::Current()); 537 invoker.Flush(Thread::Current());
536 EXPECT_TRUE(flag1.get()); 538 EXPECT_TRUE(flag1.get());
537 EXPECT_TRUE(flag2.get()); 539 EXPECT_TRUE(flag2.get());
538 } 540 }
539 541
540 TEST_F(AsyncInvokeTest, FlushWithIds) { 542 TEST_F(AsyncInvokeTest, FlushWithIds) {
541 AsyncInvoker invoker; 543 AsyncInvoker invoker;
542 AtomicBool flag1; 544 AtomicBool flag1;
543 AtomicBool flag2; 545 AtomicBool flag2;
544 // Queue two async calls to the current thread, one with a message id. 546 // Queue two async calls to the current thread, one with a message id.
545 invoker.AsyncInvoke<void>(Thread::Current(), 547 invoker.AsyncInvoke<void>(FROM_HERE, Thread::Current(), FunctorB(&flag1), 5);
546 FunctorB(&flag1), 548 invoker.AsyncInvoke<void>(FROM_HERE, Thread::Current(), FunctorB(&flag2));
547 5);
548 invoker.AsyncInvoke<void>(Thread::Current(),
549 FunctorB(&flag2));
550 // Because we haven't pumped messages, these should not have run yet. 549 // Because we haven't pumped messages, these should not have run yet.
551 EXPECT_FALSE(flag1.get()); 550 EXPECT_FALSE(flag1.get());
552 EXPECT_FALSE(flag2.get()); 551 EXPECT_FALSE(flag2.get());
553 // Execute pending calls with id == 5. 552 // Execute pending calls with id == 5.
554 invoker.Flush(Thread::Current(), 5); 553 invoker.Flush(Thread::Current(), 5);
555 EXPECT_TRUE(flag1.get()); 554 EXPECT_TRUE(flag1.get());
556 EXPECT_FALSE(flag2.get()); 555 EXPECT_FALSE(flag2.get());
557 flag1 = false; 556 flag1 = false;
558 // Execute all pending calls. The id == 5 call should not execute again. 557 // Execute all pending calls. The id == 5 call should not execute again.
559 invoker.Flush(Thread::Current()); 558 invoker.Flush(Thread::Current());
560 EXPECT_FALSE(flag1.get()); 559 EXPECT_FALSE(flag1.get());
561 EXPECT_TRUE(flag2.get()); 560 EXPECT_TRUE(flag2.get());
562 } 561 }
563 562
564 class GuardedAsyncInvokeTest : public testing::Test { 563 class GuardedAsyncInvokeTest : public testing::Test {
565 public: 564 public:
566 void IntCallback(int value) { 565 void IntCallback(int value) {
567 EXPECT_EQ(expected_thread_, Thread::Current()); 566 EXPECT_EQ(expected_thread_, Thread::Current());
568 int_value_ = value; 567 int_value_ = value;
569 } 568 }
570 void AsyncInvokeIntCallback(GuardedAsyncInvoker* invoker, Thread* thread) { 569 void AsyncInvokeIntCallback(GuardedAsyncInvoker* invoker, Thread* thread) {
571 expected_thread_ = thread; 570 expected_thread_ = thread;
572 invoker->AsyncInvoke(FunctorC(), &GuardedAsyncInvokeTest::IntCallback, 571 invoker->AsyncInvoke(FROM_HERE, FROM_HERE, FunctorC(),
572 &GuardedAsyncInvokeTest::IntCallback,
573 static_cast<GuardedAsyncInvokeTest*>(this)); 573 static_cast<GuardedAsyncInvokeTest*>(this));
574 invoke_started_.Set(); 574 invoke_started_.Set();
575 } 575 }
576 void SetExpectedThreadForIntCallback(Thread* thread) { 576 void SetExpectedThreadForIntCallback(Thread* thread) {
577 expected_thread_ = thread; 577 expected_thread_ = thread;
578 } 578 }
579 579
580 protected: 580 protected:
581 const static int kWaitTimeout = 1000; 581 const static int kWaitTimeout = 1000;
582 GuardedAsyncInvokeTest() 582 GuardedAsyncInvokeTest()
(...skipping 14 matching lines...) Expand all
597 std::unique_ptr<GuardedAsyncInvoker>* invoker_; 597 std::unique_ptr<GuardedAsyncInvoker>* invoker_;
598 }; 598 };
599 599
600 // Test that we can call AsyncInvoke<void>() after the thread died. 600 // Test that we can call AsyncInvoke<void>() after the thread died.
601 TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) { 601 TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) {
602 // Create and start the thread. 602 // Create and start the thread.
603 std::unique_ptr<Thread> thread(new Thread()); 603 std::unique_ptr<Thread> thread(new Thread());
604 thread->Start(); 604 thread->Start();
605 std::unique_ptr<GuardedAsyncInvoker> invoker; 605 std::unique_ptr<GuardedAsyncInvoker> invoker;
606 // Create the invoker on |thread|. 606 // Create the invoker on |thread|.
607 thread->Invoke<void>(CreateInvoker(&invoker)); 607 thread->Invoke<void>(FROM_HERE, CreateInvoker(&invoker));
608 // Kill |thread|. 608 // Kill |thread|.
609 thread = nullptr; 609 thread = nullptr;
610 // Try calling functor. 610 // Try calling functor.
611 AtomicBool called; 611 AtomicBool called;
612 EXPECT_FALSE(invoker->AsyncInvoke<void>(FunctorB(&called))); 612 EXPECT_FALSE(invoker->AsyncInvoke<void>(FROM_HERE, FunctorB(&called)));
613 // With thread gone, nothing should happen. 613 // With thread gone, nothing should happen.
614 WAIT(called.get(), kWaitTimeout); 614 WAIT(called.get(), kWaitTimeout);
615 EXPECT_FALSE(called.get()); 615 EXPECT_FALSE(called.get());
616 } 616 }
617 617
618 // Test that we can call AsyncInvoke with callback after the thread died. 618 // Test that we can call AsyncInvoke with callback after the thread died.
619 TEST_F(GuardedAsyncInvokeTest, KillThreadWithCallback) { 619 TEST_F(GuardedAsyncInvokeTest, KillThreadWithCallback) {
620 // Create and start the thread. 620 // Create and start the thread.
621 std::unique_ptr<Thread> thread(new Thread()); 621 std::unique_ptr<Thread> thread(new Thread());
622 thread->Start(); 622 thread->Start();
623 std::unique_ptr<GuardedAsyncInvoker> invoker; 623 std::unique_ptr<GuardedAsyncInvoker> invoker;
624 // Create the invoker on |thread|. 624 // Create the invoker on |thread|.
625 thread->Invoke<void>(CreateInvoker(&invoker)); 625 thread->Invoke<void>(FROM_HERE, CreateInvoker(&invoker));
626 // Kill |thread|. 626 // Kill |thread|.
627 thread = nullptr; 627 thread = nullptr;
628 // Try calling functor. 628 // Try calling functor.
629 EXPECT_FALSE( 629 EXPECT_FALSE(invoker->AsyncInvoke(
630 invoker->AsyncInvoke(FunctorC(), &GuardedAsyncInvokeTest::IntCallback, 630 FROM_HERE, FROM_HERE, FunctorC(), &GuardedAsyncInvokeTest::IntCallback,
631 static_cast<GuardedAsyncInvokeTest*>(this))); 631 static_cast<GuardedAsyncInvokeTest*>(this)));
632 // With thread gone, callback should be cancelled. 632 // With thread gone, callback should be cancelled.
633 Thread::Current()->ProcessMessages(kWaitTimeout); 633 Thread::Current()->ProcessMessages(kWaitTimeout);
634 EXPECT_EQ(0, int_value_); 634 EXPECT_EQ(0, int_value_);
635 } 635 }
636 636
637 // The remaining tests check that GuardedAsyncInvoker behaves as AsyncInvoker 637 // The remaining tests check that GuardedAsyncInvoker behaves as AsyncInvoker
638 // when Thread is still alive. 638 // when Thread is still alive.
639 TEST_F(GuardedAsyncInvokeTest, FireAndForget) { 639 TEST_F(GuardedAsyncInvokeTest, FireAndForget) {
640 GuardedAsyncInvoker invoker; 640 GuardedAsyncInvoker invoker;
641 // Try calling functor. 641 // Try calling functor.
642 AtomicBool called; 642 AtomicBool called;
643 EXPECT_TRUE(invoker.AsyncInvoke<void>(FunctorB(&called))); 643 EXPECT_TRUE(invoker.AsyncInvoke<void>(FROM_HERE, FunctorB(&called)));
644 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); 644 EXPECT_TRUE_WAIT(called.get(), kWaitTimeout);
645 } 645 }
646 646
647 TEST_F(GuardedAsyncInvokeTest, WithCallback) { 647 TEST_F(GuardedAsyncInvokeTest, WithCallback) {
648 GuardedAsyncInvoker invoker; 648 GuardedAsyncInvoker invoker;
649 // Try calling functor. 649 // Try calling functor.
650 SetExpectedThreadForIntCallback(Thread::Current()); 650 SetExpectedThreadForIntCallback(Thread::Current());
651 EXPECT_TRUE(invoker.AsyncInvoke(FunctorA(), 651 EXPECT_TRUE(invoker.AsyncInvoke(FROM_HERE, FROM_HERE, FunctorA(),
652 &GuardedAsyncInvokeTest::IntCallback, 652 &GuardedAsyncInvokeTest::IntCallback,
653 static_cast<GuardedAsyncInvokeTest*>(this))); 653 static_cast<GuardedAsyncInvokeTest*>(this)));
654 EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout); 654 EXPECT_EQ_WAIT(42, int_value_, kWaitTimeout);
655 } 655 }
656 656
657 TEST_F(GuardedAsyncInvokeTest, CancelInvoker) { 657 TEST_F(GuardedAsyncInvokeTest, CancelInvoker) {
658 // Try destroying invoker during call. 658 // Try destroying invoker during call.
659 { 659 {
660 GuardedAsyncInvoker invoker; 660 GuardedAsyncInvoker invoker;
661 EXPECT_TRUE( 661 EXPECT_TRUE(invoker.AsyncInvoke(
662 invoker.AsyncInvoke(FunctorC(), &GuardedAsyncInvokeTest::IntCallback, 662 FROM_HERE, FROM_HERE, FunctorC(), &GuardedAsyncInvokeTest::IntCallback,
663 static_cast<GuardedAsyncInvokeTest*>(this))); 663 static_cast<GuardedAsyncInvokeTest*>(this)));
664 } 664 }
665 // With invoker gone, callback should be cancelled. 665 // With invoker gone, callback should be cancelled.
666 Thread::Current()->ProcessMessages(kWaitTimeout); 666 Thread::Current()->ProcessMessages(kWaitTimeout);
667 EXPECT_EQ(0, int_value_); 667 EXPECT_EQ(0, int_value_);
668 } 668 }
669 669
670 TEST_F(GuardedAsyncInvokeTest, CancelCallingThread) { 670 TEST_F(GuardedAsyncInvokeTest, CancelCallingThread) {
671 GuardedAsyncInvoker invoker; 671 GuardedAsyncInvoker invoker;
672 // Try destroying calling thread during call. 672 // Try destroying calling thread during call.
673 { 673 {
674 Thread thread; 674 Thread thread;
675 thread.Start(); 675 thread.Start();
676 // Try calling functor. 676 // Try calling functor.
677 thread.Invoke<void>(Bind(&GuardedAsyncInvokeTest::AsyncInvokeIntCallback, 677 thread.Invoke<void>(
678 static_cast<GuardedAsyncInvokeTest*>(this), 678 FROM_HERE, Bind(&GuardedAsyncInvokeTest::AsyncInvokeIntCallback,
679 &invoker, Thread::Current())); 679 static_cast<GuardedAsyncInvokeTest*>(this), &invoker,
680 Thread::Current()));
680 // Wait for the call to begin. 681 // Wait for the call to begin.
681 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); 682 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout));
682 } 683 }
683 // Calling thread is gone. Return message shouldn't happen. 684 // Calling thread is gone. Return message shouldn't happen.
684 Thread::Current()->ProcessMessages(kWaitTimeout); 685 Thread::Current()->ProcessMessages(kWaitTimeout);
685 EXPECT_EQ(0, int_value_); 686 EXPECT_EQ(0, int_value_);
686 } 687 }
687 688
688 TEST_F(GuardedAsyncInvokeTest, KillInvokerBeforeExecute) { 689 TEST_F(GuardedAsyncInvokeTest, KillInvokerBeforeExecute) {
689 Thread thread; 690 Thread thread;
690 thread.Start(); 691 thread.Start();
691 { 692 {
692 GuardedAsyncInvoker invoker; 693 GuardedAsyncInvoker invoker;
693 // Try calling functor. 694 // Try calling functor.
694 thread.Invoke<void>(Bind(&GuardedAsyncInvokeTest::AsyncInvokeIntCallback, 695 thread.Invoke<void>(
695 static_cast<GuardedAsyncInvokeTest*>(this), 696 FROM_HERE, Bind(&GuardedAsyncInvokeTest::AsyncInvokeIntCallback,
696 &invoker, Thread::Current())); 697 static_cast<GuardedAsyncInvokeTest*>(this), &invoker,
698 Thread::Current()));
697 // Wait for the call to begin. 699 // Wait for the call to begin.
698 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout)); 700 ASSERT_TRUE(invoke_started_.Wait(kWaitTimeout));
699 } 701 }
700 // Invoker is destroyed. Function should not execute. 702 // Invoker is destroyed. Function should not execute.
701 Thread::Current()->ProcessMessages(kWaitTimeout); 703 Thread::Current()->ProcessMessages(kWaitTimeout);
702 EXPECT_EQ(0, int_value_); 704 EXPECT_EQ(0, int_value_);
703 } 705 }
704 706
705 TEST_F(GuardedAsyncInvokeTest, Flush) { 707 TEST_F(GuardedAsyncInvokeTest, Flush) {
706 GuardedAsyncInvoker invoker; 708 GuardedAsyncInvoker invoker;
707 AtomicBool flag1; 709 AtomicBool flag1;
708 AtomicBool flag2; 710 AtomicBool flag2;
709 // Queue two async calls to the current thread. 711 // Queue two async calls to the current thread.
710 EXPECT_TRUE(invoker.AsyncInvoke<void>(FunctorB(&flag1))); 712 EXPECT_TRUE(invoker.AsyncInvoke<void>(FROM_HERE, FunctorB(&flag1)));
711 EXPECT_TRUE(invoker.AsyncInvoke<void>(FunctorB(&flag2))); 713 EXPECT_TRUE(invoker.AsyncInvoke<void>(FROM_HERE, FunctorB(&flag2)));
712 // Because we haven't pumped messages, these should not have run yet. 714 // Because we haven't pumped messages, these should not have run yet.
713 EXPECT_FALSE(flag1.get()); 715 EXPECT_FALSE(flag1.get());
714 EXPECT_FALSE(flag2.get()); 716 EXPECT_FALSE(flag2.get());
715 // Force them to run now. 717 // Force them to run now.
716 EXPECT_TRUE(invoker.Flush()); 718 EXPECT_TRUE(invoker.Flush());
717 EXPECT_TRUE(flag1.get()); 719 EXPECT_TRUE(flag1.get());
718 EXPECT_TRUE(flag2.get()); 720 EXPECT_TRUE(flag2.get());
719 } 721 }
720 722
721 TEST_F(GuardedAsyncInvokeTest, FlushWithIds) { 723 TEST_F(GuardedAsyncInvokeTest, FlushWithIds) {
722 GuardedAsyncInvoker invoker; 724 GuardedAsyncInvoker invoker;
723 AtomicBool flag1; 725 AtomicBool flag1;
724 AtomicBool flag2; 726 AtomicBool flag2;
725 // Queue two async calls to the current thread, one with a message id. 727 // Queue two async calls to the current thread, one with a message id.
726 EXPECT_TRUE(invoker.AsyncInvoke<void>(FunctorB(&flag1), 5)); 728 EXPECT_TRUE(invoker.AsyncInvoke<void>(FROM_HERE, FunctorB(&flag1), 5));
727 EXPECT_TRUE(invoker.AsyncInvoke<void>(FunctorB(&flag2))); 729 EXPECT_TRUE(invoker.AsyncInvoke<void>(FROM_HERE, FunctorB(&flag2)));
728 // Because we haven't pumped messages, these should not have run yet. 730 // Because we haven't pumped messages, these should not have run yet.
729 EXPECT_FALSE(flag1.get()); 731 EXPECT_FALSE(flag1.get());
730 EXPECT_FALSE(flag2.get()); 732 EXPECT_FALSE(flag2.get());
731 // Execute pending calls with id == 5. 733 // Execute pending calls with id == 5.
732 EXPECT_TRUE(invoker.Flush(5)); 734 EXPECT_TRUE(invoker.Flush(5));
733 EXPECT_TRUE(flag1.get()); 735 EXPECT_TRUE(flag1.get());
734 EXPECT_FALSE(flag2.get()); 736 EXPECT_FALSE(flag2.get());
735 flag1 = false; 737 flag1 = false;
736 // Execute all pending calls. The id == 5 call should not execute again. 738 // Execute all pending calls. The id == 5 call should not execute again.
737 EXPECT_TRUE(invoker.Flush()); 739 EXPECT_TRUE(invoker.Flush());
(...skipping 14 matching lines...) Expand all
752 CoUninitialize(); 754 CoUninitialize();
753 } 755 }
754 done_ = true; 756 done_ = true;
755 } 757 }
756 bool done_; 758 bool done_;
757 }; 759 };
758 760
759 TEST_F(ComThreadTest, ComInited) { 761 TEST_F(ComThreadTest, ComInited) {
760 Thread* thread = new ComThread(); 762 Thread* thread = new ComThread();
761 EXPECT_TRUE(thread->Start()); 763 EXPECT_TRUE(thread->Start());
762 thread->Post(this, 0); 764 thread->Post(FROM_HERE, this, 0);
763 EXPECT_TRUE_WAIT(done_, 1000); 765 EXPECT_TRUE_WAIT(done_, 1000);
764 delete thread; 766 delete thread;
765 } 767 }
766 #endif 768 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698