23 #include "gmock/gmock.h" 24 #include "gtest/gtest.h" 32 MOCK_METHOD0(
Run,
void());
35 class FakeThreadPool :
public ThreadPoolInterface {
37 void NotifyDependenciesCompleted(Task* task)
override {
44 std::weak_ptr<Task> Schedule(std::unique_ptr<Task> task)
override {
45 std::shared_ptr<Task> shared_task;
48 EXPECT_TRUE(it.second);
49 shared_task = it.first->second;
50 SetThreadPool(shared_task.get());
67 class TaskTest :
public ::testing::Test {
73 TEST_F(TaskTest, RunTask) {
74 auto a = make_unique<Task>();
75 MockCallback callback;
76 a->SetWorkItem([&callback]() { callback.Run(); });
78 auto shared_a = thread_pool()->Schedule(std::move(a)).lock();
79 EXPECT_NE(shared_a,
nullptr);
81 EXPECT_CALL(callback,
Run()).Times(1);
82 thread_pool()->RunNext();
84 EXPECT_TRUE(thread_pool()->IsEmpty());
87 TEST_F(TaskTest, RunTaskWithDependency) {
88 auto a = make_unique<Task>();
89 auto b = make_unique<Task>();
90 MockCallback callback_a;
91 a->SetWorkItem([&callback_a]() { callback_a.Run(); });
92 MockCallback callback_b;
93 b->SetWorkItem([&callback_b]() { callback_b.Run(); });
97 ::testing::InSequence dummy;
98 EXPECT_CALL(callback_a,
Run()).Times(1);
99 EXPECT_CALL(callback_b,
Run()).Times(1);
101 auto shared_a = thread_pool()->Schedule(std::move(a)).lock();
102 EXPECT_NE(shared_a,
nullptr);
103 b->AddDependency(shared_a);
104 auto shared_b = thread_pool()->Schedule(std::move(b)).lock();
105 EXPECT_NE(shared_b,
nullptr);
108 thread_pool()->RunNext();
110 thread_pool()->RunNext();
115 TEST_F(TaskTest, RunTaskWithTwoDependency) {
119 auto a = make_unique<Task>();
120 auto b = make_unique<Task>();
121 auto c = make_unique<Task>();
122 auto d = make_unique<Task>();
123 MockCallback callback_a;
124 a->SetWorkItem([&callback_a]() { callback_a.Run(); });
125 MockCallback callback_b;
126 b->SetWorkItem([&callback_b]() { callback_b.Run(); });
127 MockCallback callback_c;
128 c->SetWorkItem([&callback_c]() { callback_c.Run(); });
129 MockCallback callback_d;
130 d->SetWorkItem([&callback_d]() { callback_d.Run(); });
131 EXPECT_CALL(callback_a,
Run()).Times(1);
132 EXPECT_CALL(callback_b,
Run()).Times(1);
133 EXPECT_CALL(callback_c,
Run()).Times(1);
134 EXPECT_CALL(callback_d,
Run()).Times(1);
135 auto shared_a = thread_pool()->Schedule(std::move(a)).lock();
136 EXPECT_NE(shared_a,
nullptr);
137 b->AddDependency(shared_a);
138 auto shared_b = thread_pool()->Schedule(std::move(b)).lock();
139 EXPECT_NE(shared_b,
nullptr);
140 auto shared_c = thread_pool()->Schedule(std::move(c)).lock();
141 EXPECT_NE(shared_c,
nullptr);
142 d->AddDependency(shared_b);
143 d->AddDependency(shared_c);
144 auto shared_d = thread_pool()->Schedule(std::move(d)).lock();
145 EXPECT_NE(shared_d,
nullptr);
148 thread_pool()->RunNext();
152 thread_pool()->RunNext();
153 thread_pool()->RunNext();
157 thread_pool()->RunNext();
161 TEST_F(TaskTest, RunWithCompletedDependency) {
162 auto a = make_unique<Task>();
163 MockCallback callback_a;
164 a->SetWorkItem([&callback_a]() { callback_a.Run(); });
165 auto shared_a = thread_pool()->Schedule(std::move(a)).lock();
166 EXPECT_NE(shared_a,
nullptr);
168 EXPECT_CALL(callback_a,
Run()).Times(1);
169 thread_pool()->RunNext();
171 auto b = make_unique<Task>();
172 MockCallback callback_b;
173 b->SetWorkItem([&callback_b]() { callback_b.Run(); });
174 b->AddDependency(shared_a);
176 auto shared_b = thread_pool()->Schedule(std::move(b)).lock();
177 EXPECT_NE(shared_b,
nullptr);
179 EXPECT_CALL(callback_b,
Run()).Times(1);
180 thread_pool()->RunNext();
std::map< Task *, std::shared_ptr< Task > > tasks_not_ready_
FakeThreadPool thread_pool_
std::deque< std::shared_ptr< Task > > task_queue_
void Run(const std::string &configuration_directory, const std::string &configuration_basename)