summaryrefslogtreecommitdiffstats
path: root/base/test
diff options
context:
space:
mode:
authorjochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-28 06:43:20 +0000
committerjochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-28 06:43:20 +0000
commit8aaae7bcd3882f6fddc17b8fb47963878b8ae98a (patch)
treebb975aeffd798e068704d8a1ec034721e9343a34 /base/test
parentc071da723c5ae857625e9b66dab7fa692c38c47a (diff)
downloadchromium_src-8aaae7bcd3882f6fddc17b8fb47963878b8ae98a.zip
chromium_src-8aaae7bcd3882f6fddc17b8fb47963878b8ae98a.tar.gz
chromium_src-8aaae7bcd3882f6fddc17b8fb47963878b8ae98a.tar.bz2
Move the thread test helper to base/test
BUG=none TEST=none Review URL: http://codereview.chromium.org/7239023 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90723 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/test')
-rw-r--r--base/test/thread_test_helper.cc33
-rw-r--r--base/test/thread_test_helper.h49
2 files changed, 82 insertions, 0 deletions
diff --git a/base/test/thread_test_helper.cc b/base/test/thread_test_helper.cc
new file mode 100644
index 0000000..e79ca92
--- /dev/null
+++ b/base/test/thread_test_helper.cc
@@ -0,0 +1,33 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/test/thread_test_helper.h"
+
+namespace base {
+
+ThreadTestHelper::ThreadTestHelper(MessageLoopProxy* target_thread)
+ : test_result_(false),
+ target_thread_(target_thread),
+ done_event_(false, false) {
+}
+
+bool ThreadTestHelper::Run() {
+ if (!target_thread_->PostTask(FROM_HERE, NewRunnableMethod(
+ this, &ThreadTestHelper::RunInThread))) {
+ return false;
+ }
+ done_event_.Wait();
+ return test_result_;
+}
+
+void ThreadTestHelper::RunTest() { set_test_result(true); }
+
+ThreadTestHelper::~ThreadTestHelper() {}
+
+void ThreadTestHelper::RunInThread() {
+ RunTest();
+ done_event_.Signal();
+}
+
+} // namespace base
diff --git a/base/test/thread_test_helper.h b/base/test/thread_test_helper.h
new file mode 100644
index 0000000..e6b32bc
--- /dev/null
+++ b/base/test/thread_test_helper.h
@@ -0,0 +1,49 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_TEST_THREAD_TEST_HELPER_H_
+#define BASE_TEST_THREAD_TEST_HELPER_H_
+#pragma once
+
+#include "base/compiler_specific.h"
+#include "base/memory/ref_counted.h"
+#include "base/message_loop_proxy.h"
+#include "base/synchronization/waitable_event.h"
+
+namespace base {
+
+// Helper class that executes code on a given thread while blocking on the
+// invoking thread. To use, derive from this class and overwrite RunTest. An
+// alternative use of this class is to use it directly. It will then block
+// until all pending tasks on a given thread have been executed.
+class ThreadTestHelper : public RefCountedThreadSafe<ThreadTestHelper> {
+ public:
+ explicit ThreadTestHelper(MessageLoopProxy* target_thread);
+
+ // True if RunTest() was successfully executed on the target thread.
+ bool Run() WARN_UNUSED_RESULT;
+
+ virtual void RunTest();
+
+ protected:
+ friend class RefCountedThreadSafe<ThreadTestHelper>;
+
+ virtual ~ThreadTestHelper();
+
+ // Use this method to store the result of RunTest().
+ void set_test_result(bool test_result) { test_result_ = test_result; }
+
+ private:
+ void RunInThread();
+
+ bool test_result_;
+ scoped_refptr<MessageLoopProxy> target_thread_;
+ WaitableEvent done_event_;
+
+ DISALLOW_COPY_AND_ASSIGN(ThreadTestHelper);
+};
+
+} // namespace base
+
+#endif // BASE_TEST_THREAD_TEST_HELPER_H_