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