Skip to content

Commit 9d3430c

Browse files
committed
add tests for Task pipeline stage order validation and update Task methods to enforce correct call sequence
1 parent e1e2960 commit 9d3430c

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

modules/core/task/include/task.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ class Task {
111111
/// @brief Validates input data and task attributes before execution.
112112
/// @return True if validation is successful.
113113
virtual bool Validation() final {
114-
if (stage_ == PipelineStage::kNone) {
114+
if (stage_ == PipelineStage::kNone || stage_ == PipelineStage::kDone) {
115115
stage_ = PipelineStage::kValidation;
116+
} else {
117+
throw std::runtime_error("Validation should be called before preprocessing");
116118
}
117119
return ValidationImpl();
118120
}
@@ -122,6 +124,8 @@ class Task {
122124
virtual bool PreProcessing() final {
123125
if (stage_ == PipelineStage::kValidation) {
124126
stage_ = PipelineStage::kPreProcessing;
127+
} else {
128+
throw std::runtime_error("Preprocessing should be called after validation");
125129
}
126130
if (state_of_testing_ == StateOfTesting::kFunc) {
127131
InternalTimeTest();
@@ -134,6 +138,8 @@ class Task {
134138
virtual bool Run() final {
135139
if (stage_ == PipelineStage::kPreProcessing || stage_ == PipelineStage::kRun) {
136140
stage_ = PipelineStage::kRun;
141+
} else {
142+
throw std::runtime_error("Run should be called after preprocessing");
137143
}
138144
return RunImpl();
139145
}
@@ -143,6 +149,8 @@ class Task {
143149
virtual bool PostProcessing() final {
144150
if (stage_ == PipelineStage::kRun) {
145151
stage_ = PipelineStage::kDone;
152+
} else {
153+
throw std::runtime_error("Postprocessing should be called after run");
146154
}
147155
if (state_of_testing_ == StateOfTesting::kFunc) {
148156
InternalTimeTest();

modules/core/task/tests/task_tests.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cstdint>
66
#include <exception>
77
#include <fstream>
8+
#include <memory>
89
#include <stdexcept>
910
#include <thread>
1011
#include <vector>
@@ -190,6 +191,50 @@ TEST(TaskTest, InternalTimeTest_ThrowsIfTimeoutExceeded) {
190191
EXPECT_THROW(task.PostProcessing(), std::runtime_error);
191192
}
192193

194+
class DummyTask : public ppc::core::Task<int, int> {
195+
public:
196+
using Task::Task;
197+
bool ValidationImpl() override { return true; }
198+
bool PreProcessingImpl() override { return true; }
199+
bool RunImpl() override { return true; }
200+
bool PostProcessingImpl() override { return true; }
201+
};
202+
203+
TEST(TaskTest, ValidationThrowsIfCalledTwice) {
204+
auto test_func = [&] {
205+
auto task = std::make_shared<DummyTask>();
206+
task->Validation();
207+
EXPECT_THROW(task->Validation(), std::runtime_error);
208+
};
209+
EXPECT_DEATH_IF_SUPPORTED({ test_func(); }, "ORDER OF FUNCTIONS IS NOT RIGHT");
210+
}
211+
212+
TEST(TaskTest, PreProcessingThrowsIfCalledBeforeValidation) {
213+
auto test_func = [&] {
214+
auto task = std::make_shared<DummyTask>();
215+
EXPECT_THROW(task->PreProcessing(), std::runtime_error);
216+
};
217+
EXPECT_DEATH_IF_SUPPORTED({ test_func(); }, "ORDER OF FUNCTIONS IS NOT RIGHT");
218+
}
219+
220+
TEST(TaskTest, RunThrowsIfCalledBeforePreProcessing) {
221+
auto test_func = [&] {
222+
auto task = std::make_shared<DummyTask>();
223+
EXPECT_THROW(task->Run(), std::runtime_error);
224+
};
225+
EXPECT_DEATH_IF_SUPPORTED({ test_func(); }, "ORDER OF FUNCTIONS IS NOT RIGHT");
226+
}
227+
228+
TEST(TaskTest, PostProcessingThrowsIfCalledBeforeRun) {
229+
auto test_func = [&] {
230+
auto task = std::make_shared<DummyTask>();
231+
task->Validation();
232+
task->PreProcessing();
233+
EXPECT_THROW(task->PostProcessing(), std::runtime_error);
234+
};
235+
EXPECT_DEATH_IF_SUPPORTED({ test_func(); }, "ORDER OF FUNCTIONS IS NOT RIGHT");
236+
}
237+
193238
int main(int argc, char** argv) {
194239
testing::InitGoogleTest(&argc, argv);
195240
return RUN_ALL_TESTS();

0 commit comments

Comments
 (0)