blob: 3c496314fa2d86442bb3469f08c67168f01dbfb8 [file] [log] [blame]
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Borrowed from Chromium's src/base/threading/thread_checker_unittest.cc.
12
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000013#include "testing/gtest/include/gtest/gtest.h"
henrik.lundin@webrtc.org18617cf2014-09-15 11:19:35 +000014#include "webrtc/base/checks.h"
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000015#include "webrtc/base/thread.h"
16#include "webrtc/base/thread_checker.h"
17#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgfded02c2014-09-19 13:10:10 +000018#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000019
20// Duplicated from base/threading/thread_checker.h so that we can be
21// good citizens there and undef the macro.
22#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
23#define ENABLE_THREAD_CHECKER 1
24#else
25#define ENABLE_THREAD_CHECKER 0
26#endif
27
28namespace rtc {
29
30namespace {
31
32// Simple class to exercise the basics of ThreadChecker.
33// Both the destructor and DoStuff should verify that they were
34// called on the same thread as the constructor.
35class ThreadCheckerClass : public ThreadChecker {
36 public:
37 ThreadCheckerClass() {}
38
39 // Verifies that it was called on the same thread as the constructor.
40 void DoStuff() {
henrik.lundin@webrtc.org18617cf2014-09-15 11:19:35 +000041 DCHECK(CalledOnValidThread());
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000042 }
43
44 void DetachFromThread() {
45 ThreadChecker::DetachFromThread();
46 }
47
48 static void MethodOnDifferentThreadImpl();
49 static void DetachThenCallFromDifferentThreadImpl();
50
51 private:
52 DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass);
53};
54
55// Calls ThreadCheckerClass::DoStuff on another thread.
56class CallDoStuffOnThread : public Thread {
57 public:
58 explicit CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class)
59 : Thread(),
60 thread_checker_class_(thread_checker_class) {
61 SetName("call_do_stuff_on_thread", NULL);
62 }
63
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 void Run() override { thread_checker_class_->DoStuff(); }
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000065
66 // New method. Needed since Thread::Join is protected, and it is called by
67 // the TEST.
68 void Join() {
69 Thread::Join();
70 }
71
72 private:
73 ThreadCheckerClass* thread_checker_class_;
74
75 DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread);
76};
77
78// Deletes ThreadCheckerClass on a different thread.
79class DeleteThreadCheckerClassOnThread : public Thread {
80 public:
81 explicit DeleteThreadCheckerClassOnThread(
82 ThreadCheckerClass* thread_checker_class)
83 : Thread(),
84 thread_checker_class_(thread_checker_class) {
85 SetName("delete_thread_checker_class_on_thread", NULL);
86 }
87
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000088 void Run() override { thread_checker_class_.reset(); }
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000089
90 // New method. Needed since Thread::Join is protected, and it is called by
91 // the TEST.
92 void Join() {
93 Thread::Join();
94 }
95
96 private:
97 scoped_ptr<ThreadCheckerClass> thread_checker_class_;
98
99 DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread);
100};
101
102} // namespace
103
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000104TEST(ThreadCheckerTest, CallsAllowedOnSameThread) {
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +0000105 scoped_ptr<ThreadCheckerClass> thread_checker_class(
106 new ThreadCheckerClass);
107
108 // Verify that DoStuff doesn't assert.
109 thread_checker_class->DoStuff();
110
111 // Verify that the destructor doesn't assert.
112 thread_checker_class.reset();
113}
114
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000115TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) {
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +0000116 scoped_ptr<ThreadCheckerClass> thread_checker_class(
117 new ThreadCheckerClass);
118
119 // Verify that the destructor doesn't assert
120 // when called on a different thread.
121 DeleteThreadCheckerClassOnThread delete_on_thread(
122 thread_checker_class.release());
123
124 delete_on_thread.Start();
125 delete_on_thread.Join();
126}
127
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000128TEST(ThreadCheckerTest, DetachFromThread) {
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +0000129 scoped_ptr<ThreadCheckerClass> thread_checker_class(
130 new ThreadCheckerClass);
131
132 // Verify that DoStuff doesn't assert when called on a different thread after
133 // a call to DetachFromThread.
134 thread_checker_class->DetachFromThread();
135 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
136
137 call_on_thread.Start();
138 call_on_thread.Join();
139}
140
141#if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
142
143void ThreadCheckerClass::MethodOnDifferentThreadImpl() {
144 scoped_ptr<ThreadCheckerClass> thread_checker_class(
145 new ThreadCheckerClass);
146
147 // DoStuff should assert in debug builds only when called on a
148 // different thread.
149 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
150
151 call_on_thread.Start();
152 call_on_thread.Join();
153}
154
155#if ENABLE_THREAD_CHECKER
tommi@webrtc.org2eb16602015-02-07 19:17:47 +0000156TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +0000157 ASSERT_DEATH({
158 ThreadCheckerClass::MethodOnDifferentThreadImpl();
159 }, "");
160}
161#else
162TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) {
163 ThreadCheckerClass::MethodOnDifferentThreadImpl();
164}
165#endif // ENABLE_THREAD_CHECKER
166
167void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() {
168 scoped_ptr<ThreadCheckerClass> thread_checker_class(
169 new ThreadCheckerClass);
170
171 // DoStuff doesn't assert when called on a different thread
172 // after a call to DetachFromThread.
173 thread_checker_class->DetachFromThread();
174 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
175
176 call_on_thread.Start();
177 call_on_thread.Join();
178
179 // DoStuff should assert in debug builds only after moving to
180 // another thread.
181 thread_checker_class->DoStuff();
182}
183
184#if ENABLE_THREAD_CHECKER
tommi@webrtc.org2eb16602015-02-07 19:17:47 +0000185TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) {
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +0000186 ASSERT_DEATH({
187 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
188 }, "");
189}
190#else
191TEST(ThreadCheckerTest, DetachFromThreadInRelease) {
192 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
193}
194#endif // ENABLE_THREAD_CHECKER
195
196#endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
197
198// Just in case we ever get lumped together with other compilation units.
199#undef ENABLE_THREAD_CHECKER
200
201} // namespace rtc