OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/sequence_checker.h" |
| 6 |
5 #include <stddef.h> | 7 #include <stddef.h> |
6 | 8 |
7 #include <memory> | 9 #include <memory> |
8 #include <string> | 10 #include <string> |
9 | 11 |
10 #include "base/bind.h" | 12 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
12 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
13 #include "base/macros.h" | 15 #include "base/macros.h" |
14 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
15 #include "base/sequence_checker_impl.h" | |
16 #include "base/sequence_token.h" | 17 #include "base/sequence_token.h" |
17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 19 #include "base/test/gtest_util.h" |
18 #include "base/test/sequenced_worker_pool_owner.h" | 20 #include "base/test/sequenced_worker_pool_owner.h" |
19 #include "base/threading/simple_thread.h" | 21 #include "base/threading/simple_thread.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
21 | 23 |
22 namespace base { | 24 namespace base { |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 constexpr size_t kNumWorkerThreads = 3; | 28 constexpr size_t kNumWorkerThreads = 3; |
27 | 29 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 FlushSequencedWorkerPoolForTesting(); | 251 FlushSequencedWorkerPoolForTesting(); |
250 | 252 |
251 SequencedWorkerPoolOwner second_pool_owner(kNumWorkerThreads, "test2"); | 253 SequencedWorkerPoolOwner second_pool_owner(kNumWorkerThreads, "test2"); |
252 second_pool_owner.pool()->PostNamedSequencedWorkerTask( | 254 second_pool_owner.pool()->PostNamedSequencedWorkerTask( |
253 "A", FROM_HERE, | 255 "A", FROM_HERE, |
254 base::BindOnce(&ExpectNotCalledOnValidSequence, | 256 base::BindOnce(&ExpectNotCalledOnValidSequence, |
255 base::Unretained(&sequence_checker))); | 257 base::Unretained(&sequence_checker))); |
256 second_pool_owner.pool()->FlushForTesting(); | 258 second_pool_owner.pool()->FlushForTesting(); |
257 } | 259 } |
258 | 260 |
| 261 namespace { |
| 262 |
| 263 // This fixture is a helper for unit testing the sequence checker macros as it |
| 264 // is not possible to inline ExpectDeathOnOtherSequence() and |
| 265 // ExpectNoDeathOnOtherSequenceAfterDetach() as lambdas since binding |
| 266 // |Unretained(&my_sequence_checker)| wouldn't compile on non-dcheck builds |
| 267 // where it won't be defined. |
| 268 class SequenceCheckerMacroTest : public SequenceCheckerTest { |
| 269 public: |
| 270 SequenceCheckerMacroTest() = default; |
| 271 |
| 272 void ExpectDeathOnOtherSequence() { |
| 273 #if DCHECK_IS_ON() |
| 274 EXPECT_DCHECK_DEATH( |
| 275 { DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_); }); |
| 276 #else |
| 277 // Happily no-ops on non-dcheck builds. |
| 278 DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_); |
| 279 #endif |
| 280 } |
| 281 |
| 282 void ExpectNoDeathOnOtherSequenceAfterDetach() { |
| 283 DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_); |
| 284 } |
| 285 |
| 286 protected: |
| 287 SEQUENCE_CHECKER(my_sequence_checker_); |
| 288 |
| 289 private: |
| 290 DISALLOW_COPY_AND_ASSIGN(SequenceCheckerMacroTest); |
| 291 }; |
| 292 |
| 293 } // namespace |
| 294 |
| 295 TEST_F(SequenceCheckerMacroTest, Macros) { |
| 296 PostToSequencedWorkerPool( |
| 297 Bind(&SequenceCheckerMacroTest::ExpectDeathOnOtherSequence, |
| 298 Unretained(this)), |
| 299 "A"); |
| 300 FlushSequencedWorkerPoolForTesting(); |
| 301 |
| 302 DETACH_FROM_SEQUENCE(my_sequence_checker_); |
| 303 |
| 304 PostToSequencedWorkerPool( |
| 305 Bind(&SequenceCheckerMacroTest::ExpectNoDeathOnOtherSequenceAfterDetach, |
| 306 Unretained(this)), |
| 307 "A"); |
| 308 FlushSequencedWorkerPoolForTesting(); |
| 309 } |
| 310 |
259 } // namespace base | 311 } // namespace base |
OLD | NEW |