Fix some JS style nits.
[chromium-blink-merge.git] / base / sequence_checker_unittest.cc
blob0aa0f9cdbb74d8cd0711f5621c3a81067d5d6db6
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"
6 #include "base/bind.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/sequence_checker.h"
13 #include "base/single_thread_task_runner.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
22 #else
23 #define ENABLE_SEQUENCE_CHECKER 0
24 #endif
26 namespace base {
28 namespace {
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 {
36 public:
37 SequenceCheckedObject() {}
38 ~SequenceCheckedObject() {}
40 // Verifies that it was called on the same thread as the constructor.
41 void DoStuff() {
42 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
45 void DetachFromSequence() {
46 sequence_checker_.DetachFromSequence();
49 private:
50 SequenceChecker sequence_checker_;
52 DISALLOW_COPY_AND_ASSIGN(SequenceCheckedObject);
55 class SequenceCheckerTest : public testing::Test {
56 public:
57 SequenceCheckerTest() : other_thread_("sequence_checker_test_other_thread") {}
59 void SetUp() override {
60 other_thread_.Start();
61 ResetPool();
64 void TearDown() override {
65 other_thread_.Stop();
66 pool()->Shutdown();
69 protected:
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(
79 token_name,
80 FROM_HERE,
81 base::Bind(&SequenceCheckedObject::DoStuff,
82 base::Unretained(sequence_checked_object)));
85 void PostDoStuffToOtherThread(
86 SequenceCheckedObject* sequence_checked_object) {
87 other_thread()->task_runner()->PostTask(
88 FROM_HERE, base::Bind(&SequenceCheckedObject::DoStuff,
89 base::Unretained(sequence_checked_object)));
92 void PostDeleteToOtherThread(
93 scoped_ptr<SequenceCheckedObject> sequence_checked_object) {
94 other_thread()->message_loop()->DeleteSoon(
95 FROM_HERE,
96 sequence_checked_object.release());
99 // Destroys the SequencedWorkerPool instance, blocking until it is fully shut
100 // down, and creates a new instance.
101 void ResetPool() {
102 pool_owner_.reset(new SequencedWorkerPoolOwner(kNumWorkerThreads, "test"));
105 void MethodOnDifferentThreadDeathTest();
106 void DetachThenCallFromDifferentThreadDeathTest();
107 void DifferentSequenceTokensDeathTest();
108 void WorkerPoolAndSimpleThreadDeathTest();
109 void TwoDifferentWorkerPoolsDeathTest();
111 private:
112 MessageLoop message_loop_; // Needed by SequencedWorkerPool to function.
113 base::Thread other_thread_;
114 scoped_ptr<SequencedWorkerPoolOwner> pool_owner_;
117 TEST_F(SequenceCheckerTest, CallsAllowedOnSameThread) {
118 scoped_ptr<SequenceCheckedObject> sequence_checked_object(
119 new SequenceCheckedObject);
121 // Verify that DoStuff doesn't assert.
122 sequence_checked_object->DoStuff();
124 // Verify that the destructor doesn't assert.
125 sequence_checked_object.reset();
128 TEST_F(SequenceCheckerTest, DestructorAllowedOnDifferentThread) {
129 scoped_ptr<SequenceCheckedObject> sequence_checked_object(
130 new SequenceCheckedObject);
132 // Verify the destructor doesn't assert when called on a different thread.
133 PostDeleteToOtherThread(sequence_checked_object.Pass());
134 other_thread()->Stop();
137 TEST_F(SequenceCheckerTest, DetachFromSequence) {
138 scoped_ptr<SequenceCheckedObject> sequence_checked_object(
139 new SequenceCheckedObject);
141 // Verify that DoStuff doesn't assert when called on a different thread after
142 // a call to DetachFromSequence.
143 sequence_checked_object->DetachFromSequence();
145 PostDoStuffToOtherThread(sequence_checked_object.get());
146 other_thread()->Stop();
149 TEST_F(SequenceCheckerTest, SameSequenceTokenValid) {
150 scoped_ptr<SequenceCheckedObject> sequence_checked_object(
151 new SequenceCheckedObject);
153 sequence_checked_object->DetachFromSequence();
154 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
155 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
156 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
157 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
158 pool()->FlushForTesting();
160 PostDeleteToOtherThread(sequence_checked_object.Pass());
161 other_thread()->Stop();
164 TEST_F(SequenceCheckerTest, DetachSequenceTokenValid) {
165 scoped_ptr<SequenceCheckedObject> sequence_checked_object(
166 new SequenceCheckedObject);
168 sequence_checked_object->DetachFromSequence();
169 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
170 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
171 pool()->FlushForTesting();
173 sequence_checked_object->DetachFromSequence();
174 PostDoStuffToWorkerPool(sequence_checked_object.get(), "B");
175 PostDoStuffToWorkerPool(sequence_checked_object.get(), "B");
176 pool()->FlushForTesting();
178 PostDeleteToOtherThread(sequence_checked_object.Pass());
179 other_thread()->Stop();
182 #if GTEST_HAS_DEATH_TEST || !ENABLE_SEQUENCE_CHECKER
184 void SequenceCheckerTest::MethodOnDifferentThreadDeathTest() {
185 scoped_ptr<SequenceCheckedObject> sequence_checked_object(
186 new SequenceCheckedObject);
188 // DoStuff should assert in debug builds only when called on a
189 // different thread.
190 PostDoStuffToOtherThread(sequence_checked_object.get());
191 other_thread()->Stop();
194 #if ENABLE_SEQUENCE_CHECKER
195 TEST_F(SequenceCheckerTest, MethodNotAllowedOnDifferentThreadDeathTestInDebug) {
196 // The default style "fast" does not support multi-threaded tests.
197 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
198 ASSERT_DEATH({
199 MethodOnDifferentThreadDeathTest();
200 }, "");
202 #else
203 TEST_F(SequenceCheckerTest, MethodAllowedOnDifferentThreadDeathTestInRelease) {
204 MethodOnDifferentThreadDeathTest();
206 #endif // ENABLE_SEQUENCE_CHECKER
208 void SequenceCheckerTest::DetachThenCallFromDifferentThreadDeathTest() {
209 scoped_ptr<SequenceCheckedObject> sequence_checked_object(
210 new SequenceCheckedObject);
212 // DoStuff doesn't assert when called on a different thread
213 // after a call to DetachFromSequence.
214 sequence_checked_object->DetachFromSequence();
215 PostDoStuffToOtherThread(sequence_checked_object.get());
216 other_thread()->Stop();
218 // DoStuff should assert in debug builds only after moving to
219 // another thread.
220 sequence_checked_object->DoStuff();
223 #if ENABLE_SEQUENCE_CHECKER
224 TEST_F(SequenceCheckerTest, DetachFromSequenceDeathTestInDebug) {
225 // The default style "fast" does not support multi-threaded tests.
226 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
227 ASSERT_DEATH({
228 DetachThenCallFromDifferentThreadDeathTest();
229 }, "");
231 #else
232 TEST_F(SequenceCheckerTest, DetachFromThreadDeathTestInRelease) {
233 DetachThenCallFromDifferentThreadDeathTest();
235 #endif // ENABLE_SEQUENCE_CHECKER
237 void SequenceCheckerTest::DifferentSequenceTokensDeathTest() {
238 scoped_ptr<SequenceCheckedObject> sequence_checked_object(
239 new SequenceCheckedObject);
241 sequence_checked_object->DetachFromSequence();
242 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
243 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
244 PostDoStuffToWorkerPool(sequence_checked_object.get(), "B");
245 PostDoStuffToWorkerPool(sequence_checked_object.get(), "B");
246 pool()->FlushForTesting();
248 PostDeleteToOtherThread(sequence_checked_object.Pass());
249 other_thread()->Stop();
252 #if ENABLE_SEQUENCE_CHECKER
253 TEST_F(SequenceCheckerTest, DifferentSequenceTokensDeathTestInDebug) {
254 // The default style "fast" does not support multi-threaded tests.
255 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
256 ASSERT_DEATH({
257 DifferentSequenceTokensDeathTest();
258 }, "");
260 #else
261 TEST_F(SequenceCheckerTest, DifferentSequenceTokensDeathTestInRelease) {
262 DifferentSequenceTokensDeathTest();
264 #endif // ENABLE_SEQUENCE_CHECKER
266 void SequenceCheckerTest::WorkerPoolAndSimpleThreadDeathTest() {
267 scoped_ptr<SequenceCheckedObject> sequence_checked_object(
268 new SequenceCheckedObject);
270 sequence_checked_object->DetachFromSequence();
271 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
272 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
273 pool()->FlushForTesting();
275 PostDoStuffToOtherThread(sequence_checked_object.get());
276 other_thread()->Stop();
279 #if ENABLE_SEQUENCE_CHECKER
280 TEST_F(SequenceCheckerTest, WorkerPoolAndSimpleThreadDeathTestInDebug) {
281 // The default style "fast" does not support multi-threaded tests.
282 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
283 ASSERT_DEATH({
284 WorkerPoolAndSimpleThreadDeathTest();
285 }, "");
287 #else
288 TEST_F(SequenceCheckerTest, WorkerPoolAndSimpleThreadDeathTestInRelease) {
289 WorkerPoolAndSimpleThreadDeathTest();
291 #endif // ENABLE_SEQUENCE_CHECKER
293 void SequenceCheckerTest::TwoDifferentWorkerPoolsDeathTest() {
294 scoped_ptr<SequenceCheckedObject> sequence_checked_object(
295 new SequenceCheckedObject);
297 sequence_checked_object->DetachFromSequence();
298 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
299 PostDoStuffToWorkerPool(sequence_checked_object.get(), "A");
300 pool()->FlushForTesting();
302 SequencedWorkerPoolOwner second_pool_owner(kNumWorkerThreads, "test2");
303 second_pool_owner.pool()->PostNamedSequencedWorkerTask(
304 "A",
305 FROM_HERE,
306 base::Bind(&SequenceCheckedObject::DoStuff,
307 base::Unretained(sequence_checked_object.get())));
308 second_pool_owner.pool()->FlushForTesting();
309 second_pool_owner.pool()->Shutdown();
312 #if ENABLE_SEQUENCE_CHECKER
313 TEST_F(SequenceCheckerTest, TwoDifferentWorkerPoolsDeathTestInDebug) {
314 // The default style "fast" does not support multi-threaded tests.
315 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
316 ASSERT_DEATH({
317 TwoDifferentWorkerPoolsDeathTest();
318 }, "");
320 #else
321 TEST_F(SequenceCheckerTest, TwoDifferentWorkerPoolsDeathTestInRelease) {
322 TwoDifferentWorkerPoolsDeathTest();
324 #endif // ENABLE_SEQUENCE_CHECKER
326 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_SEQUENCE_CHECKER
328 } // namespace
330 } // namespace base
332 // Just in case we ever get lumped together with other compilation units.
333 #undef ENABLE_SEQUENCE_CHECKER