Index: base/threading/thread_unittest.cc |
diff --git a/base/threading/thread_unittest.cc b/base/threading/thread_unittest.cc |
index e15c40b96a36365d21db3df22eb0d7628f0dcde7..0867a6401e9d0a3a52491c21fb9c6d912b73c07c 100644 |
--- a/base/threading/thread_unittest.cc |
+++ b/base/threading/thread_unittest.cc |
@@ -18,6 +18,7 @@ |
#include "base/test/gtest_util.h" |
#include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
#include "base/threading/platform_thread.h" |
+#include "base/time/time.h" |
#include "build/build_config.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "testing/platform_test.h" |
@@ -436,6 +437,39 @@ TEST_F(ThreadTest, MultipleWaitUntilThreadStarted) { |
EXPECT_TRUE(a.WaitUntilThreadStarted()); |
} |
+TEST_F(ThreadTest, FlushForTesting) { |
+ Thread a("FlushForTesting"); |
+ |
+ // Flushing a non-running thread should be a no-op. |
+ a.FlushForTesting(); |
+ |
+ ASSERT_TRUE(a.Start()); |
+ |
+ // Flushing a thread with no tasks shouldn't block. |
+ a.FlushForTesting(); |
+ |
+ constexpr base::TimeDelta kSleepPerTestTask = |
+ base::TimeDelta::FromMilliseconds(50); |
+ constexpr size_t kNumSleepTasks = 5; |
+ |
+ const base::TimeTicks ticks_before_post = base::TimeTicks::Now(); |
+ |
+ for (size_t i = 0; i < kNumSleepTasks; ++i) { |
+ a.task_runner()->PostTask( |
+ FROM_HERE, base::Bind(&base::PlatformThread::Sleep, kSleepPerTestTask)); |
+ } |
+ |
+ // All tasks should have executed, as reflected by the elapsed time. |
+ a.FlushForTesting(); |
+ EXPECT_GE(base::TimeTicks::Now() - ticks_before_post, |
+ kNumSleepTasks * kSleepPerTestTask); |
+ |
+ a.Stop(); |
+ |
+ // Flushing a stopped thread should be a no-op. |
+ a.FlushForTesting(); |
+} |
+ |
namespace { |
// A Thread which uses a MessageLoop on the stack. It won't start a real |