OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/task_scheduler/task_scheduler_impl.h" | 5 #include "base/task_scheduler/task_scheduler_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
20 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 20 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
21 #include "base/task_scheduler/task_traits.h" | 21 #include "base/task_scheduler/task_traits.h" |
22 #include "base/task_scheduler/test_task_factory.h" | 22 #include "base/task_scheduler/test_task_factory.h" |
| 23 #include "base/task_scheduler/test_utils.h" |
23 #include "base/test/test_timeouts.h" | 24 #include "base/test/test_timeouts.h" |
24 #include "base/threading/platform_thread.h" | 25 #include "base/threading/platform_thread.h" |
25 #include "base/threading/simple_thread.h" | 26 #include "base/threading/simple_thread.h" |
26 #include "base/threading/thread.h" | 27 #include "base/threading/thread.h" |
27 #include "base/threading/thread_restrictions.h" | 28 #include "base/threading/thread_restrictions.h" |
28 #include "base/time/time.h" | 29 #include "base/time/time.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
30 | 31 |
31 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
32 #include <objbase.h> | 33 #include <objbase.h> |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 const test::ExecutionMode execution_modes[] = { | 160 const test::ExecutionMode execution_modes[] = { |
160 test::ExecutionMode::PARALLEL, test::ExecutionMode::SEQUENCED, | 161 test::ExecutionMode::PARALLEL, test::ExecutionMode::SEQUENCED, |
161 test::ExecutionMode::SINGLE_THREADED}; | 162 test::ExecutionMode::SINGLE_THREADED}; |
162 | 163 |
163 for (test::ExecutionMode execution_mode : execution_modes) { | 164 for (test::ExecutionMode execution_mode : execution_modes) { |
164 for (size_t priority_index = static_cast<size_t>(TaskPriority::LOWEST); | 165 for (size_t priority_index = static_cast<size_t>(TaskPriority::LOWEST); |
165 priority_index <= static_cast<size_t>(TaskPriority::HIGHEST); | 166 priority_index <= static_cast<size_t>(TaskPriority::HIGHEST); |
166 ++priority_index) { | 167 ++priority_index) { |
167 const TaskPriority priority = static_cast<TaskPriority>(priority_index); | 168 const TaskPriority priority = static_cast<TaskPriority>(priority_index); |
168 params.push_back(TraitsExecutionModePair( | 169 params.push_back(TraitsExecutionModePair( |
169 TaskTraits().WithPriority(priority), execution_mode)); | 170 test::CreateTaskTraits().WithPriority(priority), execution_mode)); |
170 params.push_back(TraitsExecutionModePair( | 171 params.push_back(TraitsExecutionModePair( |
171 TaskTraits().WithPriority(priority).MayBlock(), execution_mode)); | 172 test::CreateTaskTraits().WithPriority(priority).MayBlock(), |
| 173 execution_mode)); |
172 } | 174 } |
173 } | 175 } |
174 | 176 |
175 return params; | 177 return params; |
176 } | 178 } |
177 | 179 |
178 class TaskSchedulerImplTest | 180 class TaskSchedulerImplTest |
179 : public testing::TestWithParam<TraitsExecutionModePair> { | 181 : public testing::TestWithParam<TraitsExecutionModePair> { |
180 protected: | 182 protected: |
181 TaskSchedulerImplTest() = default; | 183 TaskSchedulerImplTest() = default; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 threads_posting_tasks.back()->Start(); | 280 threads_posting_tasks.back()->Start(); |
279 } | 281 } |
280 | 282 |
281 for (const auto& thread : threads_posting_tasks) { | 283 for (const auto& thread : threads_posting_tasks) { |
282 thread->WaitForAllTasksToRun(); | 284 thread->WaitForAllTasksToRun(); |
283 thread->Join(); | 285 thread->Join(); |
284 } | 286 } |
285 } | 287 } |
286 | 288 |
287 TEST_F(TaskSchedulerImplTest, GetMaxConcurrentTasksWithTraitsDeprecated) { | 289 TEST_F(TaskSchedulerImplTest, GetMaxConcurrentTasksWithTraitsDeprecated) { |
288 EXPECT_EQ(1, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( | |
289 TaskTraits().WithPriority(TaskPriority::BACKGROUND))); | |
290 EXPECT_EQ( | 290 EXPECT_EQ( |
291 3, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( | 291 1, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( |
292 TaskTraits().WithPriority(TaskPriority::BACKGROUND).MayBlock())); | 292 test::CreateTaskTraits().WithPriority(TaskPriority::BACKGROUND))); |
293 EXPECT_EQ(4, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( | 293 EXPECT_EQ(3, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( |
294 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE))); | 294 test::CreateTaskTraits() |
| 295 .WithPriority(TaskPriority::BACKGROUND) |
| 296 .MayBlock())); |
295 EXPECT_EQ( | 297 EXPECT_EQ( |
296 12, | 298 4, |
297 scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( | 299 scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( |
298 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE).MayBlock())); | 300 test::CreateTaskTraits().WithPriority(TaskPriority::USER_VISIBLE))); |
299 EXPECT_EQ(4, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( | 301 EXPECT_EQ(12, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( |
300 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING))); | 302 test::CreateTaskTraits() |
| 303 .WithPriority(TaskPriority::USER_VISIBLE) |
| 304 .MayBlock())); |
301 EXPECT_EQ( | 305 EXPECT_EQ( |
302 12, | 306 4, |
303 scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( | 307 scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( |
304 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING).MayBlock())); | 308 test::CreateTaskTraits().WithPriority(TaskPriority::USER_BLOCKING))); |
| 309 EXPECT_EQ(12, scheduler_->GetMaxConcurrentTasksWithTraitsDeprecated( |
| 310 test::CreateTaskTraits() |
| 311 .WithPriority(TaskPriority::USER_BLOCKING) |
| 312 .MayBlock())); |
305 } | 313 } |
306 | 314 |
307 // Verify that the RunsTasksOnCurrentThread() method of a SequencedTaskRunner | 315 // Verify that the RunsTasksOnCurrentThread() method of a SequencedTaskRunner |
308 // returns false when called from a task that isn't part of the sequence. | 316 // returns false when called from a task that isn't part of the sequence. |
309 TEST_F(TaskSchedulerImplTest, SequencedRunsTasksOnCurrentThread) { | 317 TEST_F(TaskSchedulerImplTest, SequencedRunsTasksOnCurrentThread) { |
310 auto single_thread_task_runner = | 318 auto single_thread_task_runner = |
311 scheduler_->CreateSingleThreadTaskRunnerWithTraits(TaskTraits()); | 319 scheduler_->CreateSingleThreadTaskRunnerWithTraits( |
| 320 test::CreateTaskTraits()); |
312 auto sequenced_task_runner = | 321 auto sequenced_task_runner = |
313 scheduler_->CreateSequencedTaskRunnerWithTraits(TaskTraits()); | 322 scheduler_->CreateSequencedTaskRunnerWithTraits(test::CreateTaskTraits()); |
314 | 323 |
315 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 324 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
316 WaitableEvent::InitialState::NOT_SIGNALED); | 325 WaitableEvent::InitialState::NOT_SIGNALED); |
317 single_thread_task_runner->PostTask( | 326 single_thread_task_runner->PostTask( |
318 FROM_HERE, | 327 FROM_HERE, |
319 BindOnce( | 328 BindOnce( |
320 [](scoped_refptr<TaskRunner> sequenced_task_runner, | 329 [](scoped_refptr<TaskRunner> sequenced_task_runner, |
321 WaitableEvent* task_ran) { | 330 WaitableEvent* task_ran) { |
322 EXPECT_FALSE(sequenced_task_runner->RunsTasksOnCurrentThread()); | 331 EXPECT_FALSE(sequenced_task_runner->RunsTasksOnCurrentThread()); |
323 task_ran->Signal(); | 332 task_ran->Signal(); |
324 }, | 333 }, |
325 sequenced_task_runner, Unretained(&task_ran))); | 334 sequenced_task_runner, Unretained(&task_ran))); |
326 task_ran.Wait(); | 335 task_ran.Wait(); |
327 } | 336 } |
328 | 337 |
329 // Verify that the RunsTasksOnCurrentThread() method of a SingleThreadTaskRunner | 338 // Verify that the RunsTasksOnCurrentThread() method of a SingleThreadTaskRunner |
330 // returns false when called from a task that isn't part of the sequence. | 339 // returns false when called from a task that isn't part of the sequence. |
331 TEST_F(TaskSchedulerImplTest, SingleThreadRunsTasksOnCurrentThread) { | 340 TEST_F(TaskSchedulerImplTest, SingleThreadRunsTasksOnCurrentThread) { |
332 auto sequenced_task_runner = | 341 auto sequenced_task_runner = |
333 scheduler_->CreateSequencedTaskRunnerWithTraits(TaskTraits()); | 342 scheduler_->CreateSequencedTaskRunnerWithTraits(test::CreateTaskTraits()); |
334 auto single_thread_task_runner = | 343 auto single_thread_task_runner = |
335 scheduler_->CreateSingleThreadTaskRunnerWithTraits(TaskTraits()); | 344 scheduler_->CreateSingleThreadTaskRunnerWithTraits( |
| 345 test::CreateTaskTraits()); |
336 | 346 |
337 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 347 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
338 WaitableEvent::InitialState::NOT_SIGNALED); | 348 WaitableEvent::InitialState::NOT_SIGNALED); |
339 sequenced_task_runner->PostTask( | 349 sequenced_task_runner->PostTask( |
340 FROM_HERE, | 350 FROM_HERE, |
341 BindOnce( | 351 BindOnce( |
342 [](scoped_refptr<TaskRunner> single_thread_task_runner, | 352 [](scoped_refptr<TaskRunner> single_thread_task_runner, |
343 WaitableEvent* task_ran) { | 353 WaitableEvent* task_ran) { |
344 EXPECT_FALSE(single_thread_task_runner->RunsTasksOnCurrentThread()); | 354 EXPECT_FALSE(single_thread_task_runner->RunsTasksOnCurrentThread()); |
345 task_ran->Signal(); | 355 task_ran->Signal(); |
346 }, | 356 }, |
347 single_thread_task_runner, Unretained(&task_ran))); | 357 single_thread_task_runner, Unretained(&task_ran))); |
348 task_ran.Wait(); | 358 task_ran.Wait(); |
349 } | 359 } |
350 | 360 |
351 #if defined(OS_WIN) | 361 #if defined(OS_WIN) |
352 TEST_F(TaskSchedulerImplTest, COMSTATaskRunnersRunWithCOMSTA) { | 362 TEST_F(TaskSchedulerImplTest, COMSTATaskRunnersRunWithCOMSTA) { |
353 auto com_sta_task_runner = | 363 auto com_sta_task_runner = |
354 scheduler_->CreateCOMSTATaskRunnerWithTraits(TaskTraits()); | 364 scheduler_->CreateCOMSTATaskRunnerWithTraits(test::CreateTaskTraits()); |
355 | 365 |
356 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 366 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
357 WaitableEvent::InitialState::NOT_SIGNALED); | 367 WaitableEvent::InitialState::NOT_SIGNALED); |
358 com_sta_task_runner->PostTask( | 368 com_sta_task_runner->PostTask( |
359 FROM_HERE, | 369 FROM_HERE, |
360 Bind( | 370 Bind( |
361 [](scoped_refptr<TaskRunner> single_thread_task_runner, | 371 [](scoped_refptr<TaskRunner> single_thread_task_runner, |
362 WaitableEvent* task_ran) { | 372 WaitableEvent* task_ran) { |
363 HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); | 373 HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); |
364 if (SUCCEEDED(hr)) { | 374 if (SUCCEEDED(hr)) { |
365 ADD_FAILURE() << "COM STA was not initialized on this thread"; | 375 ADD_FAILURE() << "COM STA was not initialized on this thread"; |
366 CoUninitialize(); | 376 CoUninitialize(); |
367 } | 377 } |
368 task_ran->Signal(); | 378 task_ran->Signal(); |
369 }, | 379 }, |
370 com_sta_task_runner, Unretained(&task_ran))); | 380 com_sta_task_runner, Unretained(&task_ran))); |
371 task_ran.Wait(); | 381 task_ran.Wait(); |
372 } | 382 } |
373 #endif // defined(OS_WIN) | 383 #endif // defined(OS_WIN) |
374 | 384 |
375 } // namespace internal | 385 } // namespace internal |
376 } // namespace base | 386 } // namespace base |
OLD | NEW |