22 #include "gtest/gtest.h" 30 void Receive(
int number) {
31 Mutex::Locker locker(&
mutex_);
32 received_numbers_.push_back(number);
35 void WaitForNumberSequence(
const std::vector<int>& expected_numbers) {
38 return (received_numbers_.size() >= expected_numbers.size());
40 EXPECT_EQ(expected_numbers, received_numbers_);
44 std::vector<int> received_numbers_
GUARDED_BY(mutex_);
47 TEST(ThreadPoolTest, RunTask) {
50 auto task = common::make_unique<Task>();
51 task->SetWorkItem([&receiver]() { receiver.Receive(1); });
52 pool.Schedule(std::move(task));
53 receiver.WaitForNumberSequence({1});
56 TEST(ThreadPoolTest, ManyTasks) {
57 for (
int a = 0; a < 5; ++a) {
61 for (
int i = 0; i < kNumTasks; ++i) {
62 auto task = common::make_unique<Task>();
63 task->SetWorkItem([&receiver]() { receiver.Receive(1); });
64 pool.Schedule(std::move(task));
66 receiver.WaitForNumberSequence(std::vector<int>(kNumTasks, 1));
70 TEST(ThreadPoolTest, RunWithDependency) {
73 auto task_2 = common::make_unique<Task>();
74 task_2->SetWorkItem([&receiver]() { receiver.Receive(2); });
75 auto task_1 = common::make_unique<Task>();
76 task_1->SetWorkItem([&receiver]() { receiver.Receive(1); });
77 auto weak_task_1 = pool.Schedule(std::move(task_1));
78 task_2->AddDependency(weak_task_1);
79 pool.Schedule(std::move(task_2));
80 receiver.WaitForNumberSequence({1, 2});
83 TEST(ThreadPoolTest, RunWithOutOfScopeDependency) {
86 auto task_2 = common::make_unique<Task>();
87 task_2->SetWorkItem([&receiver]() { receiver.Receive(2); });
89 auto task_1 = common::make_unique<Task>();
90 task_1->SetWorkItem([&receiver]() { receiver.Receive(1); });
91 auto weak_task_1 = pool.Schedule(std::move(task_1));
92 task_2->AddDependency(weak_task_1);
94 pool.Schedule(std::move(task_2));
95 receiver.WaitForNumberSequence({1, 2});
98 TEST(ThreadPoolTest, ManyDependencies) {
99 for (
int a = 0; a < 5; ++a) {
102 int kNumDependencies = 10;
103 auto task = common::make_unique<Task>();
104 task->SetWorkItem([&receiver]() { receiver.Receive(1); });
105 for (
int i = 0; i < kNumDependencies; ++i) {
106 auto dependency_task = common::make_unique<Task>();
107 dependency_task->SetWorkItem([]() {});
108 task->AddDependency(pool.Schedule(std::move(dependency_task)));
110 pool.Schedule(std::move(task));
111 receiver.WaitForNumberSequence({1});
115 TEST(ThreadPoolTest, ManyDependants) {
116 for (
int a = 0; a < 5; ++a) {
119 int kNumDependants = 10;
120 auto dependency_task = common::make_unique<Task>();
121 dependency_task->SetWorkItem([]() {});
122 auto dependency_handle = pool.Schedule(std::move(dependency_task));
123 for (
int i = 0; i < kNumDependants; ++i) {
124 auto task = common::make_unique<Task>();
125 task->AddDependency(dependency_handle);
126 task->SetWorkItem([&receiver]() { receiver.Receive(1); });
127 pool.Schedule(std::move(task));
129 receiver.WaitForNumberSequence(std::vector<int>(kNumDependants, 1));
133 TEST(ThreadPoolTest, RunWithMultipleDependencies) {
136 auto task_1 = common::make_unique<Task>();
137 task_1->SetWorkItem([&receiver]() { receiver.Receive(1); });
138 auto task_2a = common::make_unique<Task>();
139 task_2a->SetWorkItem([&receiver]() { receiver.Receive(2); });
140 auto task_2b = common::make_unique<Task>();
141 task_2b->SetWorkItem([&receiver]() { receiver.Receive(2); });
142 auto task_3 = common::make_unique<Task>();
143 task_3->SetWorkItem([&receiver]() { receiver.Receive(3); });
147 auto weak_task_1 = pool.Schedule(std::move(task_1));
148 task_2a->AddDependency(weak_task_1);
149 auto weak_task_2a = pool.Schedule(std::move(task_2a));
150 task_3->AddDependency(weak_task_1);
151 task_3->AddDependency(weak_task_2a);
152 task_2b->AddDependency(weak_task_1);
153 auto weak_task_2b = pool.Schedule(std::move(task_2b));
154 task_3->AddDependency(weak_task_2b);
155 pool.Schedule(std::move(task_3));
156 receiver.WaitForNumberSequence({1, 2, 2, 3});
159 TEST(ThreadPoolTest, RunWithFinishedDependency) {
162 auto task_1 = common::make_unique<Task>();
163 task_1->SetWorkItem([&receiver]() { receiver.Receive(1); });
164 auto task_2 = common::make_unique<Task>();
165 task_2->SetWorkItem([&receiver]() { receiver.Receive(2); });
166 auto weak_task_1 = pool.Schedule(std::move(task_1));
167 task_2->AddDependency(weak_task_1);
168 receiver.WaitForNumberSequence({1});
169 pool.Schedule(std::move(task_2));
170 receiver.WaitForNumberSequence({1, 2});
Mutex::Locker MutexLocker
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)