Index: base/task_scheduler/scheduler_thread_pool_unittest.cc |
diff --git a/base/task_scheduler/scheduler_thread_pool_unittest.cc b/base/task_scheduler/scheduler_thread_pool_unittest.cc |
index 7e1ec5c2cb1195864e6e812072b13f7a540ad4e6..29306663c237c2c8ea2df01586c5fe840ca4a4e3 100644 |
--- a/base/task_scheduler/scheduler_thread_pool_unittest.cc |
+++ b/base/task_scheduler/scheduler_thread_pool_unittest.cc |
@@ -25,6 +25,7 @@ |
#include "base/task_scheduler/task_tracker.h" |
#include "base/threading/platform_thread.h" |
#include "base/threading/simple_thread.h" |
+#include "base/threading/thread_checker_impl.h" |
#include "testing/gtest/include/gtest/gtest.h" |
namespace base { |
@@ -79,7 +80,11 @@ class TaskFactory { |
: cv_(&lock_), |
task_runner_(thread_pool->CreateTaskRunnerWithTraits(TaskTraits(), |
execution_mode)), |
- execution_mode_(execution_mode) {} |
+ execution_mode_(execution_mode) { |
+ // Detach |thread_checker_| from the current thread. It will be attached to |
+ // the first thread that calls ThreadCheckerImpl::CalledOnValidThread(). |
+ thread_checker_.DetachFromThread(); |
+ } |
// Posts a task through |task_runner_|. If |post_nested_task| is true, the |
// task will post a new task when it runs. If |event| is set, the task will |
@@ -119,11 +124,15 @@ class TaskFactory { |
{ |
AutoLock auto_lock(lock_); |
- if (execution_mode_ == ExecutionMode::SEQUENCED && |
+ if ((execution_mode_ == ExecutionMode::SEQUENCED || |
+ execution_mode_ == ExecutionMode::SINGLE_THREADED) && |
task_index != ran_tasks_.size()) { |
- ADD_FAILURE() << "A SEQUENCED task didn't run in the expected order."; |
+ ADD_FAILURE() << "A task didn't run in the expected order."; |
} |
+ if (execution_mode_ == ExecutionMode::SINGLE_THREADED) |
+ EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
+ |
if (ran_tasks_.find(task_index) != ran_tasks_.end()) |
ADD_FAILURE() << "A task ran more than once."; |
ran_tasks_.insert(task_index); |
@@ -153,6 +162,10 @@ class TaskFactory { |
// Indexes of tasks that ran. |
std::unordered_set<size_t> ran_tasks_; |
+ // Used to verify that all tasks run on the same thread when |execution_mode_| |
+ // is SINGLE_THREADED. |
+ ThreadCheckerImpl thread_checker_; |
+ |
DISALLOW_COPY_AND_ASSIGN(TaskFactory); |
}; |
@@ -277,8 +290,8 @@ TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) { |
TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { |
// Post tasks to keep all threads busy except one until |event| is signaled. |
- // Use different factories so that tasks are added to different sequences and |
- // can run simultaneously when the execution mode is SEQUENCED. |
+ // Use different factories so that tasks are assigned to different sequences |
+ // or threads when the execution mode is SEQUENCED or SINGLE_THREADED. |
WaitableEvent event(true, false); |
std::vector<std::unique_ptr<TaskFactory>> blocked_task_factories; |
for (size_t i = 0; i < (kNumThreadsInThreadPool - 1); ++i) { |
@@ -306,8 +319,8 @@ TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) { |
TEST_P(TaskSchedulerThreadPoolTest, Saturate) { |
// Verify that it is possible to have |kNumThreadsInThreadPool| |
// tasks/sequences running simultaneously. Use different factories so that |
- // tasks are added to different sequences and can run simultaneously when the |
- // execution mode is SEQUENCED. |
+ // tasks are assigned to different sequences or threads when the execution |
+ // mode is SEQUENCED or SINGLE_THREADED. |
WaitableEvent event(true, false); |
std::vector<std::unique_ptr<TaskFactory>> factories; |
for (size_t i = 0; i < kNumThreadsInThreadPool; ++i) { |
@@ -331,6 +344,9 @@ INSTANTIATE_TEST_CASE_P(Parallel, |
INSTANTIATE_TEST_CASE_P(Sequenced, |
TaskSchedulerThreadPoolTest, |
::testing::Values(ExecutionMode::SEQUENCED)); |
+INSTANTIATE_TEST_CASE_P(SingleThreaded, |
+ TaskSchedulerThreadPoolTest, |
+ ::testing::Values(ExecutionMode::SINGLE_THREADED)); |
} // namespace |
} // namespace internal |