Skip to content

Commit a6ffe88

Browse files
committed
iox-#1613 Move implementation to inl file
1 parent 40d9df7 commit a6ffe88

File tree

2 files changed

+108
-61
lines changed

2 files changed

+108
-61
lines changed

iceoryx_hoofs/testing/include/iceoryx_hoofs/testing/fatal_failure.hpp

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -40,46 +40,9 @@ namespace detail
4040
/// @param[in] onNonFatalFailurePath This function will be executed on the non-failure path if no failure was detected
4141
/// @return true if a fatal failure occurs, false otherwise
4242
template <typename ErrorType>
43-
bool FATAL_FAILURE_TEST(const std::function<void()>& testFunction,
44-
const std::function<void(const ErrorType, const iox::ErrorLevel)>& onFatalFailurePath,
45-
const std::function<void()>& onNonFatalFailurePath)
46-
{
47-
std::atomic<bool> hasFatalFailure{false};
48-
auto th = std::thread([&] {
49-
constexpr int JMP_VALUE{1};
50-
std::jmp_buf jmpBuffer;
51-
52-
optional<ErrorType> detectedError;
53-
optional<iox::ErrorLevel> detectedErrorLevel;
54-
55-
auto errorHandlerGuard =
56-
iox::ErrorHandlerMock::setTemporaryErrorHandler<ErrorType>([&](const auto error, const auto errorLevel) {
57-
detectedError.emplace(error);
58-
detectedErrorLevel.emplace(errorLevel);
59-
60-
// NOLINTNEXTLINE(cert-err52-cpp) exception cannot be used and longjmp/setjmp is a working fallback
61-
std::longjmp(&jmpBuffer[0], JMP_VALUE);
62-
});
63-
64-
// NOLINTNEXTLINE(cert-err52-cpp) exception cannot be used and longjmp/setjmp is a working fallback
65-
if (setjmp(&jmpBuffer[0]) == JMP_VALUE)
66-
{
67-
hasFatalFailure = true;
68-
// using value directly is save since this path is only executed if the error handler was called and the
69-
// respective values were set
70-
onFatalFailurePath(detectedError.value(), detectedErrorLevel.value());
71-
return;
72-
}
73-
74-
testFunction();
75-
76-
onNonFatalFailurePath();
77-
});
78-
79-
th.join();
80-
81-
return hasFatalFailure.load(std::memory_order_relaxed);
82-
}
43+
bool IOX_FATAL_FAILURE_TEST(const std::function<void()>& testFunction,
44+
const std::function<void(const ErrorType, const iox::ErrorLevel)>& onFatalFailurePath,
45+
const std::function<void()>& onNonFatalFailurePath);
8346
} // namespace detail
8447

8548
/// @brief This function is used in cases a fatal failure is expected. The function only works in combination with the
@@ -95,16 +58,7 @@ bool FATAL_FAILURE_TEST(const std::function<void()>& testFunction,
9558
/// @param[in] expectedError The error value which triggered the fatal failure
9659
/// @return true if a fatal failure occurs, false otherwise
9760
template <typename ErrorType>
98-
bool IOX_EXPECT_FATAL_FAILURE(const std::function<void()>& testFunction, const ErrorType expectedError)
99-
{
100-
return detail::FATAL_FAILURE_TEST<ErrorType>(
101-
testFunction,
102-
[&](const auto error, const auto errorLevel) {
103-
EXPECT_THAT(error, ::testing::Eq(expectedError));
104-
EXPECT_THAT(errorLevel, ::testing::Eq(iox::ErrorLevel::FATAL));
105-
},
106-
[&] { GTEST_FAIL() << "Expected fatal failure but execution continued!"; });
107-
}
61+
bool IOX_EXPECT_FATAL_FAILURE(const std::function<void()>& testFunction, const ErrorType expectedError);
10862

10963
/// @brief This function is used in cases no fatal failure is expected but could potentially occur. The function only
11064
/// works in combination with the iceoryx error handler.
@@ -118,19 +72,12 @@ bool IOX_EXPECT_FATAL_FAILURE(const std::function<void()>& testFunction, const E
11872
/// @param[in] testFunction This function will be executed as SUT and is not expected to call the error handler
11973
/// @return true if no fatal failure occurs, false otherwise
12074
template <typename ErrorType>
121-
bool IOX_EXPECT_NO_FATAL_FAILURE(const std::function<void()>& testFunction)
122-
{
123-
return !detail::FATAL_FAILURE_TEST<ErrorType>(
124-
testFunction,
125-
[&](const auto error, const auto errorLevel) {
126-
GTEST_FAIL() << "Expected no fatal failure but execution failed! Error code: "
127-
<< static_cast<uint64_t>(error) << "; Error level: " << static_cast<uint64_t>(errorLevel);
128-
},
129-
[&] {});
130-
return false;
131-
}
75+
bool IOX_EXPECT_NO_FATAL_FAILURE(const std::function<void()>& testFunction);
13276

13377
} // namespace testing
13478
} // namespace iox
13579

80+
81+
#include "iceoryx_hoofs/testing/fatal_failure.inl"
82+
13683
#endif // IOX_HOOFS_TESTING_FATAL_FAILURE_HPP
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
#ifndef IOX_HOOFS_TESTING_FATAL_FAILURE_INL
18+
#define IOX_HOOFS_TESTING_FATAL_FAILURE_INL
19+
20+
#include "iceoryx_hoofs/testing/fatal_failure.hpp"
21+
22+
namespace iox
23+
{
24+
namespace testing
25+
{
26+
namespace detail
27+
{
28+
template <typename ErrorType>
29+
inline bool
30+
IOX_FATAL_FAILURE_TEST(const std::function<void()>& testFunction,
31+
const std::function<void(const ErrorType, const iox::ErrorLevel)>& onFatalFailurePath,
32+
const std::function<void()>& onNonFatalFailurePath)
33+
{
34+
std::atomic<bool> hasFatalFailure{false};
35+
auto th = std::thread([&] {
36+
constexpr int JMP_VALUE{1};
37+
std::jmp_buf jmpBuffer;
38+
39+
optional<ErrorType> detectedError;
40+
optional<iox::ErrorLevel> detectedErrorLevel;
41+
42+
auto errorHandlerGuard =
43+
iox::ErrorHandlerMock::setTemporaryErrorHandler<ErrorType>([&](const auto error, const auto errorLevel) {
44+
detectedError.emplace(error);
45+
detectedErrorLevel.emplace(errorLevel);
46+
47+
// NOLINTNEXTLINE(cert-err52-cpp) exception cannot be used and longjmp/setjmp is a working fallback
48+
std::longjmp(&jmpBuffer[0], JMP_VALUE);
49+
});
50+
51+
// NOLINTNEXTLINE(cert-err52-cpp) exception cannot be used and longjmp/setjmp is a working fallback
52+
if (setjmp(&jmpBuffer[0]) == JMP_VALUE)
53+
{
54+
hasFatalFailure = true;
55+
// using value directly is save since this path is only executed if the error handler was called and the
56+
// respective values were set
57+
onFatalFailurePath(detectedError.value(), detectedErrorLevel.value());
58+
return;
59+
}
60+
61+
testFunction();
62+
63+
onNonFatalFailurePath();
64+
});
65+
66+
th.join();
67+
68+
return hasFatalFailure.load(std::memory_order_relaxed);
69+
}
70+
} // namespace detail
71+
72+
template <typename ErrorType>
73+
inline bool IOX_EXPECT_FATAL_FAILURE(const std::function<void()>& testFunction, const ErrorType expectedError)
74+
{
75+
return detail::IOX_FATAL_FAILURE_TEST<ErrorType>(
76+
testFunction,
77+
[&](const auto error, const auto errorLevel) {
78+
EXPECT_THAT(error, ::testing::Eq(expectedError));
79+
EXPECT_THAT(errorLevel, ::testing::Eq(iox::ErrorLevel::FATAL));
80+
},
81+
[&] { GTEST_FAIL() << "Expected fatal failure but execution continued!"; });
82+
}
83+
84+
template <typename ErrorType>
85+
inline bool IOX_EXPECT_NO_FATAL_FAILURE(const std::function<void()>& testFunction)
86+
{
87+
return !detail::IOX_FATAL_FAILURE_TEST<ErrorType>(
88+
testFunction,
89+
[&](const auto error, const auto errorLevel) {
90+
GTEST_FAIL() << "Expected no fatal failure but execution failed! Error code: "
91+
<< static_cast<uint64_t>(error) << "; Error level: " << static_cast<uint64_t>(errorLevel);
92+
},
93+
[&] { GTEST_SUCCEED() << "Non-fatal path taken!"; });
94+
return false;
95+
}
96+
97+
} // namespace testing
98+
} // namespace iox
99+
100+
#endif // IOX_HOOFS_TESTING_FATAL_FAILURE_INL

0 commit comments

Comments
 (0)