Skip to content

Commit a7eaf77

Browse files
committed
add SimpleInit utility for gtest, refactor tests to replace EXPECT_DEATH_IF_SUPPORTED with EXPECT_TRUE using DestructorFailureFlag, and update runner initialization methods for consistency
1 parent 1a12d05 commit a7eaf77

7 files changed

Lines changed: 64 additions & 28 deletions

File tree

modules/core/performance/tests/perf_tests.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,13 @@ TEST(PerfTest, PipelineRunAndTaskRun) {
296296
}
297297

298298
TEST(PerfTest, PrintPerfStatisticThrowsOnNone) {
299-
testing::FLAGS_gtest_death_test_style = "threadsafe";
300-
auto test_func = [&] {
299+
{
301300
auto task_ptr = std::make_shared<DummyTask>();
302301
ppc::core::Perf<int, int> perf(task_ptr);
303302
EXPECT_THROW(perf.PrintPerfStatistic("test"), std::runtime_error);
304-
};
305-
ASSERT_DEATH_IF_SUPPORTED({ test_func(); }, "");
303+
}
304+
EXPECT_TRUE(ppc::util::DestructorFailureFlag::Get());
305+
ppc::util::DestructorFailureFlag::Unset();
306306
}
307307

308308
TEST(PerfTest, GetStringParamNameTest) {
@@ -312,15 +312,15 @@ TEST(PerfTest, GetStringParamNameTest) {
312312
}
313313

314314
TEST(TaskTest, Destructor_InvalidPipelineOrderTerminates_PartialPipeline) {
315-
testing::FLAGS_gtest_death_test_style = "threadsafe";
316-
auto test_func = [&] {
315+
{
317316
struct BadTask : ppc::core::Task<int, int> {
318317
bool ValidationImpl() override { return true; }
319318
bool PreProcessingImpl() override { return true; }
320319
bool RunImpl() override { return true; }
321320
bool PostProcessingImpl() override { return true; }
322321
} task;
323322
task.Validation();
324-
};
325-
ASSERT_DEATH_IF_SUPPORTED({ test_func(); }, "ORDER OF FUNCTIONS IS NOT RIGHT");
323+
}
324+
EXPECT_TRUE(ppc::util::DestructorFailureFlag::Get());
325+
ppc::util::DestructorFailureFlag::Unset();
326326
}

modules/core/runners/include/runners.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ class WorkerTestFailurePrinter : public ::testing::EmptyTestEventListener {
4343
/// finalization fails.
4444
int Init(int argc, char** argv);
4545

46+
/// @brief Initializes the testing environment only for gtest.
47+
/// @param argc Argument count.
48+
/// @param argv Argument vector.
49+
/// @return Exit code from RUN_ALL_TESTS.
50+
int SimpleInit(int argc, char** argv);
51+
4652
} // namespace ppc::core

modules/core/runners/src/runners.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <format>
88
#include <iostream>
99
#include <memory>
10+
#include <stdexcept>
1011
#include <string>
1112

1213
#include "core/util/include/util.hpp"
@@ -82,6 +83,11 @@ int Init(int argc, char** argv) {
8283
listeners.Append(new ppc::core::UnreadMessagesDetector());
8384
auto status = RUN_ALL_TESTS();
8485

86+
if (ppc::util::DestructorFailureFlag::Get()) {
87+
throw std::runtime_error(
88+
std::format("[ ERROR ] Destructor failed with code {}", ppc::util::DestructorFailureFlag::Get()));
89+
}
90+
8591
const int finalize_res = MPI_Finalize();
8692
if (finalize_res != MPI_SUCCESS) {
8793
std::cerr << std::format("[ ERROR ] MPI_Finalize failed with code {}", finalize_res) << '\n';
@@ -91,4 +97,17 @@ int Init(int argc, char** argv) {
9197
return status;
9298
}
9399

100+
int SimpleInit(int argc, char** argv) {
101+
// Limit the number of threads in TBB
102+
tbb::global_control control(tbb::global_control::max_allowed_parallelism, ppc::util::GetNumThreads());
103+
104+
testing::InitGoogleTest(&argc, argv);
105+
auto status = RUN_ALL_TESTS();
106+
if (ppc::util::DestructorFailureFlag::Get()) {
107+
throw std::runtime_error(
108+
std::format("[ ERROR ] Destructor failed with code {}", ppc::util::DestructorFailureFlag::Get()));
109+
}
110+
return status;
111+
}
112+
94113
} // namespace ppc::core

modules/core/task/include/task.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ class Task {
194194
/// @note Terminates the program if the pipeline order is incorrect or incomplete.
195195
virtual ~Task() {
196196
if (stage_ != PipelineStage::kDone && stage_ != PipelineStage::kException) {
197-
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT" << '\n';
198-
std::terminate();
197+
ppc::util::DestructorFailureFlag::Set();
199198
}
200199
#if _OPENMP >= 201811
201200
omp_pause_resource_all(omp_pause_soft);

modules/core/task/tests/task_tests.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <thread>
1111
#include <vector>
1212

13+
#include "core/runners/include/runners.hpp"
1314
#include "core/task/include/task.hpp"
1415
#include "core/task/tests/test_task.hpp"
1516

@@ -146,8 +147,7 @@ TEST(TaskTest, GetStringTaskType_ThrowsIfKeyMissing) {
146147
}
147148

148149
TEST(TaskTest, TaskDestructor_ThrowsIfStageIncomplete) {
149-
testing::FLAGS_gtest_death_test_style = "threadsafe";
150-
auto destroy_func = [] {
150+
{
151151
std::vector<int32_t> in(20, 1);
152152
struct LocalTask : ppc::core::Task<std::vector<int32_t>, int32_t> {
153153
explicit LocalTask(const std::vector<int32_t>& in) { this->GetInput() = in; }
@@ -157,13 +157,13 @@ TEST(TaskTest, TaskDestructor_ThrowsIfStageIncomplete) {
157157
bool PostProcessingImpl() override { return true; }
158158
} task(in);
159159
task.Validation();
160-
};
161-
EXPECT_DEATH_IF_SUPPORTED({ destroy_func(); }, ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
160+
}
161+
EXPECT_TRUE(ppc::util::DestructorFailureFlag::Get());
162+
ppc::util::DestructorFailureFlag::Unset();
162163
}
163164

164165
TEST(TaskTest, TaskDestructor_ThrowsIfEmpty) {
165-
testing::FLAGS_gtest_death_test_style = "threadsafe";
166-
auto destroy_func = [] {
166+
{
167167
std::vector<int32_t> in(20, 1);
168168
struct LocalTask : ppc::core::Task<std::vector<int32_t>, int32_t> {
169169
explicit LocalTask(const std::vector<int32_t>& in) { this->GetInput() = in; }
@@ -172,8 +172,9 @@ TEST(TaskTest, TaskDestructor_ThrowsIfEmpty) {
172172
bool RunImpl() override { return true; }
173173
bool PostProcessingImpl() override { return true; }
174174
} task(in);
175-
};
176-
EXPECT_DEATH_IF_SUPPORTED({ destroy_func(); }, ".*ORDER OF FUNCTIONS IS NOT RIGHT.*");
175+
}
176+
EXPECT_TRUE(ppc::util::DestructorFailureFlag::Get());
177+
ppc::util::DestructorFailureFlag::Unset();
177178
}
178179

179180
TEST(TaskTest, InternalTimeTest_ThrowsIfTimeoutExceeded) {
@@ -229,7 +230,4 @@ TEST(TaskTest, PostProcessingThrowsIfCalledBeforeRun) {
229230
EXPECT_THROW(task->PostProcessing(), std::runtime_error);
230231
}
231232

232-
int main(int argc, char** argv) {
233-
testing::InitGoogleTest(&argc, argv);
234-
return RUN_ALL_TESTS();
235-
}
233+
int main(int argc, char** argv) { return ppc::core::SimpleInit(argc, argv); }

modules/core/util/include/util.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <atomic>
34
#include <cstdint>
45
#include <cstdlib>
56
#include <memory>
@@ -29,6 +30,24 @@ using NlohmannJsonTypeError = nlohmann::json::type_error;
2930

3031
namespace ppc::util {
3132

33+
/// @brief Utility class for tracking destructor failure across tests.
34+
/// @details Provides thread-safe methods to set, unset, and check the failure flag.
35+
class DestructorFailureFlag {
36+
public:
37+
/// @brief Marks that a destructor failure has occurred.
38+
static void Set() { failure_flag.store(true); }
39+
40+
/// @brief Clears the destructor failure flag.
41+
static void Unset() { failure_flag.store(false); }
42+
43+
/// @brief Checks if a destructor failure was recorded.
44+
/// @return True if failure occurred, false otherwise.
45+
static bool Get() { return failure_flag.load(); }
46+
47+
private:
48+
inline static std::atomic<bool> failure_flag{false};
49+
};
50+
3251
enum GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams };
3352

3453
std::string GetAbsoluteTaskPath(const std::string& id_path, const std::string& relative_path);

tasks/common/runners/functional.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,5 @@ int main(int argc, char** argv) {
88
if (ppc::util::IsUnderMpirun()) {
99
return ppc::core::Init(argc, argv);
1010
}
11-
12-
// Limit the number of threads in TBB
13-
tbb::global_control control(tbb::global_control::max_allowed_parallelism, ppc::util::GetNumThreads());
14-
15-
::testing::InitGoogleTest(&argc, argv);
16-
return RUN_ALL_TESTS();
11+
return ppc::core::SimpleInit(argc, argv);
1712
}

0 commit comments

Comments
 (0)