| Index: base/threading/worker_pool_unittest.cc
|
| diff --git a/base/threading/worker_pool_unittest.cc b/base/threading/worker_pool_unittest.cc
|
| index bbdd8cf61ca779ff0026cc2b70f0c6cc8f322081..4960cfb51ccb26ddc51888958b9dabe06699c7d7 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) {
|
| @@ -42,4 +89,18 @@ TEST_F(WorkerPoolTest, PostTask) {
|
| long_test_event.Wait();
|
| }
|
|
|
| +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
|
|
|