Skip to content

Commit 447ab3a

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

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,50 @@ TEST(TaskTest, InternalTimeTest_ThrowsIfTimeoutExceeded) {
190190
EXPECT_THROW(task.PostProcessing(), std::runtime_error);
191191
}
192192

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

0 commit comments

Comments
 (0)