[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 1 | // Copyright (c) 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. |
| 4 | |
| 5 | #include "base/deferred_sequenced_task_runner.h" |
| 6 | |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 9 | #include "base/callback_forward.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 10 | #include "base/location.h" |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 12 | #include "base/run_loop.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 13 | #include "base/single_thread_task_runner.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 14 | #include "base/test/task_environment.h" |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 15 | #include "base/threading/thread.h" |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 16 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 17 | #include "testing/gmock/include/gmock/gmock.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 20 | namespace base { |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 21 | namespace { |
| 22 | |
gab | 7d2fae4 | 2017-06-01 14:02:55 | [diff] [blame] | 23 | class DeferredSequencedTaskRunnerTest : public testing::Test { |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 24 | public: |
Yuki Shiino | ae6ae09 | 2020-04-28 06:20:51 | [diff] [blame] | 25 | class ExecuteTaskOnDestructor : public RefCounted<ExecuteTaskOnDestructor> { |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 26 | public: |
| 27 | ExecuteTaskOnDestructor( |
| 28 | DeferredSequencedTaskRunnerTest* executor, |
| 29 | int task_id) |
| 30 | : executor_(executor), |
| 31 | task_id_(task_id) { |
| 32 | } |
| 33 | private: |
Yuki Shiino | ae6ae09 | 2020-04-28 06:20:51 | [diff] [blame] | 34 | friend class RefCounted<ExecuteTaskOnDestructor>; |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 35 | virtual ~ExecuteTaskOnDestructor() { executor_->ExecuteTask(task_id_); } |
| 36 | DeferredSequencedTaskRunnerTest* executor_; |
| 37 | int task_id_; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | void ExecuteTask(int task_id) { |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 41 | AutoLock lock(lock_); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 42 | executed_task_ids_.push_back(task_id); |
| 43 | } |
| 44 | |
| 45 | void PostExecuteTask(int task_id) { |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 46 | runner_->PostTask(FROM_HERE, |
| 47 | BindOnce(&DeferredSequencedTaskRunnerTest::ExecuteTask, |
| 48 | Unretained(this), task_id)); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void StartRunner() { |
| 52 | runner_->Start(); |
| 53 | } |
| 54 | |
| 55 | void DoNothing(ExecuteTaskOnDestructor* object) { |
| 56 | } |
| 57 | |
| 58 | protected: |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 59 | DeferredSequencedTaskRunnerTest() |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 60 | : runner_( |
| 61 | new DeferredSequencedTaskRunner(ThreadTaskRunnerHandle::Get())) {} |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 62 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 63 | test::TaskEnvironment task_environment_; |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 64 | scoped_refptr<DeferredSequencedTaskRunner> runner_; |
| 65 | mutable Lock lock_; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 66 | std::vector<int> executed_task_ids_; |
| 67 | }; |
| 68 | |
| 69 | TEST_F(DeferredSequencedTaskRunnerTest, Stopped) { |
| 70 | PostExecuteTask(1); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 71 | RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 72 | EXPECT_THAT(executed_task_ids_, testing::ElementsAre()); |
| 73 | } |
| 74 | |
| 75 | TEST_F(DeferredSequencedTaskRunnerTest, Start) { |
| 76 | StartRunner(); |
| 77 | PostExecuteTask(1); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 78 | RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 79 | EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1)); |
| 80 | } |
| 81 | |
| 82 | TEST_F(DeferredSequencedTaskRunnerTest, StartWithMultipleElements) { |
| 83 | StartRunner(); |
| 84 | for (int i = 1; i < 5; ++i) |
| 85 | PostExecuteTask(i); |
| 86 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 87 | RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 88 | EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4)); |
| 89 | } |
| 90 | |
| 91 | TEST_F(DeferredSequencedTaskRunnerTest, DeferredStart) { |
| 92 | PostExecuteTask(1); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 93 | RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 94 | EXPECT_THAT(executed_task_ids_, testing::ElementsAre()); |
| 95 | |
| 96 | StartRunner(); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 97 | RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 98 | EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1)); |
| 99 | |
| 100 | PostExecuteTask(2); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 101 | RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 102 | EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2)); |
| 103 | } |
| 104 | |
| 105 | TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleElements) { |
| 106 | for (int i = 1; i < 5; ++i) |
| 107 | PostExecuteTask(i); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 108 | RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 109 | EXPECT_THAT(executed_task_ids_, testing::ElementsAre()); |
| 110 | |
| 111 | StartRunner(); |
| 112 | for (int i = 5; i < 9; ++i) |
| 113 | PostExecuteTask(i); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 114 | RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 115 | EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8)); |
| 116 | } |
| 117 | |
| 118 | TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) { |
| 119 | { |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 120 | Thread thread1("DeferredSequencedTaskRunnerTestThread1"); |
| 121 | Thread thread2("DeferredSequencedTaskRunnerTestThread2"); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 122 | thread1.Start(); |
| 123 | thread2.Start(); |
| 124 | for (int i = 0; i < 5; ++i) { |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 125 | thread1.task_runner()->PostTask( |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 126 | FROM_HERE, BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask, |
| 127 | Unretained(this), 2 * i)); |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 128 | thread2.task_runner()->PostTask( |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 129 | FROM_HERE, BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask, |
| 130 | Unretained(this), 2 * i + 1)); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 131 | if (i == 2) { |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 132 | thread1.task_runner()->PostTask( |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 133 | FROM_HERE, BindOnce(&DeferredSequencedTaskRunnerTest::StartRunner, |
| 134 | Unretained(this))); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 139 | RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 140 | EXPECT_THAT(executed_task_ids_, |
| 141 | testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))); |
| 142 | } |
| 143 | |
| 144 | TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) { |
| 145 | { |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 146 | Thread thread("DeferredSequencedTaskRunnerTestThread"); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 147 | thread.Start(); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 148 | runner_ = new DeferredSequencedTaskRunner(thread.task_runner()); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 149 | for (int i = 0; i < 5; ++i) { |
| 150 | { |
| 151 | // Use a block to ensure that no reference to |short_lived_object| |
| 152 | // is kept on the main thread after it is posted to |runner_|. |
| 153 | scoped_refptr<ExecuteTaskOnDestructor> short_lived_object = |
| 154 | new ExecuteTaskOnDestructor(this, 2 * i); |
| 155 | runner_->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 156 | FROM_HERE, |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 157 | BindOnce(&DeferredSequencedTaskRunnerTest::DoNothing, |
| 158 | Unretained(this), RetainedRef(short_lived_object))); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 159 | } |
| 160 | // |short_lived_object| with id |2 * i| should be destroyed before the |
| 161 | // task |2 * i + 1| is executed. |
| 162 | PostExecuteTask(2 * i + 1); |
| 163 | } |
| 164 | StartRunner(); |
| 165 | } |
| 166 | |
| 167 | // All |short_lived_object| with id |2 * i| are destroyed before the task |
| 168 | // |2 * i + 1| is executed. |
| 169 | EXPECT_THAT(executed_task_ids_, |
| 170 | testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); |
| 171 | } |
| 172 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 173 | void GetRunsTasksInCurrentSequence(bool* result, |
| 174 | scoped_refptr<SequencedTaskRunner> runner, |
| 175 | OnceClosure quit) { |
| 176 | *result = runner->RunsTasksInCurrentSequence(); |
| 177 | std::move(quit).Run(); |
| 178 | } |
| 179 | |
| 180 | TEST_F(DeferredSequencedTaskRunnerTest, RunsTasksInCurrentSequence) { |
| 181 | scoped_refptr<DeferredSequencedTaskRunner> runner = |
| 182 | MakeRefCounted<DeferredSequencedTaskRunner>(); |
| 183 | EXPECT_TRUE(runner->RunsTasksInCurrentSequence()); |
| 184 | |
| 185 | Thread thread1("DeferredSequencedTaskRunnerTestThread1"); |
| 186 | thread1.Start(); |
| 187 | bool runs_task_in_current_thread = true; |
| 188 | base::RunLoop run_loop; |
| 189 | thread1.task_runner()->PostTask( |
| 190 | FROM_HERE, |
| 191 | BindOnce(&GetRunsTasksInCurrentSequence, &runs_task_in_current_thread, |
| 192 | runner, run_loop.QuitClosure())); |
| 193 | run_loop.Run(); |
| 194 | EXPECT_FALSE(runs_task_in_current_thread); |
| 195 | } |
| 196 | |
| 197 | TEST_F(DeferredSequencedTaskRunnerTest, StartWithTaskRunner) { |
| 198 | scoped_refptr<DeferredSequencedTaskRunner> runner = |
| 199 | MakeRefCounted<DeferredSequencedTaskRunner>(); |
| 200 | bool run_called = false; |
| 201 | base::RunLoop run_loop; |
| 202 | runner->PostTask(FROM_HERE, |
| 203 | BindOnce( |
kylechar | 01598d7 | 2019-05-21 18:35:31 | [diff] [blame] | 204 | [](bool* run_called, base::OnceClosure quit_closure) { |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 205 | *run_called = true; |
| 206 | std::move(quit_closure).Run(); |
| 207 | }, |
| 208 | &run_called, run_loop.QuitClosure())); |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 209 | runner->StartWithTaskRunner(ThreadTaskRunnerHandle::Get()); |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 210 | run_loop.Run(); |
| 211 | EXPECT_TRUE(run_called); |
| 212 | } |
| 213 | |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 214 | } // namespace |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 215 | } // namespace base |