1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
7 #include "base/bind_helpers.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/sequence_checker.h"
14 #include "base/test/sequenced_worker_pool_owner.h"
15 #include "base/threading/thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 // Duplicated from base/sequence_checker.h so that we can be good citizens
19 // there and undef the macro.
20 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
21 #define ENABLE_SEQUENCE_CHECKER 1
23 #define ENABLE_SEQUENCE_CHECKER 0
30 const size_t kNumWorkerThreads
= 3;
32 // Simple class to exercise the basics of SequenceChecker.
33 // DoStuff should verify that it's called on a valid sequenced thread.
34 // SequenceCheckedObject can be destroyed on any thread (like WeakPtr).
35 class SequenceCheckedObject
{
37 SequenceCheckedObject() {}
38 ~SequenceCheckedObject() {}
40 // Verifies that it was called on the same thread as the constructor.
42 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
45 void DetachFromSequence() {
46 sequence_checker_
.DetachFromSequence();
50 SequenceChecker sequence_checker_
;
52 DISALLOW_COPY_AND_ASSIGN(SequenceCheckedObject
);
55 class SequenceCheckerTest
: public testing::Test
{
57 SequenceCheckerTest() : other_thread_("sequence_checker_test_other_thread") {}
59 void SetUp() override
{
60 other_thread_
.Start();
64 void TearDown() override
{
70 base::Thread
* other_thread() { return &other_thread_
; }
72 const scoped_refptr
<SequencedWorkerPool
>& pool() {
73 return pool_owner_
->pool();
76 void PostDoStuffToWorkerPool(SequenceCheckedObject
* sequence_checked_object
,
77 const std::string
& token_name
) {
78 pool()->PostNamedSequencedWorkerTask(
81 base::Bind(&SequenceCheckedObject::DoStuff
,
82 base::Unretained(sequence_checked_object
)));
85 void PostDoStuffToOtherThread(
86 SequenceCheckedObject
* sequence_checked_object
) {
87 other_thread()->message_loop()->PostTask(
89 base::Bind(&SequenceCheckedObject::DoStuff
,
90 base::Unretained(sequence_checked_object
)));
93 void PostDeleteToOtherThread(
94 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object
) {
95 other_thread()->message_loop()->DeleteSoon(
97 sequence_checked_object
.release());
100 // Destroys the SequencedWorkerPool instance, blocking until it is fully shut
101 // down, and creates a new instance.
103 pool_owner_
.reset(new SequencedWorkerPoolOwner(kNumWorkerThreads
, "test"));
106 void MethodOnDifferentThreadDeathTest();
107 void DetachThenCallFromDifferentThreadDeathTest();
108 void DifferentSequenceTokensDeathTest();
109 void WorkerPoolAndSimpleThreadDeathTest();
110 void TwoDifferentWorkerPoolsDeathTest();
113 MessageLoop message_loop_
; // Needed by SequencedWorkerPool to function.
114 base::Thread other_thread_
;
115 scoped_ptr
<SequencedWorkerPoolOwner
> pool_owner_
;
118 TEST_F(SequenceCheckerTest
, CallsAllowedOnSameThread
) {
119 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object(
120 new SequenceCheckedObject
);
122 // Verify that DoStuff doesn't assert.
123 sequence_checked_object
->DoStuff();
125 // Verify that the destructor doesn't assert.
126 sequence_checked_object
.reset();
129 TEST_F(SequenceCheckerTest
, DestructorAllowedOnDifferentThread
) {
130 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object(
131 new SequenceCheckedObject
);
133 // Verify the destructor doesn't assert when called on a different thread.
134 PostDeleteToOtherThread(sequence_checked_object
.Pass());
135 other_thread()->Stop();
138 TEST_F(SequenceCheckerTest
, DetachFromSequence
) {
139 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object(
140 new SequenceCheckedObject
);
142 // Verify that DoStuff doesn't assert when called on a different thread after
143 // a call to DetachFromSequence.
144 sequence_checked_object
->DetachFromSequence();
146 PostDoStuffToOtherThread(sequence_checked_object
.get());
147 other_thread()->Stop();
150 TEST_F(SequenceCheckerTest
, SameSequenceTokenValid
) {
151 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object(
152 new SequenceCheckedObject
);
154 sequence_checked_object
->DetachFromSequence();
155 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
156 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
157 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
158 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
159 pool()->FlushForTesting();
161 PostDeleteToOtherThread(sequence_checked_object
.Pass());
162 other_thread()->Stop();
165 TEST_F(SequenceCheckerTest
, DetachSequenceTokenValid
) {
166 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object(
167 new SequenceCheckedObject
);
169 sequence_checked_object
->DetachFromSequence();
170 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
171 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
172 pool()->FlushForTesting();
174 sequence_checked_object
->DetachFromSequence();
175 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "B");
176 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "B");
177 pool()->FlushForTesting();
179 PostDeleteToOtherThread(sequence_checked_object
.Pass());
180 other_thread()->Stop();
183 #if GTEST_HAS_DEATH_TEST || !ENABLE_SEQUENCE_CHECKER
185 void SequenceCheckerTest::MethodOnDifferentThreadDeathTest() {
186 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object(
187 new SequenceCheckedObject
);
189 // DoStuff should assert in debug builds only when called on a
191 PostDoStuffToOtherThread(sequence_checked_object
.get());
192 other_thread()->Stop();
195 #if ENABLE_SEQUENCE_CHECKER
196 TEST_F(SequenceCheckerTest
, MethodNotAllowedOnDifferentThreadDeathTestInDebug
) {
197 // The default style "fast" does not support multi-threaded tests.
198 ::testing::FLAGS_gtest_death_test_style
= "threadsafe";
200 MethodOnDifferentThreadDeathTest();
204 TEST_F(SequenceCheckerTest
, MethodAllowedOnDifferentThreadDeathTestInRelease
) {
205 MethodOnDifferentThreadDeathTest();
207 #endif // ENABLE_SEQUENCE_CHECKER
209 void SequenceCheckerTest::DetachThenCallFromDifferentThreadDeathTest() {
210 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object(
211 new SequenceCheckedObject
);
213 // DoStuff doesn't assert when called on a different thread
214 // after a call to DetachFromSequence.
215 sequence_checked_object
->DetachFromSequence();
216 PostDoStuffToOtherThread(sequence_checked_object
.get());
217 other_thread()->Stop();
219 // DoStuff should assert in debug builds only after moving to
221 sequence_checked_object
->DoStuff();
224 #if ENABLE_SEQUENCE_CHECKER
225 TEST_F(SequenceCheckerTest
, DetachFromSequenceDeathTestInDebug
) {
226 // The default style "fast" does not support multi-threaded tests.
227 ::testing::FLAGS_gtest_death_test_style
= "threadsafe";
229 DetachThenCallFromDifferentThreadDeathTest();
233 TEST_F(SequenceCheckerTest
, DetachFromThreadDeathTestInRelease
) {
234 DetachThenCallFromDifferentThreadDeathTest();
236 #endif // ENABLE_SEQUENCE_CHECKER
238 void SequenceCheckerTest::DifferentSequenceTokensDeathTest() {
239 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object(
240 new SequenceCheckedObject
);
242 sequence_checked_object
->DetachFromSequence();
243 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
244 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
245 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "B");
246 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "B");
247 pool()->FlushForTesting();
249 PostDeleteToOtherThread(sequence_checked_object
.Pass());
250 other_thread()->Stop();
253 #if ENABLE_SEQUENCE_CHECKER
254 TEST_F(SequenceCheckerTest
, DifferentSequenceTokensDeathTestInDebug
) {
255 // The default style "fast" does not support multi-threaded tests.
256 ::testing::FLAGS_gtest_death_test_style
= "threadsafe";
258 DifferentSequenceTokensDeathTest();
262 TEST_F(SequenceCheckerTest
, DifferentSequenceTokensDeathTestInRelease
) {
263 DifferentSequenceTokensDeathTest();
265 #endif // ENABLE_SEQUENCE_CHECKER
267 void SequenceCheckerTest::WorkerPoolAndSimpleThreadDeathTest() {
268 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object(
269 new SequenceCheckedObject
);
271 sequence_checked_object
->DetachFromSequence();
272 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
273 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
274 pool()->FlushForTesting();
276 PostDoStuffToOtherThread(sequence_checked_object
.get());
277 other_thread()->Stop();
280 #if ENABLE_SEQUENCE_CHECKER
281 TEST_F(SequenceCheckerTest
, WorkerPoolAndSimpleThreadDeathTestInDebug
) {
282 // The default style "fast" does not support multi-threaded tests.
283 ::testing::FLAGS_gtest_death_test_style
= "threadsafe";
285 WorkerPoolAndSimpleThreadDeathTest();
289 TEST_F(SequenceCheckerTest
, WorkerPoolAndSimpleThreadDeathTestInRelease
) {
290 WorkerPoolAndSimpleThreadDeathTest();
292 #endif // ENABLE_SEQUENCE_CHECKER
294 void SequenceCheckerTest::TwoDifferentWorkerPoolsDeathTest() {
295 scoped_ptr
<SequenceCheckedObject
> sequence_checked_object(
296 new SequenceCheckedObject
);
298 sequence_checked_object
->DetachFromSequence();
299 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
300 PostDoStuffToWorkerPool(sequence_checked_object
.get(), "A");
301 pool()->FlushForTesting();
303 SequencedWorkerPoolOwner
second_pool_owner(kNumWorkerThreads
, "test2");
304 second_pool_owner
.pool()->PostNamedSequencedWorkerTask(
307 base::Bind(&SequenceCheckedObject::DoStuff
,
308 base::Unretained(sequence_checked_object
.get())));
309 second_pool_owner
.pool()->FlushForTesting();
310 second_pool_owner
.pool()->Shutdown();
313 #if ENABLE_SEQUENCE_CHECKER
314 TEST_F(SequenceCheckerTest
, TwoDifferentWorkerPoolsDeathTestInDebug
) {
315 // The default style "fast" does not support multi-threaded tests.
316 ::testing::FLAGS_gtest_death_test_style
= "threadsafe";
318 TwoDifferentWorkerPoolsDeathTest();
322 TEST_F(SequenceCheckerTest
, TwoDifferentWorkerPoolsDeathTestInRelease
) {
323 TwoDifferentWorkerPoolsDeathTest();
325 #endif // ENABLE_SEQUENCE_CHECKER
327 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_SEQUENCE_CHECKER
333 // Just in case we ever get lumped together with other compilation units.
334 #undef ENABLE_SEQUENCE_CHECKER