OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/task/cancelable_task_tracker.h" | 5 #include "base/task/cancelable_task_tracker.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/test/gtest_util.h" |
18 #include "base/test/test_simple_task_runner.h" | 19 #include "base/test/test_simple_task_runner.h" |
19 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 class CancelableTaskTrackerTest : public testing::Test { | 27 class CancelableTaskTrackerTest : public testing::Test { |
27 protected: | 28 protected: |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 CancelableTaskTrackerDeathTest() { | 342 CancelableTaskTrackerDeathTest() { |
342 // The default style "fast" does not support multi-threaded tests. | 343 // The default style "fast" does not support multi-threaded tests. |
343 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 344 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
344 } | 345 } |
345 }; | 346 }; |
346 | 347 |
347 // Runs |fn| with |task_tracker|, expecting it to crash in debug mode. | 348 // Runs |fn| with |task_tracker|, expecting it to crash in debug mode. |
348 void MaybeRunDeadlyTaskTrackerMemberFunction( | 349 void MaybeRunDeadlyTaskTrackerMemberFunction( |
349 CancelableTaskTracker* task_tracker, | 350 CancelableTaskTracker* task_tracker, |
350 const Callback<void(CancelableTaskTracker*)>& fn) { | 351 const Callback<void(CancelableTaskTracker*)>& fn) { |
351 // CancelableTask uses DCHECKs with its ThreadChecker (itself only | 352 EXPECT_DCHECK_DEATH(fn.Run(task_tracker)); |
352 // enabled in debug mode). | |
353 #if DCHECK_IS_ON() | |
354 EXPECT_DEATH_IF_SUPPORTED(fn.Run(task_tracker), ""); | |
355 #endif | |
356 } | 353 } |
357 | 354 |
358 void PostDoNothingTask(CancelableTaskTracker* task_tracker) { | 355 void PostDoNothingTask(CancelableTaskTracker* task_tracker) { |
359 ignore_result(task_tracker->PostTask( | 356 ignore_result(task_tracker->PostTask( |
360 scoped_refptr<TestSimpleTaskRunner>(new TestSimpleTaskRunner()).get(), | 357 scoped_refptr<TestSimpleTaskRunner>(new TestSimpleTaskRunner()).get(), |
361 FROM_HERE, | 358 FROM_HERE, |
362 Bind(&DoNothing))); | 359 Bind(&DoNothing))); |
363 } | 360 } |
364 | 361 |
365 TEST_F(CancelableTaskTrackerDeathTest, PostFromDifferentThread) { | 362 TEST_F(CancelableTaskTrackerDeathTest, PostFromDifferentThread) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 | 404 |
408 bad_thread.task_runner()->PostTask( | 405 bad_thread.task_runner()->PostTask( |
409 FROM_HERE, | 406 FROM_HERE, |
410 Bind(&MaybeRunDeadlyTaskTrackerMemberFunction, Unretained(&task_tracker_), | 407 Bind(&MaybeRunDeadlyTaskTrackerMemberFunction, Unretained(&task_tracker_), |
411 Bind(&CancelableTaskTracker::TryCancelAll))); | 408 Bind(&CancelableTaskTracker::TryCancelAll))); |
412 | 409 |
413 test_task_runner->RunUntilIdle(); | 410 test_task_runner->RunUntilIdle(); |
414 } | 411 } |
415 | 412 |
416 } // namespace base | 413 } // namespace base |
OLD | NEW |