// Copyright 2014 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "base/task/cancelable_task_tracker.h" #include <cstddef> #include <tuple> #include "base/check_op.h" #include "base/functional/bind.h" #include "base/functional/callback_helpers.h" #include "base/location.h" #include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" #include "base/run_loop.h" #include "base/task/single_thread_task_runner.h" #include "base/test/bind.h" #include "base/test/gtest_util.h" #include "base/test/task_environment.h" #include "base/test/test_simple_task_runner.h" #include "base/threading/thread.h" #include "testing/gtest/include/gtest/gtest.h" namespace base { namespace { class CancelableTaskTrackerTest : public testing::Test { … }; } // namespace // With the task tracker, post a task, a task with a reply, and get a // new task id without canceling any of them. The tasks and the reply // should run and the "is canceled" callback should return false. TEST_F(CancelableTaskTrackerTest, NoCancel) { … } // Post a task with the task tracker but cancel it before running the // task runner. The task should not run. TEST_F(CancelableTaskTrackerTest, CancelPostedTask) { … } // Post a task with reply with the task tracker and cancel it before // running the task runner. Neither the task nor the reply should // run. TEST_F(CancelableTaskTrackerTest, CancelPostedTaskAndReply) { … } // Post a task with reply with the task tracker and cancel it after // running the task runner but before running the current message // loop. The task should run but the reply should not. TEST_F(CancelableTaskTrackerTest, CancelReply) { … } // Post a task with reply with the task tracker on a worker thread and // cancel it before running the current message loop. The task should // run but the reply should not. TEST_F(CancelableTaskTrackerTest, CancelReplyDifferentThread) { … } // Create a new task ID and check its status on a separate thread // before and after canceling. The is-canceled callback should be // thread-safe (i.e., nothing should blow up). TEST_F(CancelableTaskTrackerTest, NewTrackedTaskIdDifferentThread) { … } // With the task tracker, post a task, a task with a reply, get a new // task id, and then cancel all of them. None of the tasks nor the // reply should run and the "is canceled" callback should return // true. TEST_F(CancelableTaskTrackerTest, CancelAll) { … } // With the task tracker, post a task, a task with a reply, get a new // task id, and then cancel all of them. None of the tasks nor the // reply should run and the "is canceled" callback should return // true. TEST_F(CancelableTaskTrackerTest, DestructionCancelsAll) { … } // Post a task and cancel it. HasTrackedTasks() should return false as soon as // TryCancel() returns, otherwise we may have leaked per-task state. TEST_F(CancelableTaskTrackerTest, HasTrackedTasksCancelById) { … } // Post a task and then cancel all tasks. HasTrackedTasks() should return false // as soon as TryCancelAll() is called. TEST_F(CancelableTaskTrackerTest, HasTrackedTasksPostCancelAll) { … } // Post a task with a reply and cancel it. HasTrackedTasks() should return false // as soon as TryCancelAll() is called. TEST_F(CancelableTaskTrackerTest, HasTrackedTasksPostWithReplyCancelAll) { … } // Create a new tracked task ID. HasTrackedTasks() should return false as soon // as TryCancelAll() is called. TEST_F(CancelableTaskTrackerTest, HasTrackedTasksIsCancelledCancelAll) { … } // The death tests below make sure that calling task tracker member // functions from a thread different from its owner thread DCHECKs in // debug mode. class CancelableTaskTrackerDeathTest : public CancelableTaskTrackerTest { … }; // Runs |fn| with |task_tracker|, expecting it to crash in debug mode. void MaybeRunDeadlyTaskTrackerMemberFunction( CancelableTaskTracker* task_tracker, OnceCallback<void(CancelableTaskTracker*)> fn) { … } TEST_F(CancelableTaskTrackerDeathTest, PostFromDifferentThread) { … } TEST_F(CancelableTaskTrackerDeathTest, CancelOnDifferentThread) { … } TEST_F(CancelableTaskTrackerDeathTest, CancelAllOnDifferentThread) { … } } // namespace base