|
| 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 | +#include "iceoryx_hoofs/testing/mocks/error_handler_mock.hpp" |
| 18 | +#include "test.hpp" |
| 19 | + |
| 20 | +#include <csetjmp> |
| 21 | +#include <thread> |
| 22 | + |
| 23 | +namespace iox |
| 24 | +{ |
| 25 | +namespace testing |
| 26 | +{ |
| 27 | +template <typename ErrorType> |
| 28 | +void EXPECT_FATAL_FAILURE(const std::function<void()>& testFunction, |
| 29 | + const ErrorType expectedError, |
| 30 | + const iox::ErrorLevel expectedErrorLevel) |
| 31 | +{ |
| 32 | + auto th = std::thread([&] { |
| 33 | + constexpr int JMP_VALUE{1}; |
| 34 | + std::jmp_buf jmpBuffer; |
| 35 | + |
| 36 | + auto errorHandlerGuard = |
| 37 | + iox::ErrorHandlerMock::setTemporaryErrorHandler<ErrorType>([&](const auto error, const auto errorLevel) { |
| 38 | + EXPECT_THAT(error, ::testing::Eq(expectedError)); |
| 39 | + EXPECT_THAT(errorLevel, ::testing::Eq(expectedErrorLevel)); |
| 40 | + |
| 41 | + // NOLINTNEXTLINE(cert-err52-cpp) exception cannot be used and longjmp/setjmp is a working fallback |
| 42 | + std::longjmp(&jmpBuffer[0], JMP_VALUE); |
| 43 | + }); |
| 44 | + |
| 45 | + // NOLINTNEXTLINE(cert-err52-cpp) exception cannot be used and longjmp/setjmp is a working fallback |
| 46 | + if (setjmp(&jmpBuffer[0]) == JMP_VALUE) |
| 47 | + { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + testFunction(); |
| 52 | + |
| 53 | + GTEST_FAIL() << "Expected fatal failure but execution continued!"; |
| 54 | + }); |
| 55 | + |
| 56 | + th.join(); |
| 57 | +} |
| 58 | +} // namespace testing |
| 59 | +} // namespace iox |
0 commit comments