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

Unified Diff: base/threading/worker_pool_unittest.cc

Issue 8139028: Add WorkerPool::PostTaskAndReply and use in DHCP code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing review comments. Fixing flakiness in reuse test. Created 9 years, 2 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
Index: base/threading/worker_pool_unittest.cc
diff --git a/base/threading/worker_pool_unittest.cc b/base/threading/worker_pool_unittest.cc
index 2d2b05574e2b17f8e14eb597c29ea434e9e8da14..2b08cef598307a9c8a09ed882b40ded937d26d6d 100644
--- a/base/threading/worker_pool_unittest.cc
+++ b/base/threading/worker_pool_unittest.cc
@@ -4,8 +4,13 @@
#include "base/threading/worker_pool.h"
+#include "base/bind.h"
#include "base/location.h"
+#include "base/message_loop.h"
#include "base/task.h"
+#include "base/test/test_timeouts.h"
+#include "base/time.h"
+#include "base/threading/thread_checker_impl.h"
#include "base/synchronization/waitable_event.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
@@ -29,6 +34,48 @@ class PostTaskTestTask : public Task {
WaitableEvent* event_;
};
+class PostTaskAndReplyTester
+ : public base::RefCountedThreadSafe<PostTaskAndReplyTester> {
+ public:
+ PostTaskAndReplyTester() : finished_(false), test_event_(false, false) {
+ }
+
+ void RunTest() {
+ ASSERT_TRUE(thread_checker_.CalledOnValidThread());
+ WorkerPool::PostTaskAndReply(
+ FROM_HERE,
+ base::Bind(&PostTaskAndReplyTester::OnWorkerThread, this),
+ base::Bind(&PostTaskAndReplyTester::OnOriginalThread, this),
+ false);
+
+ bool signaled = test_event_.Wait();
+ EXPECT_TRUE(signaled);
+ }
+
+ void OnWorkerThread() {
+ // We're not on the original thread.
+ EXPECT_FALSE(thread_checker_.CalledOnValidThread());
+
+ test_event_.Signal();
+ }
+
+ void OnOriginalThread() {
+ EXPECT_TRUE(thread_checker_.CalledOnValidThread());
+ finished_ = true;
+ }
+
+ bool finished() const {
+ return finished_;
+ }
+
+ private:
+ bool finished_;
+ WaitableEvent test_event_;
+
+ // The Impl version performs its checks even in release builds.
+ ThreadCheckerImpl thread_checker_;
+};
+
} // namespace
TEST_F(WorkerPoolTest, PostTask) {
@@ -45,4 +92,18 @@ TEST_F(WorkerPoolTest, PostTask) {
EXPECT_TRUE(signaled);
}
+TEST_F(WorkerPoolTest, PostTaskAndReply) {
+ MessageLoop message_loop;
+ scoped_refptr<PostTaskAndReplyTester> tester(new PostTaskAndReplyTester());
+ tester->RunTest();
+
+ const TimeDelta kMaxDuration =
+ TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms());
+ TimeTicks start = TimeTicks::Now();
+ while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) {
+ MessageLoop::current()->RunAllPending();
+ }
+ EXPECT_TRUE(tester->finished());
+}
+
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698