Skip to content

Commit

Permalink
iox-eclipse-iceoryx#454 add removed code and remove added method getE…
Browse files Browse the repository at this point in the history
…rrorString

Signed-off-by: Jimmy Belloche <[email protected]>
  • Loading branch information
JimmyBch committed Jan 8, 2021
1 parent 8cb7cab commit f82ab5f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
13 changes: 11 additions & 2 deletions iceoryx_posh/test/mocks/roudi_memory_provider_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,30 @@ class MemoryProviderTestImpl : public iox::roudi::MemoryProvider
iox::cxx::expected<void*, iox::roudi::MemoryProviderError> createMemory(const uint64_t size,
const uint64_t alignment) noexcept override
{

if (m_mockCallsEnabled)
{
createMemoryMock(size, alignment);
}

dummyMemory = static_cast<uint8_t*>(iox::cxx::alignedAlloc(alignment, size));
return iox::cxx::success<void*>(dummyMemory);
}
MOCK_METHOD2(createMemoryMock, void(uint64_t, uint64_t));

iox::cxx::expected<iox::roudi::MemoryProviderError> destroyMemory() noexcept override
{
if (m_mockCallsEnabled)
{
destroyMemoryMock();
}

iox::cxx::alignedFree(dummyMemory);
dummyMemory = nullptr;

return iox::cxx::success<void>();
}

MOCK_METHOD0(destroyMemoryMock, void());

uint8_t* dummyMemory{nullptr};

protected:
Expand Down
6 changes: 5 additions & 1 deletion iceoryx_posh/test/moduletests/test_roudi_memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RouDiMemoryManager_Test : public Test
}

static const int32_t nbTestCase = 4;

RouDiMemoryManagerError m_testCombinationRoudiMemoryManagerError[nbTestCase] =
{
RouDiMemoryManagerError::MEMORY_PROVIDER_EXHAUSTED,
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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)
Expand Down
25 changes: 17 additions & 8 deletions iceoryx_posh/test/moduletests/test_roudi_memory_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ using namespace iox::roudi;
class MemoryProviderFailingCreation : public iox::roudi::MemoryProvider
{
public:

using MemoryProvider::getErrorString;

iox::cxx::expected<void*, MemoryProviderError>
createMemory(const uint64_t size [[gnu::unused]], const uint64_t alignment [[gnu::unused]]) noexcept override
{
Expand All @@ -38,13 +41,6 @@ class MemoryProviderFailingCreation : public iox::roudi::MemoryProvider
{
return iox::cxx::error<MemoryProviderError>(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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -196,6 +196,7 @@ TEST_F(MemoryProvider_Test, CreateAndAnnounceWithOneMemoryBlock)
{
commonSetup();

EXPECT_CALL(memoryBlock1, memoryAvailableMock(_)).Times(1);
sut.announceMemoryAvailable();

EXPECT_THAT(sut.isAvailableAnnounced(), Eq(true));
Expand All @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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]));
}
}

0 comments on commit f82ab5f

Please sign in to comment.