From 310931d949d06bd783e28a3dcdb12fcbb4980126 Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Tue, 5 Sep 2023 11:26:45 +0200 Subject: [PATCH] iox-#1613 Replace 'EXPECT_DEATH' with 'IOX_EXPECT_FATAL_FAILURE' where possible --- doc/design/error-handling.md | 8 +++++ iceoryx_posh/source/mepoo/mem_pool.cpp | 3 +- .../test_shm_sigbus_handler.cpp | 1 + .../moduletests/test_mepoo_memory_manager.cpp | 7 +++- .../test/moduletests/test_mepoo_mempool.cpp | 32 ++++++++++++++----- .../test/moduletests/test_posh_runtime.cpp | 5 +++ .../test_runtime_ipc_interface_creator.cpp | 7 +++- 7 files changed, 52 insertions(+), 11 deletions(-) diff --git a/doc/design/error-handling.md b/doc/design/error-handling.md index c600d03985..5667bf1245 100644 --- a/doc/design/error-handling.md +++ b/doc/design/error-handling.md @@ -281,6 +281,14 @@ TEST(MyTest, valueOnNulloptIsFatal) { } ``` +In cases where the `IOX_EXPECT_FATAL_FAILURE` cannot be used, e.g. testing the `PoshRuntime` with its singleton, `EXPECT_DEATH` +shall be used with `GTEST_FLAG(death_test_style) = "threadsafe";`. + +```cpp +GTEST_FLAG(death_test_style) = "threadsafe"; +EXPECT_DEATH(bad_call(), ".*"); +``` + ## Open points ### Centralized error handling diff --git a/iceoryx_posh/source/mepoo/mem_pool.cpp b/iceoryx_posh/source/mepoo/mem_pool.cpp index 393c42eb28..7532d336fa 100644 --- a/iceoryx_posh/source/mepoo/mem_pool.cpp +++ b/iceoryx_posh/source/mepoo/mem_pool.cpp @@ -62,7 +62,8 @@ MemPool::MemPool(const greater_or_equal chunkS else { IOX_LOG(FATAL) << "Chunk size must be multiple of '" << CHUNK_MEMORY_ALIGNMENT << "'! Requested size is " - << chunkSize << " for " << numberOfChunks << " chunks!"; + << static_cast(chunkSize) << " for " << static_cast(numberOfChunks) + << " chunks!"; errorHandler(PoshError::MEPOO__MEMPOOL_CHUNKSIZE_MUST_BE_MULTIPLE_OF_CHUNK_MEMORY_ALIGNMENT); } } diff --git a/iceoryx_posh/test/integrationtests/test_shm_sigbus_handler.cpp b/iceoryx_posh/test/integrationtests/test_shm_sigbus_handler.cpp index 68c8dd803a..ae19ff066d 100644 --- a/iceoryx_posh/test/integrationtests/test_shm_sigbus_handler.cpp +++ b/iceoryx_posh/test/integrationtests/test_shm_sigbus_handler.cpp @@ -45,6 +45,7 @@ TEST(ShmCreatorDeathTest, AllocatingTooMuchMemoryLeadsToExitWithSIGBUS) TEST_SHM_NAME, iox::posix::AccessMode::READ_WRITE, iox::posix::OpenMode::PURGE_AND_CREATE); ASSERT_FALSE(badShmProvider.addMemoryBlock(&badmempools).has_error()); + GTEST_FLAG(death_test_style) = "threadsafe"; EXPECT_DEATH(IOX_DISCARD_RESULT(badShmProvider.create()), ".*"); } diff --git a/iceoryx_posh/test/moduletests/test_mepoo_memory_manager.cpp b/iceoryx_posh/test/moduletests/test_mepoo_memory_manager.cpp index 4a79a4f220..2b01a8e0b3 100644 --- a/iceoryx_posh/test/moduletests/test_mepoo_memory_manager.cpp +++ b/iceoryx_posh/test/moduletests/test_mepoo_memory_manager.cpp @@ -15,6 +15,8 @@ // // SPDX-License-Identifier: Apache-2.0 +#include "iceoryx_hoofs/error_handling/error_handling.hpp" +#include "iceoryx_hoofs/testing/fatal_failure.hpp" #include "iceoryx_hoofs/testing/mocks/logger_mock.hpp" #include "iceoryx_posh/internal/mepoo/memory_manager.hpp" #include "iceoryx_posh/mepoo/mepoo_config.hpp" @@ -24,6 +26,7 @@ namespace { using namespace ::testing; +using namespace iox::testing; using iox::mepoo::ChunkHeader; using iox::mepoo::ChunkSettings; @@ -472,7 +475,9 @@ TEST_F(MemoryManager_test, addMemPoolWithChunkCountZeroShouldFail) { ::testing::Test::RecordProperty("TEST_ID", "be653b65-a2d1-42eb-98b5-d161c6ba7c08"); mempoolconf.addMemPool({32, 0}); - EXPECT_DEATH({ sut->configureMemoryManager(mempoolconf, *allocator, *allocator); }, ".*"); + + IOX_EXPECT_FATAL_FAILURE([&] { sut->configureMemoryManager(mempoolconf, *allocator, *allocator); }, + iox::HoofsError::EXPECTS_ENSURES_FAILED); } TEST(MemoryManagerEnumString_test, asStringLiteralConvertsEnumValuesToStrings) diff --git a/iceoryx_posh/test/moduletests/test_mepoo_mempool.cpp b/iceoryx_posh/test/moduletests/test_mepoo_mempool.cpp index 9fb28596d7..7fa3e8eeeb 100644 --- a/iceoryx_posh/test/moduletests/test_mepoo_mempool.cpp +++ b/iceoryx_posh/test/moduletests/test_mepoo_mempool.cpp @@ -15,6 +15,9 @@ // // SPDX-License-Identifier: Apache-2.0 +#include "iceoryx_hoofs/error_handling/error_handling.hpp" +#include "iceoryx_hoofs/testing/fatal_failure.hpp" +#include "iceoryx_posh/error_handling/error_handling.hpp" #include "iceoryx_posh/internal/mepoo/mem_pool.hpp" #include "iox/bump_allocator.hpp" #include "test.hpp" @@ -23,6 +26,7 @@ namespace { using namespace ::testing; using namespace iox::mepoo; +using namespace iox::testing; class MemPool_test : public Test { @@ -90,16 +94,21 @@ TEST_F(MemPool_test, MempoolCtorWhenChunkSizeIsSmallerThanChunkMemoryAlignmentGe ::testing::Test::RecordProperty("TEST_ID", "52df897a-0847-476c-9d2f-99cb16432199"); constexpr uint32_t CHUNK_SIZE_SMALLER_THAN_MEMORY_ALIGNMENT = iox::mepoo::MemPool::CHUNK_MEMORY_ALIGNMENT - 1U; - EXPECT_DEATH( - { iox::mepoo::MemPool sut(CHUNK_SIZE_SMALLER_THAN_MEMORY_ALIGNMENT, NUMBER_OF_CHUNKS, allocator, allocator); }, - ".*"); + IOX_EXPECT_FATAL_FAILURE( + [&] { + iox::mepoo::MemPool sut(CHUNK_SIZE_SMALLER_THAN_MEMORY_ALIGNMENT, NUMBER_OF_CHUNKS, allocator, allocator); + }, + iox::HoofsError::EXPECTS_ENSURES_FAILED); } TEST_F(MemPool_test, MempoolCtorWhenNumberOfChunksIsZeroGetsTerminated) { ::testing::Test::RecordProperty("TEST_ID", "a5c43e1b-e5b5-4a69-8c4c-8b95752ade8e"); constexpr uint32_t INVALID_NUMBER_OF_CHUNKS = 0U; - EXPECT_DEATH({ iox::mepoo::MemPool sut(CHUNK_SIZE, INVALID_NUMBER_OF_CHUNKS, allocator, allocator); }, ".*"); + + IOX_EXPECT_FATAL_FAILURE( + [&] { iox::mepoo::MemPool sut(CHUNK_SIZE, INVALID_NUMBER_OF_CHUNKS, allocator, allocator); }, + iox::HoofsError::EXPECTS_ENSURES_FAILED); } TEST_F(MemPool_test, GetChunkMethodWhenAllTheChunksAreUsedReturnsNullPointer) { @@ -191,11 +200,12 @@ TEST_F(MemPool_test, FreeChunkMethodWhenSameChunkIsTriedToFreeTwiceReturnsError) TEST_F(MemPool_test, FreeChunkMethodWhenTheChunkIndexIsInvalidReturnsError) { ::testing::Test::RecordProperty("TEST_ID", "fb68953e-d47a-4658-8109-6b1974a8baab"); - std::vector chunks; + iox::vector chunks; constexpr uint32_t INVALID_INDEX{1U}; chunks.push_back(reinterpret_cast(sut.getChunk())); - EXPECT_DEATH({ sut.freeChunk(chunks[INVALID_INDEX]); }, ".*"); + IOX_EXPECT_FATAL_FAILURE([&] { sut.freeChunk(chunks[INVALID_INDEX]); }, + iox::HoofsError::EXPECTS_ENSURES_FAILED); } TEST_F(MemPool_test, GetMinFreeMethodReturnsTheNumberOfFreeChunks) @@ -212,13 +222,19 @@ TEST_F(MemPool_test, GetMinFreeMethodReturnsTheNumberOfFreeChunks) TEST_F(MemPool_test, dieWhenMempoolChunkSizeIsSmallerThan32Bytes) { ::testing::Test::RecordProperty("TEST_ID", "7704246e-42b5-46fd-8827-ebac200390e1"); - EXPECT_DEATH({ iox::mepoo::MemPool sut(12, 10, allocator, allocator); }, ".*"); + + IOX_EXPECT_FATAL_FAILURE( + [&] { iox::mepoo::MemPool sut(12, 10, allocator, allocator); }, + iox::PoshError::MEPOO__MEMPOOL_CHUNKSIZE_MUST_BE_MULTIPLE_OF_CHUNK_MEMORY_ALIGNMENT); } TEST_F(MemPool_test, dieWhenMempoolChunkSizeIsNotPowerOf32) { ::testing::Test::RecordProperty("TEST_ID", "6a354976-235a-4a94-8af2-2bc872f705f4"); - EXPECT_DEATH({ iox::mepoo::MemPool sut(333, 10, allocator, allocator); }, ".*"); + + IOX_EXPECT_FATAL_FAILURE( + [&] { iox::mepoo::MemPool sut(333, 10, allocator, allocator); }, + iox::PoshError::MEPOO__MEMPOOL_CHUNKSIZE_MUST_BE_MULTIPLE_OF_CHUNK_MEMORY_ALIGNMENT); } } // namespace diff --git a/iceoryx_posh/test/moduletests/test_posh_runtime.cpp b/iceoryx_posh/test/moduletests/test_posh_runtime.cpp index fd1fe0867b..fee7dc6d3b 100644 --- a/iceoryx_posh/test/moduletests/test_posh_runtime.cpp +++ b/iceoryx_posh/test/moduletests/test_posh_runtime.cpp @@ -140,6 +140,7 @@ TEST_F(PoshRuntime_test, NoAppName) ::testing::Test::RecordProperty("TEST_ID", "e053d114-c79c-4391-91e1-8fcfe90ee8e4"); const iox::RuntimeName_t invalidAppName(""); + GTEST_FLAG(death_test_style) = "threadsafe"; EXPECT_DEATH({ PoshRuntime::initRuntime(invalidAppName); }, ""); } @@ -161,6 +162,7 @@ TEST(PoshRuntime, RuntimeFailsWhenAppNameIsNotAFileName) { const iox::RuntimeName_t invalidAppName(iox::TruncateToCapacity, i); + GTEST_FLAG(death_test_style) = "threadsafe"; EXPECT_DEATH(PoshRuntime::initRuntime(invalidAppName), ".*"); } } @@ -171,6 +173,8 @@ TEST(PoshRuntime, RuntimeFailsWhenAppNameIsNotAFileName) TEST(PoshRuntime, AppNameEmpty) { ::testing::Test::RecordProperty("TEST_ID", "63900656-4fbb-466d-b6cc-f2139121092c"); + + GTEST_FLAG(death_test_style) = "threadsafe"; EXPECT_DEATH({ iox::runtime::PoshRuntime::getInstance(); }, ".*"); } @@ -1138,6 +1142,7 @@ TEST(PoshRuntimeFactory_test, SetEmptyRuntimeFactoryFails) auto mockRuntime = PoshRuntimeMock::create("hypnotoad"); // do not use the setRuntimeFactory in a test with a running RouDiEnvironment + GTEST_FLAG(death_test_style) = "threadsafe"; EXPECT_DEATH( { class FactoryAccess : public PoshRuntime diff --git a/iceoryx_posh/test/moduletests/test_runtime_ipc_interface_creator.cpp b/iceoryx_posh/test/moduletests/test_runtime_ipc_interface_creator.cpp index 72a85c89ac..8881d186d6 100644 --- a/iceoryx_posh/test/moduletests/test_runtime_ipc_interface_creator.cpp +++ b/iceoryx_posh/test/moduletests/test_runtime_ipc_interface_creator.cpp @@ -15,6 +15,8 @@ // SPDX-License-Identifier: Apache-2.0 #if !defined(_WIN32) +#include "iceoryx_hoofs/testing/fatal_failure.hpp" +#include "iceoryx_posh/error_handling/error_handling.hpp" #include "iceoryx_posh/internal/runtime/ipc_interface_creator.hpp" #include "test.hpp" @@ -27,6 +29,7 @@ using namespace ::testing; using namespace iox; using namespace iox::posix; using namespace iox::runtime; +using namespace iox::testing; constexpr char goodName[] = "channel_test"; constexpr char anotherGoodName[] = "horst"; @@ -67,7 +70,9 @@ TEST_F(IpcInterfaceCreator_test, CreateWithSameNameLeadsToError) { ::testing::Test::RecordProperty("TEST_ID", "2e8c15c8-1b7b-465b-aae5-6db24fc3c34a"); IpcInterfaceCreator m_sut{goodName}; - EXPECT_DEATH({ IpcInterfaceCreator m_sut2{goodName}; }, ".*"); + + IOX_EXPECT_FATAL_FAILURE([&] { IpcInterfaceCreator m_sut2{goodName}; }, + iox::PoshError::IPC_INTERFACE__APP_WITH_SAME_NAME_STILL_RUNNING); } } // namespace