Push API: Fix unsubscribing from GCM on Android
[chromium-blink-merge.git] / base / sequence_checker_unittest.cc
blob9aae82c684aff1dabcb718e14d274ebcc6609b77
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/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
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()->message_loop()->PostTask(
88 FROM_HERE,
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(
96 FROM_HERE,
97 sequence_checked_object.release());
100 // Destroys the SequencedWorkerPool instance, blocking until it is fully shut
101 // down, and creates a new instance.
102 void ResetPool() {
103 pool_owner_.reset(new SequencedWorkerPoolOwner(kNumWorkerThreads, "test"));
106 void MethodOnDifferentThreadDeathTest();
107 void DetachThenCallFromDifferentThreadDeathTest();
108 void DifferentSequenceTokensDeathTest();
109 void WorkerPoolAndSimpleThreadDeathTest();
110 void TwoDifferentWorkerPoolsDeathTest();
112 private:
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
190 // different thread.
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";
199 ASSERT_DEATH({
200 MethodOnDifferentThreadDeathTest();
201 }, "");
203 #else
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
220 // another thread.
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";
228 ASSERT_DEATH({
229 DetachThenCallFromDifferentThreadDeathTest();
230 }, "");
232 #else
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";
257 ASSERT_DEATH({
258 DifferentSequenceTokensDeathTest();
259 }, "");
261 #else
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";
284 ASSERT_DEATH({
285 WorkerPoolAndSimpleThreadDeathTest();
286 }, "");
288 #else
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(
305 "A",
306 FROM_HERE,
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";
317 ASSERT_DEATH({
318 TwoDifferentWorkerPoolsDeathTest();
319 }, "");
321 #else
322 TEST_F(SequenceCheckerTest, TwoDifferentWorkerPoolsDeathTestInRelease) {
323 TwoDifferentWorkerPoolsDeathTest();
325 #endif // ENABLE_SEQUENCE_CHECKER
327 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_SEQUENCE_CHECKER
329 } // namespace
331 } // namespace base
333 // Just in case we ever get lumped together with other compilation units.
334 #undef ENABLE_SEQUENCE_CHECKER