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

Unified Diff: base/threading/thread_unittest.cc

Issue 2337253003: Add FlushForTesting to base::Thread. (Closed)
Patch Set: Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
« base/threading/thread.cc ('K') | « base/threading/thread.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« base/threading/thread.cc ('K') | « base/threading/thread.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698