This source file includes following definitions.
- DoStuff
- DetachFromThread
- thread_checker_class_
- Run
- thread_checker_class_
- Run
- TEST
- TEST
- TEST
- MethodOnDifferentThreadImpl
- TEST
- TEST
- DetachThenCallFromDifferentThreadImpl
- TEST
- TEST
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread_checker.h"
#include "base/threading/simple_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
#define ENABLE_THREAD_CHECKER 1
#else
#define ENABLE_THREAD_CHECKER 0
#endif
namespace base {
namespace {
class ThreadCheckerClass : public ThreadChecker {
public:
ThreadCheckerClass() {}
void DoStuff() {
DCHECK(CalledOnValidThread());
}
void DetachFromThread() {
ThreadChecker::DetachFromThread();
}
static void MethodOnDifferentThreadImpl();
static void DetachThenCallFromDifferentThreadImpl();
private:
DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass);
};
class CallDoStuffOnThread : public base::SimpleThread {
public:
explicit CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class)
: SimpleThread("call_do_stuff_on_thread"),
thread_checker_class_(thread_checker_class) {
}
virtual void Run() OVERRIDE {
thread_checker_class_->DoStuff();
}
private:
ThreadCheckerClass* thread_checker_class_;
DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread);
};
class DeleteThreadCheckerClassOnThread : public base::SimpleThread {
public:
explicit DeleteThreadCheckerClassOnThread(
ThreadCheckerClass* thread_checker_class)
: SimpleThread("delete_thread_checker_class_on_thread"),
thread_checker_class_(thread_checker_class) {
}
virtual void Run() OVERRIDE {
thread_checker_class_.reset();
}
private:
scoped_ptr<ThreadCheckerClass> thread_checker_class_;
DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread);
};
}
TEST(ThreadCheckerTest, CallsAllowedOnSameThread) {
scoped_ptr<ThreadCheckerClass> thread_checker_class(
new ThreadCheckerClass);
thread_checker_class->DoStuff();
thread_checker_class.reset();
}
TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) {
scoped_ptr<ThreadCheckerClass> thread_checker_class(
new ThreadCheckerClass);
DeleteThreadCheckerClassOnThread delete_on_thread(
thread_checker_class.release());
delete_on_thread.Start();
delete_on_thread.Join();
}
TEST(ThreadCheckerTest, DetachFromThread) {
scoped_ptr<ThreadCheckerClass> thread_checker_class(
new ThreadCheckerClass);
thread_checker_class->DetachFromThread();
CallDoStuffOnThread call_on_thread(thread_checker_class.get());
call_on_thread.Start();
call_on_thread.Join();
}
#if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
void ThreadCheckerClass::MethodOnDifferentThreadImpl() {
scoped_ptr<ThreadCheckerClass> thread_checker_class(
new ThreadCheckerClass);
CallDoStuffOnThread call_on_thread(thread_checker_class.get());
call_on_thread.Start();
call_on_thread.Join();
}
#if ENABLE_THREAD_CHECKER
TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
ASSERT_DEATH({
ThreadCheckerClass::MethodOnDifferentThreadImpl();
}, "");
}
#else
TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) {
ThreadCheckerClass::MethodOnDifferentThreadImpl();
}
#endif
void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() {
scoped_ptr<ThreadCheckerClass> thread_checker_class(
new ThreadCheckerClass);
thread_checker_class->DetachFromThread();
CallDoStuffOnThread call_on_thread(thread_checker_class.get());
call_on_thread.Start();
call_on_thread.Join();
thread_checker_class->DoStuff();
}
#if ENABLE_THREAD_CHECKER
TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) {
ASSERT_DEATH({
ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
}, "");
}
#else
TEST(ThreadCheckerTest, DetachFromThreadInRelease) {
ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
}
#endif
#endif
#undef ENABLE_THREAD_CHECKER
}