toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 "media/midi/task_service.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "base/bind.h" |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 10 | #include "base/callback.h" |
danakj | 7031085 | 2020-11-11 16:01:35 +0000 | [diff] [blame] | 11 | #include "base/callback_helpers.h" |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/run_loop.h" |
| 14 | #include "base/synchronization/lock.h" |
| 15 | #include "base/test/test_simple_task_runner.h" |
| 16 | #include "base/threading/thread_task_runner_handle.h" |
| 17 | #include "base/time/time.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | namespace midi { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | enum { |
| 25 | kDefaultRunner = TaskService::kDefaultRunnerId, |
| 26 | kFirstRunner, |
| 27 | kSecondRunner |
| 28 | }; |
| 29 | |
| 30 | base::WaitableEvent* GetEvent() { |
| 31 | static base::WaitableEvent* event = |
| 32 | new base::WaitableEvent(base::WaitableEvent::ResetPolicy::MANUAL, |
| 33 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 34 | return event; |
| 35 | } |
| 36 | |
| 37 | void SignalEvent() { |
| 38 | GetEvent()->Signal(); |
| 39 | } |
| 40 | |
| 41 | void WaitEvent() { |
| 42 | GetEvent()->Wait(); |
| 43 | } |
| 44 | |
| 45 | void ResetEvent() { |
| 46 | GetEvent()->Reset(); |
| 47 | } |
| 48 | |
| 49 | class TaskServiceClient { |
| 50 | public: |
| 51 | TaskServiceClient(TaskService* task_service) |
| 52 | : task_service_(task_service), |
Gyuyoung Kim | 34e191a | 2018-01-10 09:48:42 +0000 | [diff] [blame] | 53 | wait_task_event_(std::make_unique<base::WaitableEvent>( |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 54 | base::WaitableEvent::ResetPolicy::MANUAL, |
| 55 | base::WaitableEvent::InitialState::NOT_SIGNALED)), |
| 56 | count_(0u) { |
| 57 | DCHECK(task_service); |
| 58 | } |
| 59 | |
Peter Boström | 5e5c4fa | 2021-10-15 21:43:24 +0000 | [diff] [blame] | 60 | TaskServiceClient(const TaskServiceClient&) = delete; |
| 61 | TaskServiceClient& operator=(const TaskServiceClient&) = delete; |
| 62 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 63 | bool Bind() { return task_service()->BindInstance(); } |
| 64 | |
| 65 | bool Unbind() { return task_service()->UnbindInstance(); } |
| 66 | |
| 67 | void PostBoundTask(TaskService::RunnerId runner_id) { |
| 68 | task_service()->PostBoundTask( |
| 69 | runner_id, base::BindOnce(&TaskServiceClient::IncrementCount, |
| 70 | base::Unretained(this))); |
| 71 | } |
| 72 | |
| 73 | void PostBoundSignalTask(TaskService::RunnerId runner_id) { |
| 74 | task_service()->PostBoundTask( |
| 75 | runner_id, base::BindOnce(&TaskServiceClient::SignalEvent, |
| 76 | base::Unretained(this))); |
| 77 | } |
| 78 | |
| 79 | void PostBoundWaitTask(TaskService::RunnerId runner_id) { |
| 80 | wait_task_event_->Reset(); |
| 81 | task_service()->PostBoundTask( |
| 82 | runner_id, |
| 83 | base::BindOnce(&TaskServiceClient::WaitEvent, base::Unretained(this))); |
| 84 | } |
| 85 | |
| 86 | void PostBoundDelayedSignalTask(TaskService::RunnerId runner_id) { |
| 87 | task_service()->PostBoundDelayedTask( |
| 88 | runner_id, |
| 89 | base::BindOnce(&TaskServiceClient::SignalEvent, base::Unretained(this)), |
Peter Kasting | 99947e5 | 2021-10-02 03:06:35 +0000 | [diff] [blame] | 90 | base::Milliseconds(100)); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void WaitTask() { wait_task_event_->Wait(); } |
| 94 | |
| 95 | size_t count() { |
| 96 | base::AutoLock lock(lock_); |
| 97 | return count_; |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | TaskService* task_service() { return task_service_; } |
| 102 | |
| 103 | void IncrementCount() { |
| 104 | base::AutoLock lock(lock_); |
| 105 | count_++; |
| 106 | } |
| 107 | |
| 108 | void SignalEvent() { |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 109 | IncrementCount(); |
Takashi Toyoshima | 4f4ef5e | 2017-07-20 12:19:20 +0000 | [diff] [blame] | 110 | midi::SignalEvent(); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void WaitEvent() { |
Takashi Toyoshima | 4f4ef5e | 2017-07-20 12:19:20 +0000 | [diff] [blame] | 114 | IncrementCount(); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 115 | wait_task_event_->Signal(); |
| 116 | midi::WaitEvent(); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | base::Lock lock_; |
| 120 | TaskService* task_service_; |
| 121 | std::unique_ptr<base::WaitableEvent> wait_task_event_; |
| 122 | size_t count_; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | class MidiTaskServiceTest : public ::testing::Test { |
| 126 | public: |
Chris Watkins | c6cbcf6 | 2017-12-01 03:08:01 +0000 | [diff] [blame] | 127 | MidiTaskServiceTest() = default; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 128 | |
Peter Boström | 5e5c4fa | 2021-10-15 21:43:24 +0000 | [diff] [blame] | 129 | MidiTaskServiceTest(const MidiTaskServiceTest&) = delete; |
| 130 | MidiTaskServiceTest& operator=(const MidiTaskServiceTest&) = delete; |
| 131 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 132 | protected: |
| 133 | TaskService* task_service() { return &task_service_; } |
| 134 | void RunUntilIdle() { task_runner_->RunUntilIdle(); } |
| 135 | |
| 136 | private: |
| 137 | void SetUp() override { |
| 138 | ResetEvent(); |
| 139 | task_runner_ = new base::TestSimpleTaskRunner(); |
| 140 | thread_task_runner_handle_ = |
Gyuyoung Kim | 34e191a | 2018-01-10 09:48:42 +0000 | [diff] [blame] | 141 | std::make_unique<base::ThreadTaskRunnerHandle>(task_runner_); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void TearDown() override { |
| 145 | thread_task_runner_handle_.reset(); |
kylechar | 8d19106 | 2019-11-14 23:31:59 +0000 | [diff] [blame] | 146 | task_runner_.reset(); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 150 | std::unique_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 151 | TaskService task_service_; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 154 | // Tests if posted tasks without calling BindInstance() are ignored. |
| 155 | TEST_F(MidiTaskServiceTest, RunUnauthorizedBoundTask) { |
| 156 | std::unique_ptr<TaskServiceClient> client = |
Gyuyoung Kim | 34e191a | 2018-01-10 09:48:42 +0000 | [diff] [blame] | 157 | std::make_unique<TaskServiceClient>(task_service()); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 158 | |
| 159 | client->PostBoundTask(kFirstRunner); |
| 160 | |
| 161 | // Destruct |client| immediately, then see if the posted task is just ignored. |
| 162 | // If it isn't, another thread will touch the destructed instance and will |
| 163 | // cause a crash due to a use-after-free. |
| 164 | client = nullptr; |
| 165 | } |
| 166 | |
| 167 | // Tests if invalid BindInstance() calls are correctly rejected, and it does not |
| 168 | // make the service insanity. |
| 169 | TEST_F(MidiTaskServiceTest, BindTwice) { |
| 170 | std::unique_ptr<TaskServiceClient> client = |
Gyuyoung Kim | 34e191a | 2018-01-10 09:48:42 +0000 | [diff] [blame] | 171 | std::make_unique<TaskServiceClient>(task_service()); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 172 | |
| 173 | EXPECT_TRUE(client->Bind()); |
| 174 | |
| 175 | // Should not be able to call BindInstance() twice before unbinding current |
| 176 | // bound instance. |
| 177 | EXPECT_FALSE(client->Bind()); |
| 178 | |
| 179 | // Should be able to unbind only the first instance. |
| 180 | EXPECT_TRUE(client->Unbind()); |
| 181 | EXPECT_FALSE(client->Unbind()); |
| 182 | } |
| 183 | |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 184 | // Tests if posted static tasks can be processed correctly. |
| 185 | TEST_F(MidiTaskServiceTest, RunStaticTask) { |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 186 | std::unique_ptr<TaskServiceClient> client = |
Gyuyoung Kim | 34e191a | 2018-01-10 09:48:42 +0000 | [diff] [blame] | 187 | std::make_unique<TaskServiceClient>(task_service()); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 188 | |
| 189 | EXPECT_TRUE(client->Bind()); |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 190 | // Should be able to post a static task while an instance is bound. |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 191 | task_service()->PostStaticTask(kFirstRunner, base::BindOnce(&SignalEvent)); |
| 192 | WaitEvent(); |
| 193 | EXPECT_TRUE(client->Unbind()); |
| 194 | |
| 195 | ResetEvent(); |
| 196 | |
| 197 | EXPECT_TRUE(client->Bind()); |
Takashi Toyoshima | d2bdc59 | 2017-09-13 10:02:54 +0000 | [diff] [blame] | 198 | task_service()->PostStaticTask(kFirstRunner, base::BindOnce(&SignalEvent)); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 199 | // Should be able to unbind the instance to process a static task. |
| 200 | EXPECT_TRUE(client->Unbind()); |
| 201 | WaitEvent(); |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 202 | |
| 203 | ResetEvent(); |
| 204 | |
| 205 | // Should be able to post a static task without a bound instance. |
| 206 | task_service()->PostStaticTask(kFirstRunner, base::BindOnce(&SignalEvent)); |
| 207 | WaitEvent(); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Tests functionalities to run bound tasks. |
| 211 | TEST_F(MidiTaskServiceTest, RunBoundTasks) { |
| 212 | std::unique_ptr<TaskServiceClient> client = |
Gyuyoung Kim | 34e191a | 2018-01-10 09:48:42 +0000 | [diff] [blame] | 213 | std::make_unique<TaskServiceClient>(task_service()); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 214 | |
| 215 | EXPECT_TRUE(client->Bind()); |
| 216 | |
| 217 | // Tests if a post task run. |
| 218 | EXPECT_EQ(0u, client->count()); |
| 219 | client->PostBoundSignalTask(kFirstRunner); |
| 220 | WaitEvent(); |
| 221 | EXPECT_EQ(1u, client->count()); |
| 222 | |
| 223 | // Tests if another posted task is handled correctly even if the instance is |
| 224 | // unbound immediately. The posted task should run safely if it starts before |
| 225 | // UnboundInstance() is call. Otherwise, it should be ignored. It completely |
| 226 | // depends on timing. |
| 227 | client->PostBoundTask(kFirstRunner); |
| 228 | EXPECT_TRUE(client->Unbind()); |
Gyuyoung Kim | 34e191a | 2018-01-10 09:48:42 +0000 | [diff] [blame] | 229 | client = std::make_unique<TaskServiceClient>(task_service()); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 230 | |
| 231 | // Tests if an immediate call of another BindInstance() works correctly. |
| 232 | EXPECT_TRUE(client->Bind()); |
| 233 | |
| 234 | // Runs two tasks in two runners. |
| 235 | ResetEvent(); |
| 236 | client->PostBoundSignalTask(kFirstRunner); |
| 237 | client->PostBoundTask(kSecondRunner); |
| 238 | |
| 239 | // Waits only the first runner completion to see if the second runner handles |
| 240 | // the task correctly even if the bound instance is destructed. |
| 241 | WaitEvent(); |
| 242 | EXPECT_TRUE(client->Unbind()); |
| 243 | client = nullptr; |
| 244 | } |
| 245 | |
| 246 | // Tests if a blocking task does not block other task runners. |
| 247 | TEST_F(MidiTaskServiceTest, RunBlockingTask) { |
| 248 | std::unique_ptr<TaskServiceClient> client = |
Gyuyoung Kim | 34e191a | 2018-01-10 09:48:42 +0000 | [diff] [blame] | 249 | std::make_unique<TaskServiceClient>(task_service()); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 250 | |
| 251 | EXPECT_TRUE(client->Bind()); |
| 252 | |
| 253 | // Posts a task that waits until the event is signaled. |
| 254 | client->PostBoundWaitTask(kFirstRunner); |
| 255 | // Confirms if the posted task starts. Now, the task should block in the task |
| 256 | // until the second task is invoked. |
| 257 | client->WaitTask(); |
| 258 | |
| 259 | // Posts another task to the second runner. The task should be able to run |
| 260 | // even though another posted task is blocking inside a critical section that |
| 261 | // protects running tasks from an instance unbinding. |
| 262 | client->PostBoundSignalTask(kSecondRunner); |
| 263 | |
| 264 | // Wait until the second task runs. |
| 265 | WaitEvent(); |
| 266 | |
| 267 | // UnbindInstance() should wait until any running task finishes so that the |
| 268 | // instance can be destructed safely. |
| 269 | EXPECT_TRUE(client->Unbind()); |
| 270 | EXPECT_EQ(2u, client->count()); |
| 271 | client = nullptr; |
| 272 | } |
| 273 | |
| 274 | // Tests if a bound delayed task runs correctly. |
| 275 | TEST_F(MidiTaskServiceTest, RunBoundDelayedTask) { |
| 276 | std::unique_ptr<TaskServiceClient> client = |
Gyuyoung Kim | 34e191a | 2018-01-10 09:48:42 +0000 | [diff] [blame] | 277 | std::make_unique<TaskServiceClient>(task_service()); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 278 | |
| 279 | EXPECT_TRUE(client->Bind()); |
| 280 | |
| 281 | // Posts a delayed task that signals after 100msec. |
| 282 | client->PostBoundDelayedSignalTask(kFirstRunner); |
| 283 | |
| 284 | // Wait until the delayed task runs. |
| 285 | WaitEvent(); |
| 286 | |
| 287 | EXPECT_TRUE(client->Unbind()); |
| 288 | EXPECT_EQ(1u, client->count()); |
| 289 | client = nullptr; |
| 290 | } |
| 291 | |
| 292 | // Tests if a bound task runs on the thread that bound the instance. |
| 293 | TEST_F(MidiTaskServiceTest, RunBoundTaskOnDefaultRunner) { |
| 294 | std::unique_ptr<TaskServiceClient> client = |
Gyuyoung Kim | 34e191a | 2018-01-10 09:48:42 +0000 | [diff] [blame] | 295 | std::make_unique<TaskServiceClient>(task_service()); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 296 | |
| 297 | EXPECT_TRUE(client->Bind()); |
| 298 | |
| 299 | // Posts a task that increments the count on the caller thread. |
| 300 | client->PostBoundTask(kDefaultRunner); |
| 301 | |
| 302 | // The posted task should not run until the current message loop is processed. |
| 303 | EXPECT_EQ(0u, client->count()); |
| 304 | RunUntilIdle(); |
| 305 | EXPECT_EQ(1u, client->count()); |
| 306 | |
| 307 | EXPECT_TRUE(client->Unbind()); |
| 308 | } |
| 309 | |
| 310 | } // namespace |
| 311 | |
| 312 | } // namespace midi |