| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/threading/thread.h" | 5 #include "base/threading/thread.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/debug/leak_annotations.h" | 12 #include "base/debug/leak_annotations.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/test/gtest_util.h" | 18 #include "base/test/gtest_util.h" |
| 19 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 19 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 20 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
| 21 #include "base/time/time.h" |
| 21 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
| 24 | 25 |
| 25 using base::Thread; | 26 using base::Thread; |
| 26 | 27 |
| 27 typedef PlatformTest ThreadTest; | 28 typedef PlatformTest ThreadTest; |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 } | 430 } |
| 430 | 431 |
| 431 TEST_F(ThreadTest, MultipleWaitUntilThreadStarted) { | 432 TEST_F(ThreadTest, MultipleWaitUntilThreadStarted) { |
| 432 Thread a("MultipleWaitUntilThreadStarted"); | 433 Thread a("MultipleWaitUntilThreadStarted"); |
| 433 EXPECT_TRUE(a.Start()); | 434 EXPECT_TRUE(a.Start()); |
| 434 // It's OK to call WaitUntilThreadStarted() multiple times. | 435 // It's OK to call WaitUntilThreadStarted() multiple times. |
| 435 EXPECT_TRUE(a.WaitUntilThreadStarted()); | 436 EXPECT_TRUE(a.WaitUntilThreadStarted()); |
| 436 EXPECT_TRUE(a.WaitUntilThreadStarted()); | 437 EXPECT_TRUE(a.WaitUntilThreadStarted()); |
| 437 } | 438 } |
| 438 | 439 |
| 440 TEST_F(ThreadTest, FlushForTesting) { |
| 441 Thread a("FlushForTesting"); |
| 442 |
| 443 // Flushing a non-running thread should be a no-op. |
| 444 a.FlushForTesting(); |
| 445 |
| 446 ASSERT_TRUE(a.Start()); |
| 447 |
| 448 // Flushing a thread with no tasks shouldn't block. |
| 449 a.FlushForTesting(); |
| 450 |
| 451 constexpr base::TimeDelta kSleepPerTestTask = |
| 452 base::TimeDelta::FromMilliseconds(50); |
| 453 constexpr size_t kNumSleepTasks = 5; |
| 454 |
| 455 const base::TimeTicks ticks_before_post = base::TimeTicks::Now(); |
| 456 |
| 457 for (size_t i = 0; i < kNumSleepTasks; ++i) { |
| 458 a.task_runner()->PostTask( |
| 459 FROM_HERE, base::Bind(&base::PlatformThread::Sleep, kSleepPerTestTask)); |
| 460 } |
| 461 |
| 462 // All tasks should have executed, as reflected by the elapsed time. |
| 463 a.FlushForTesting(); |
| 464 EXPECT_GE(base::TimeTicks::Now() - ticks_before_post, |
| 465 kNumSleepTasks * kSleepPerTestTask); |
| 466 |
| 467 a.Stop(); |
| 468 |
| 469 // Flushing a stopped thread should be a no-op. |
| 470 a.FlushForTesting(); |
| 471 } |
| 472 |
| 439 namespace { | 473 namespace { |
| 440 | 474 |
| 441 // A Thread which uses a MessageLoop on the stack. It won't start a real | 475 // A Thread which uses a MessageLoop on the stack. It won't start a real |
| 442 // underlying thread (instead its messages can be processed by a RunLoop on the | 476 // underlying thread (instead its messages can be processed by a RunLoop on the |
| 443 // stack). | 477 // stack). |
| 444 class ExternalMessageLoopThread : public Thread { | 478 class ExternalMessageLoopThread : public Thread { |
| 445 public: | 479 public: |
| 446 ExternalMessageLoopThread() : Thread("ExternalMessageLoopThread") {} | 480 ExternalMessageLoopThread() : Thread("ExternalMessageLoopThread") {} |
| 447 | 481 |
| 448 ~ExternalMessageLoopThread() override { Stop(); } | 482 ~ExternalMessageLoopThread() override { Stop(); } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 474 | 508 |
| 475 a.Stop(); | 509 a.Stop(); |
| 476 EXPECT_FALSE(a.message_loop()); | 510 EXPECT_FALSE(a.message_loop()); |
| 477 EXPECT_FALSE(a.IsRunning()); | 511 EXPECT_FALSE(a.IsRunning()); |
| 478 | 512 |
| 479 // Confirm that running any remaining tasks posted from Stop() goes smoothly | 513 // Confirm that running any remaining tasks posted from Stop() goes smoothly |
| 480 // (e.g. https://codereview.chromium.org/2135413003/#ps300001 crashed if | 514 // (e.g. https://codereview.chromium.org/2135413003/#ps300001 crashed if |
| 481 // StopSoon() posted Thread::ThreadQuitHelper() while |run_loop_| was null). | 515 // StopSoon() posted Thread::ThreadQuitHelper() while |run_loop_| was null). |
| 482 base::RunLoop().RunUntilIdle(); | 516 base::RunLoop().RunUntilIdle(); |
| 483 } | 517 } |
| OLD | NEW |