From f82ab5f60ab002e56903f5ff34ffd78beadee89a Mon Sep 17 00:00:00 2001 From: Jimmy Belloche Date: Thu, 7 Jan 2021 16:09:20 +0100 Subject: [PATCH] iox-#454 add removed code and remove added method getErrorString Signed-off-by: Jimmy Belloche --- .../test/mocks/roudi_memory_provider_mock.hpp | 13 ++++++++-- .../moduletests/test_roudi_memory_manager.cpp | 6 ++++- .../test_roudi_memory_provider.cpp | 25 +++++++++++++------ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/iceoryx_posh/test/mocks/roudi_memory_provider_mock.hpp b/iceoryx_posh/test/mocks/roudi_memory_provider_mock.hpp index 3dbb6779ec..64af692799 100644 --- a/iceoryx_posh/test/mocks/roudi_memory_provider_mock.hpp +++ b/iceoryx_posh/test/mocks/roudi_memory_provider_mock.hpp @@ -35,21 +35,30 @@ class MemoryProviderTestImpl : public iox::roudi::MemoryProvider iox::cxx::expected createMemory(const uint64_t size, const uint64_t alignment) noexcept override { - + if (m_mockCallsEnabled) + { + createMemoryMock(size, alignment); + } dummyMemory = static_cast(iox::cxx::alignedAlloc(alignment, size)); return iox::cxx::success(dummyMemory); } + MOCK_METHOD2(createMemoryMock, void(uint64_t, uint64_t)); iox::cxx::expected destroyMemory() noexcept override { + if (m_mockCallsEnabled) + { + destroyMemoryMock(); + } iox::cxx::alignedFree(dummyMemory); dummyMemory = nullptr; return iox::cxx::success(); } - + MOCK_METHOD0(destroyMemoryMock, void()); + uint8_t* dummyMemory{nullptr}; protected: diff --git a/iceoryx_posh/test/moduletests/test_roudi_memory_manager.cpp b/iceoryx_posh/test/moduletests/test_roudi_memory_manager.cpp index 268fcd128a..e5861e4976 100644 --- a/iceoryx_posh/test/moduletests/test_roudi_memory_manager.cpp +++ b/iceoryx_posh/test/moduletests/test_roudi_memory_manager.cpp @@ -37,7 +37,7 @@ class RouDiMemoryManager_Test : public Test } static const int32_t nbTestCase = 4; - + RouDiMemoryManagerError m_testCombinationRoudiMemoryManagerError[nbTestCase] = { RouDiMemoryManagerError::MEMORY_PROVIDER_EXHAUSTED, @@ -91,6 +91,8 @@ TEST_F(RouDiMemoryManager_Test, CallingCreateMemoryWithMemoryProviderSucceeds) sut.addMemoryProvider(&memoryProvider1); sut.addMemoryProvider(&memoryProvider2); + EXPECT_CALL(memoryBlock1, destroyMock()); + EXPECT_CALL(memoryBlock2, destroyMock()); EXPECT_THAT(sut.createAndAnnounceMemory().has_error(), Eq(false)); } @@ -119,7 +121,9 @@ TEST_F(RouDiMemoryManager_Test, RouDiMemoryManagerDTorTriggersMemoryProviderDest RouDiMemoryManager sutDestroy; sutDestroy.addMemoryProvider(&memoryProvider1); sutDestroy.createAndAnnounceMemory(); + EXPECT_CALL(memoryBlock1, destroyMock()).Times(1); } + EXPECT_CALL(memoryBlock1, destroyMock()).Times(0); } TEST_F(RouDiMemoryManager_Test, AddMemoryProviderExceedsCapacity) diff --git a/iceoryx_posh/test/moduletests/test_roudi_memory_provider.cpp b/iceoryx_posh/test/moduletests/test_roudi_memory_provider.cpp index 5396551657..ead3ccad92 100644 --- a/iceoryx_posh/test/moduletests/test_roudi_memory_provider.cpp +++ b/iceoryx_posh/test/moduletests/test_roudi_memory_provider.cpp @@ -28,6 +28,9 @@ using namespace iox::roudi; class MemoryProviderFailingCreation : public iox::roudi::MemoryProvider { public: + + using MemoryProvider::getErrorString; + iox::cxx::expected createMemory(const uint64_t size [[gnu::unused]], const uint64_t alignment [[gnu::unused]]) noexcept override { @@ -38,13 +41,6 @@ class MemoryProviderFailingCreation : public iox::roudi::MemoryProvider { return iox::cxx::error(MemoryProviderError::MEMORY_DESTRUCTION_FAILED); } - - // As static function from Memoryprovider is protected, we need another method to call it in order to use it in test - static const char* getErrorStringMemoryProvider(const MemoryProviderError error) - { - return MemoryProvider::getErrorString(error); - } - }; class MemoryProvider_Test : public Test @@ -72,6 +68,9 @@ class MemoryProvider_Test : public Test sut.addMemoryBlock(&memoryBlock1); EXPECT_CALL(memoryBlock1, sizeMock()).WillRepeatedly(Return(COMMON_SETUP_MEMORY_SIZE)); EXPECT_CALL(memoryBlock1, alignmentMock()).WillRepeatedly(Return(COMMON_SETUP_MEMORY_ALIGNMENT)); + EXPECT_CALL(sut, createMemoryMock(COMMON_SETUP_MEMORY_SIZE, COMMON_SETUP_MEMORY_ALIGNMENT)).Times(1); + + EXPECT_CALL(sut, destroyMemoryMock()); EXPECT_CALL(memoryBlock1, destroyMock()); return sut.create(); @@ -157,6 +156,7 @@ TEST_F(MemoryProvider_Test, AddMemoryBlockExceedsCapacity) TEST_F(MemoryProvider_Test, CreateWithoutMemoryBlock) { + EXPECT_CALL(sut, createMemoryMock(_, _)).Times(0); auto expectError = sut.create(); ASSERT_THAT(expectError.has_error(), Eq(true)); EXPECT_THAT(expectError.get_error(), Eq(MemoryProviderError::NO_MEMORY_BLOCKS_PRESENT)); @@ -196,6 +196,7 @@ TEST_F(MemoryProvider_Test, CreateAndAnnounceWithOneMemoryBlock) { commonSetup(); + EXPECT_CALL(memoryBlock1, memoryAvailableMock(_)).Times(1); sut.announceMemoryAvailable(); EXPECT_THAT(sut.isAvailableAnnounced(), Eq(true)); @@ -213,13 +214,20 @@ TEST_F(MemoryProvider_Test, CreateAndAnnounceWithMultipleMemoryBlocks) EXPECT_CALL(memoryBlock1, alignmentMock()).WillRepeatedly(Return(MEMORY_ALIGNMENT_1)); EXPECT_CALL(memoryBlock2, sizeMock()).WillRepeatedly(Return(MEMORY_SIZE_2)); EXPECT_CALL(memoryBlock2, alignmentMock()).WillRepeatedly(Return(MEMORY_ALIGNMENT_2)); + EXPECT_CALL(sut, createMemoryMock(MEMORY_SIZE_1 + MEMORY_SIZE_2, std::max(MEMORY_ALIGNMENT_1, MEMORY_ALIGNMENT_2))) + .Times(1); EXPECT_THAT(sut.create().has_error(), Eq(false)); + EXPECT_CALL(memoryBlock1, memoryAvailableMock(_)).Times(1); + EXPECT_CALL(memoryBlock2, memoryAvailableMock(_)).Times(1); sut.announceMemoryAvailable(); EXPECT_THAT(sut.isAvailableAnnounced(), Eq(true)); + EXPECT_CALL(sut, destroyMemoryMock()); + EXPECT_CALL(memoryBlock1, destroyMock()); + EXPECT_CALL(memoryBlock2, destroyMock()); } TEST_F(MemoryProvider_Test, AddMemoryBlockAfterCreation) @@ -244,6 +252,7 @@ TEST_F(MemoryProvider_Test, MultipleAnnouncesAreSuppressed) { commonSetup(); + EXPECT_CALL(memoryBlock1, memoryAvailableMock(_)).Times(1); sut.announceMemoryAvailable(); sut.announceMemoryAvailable(); // this shouldn't trigger a second memoryAvailable call on memoryBlock1 @@ -338,7 +347,7 @@ TEST_F(MemoryProvider_Test, GetErrorString) { for(int16_t i = 0; i < nTestCase; i++) { - const char * result = MemoryProviderFailingCreation::getErrorStringMemoryProvider(m_testCombinationMemoryProviderError[i]); + const char * result = MemoryProviderFailingCreation::getErrorString(m_testCombinationMemoryProviderError[i]); EXPECT_THAT(*result, Eq(*m_testResultGetErrorString[i])); } }