[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" |
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" |
gab | f64a25e | 2017-05-12 19:42:56 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.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" |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 14 | #include "base/threading/thread.h" |
| 15 | #include "testing/gmock/include/gmock/gmock.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
gab | 7d2fae4 | 2017-06-01 14:02:55 | [diff] [blame] | 20 | class DeferredSequencedTaskRunnerTest : public testing::Test { |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 21 | public: |
| 22 | class ExecuteTaskOnDestructor : |
| 23 | public base::RefCounted<ExecuteTaskOnDestructor> { |
| 24 | public: |
| 25 | ExecuteTaskOnDestructor( |
| 26 | DeferredSequencedTaskRunnerTest* executor, |
| 27 | int task_id) |
| 28 | : executor_(executor), |
| 29 | task_id_(task_id) { |
| 30 | } |
| 31 | private: |
| 32 | friend class base::RefCounted<ExecuteTaskOnDestructor>; |
| 33 | virtual ~ExecuteTaskOnDestructor() { |
| 34 | executor_->ExecuteTask(task_id_); |
| 35 | } |
| 36 | DeferredSequencedTaskRunnerTest* executor_; |
| 37 | int task_id_; |
| 38 | }; |
| 39 | |
| 40 | void ExecuteTask(int task_id) { |
| 41 | base::AutoLock lock(lock_); |
| 42 | executed_task_ids_.push_back(task_id); |
| 43 | } |
| 44 | |
| 45 | void PostExecuteTask(int task_id) { |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 46 | runner_->PostTask( |
| 47 | FROM_HERE, base::BindOnce(&DeferredSequencedTaskRunnerTest::ExecuteTask, |
| 48 | base::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() |
| 60 | : loop_(), |
| 61 | runner_(new base::DeferredSequencedTaskRunner(loop_.task_runner())) {} |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 62 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 63 | base::MessageLoop loop_; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 64 | scoped_refptr<base::DeferredSequencedTaskRunner> runner_; |
| 65 | mutable base::Lock lock_; |
| 66 | std::vector<int> executed_task_ids_; |
| 67 | }; |
| 68 | |
| 69 | TEST_F(DeferredSequencedTaskRunnerTest, Stopped) { |
| 70 | PostExecuteTask(1); |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 71 | base::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); |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 78 | base::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 | |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 87 | base::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); |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 93 | base::RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 94 | EXPECT_THAT(executed_task_ids_, testing::ElementsAre()); |
| 95 | |
| 96 | StartRunner(); |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 97 | base::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); |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 101 | base::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); |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 108 | base::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); |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 114 | base::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 | { |
| 120 | base::Thread thread1("DeferredSequencedTaskRunnerTestThread1"); |
| 121 | base::Thread thread2("DeferredSequencedTaskRunnerTestThread2"); |
| 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( |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 126 | FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 127 | base::BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask, |
| 128 | base::Unretained(this), 2 * i)); |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 129 | thread2.task_runner()->PostTask( |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 130 | FROM_HERE, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 131 | base::BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask, |
| 132 | base::Unretained(this), 2 * i + 1)); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 133 | if (i == 2) { |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 134 | thread1.task_runner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 135 | FROM_HERE, |
| 136 | base::BindOnce(&DeferredSequencedTaskRunnerTest::StartRunner, |
| 137 | base::Unretained(this))); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 142 | base::RunLoop().RunUntilIdle(); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 143 | EXPECT_THAT(executed_task_ids_, |
| 144 | testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))); |
| 145 | } |
| 146 | |
| 147 | TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) { |
| 148 | { |
| 149 | base::Thread thread("DeferredSequencedTaskRunnerTestThread"); |
| 150 | thread.Start(); |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 151 | runner_ = new base::DeferredSequencedTaskRunner(thread.task_runner()); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 152 | for (int i = 0; i < 5; ++i) { |
| 153 | { |
| 154 | // Use a block to ensure that no reference to |short_lived_object| |
| 155 | // is kept on the main thread after it is posted to |runner_|. |
| 156 | scoped_refptr<ExecuteTaskOnDestructor> short_lived_object = |
| 157 | new ExecuteTaskOnDestructor(this, 2 * i); |
| 158 | runner_->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 159 | FROM_HERE, |
| 160 | base::BindOnce(&DeferredSequencedTaskRunnerTest::DoNothing, |
| 161 | base::Unretained(this), |
| 162 | base::RetainedRef(short_lived_object))); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 163 | } |
| 164 | // |short_lived_object| with id |2 * i| should be destroyed before the |
| 165 | // task |2 * i + 1| is executed. |
| 166 | PostExecuteTask(2 * i + 1); |
| 167 | } |
| 168 | StartRunner(); |
| 169 | } |
| 170 | |
| 171 | // All |short_lived_object| with id |2 * i| are destroyed before the task |
| 172 | // |2 * i + 1| is executed. |
| 173 | EXPECT_THAT(executed_task_ids_, |
| 174 | testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); |
| 175 | } |
| 176 | |
| 177 | } // namespace |