Skip to content

Commit 99a8a83

Browse files
committed
update Task tests and implementation: replace EXPECT_DEATH_IF_SUPPORTED with EXPECT_THROW, add kException pipeline stage, and improve error handling in Task destructor
1 parent 9d3430c commit 99a8a83

File tree

3 files changed

+38
-52
lines changed

3 files changed

+38
-52
lines changed

modules/core/performance/tests/perf_tests.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,8 @@ TEST(TaskTest, GetDynamicTypeReturnsCorrectEnum) {
238238
}
239239

240240
TEST(TaskTest, DestructorTerminatesIfWrongOrder) {
241-
testing::FLAGS_gtest_death_test_style = "threadsafe";
242-
auto test_func = [&] {
243-
DummyTask task;
244-
task.Run();
245-
};
246-
ASSERT_DEATH_IF_SUPPORTED({ test_func(); }, "");
241+
DummyTask task;
242+
EXPECT_THROW(task.Run(), std::runtime_error);
247243
}
248244

249245
namespace my {

modules/core/task/include/task.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class Task {
114114
if (stage_ == PipelineStage::kNone || stage_ == PipelineStage::kDone) {
115115
stage_ = PipelineStage::kValidation;
116116
} else {
117+
stage_ = PipelineStage::kException;
117118
throw std::runtime_error("Validation should be called before preprocessing");
118119
}
119120
return ValidationImpl();
@@ -125,6 +126,7 @@ class Task {
125126
if (stage_ == PipelineStage::kValidation) {
126127
stage_ = PipelineStage::kPreProcessing;
127128
} else {
129+
stage_ = PipelineStage::kException;
128130
throw std::runtime_error("Preprocessing should be called after validation");
129131
}
130132
if (state_of_testing_ == StateOfTesting::kFunc) {
@@ -139,6 +141,7 @@ class Task {
139141
if (stage_ == PipelineStage::kPreProcessing || stage_ == PipelineStage::kRun) {
140142
stage_ = PipelineStage::kRun;
141143
} else {
144+
stage_ = PipelineStage::kException;
142145
throw std::runtime_error("Run should be called after preprocessing");
143146
}
144147
return RunImpl();
@@ -150,6 +153,7 @@ class Task {
150153
if (stage_ == PipelineStage::kRun) {
151154
stage_ = PipelineStage::kDone;
152155
} else {
156+
stage_ = PipelineStage::kException;
153157
throw std::runtime_error("Postprocessing should be called after run");
154158
}
155159
if (state_of_testing_ == StateOfTesting::kFunc) {
@@ -189,7 +193,7 @@ class Task {
189193
/// @brief Destructor. Verifies that the pipeline was executed in the correct order.
190194
/// @note Terminates the program if the pipeline order is incorrect or incomplete.
191195
virtual ~Task() {
192-
if (stage_ != PipelineStage::kDone) {
196+
if (stage_ != PipelineStage::kDone && stage_ != PipelineStage::kException) {
193197
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT" << '\n';
194198
std::terminate();
195199
}
@@ -248,7 +252,14 @@ class Task {
248252
StatusOfTask status_of_task_ = kEnabled;
249253
static constexpr double kMaxTestTime = 1.0;
250254
std::chrono::high_resolution_clock::time_point tmp_time_point_;
251-
enum class PipelineStage : uint8_t { kNone, kValidation, kPreProcessing, kRun, kDone } stage_ = PipelineStage::kNone;
255+
enum class PipelineStage : uint8_t {
256+
kNone,
257+
kValidation,
258+
kPreProcessing,
259+
kRun,
260+
kDone,
261+
kException
262+
} stage_ = PipelineStage::kNone;
252263
};
253264

254265
/// @brief Smart pointer alias for Task.

modules/core/task/tests/task_tests.cpp

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,24 @@ TEST(task_tests, check_float) {
6262
}
6363

6464
TEST(task_tests, check_wrong_order_disabled_valgrind) {
65-
auto destroy_function = [] {
66-
std::vector<float> in(20, 1);
67-
ppc::test::task::TestTask<std::vector<float>, float> test_task(in);
68-
ASSERT_EQ(test_task.Validation(), true);
69-
test_task.PreProcessing();
70-
test_task.PostProcessing();
71-
};
72-
EXPECT_DEATH_IF_SUPPORTED(destroy_function(), ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
65+
std::vector<float> in(20, 1);
66+
ppc::test::task::TestTask<std::vector<float>, float> test_task(in);
67+
ASSERT_EQ(test_task.Validation(), true);
68+
test_task.PreProcessing();
69+
EXPECT_THROW(test_task.PostProcessing(), std::runtime_error);
7370
}
7471

7572
TEST(task_tests, premature_postprocessing_no_steps) {
76-
auto destroy_function = [] {
77-
std::vector<float> in(20, 1);
78-
ppc::test::task::TestTask<std::vector<float>, float> test_task(in);
79-
ASSERT_NO_THROW(test_task.PostProcessing());
80-
};
81-
EXPECT_DEATH_IF_SUPPORTED(destroy_function(), ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
73+
std::vector<float> in(20, 1);
74+
ppc::test::task::TestTask<std::vector<float>, float> test_task(in);
75+
EXPECT_THROW(test_task.PostProcessing(), std::runtime_error);
8276
}
8377

8478
TEST(task_tests, premature_postprocessing_after_preprocessing) {
85-
auto destroy_function = [] {
86-
std::vector<float> in(20, 1);
87-
ppc::test::task::TestTask<std::vector<float>, float> test_task(in);
88-
ASSERT_NO_THROW(test_task.PreProcessing());
89-
ASSERT_NO_THROW(test_task.PostProcessing());
90-
};
91-
EXPECT_DEATH_IF_SUPPORTED(destroy_function(), ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
79+
std::vector<float> in(20, 1);
80+
ppc::test::task::TestTask<std::vector<float>, float> test_task(in);
81+
EXPECT_THROW(test_task.PreProcessing(), std::runtime_error);
82+
EXPECT_THROW(test_task.PostProcessing(), std::runtime_error);
9283
}
9384

9485
TEST(TaskTest, GetStringTaskStatus_Disabled) {
@@ -201,38 +192,26 @@ class DummyTask : public ppc::core::Task<int, int> {
201192
};
202193

203194
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");
195+
auto task = std::make_shared<DummyTask>();
196+
task->Validation();
197+
EXPECT_THROW(task->Validation(), std::runtime_error);
210198
}
211199

212200
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");
201+
auto task = std::make_shared<DummyTask>();
202+
EXPECT_THROW(task->PreProcessing(), std::runtime_error);
218203
}
219204

220205
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");
206+
auto task = std::make_shared<DummyTask>();
207+
EXPECT_THROW(task->Run(), std::runtime_error);
226208
}
227209

228210
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");
211+
auto task = std::make_shared<DummyTask>();
212+
task->Validation();
213+
task->PreProcessing();
214+
EXPECT_THROW(task->PostProcessing(), std::runtime_error);
236215
}
237216

238217
int main(int argc, char** argv) {

0 commit comments

Comments
 (0)