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/test_task_factory.h" | 5 #include "base/task_scheduler/test_task_factory.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/sequenced_task_runner_handle.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 namespace base { | 17 namespace base { |
16 namespace internal { | 18 namespace internal { |
17 namespace test { | 19 namespace test { |
18 | 20 |
19 TestTaskFactory::TestTaskFactory(scoped_refptr<TaskRunner> task_runner, | 21 TestTaskFactory::TestTaskFactory(scoped_refptr<TaskRunner> task_runner, |
20 ExecutionMode execution_mode) | 22 ExecutionMode execution_mode) |
21 : cv_(&lock_), | 23 : cv_(&lock_), |
22 task_runner_(std::move(task_runner)), | 24 task_runner_(std::move(task_runner)), |
(...skipping 23 matching lines...) Expand all Loading... |
46 } | 48 } |
47 | 49 |
48 void TestTaskFactory::RunTaskCallback(size_t task_index, | 50 void TestTaskFactory::RunTaskCallback(size_t task_index, |
49 PostNestedTask post_nested_task, | 51 PostNestedTask post_nested_task, |
50 const Closure& after_task_closure) { | 52 const Closure& after_task_closure) { |
51 if (post_nested_task == PostNestedTask::YES) | 53 if (post_nested_task == PostNestedTask::YES) |
52 PostTask(PostNestedTask::NO, Closure()); | 54 PostTask(PostNestedTask::NO, Closure()); |
53 | 55 |
54 EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread()); | 56 EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread()); |
55 | 57 |
| 58 // Verify TaskRunnerHandles are set as expected in the task's scope. |
| 59 switch (execution_mode_) { |
| 60 case ExecutionMode::PARALLEL: |
| 61 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); |
| 62 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet()); |
| 63 break; |
| 64 case ExecutionMode::SEQUENCED: |
| 65 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); |
| 66 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); |
| 67 EXPECT_EQ(task_runner_, SequencedTaskRunnerHandle::Get()); |
| 68 break; |
| 69 case ExecutionMode::SINGLE_THREADED: |
| 70 // SequencedTaskRunnerHandle inherits from ThreadTaskRunnerHandle so |
| 71 // both are expected to be "set" in the SINGLE_THREADED case. |
| 72 EXPECT_TRUE(ThreadTaskRunnerHandle::IsSet()); |
| 73 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); |
| 74 EXPECT_EQ(task_runner_, ThreadTaskRunnerHandle::Get()); |
| 75 EXPECT_EQ(task_runner_, SequencedTaskRunnerHandle::Get()); |
| 76 break; |
| 77 } |
| 78 |
56 { | 79 { |
57 AutoLock auto_lock(lock_); | 80 AutoLock auto_lock(lock_); |
58 | 81 |
59 DCHECK_LE(task_index, num_posted_tasks_); | 82 DCHECK_LE(task_index, num_posted_tasks_); |
60 | 83 |
61 if ((execution_mode_ == ExecutionMode::SINGLE_THREADED || | 84 if ((execution_mode_ == ExecutionMode::SINGLE_THREADED || |
62 execution_mode_ == ExecutionMode::SEQUENCED) && | 85 execution_mode_ == ExecutionMode::SEQUENCED) && |
63 task_index != ran_tasks_.size()) { | 86 task_index != ran_tasks_.size()) { |
64 ADD_FAILURE() << "A task didn't run in the expected order."; | 87 ADD_FAILURE() << "A task didn't run in the expected order."; |
65 } | 88 } |
66 | 89 |
67 if (execution_mode_ == ExecutionMode::SINGLE_THREADED) | 90 if (execution_mode_ == ExecutionMode::SINGLE_THREADED) |
68 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | 91 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
69 | 92 |
70 if (ran_tasks_.find(task_index) != ran_tasks_.end()) | 93 if (ran_tasks_.find(task_index) != ran_tasks_.end()) |
71 ADD_FAILURE() << "A task ran more than once."; | 94 ADD_FAILURE() << "A task ran more than once."; |
72 ran_tasks_.insert(task_index); | 95 ran_tasks_.insert(task_index); |
73 | 96 |
74 cv_.Signal(); | 97 cv_.Signal(); |
75 } | 98 } |
76 | 99 |
77 if (!after_task_closure.is_null()) | 100 if (!after_task_closure.is_null()) |
78 after_task_closure.Run(); | 101 after_task_closure.Run(); |
79 } | 102 } |
80 | 103 |
81 } // namespace test | 104 } // namespace test |
82 } // namespace internal | 105 } // namespace internal |
83 } // namespace base | 106 } // namespace base |
OLD | NEW |