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