| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/cancelable_task_tracker.h" | 5 #include "chrome/common/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" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 return base::Bind(&RunChecker::Run, base::Owned(new RunChecker(location))); | 80 return base::Bind(&RunChecker::Run, base::Owned(new RunChecker(location))); |
| 81 } | 81 } |
| 82 | 82 |
| 83 // With the task tracker, post a task, a task with a reply, and get a | 83 // With the task tracker, post a task, a task with a reply, and get a |
| 84 // new task id without canceling any of them. The tasks and the reply | 84 // new task id without canceling any of them. The tasks and the reply |
| 85 // should run and the "is canceled" callback should return false. | 85 // should run and the "is canceled" callback should return false. |
| 86 TEST_F(CancelableTaskTrackerTest, NoCancel) { | 86 TEST_F(CancelableTaskTrackerTest, NoCancel) { |
| 87 base::Thread worker_thread("worker thread"); | 87 base::Thread worker_thread("worker thread"); |
| 88 ASSERT_TRUE(worker_thread.Start()); | 88 ASSERT_TRUE(worker_thread.Start()); |
| 89 | 89 |
| 90 ignore_result( | 90 ignore_result(task_tracker_.PostTask(worker_thread.message_loop_proxy().get(), |
| 91 task_tracker_.PostTask( | 91 FROM_HERE, |
| 92 worker_thread.message_loop_proxy(), | 92 MakeExpectedRunClosure(FROM_HERE))); |
| 93 FROM_HERE, | |
| 94 MakeExpectedRunClosure(FROM_HERE))); | |
| 95 | 93 |
| 96 ignore_result( | 94 ignore_result( |
| 97 task_tracker_.PostTaskAndReply( | 95 task_tracker_.PostTaskAndReply(worker_thread.message_loop_proxy().get(), |
| 98 worker_thread.message_loop_proxy(), | 96 FROM_HERE, |
| 99 FROM_HERE, | 97 MakeExpectedRunClosure(FROM_HERE), |
| 100 MakeExpectedRunClosure(FROM_HERE), | 98 MakeExpectedRunClosure(FROM_HERE))); |
| 101 MakeExpectedRunClosure(FROM_HERE))); | |
| 102 | 99 |
| 103 CancelableTaskTracker::IsCanceledCallback is_canceled; | 100 CancelableTaskTracker::IsCanceledCallback is_canceled; |
| 104 ignore_result(task_tracker_.NewTrackedTaskId(&is_canceled)); | 101 ignore_result(task_tracker_.NewTrackedTaskId(&is_canceled)); |
| 105 | 102 |
| 106 worker_thread.Stop(); | 103 worker_thread.Stop(); |
| 107 | 104 |
| 108 RunCurrentLoopUntilIdle(); | 105 RunCurrentLoopUntilIdle(); |
| 109 | 106 |
| 110 EXPECT_FALSE(is_canceled.Run()); | 107 EXPECT_FALSE(is_canceled.Run()); |
| 111 } | 108 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 } | 168 } |
| 172 | 169 |
| 173 // Post a task with reply with the task tracker on a worker thread and | 170 // Post a task with reply with the task tracker on a worker thread and |
| 174 // cancel it before running the current message loop. The task should | 171 // cancel it before running the current message loop. The task should |
| 175 // run but the reply should not. | 172 // run but the reply should not. |
| 176 TEST_F(CancelableTaskTrackerTest, CancelReplyDifferentThread) { | 173 TEST_F(CancelableTaskTrackerTest, CancelReplyDifferentThread) { |
| 177 base::Thread worker_thread("worker thread"); | 174 base::Thread worker_thread("worker thread"); |
| 178 ASSERT_TRUE(worker_thread.Start()); | 175 ASSERT_TRUE(worker_thread.Start()); |
| 179 | 176 |
| 180 CancelableTaskTracker::TaskId task_id = | 177 CancelableTaskTracker::TaskId task_id = |
| 181 task_tracker_.PostTaskAndReply( | 178 task_tracker_.PostTaskAndReply(worker_thread.message_loop_proxy().get(), |
| 182 worker_thread.message_loop_proxy(), | 179 FROM_HERE, |
| 183 FROM_HERE, | 180 base::Bind(&base::DoNothing), |
| 184 base::Bind(&base::DoNothing), | 181 MakeExpectedNotRunClosure(FROM_HERE)); |
| 185 MakeExpectedNotRunClosure(FROM_HERE)); | |
| 186 EXPECT_NE(CancelableTaskTracker::kBadTaskId, task_id); | 182 EXPECT_NE(CancelableTaskTracker::kBadTaskId, task_id); |
| 187 | 183 |
| 188 task_tracker_.TryCancel(task_id); | 184 task_tracker_.TryCancel(task_id); |
| 189 | 185 |
| 190 worker_thread.Stop(); | 186 worker_thread.Stop(); |
| 191 } | 187 } |
| 192 | 188 |
| 193 void ExpectIsCanceled( | 189 void ExpectIsCanceled( |
| 194 const CancelableTaskTracker::IsCanceledCallback& is_canceled, | 190 const CancelableTaskTracker::IsCanceledCallback& is_canceled, |
| 195 bool expected_is_canceled) { | 191 bool expected_is_canceled) { |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 const base::Callback<void(CancelableTaskTracker*)>& fn) { | 374 const base::Callback<void(CancelableTaskTracker*)>& fn) { |
| 379 // CancelableTask uses DCHECKs with its ThreadChecker (itself only | 375 // CancelableTask uses DCHECKs with its ThreadChecker (itself only |
| 380 // enabled in debug mode). | 376 // enabled in debug mode). |
| 381 #if ENABLE_THREAD_CHECKER | 377 #if ENABLE_THREAD_CHECKER |
| 382 EXPECT_DEATH_IF_SUPPORTED(fn.Run(task_tracker), ""); | 378 EXPECT_DEATH_IF_SUPPORTED(fn.Run(task_tracker), ""); |
| 383 #endif | 379 #endif |
| 384 } | 380 } |
| 385 | 381 |
| 386 void PostDoNothingTask(CancelableTaskTracker* task_tracker) { | 382 void PostDoNothingTask(CancelableTaskTracker* task_tracker) { |
| 387 ignore_result( | 383 ignore_result( |
| 388 task_tracker->PostTask( | 384 task_tracker->PostTask(scoped_refptr<base::TestSimpleTaskRunner>( |
| 389 scoped_refptr<base::TestSimpleTaskRunner>( | 385 new base::TestSimpleTaskRunner()) |
| 390 new base::TestSimpleTaskRunner()), | 386 .get(), |
| 391 FROM_HERE, base::Bind(&base::DoNothing))); | 387 FROM_HERE, |
| 388 base::Bind(&base::DoNothing))); |
| 392 } | 389 } |
| 393 | 390 |
| 394 TEST_F(CancelableTaskTrackerDeathTest, PostFromDifferentThread) { | 391 TEST_F(CancelableTaskTrackerDeathTest, PostFromDifferentThread) { |
| 395 base::Thread bad_thread("bad thread"); | 392 base::Thread bad_thread("bad thread"); |
| 396 ASSERT_TRUE(bad_thread.Start()); | 393 ASSERT_TRUE(bad_thread.Start()); |
| 397 | 394 |
| 398 bad_thread.message_loop_proxy()->PostTask( | 395 bad_thread.message_loop_proxy()->PostTask( |
| 399 FROM_HERE, | 396 FROM_HERE, |
| 400 base::Bind(&MaybeRunDeadlyTaskTrackerMemberFunction, | 397 base::Bind(&MaybeRunDeadlyTaskTrackerMemberFunction, |
| 401 base::Unretained(&task_tracker_), | 398 base::Unretained(&task_tracker_), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 bad_thread.message_loop_proxy()->PostTask( | 444 bad_thread.message_loop_proxy()->PostTask( |
| 448 FROM_HERE, | 445 FROM_HERE, |
| 449 base::Bind(&MaybeRunDeadlyTaskTrackerMemberFunction, | 446 base::Bind(&MaybeRunDeadlyTaskTrackerMemberFunction, |
| 450 base::Unretained(&task_tracker_), | 447 base::Unretained(&task_tracker_), |
| 451 base::Bind(&CancelableTaskTracker::TryCancelAll))); | 448 base::Bind(&CancelableTaskTracker::TryCancelAll))); |
| 452 | 449 |
| 453 test_task_runner->RunUntilIdle(); | 450 test_task_runner->RunUntilIdle(); |
| 454 } | 451 } |
| 455 | 452 |
| 456 } // namespace | 453 } // namespace |
| OLD | NEW |