Skip to content

Commit

Permalink
iox-eclipse-iceoryx#841 Remove workaround for mock due to GMock 1.8 l…
Browse files Browse the repository at this point in the history
…imitation
  • Loading branch information
elBoberido committed Jan 11, 2022
1 parent c622074 commit b02ba6b
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 166 deletions.
119 changes: 32 additions & 87 deletions iceoryx_posh/test/mocks/posh_runtime_mock.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,28 +45,37 @@ class PoshRuntimeMock : public iox::runtime::PoshRuntime
mockRuntime().reset();
}

/// @todo iox-#841 simplify this when we switch to gmock v1.10
MOCK_METHOD2(findServiceMock,
iox::cxx::expected<iox::runtime::ServiceContainer, iox::runtime::FindServiceError>(
const iox::cxx::variant<iox::runtime::Wildcard_t, iox::capro::IdString_t>,
const iox::cxx::variant<iox::runtime::Wildcard_t, iox::capro::IdString_t>));
MOCK_METHOD1(offerServiceMock, bool(const iox::capro::ServiceDescription&));
MOCK_METHOD1(stopOfferServiceMock, bool(const iox::capro::ServiceDescription&));
MOCK_METHOD3(getMiddlewarePublisherMock,
iox::PublisherPortUserType::MemberType_t*(const iox::capro::ServiceDescription&,
const iox::popo::PublisherOptions&,
const iox::runtime::PortConfigInfo&));
MOCK_METHOD3(getMiddlewareSubscriberMock,
iox::SubscriberPortUserType::MemberType_t*(const iox::capro::ServiceDescription&,
const iox::popo::SubscriberOptions&,
const iox::runtime::PortConfigInfo&));
MOCK_METHOD2(getMiddlewareInterfaceMock,
iox::popo::InterfacePortData*(const iox::capro::Interfaces, const iox::NodeName_t&));
MOCK_METHOD0(getMiddlewareApplicationMock, iox::popo::ApplicationPortData*());
MOCK_METHOD0(getMiddlewareConditionVariableMock, iox::popo::ConditionVariableData*());
MOCK_METHOD1(createNodeMock, iox::runtime::NodeData*(const iox::runtime::NodeProperty&));
MOCK_METHOD0(getServiceRegistryChangeCounterMock, const std::atomic<uint64_t>*());
MOCK_METHOD2(sendRequestToRouDiMock, bool(const iox::runtime::IpcMessage&, iox::runtime::IpcMessage&));
MOCK_METHOD((iox::cxx::expected<iox::runtime::ServiceContainer, iox::runtime::FindServiceError>),
findService,
((const iox::cxx::variant<iox::runtime::Wildcard_t, iox::capro::IdString_t>),
(const iox::cxx::variant<iox::runtime::Wildcard_t, iox::capro::IdString_t>)),
(noexcept, override));
MOCK_METHOD(bool, offerService, (const iox::capro::ServiceDescription&), (noexcept, override));
MOCK_METHOD(bool, stopOfferService, (const iox::capro::ServiceDescription&), (noexcept, override));
MOCK_METHOD(iox::PublisherPortUserType::MemberType_t*,
getMiddlewarePublisher,
(const iox::capro::ServiceDescription&,
const iox::popo::PublisherOptions&,
const iox::runtime::PortConfigInfo&),
(noexcept, override));
MOCK_METHOD(iox::SubscriberPortUserType::MemberType_t*,
getMiddlewareSubscriber,
(const iox::capro::ServiceDescription&,
const iox::popo::SubscriberOptions&,
const iox::runtime::PortConfigInfo&),
(noexcept, override));
MOCK_METHOD(iox::popo::InterfacePortData*,
getMiddlewareInterface,
(const iox::capro::Interfaces, const iox::NodeName_t&),
(noexcept, override));
MOCK_METHOD(iox::popo::ApplicationPortData*, getMiddlewareApplication, (), (noexcept, override));
MOCK_METHOD(iox::popo::ConditionVariableData*, getMiddlewareConditionVariable, (), (noexcept, override));
MOCK_METHOD(iox::runtime::NodeData*, createNode, (const iox::runtime::NodeProperty&), (noexcept, override));
MOCK_METHOD(const std::atomic<uint64_t>*, getServiceRegistryChangeCounter, (), (noexcept, override));
MOCK_METHOD(bool,
sendRequestToRouDi,
(const iox::runtime::IpcMessage&, iox::runtime::IpcMessage&),
(noexcept, override));

private:
PoshRuntimeMock(const iox::RuntimeName_t& name)
Expand All @@ -88,70 +97,6 @@ class PoshRuntimeMock : public iox::runtime::PoshRuntime
static iox::cxx::optional<PoshRuntimeMock*> runtime = iox::cxx::nullopt;
return runtime;
}

iox::cxx::expected<iox::runtime::ServiceContainer, iox::runtime::FindServiceError>
findService(const iox::cxx::variant<iox::runtime::Wildcard_t, iox::capro::IdString_t> service,
const iox::cxx::variant<iox::runtime::Wildcard_t, iox::capro::IdString_t> instance) noexcept override
{
return findServiceMock(service, instance);
}

bool offerService(const iox::capro::ServiceDescription& serviceDescription) noexcept override
{
return offerServiceMock(serviceDescription);
}

bool stopOfferService(const iox::capro::ServiceDescription& serviceDescription) noexcept override
{
return stopOfferServiceMock(serviceDescription);
}

iox::PublisherPortUserType::MemberType_t*
getMiddlewarePublisher(const iox::capro::ServiceDescription& service,
const iox::popo::PublisherOptions& publisherOptions = {},
const iox::runtime::PortConfigInfo& portConfigInfo = {}) noexcept override
{
return getMiddlewarePublisherMock(service, publisherOptions, portConfigInfo);
}

iox::SubscriberPortUserType::MemberType_t*
getMiddlewareSubscriber(const iox::capro::ServiceDescription& service,
const iox::popo::SubscriberOptions& subscriberOptions = {},
const iox::runtime::PortConfigInfo& portConfigInfo = {}) noexcept override
{
return getMiddlewareSubscriberMock(service, subscriberOptions, portConfigInfo);
}

iox::popo::InterfacePortData* getMiddlewareInterface(const iox::capro::Interfaces interface,
const iox::NodeName_t& nodeName = {}) noexcept override
{
return getMiddlewareInterfaceMock(interface, nodeName);
}

iox::popo::ApplicationPortData* getMiddlewareApplication() noexcept override
{
return getMiddlewareApplicationMock();
}

iox::popo::ConditionVariableData* getMiddlewareConditionVariable() noexcept override
{
return getMiddlewareConditionVariableMock();
}

iox::runtime::NodeData* createNode(const iox::runtime::NodeProperty& nodeProperty) noexcept override
{
return createNodeMock(nodeProperty);
}

const std::atomic<uint64_t>* getServiceRegistryChangeCounter() noexcept override
{
return getServiceRegistryChangeCounterMock();
}

bool sendRequestToRouDi(const iox::runtime::IpcMessage& msg, iox::runtime::IpcMessage& answer) noexcept override
{
return sendRequestToRouDiMock(msg, answer);
}
};

#endif // IOX_POSH_MOCKS_CHUNK_MOCK_HPP
33 changes: 5 additions & 28 deletions iceoryx_posh/test/mocks/roudi_memory_block_mock.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,33 +24,10 @@
class MemoryBlockMock final : public iox::roudi::MemoryBlock
{
public:
/// @note with gmock 1.8 it is not possible to mock functions with the noexcept specifier, therefore this
/// interesting mock implementation

uint64_t size() const noexcept override
{
return sizeMock();
}

uint64_t alignment() const noexcept override
{
return alignmentMock();
}

void onMemoryAvailable(iox::cxx::not_null<void*> memory) noexcept override
{
onMemoryAvailableMock(memory);
}

void destroy() noexcept override
{
destroyMock();
}

MOCK_CONST_METHOD0(sizeMock, uint64_t());
MOCK_CONST_METHOD0(alignmentMock, uint64_t());
MOCK_METHOD1(onMemoryAvailableMock, void(iox::cxx::not_null<void*>));
MOCK_METHOD0(destroyMock, void());
MOCK_METHOD(uint64_t, size, (), (const, noexcept, override));
MOCK_METHOD(uint64_t, alignment, (), (const, noexcept, override));
MOCK_METHOD(void, onMemoryAvailable, (iox::cxx::not_null<void*>), (noexcept, override));
MOCK_METHOD(void, destroy, (), (noexcept, override));
};

#endif // IOX_POSH_MOCKS_ROUDI_MEMORY_BLOCK_MOCK_HPP
6 changes: 3 additions & 3 deletions iceoryx_posh/test/mocks/roudi_memory_provider_mock.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,7 +49,7 @@ class MemoryProviderTestImpl : public iox::roudi::MemoryProvider
dummyMemory = static_cast<uint8_t*>(iox::cxx::alignedAlloc(alignment, size));
return iox::cxx::success<void*>(dummyMemory);
}
MOCK_METHOD2(createMemoryMock, void(uint64_t, uint64_t));
MOCK_METHOD(void, createMemoryMock, (uint64_t, uint64_t), (noexcept));

iox::cxx::expected<iox::roudi::MemoryProviderError> destroyMemory() noexcept override
{
Expand All @@ -63,7 +63,7 @@ class MemoryProviderTestImpl : public iox::roudi::MemoryProvider

return iox::cxx::success<void>();
}
MOCK_METHOD0(destroyMemoryMock, void());
MOCK_METHOD(void, destroyMemoryMock, (), (noexcept));

uint8_t* dummyMemory{nullptr};

Expand Down
8 changes: 2 additions & 6 deletions iceoryx_posh/test/moduletests/test_popo_sample.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,7 +48,7 @@ class MockPublisherInterface : public iox::popo::PublisherInterface<T, H>
auto s = std::move(sample); // this step is necessary since the mock method doesn't execute the move
return publishMock(std::move(s));
}
MOCK_METHOD1_T(publishMock, void(iox::popo::Sample<T, H>&&));
MOCK_METHOD(void, publishMock, ((iox::popo::Sample<T, H> &&)), (noexcept));
};

class SampleTest : public Test
Expand Down Expand Up @@ -125,10 +125,6 @@ TEST_F(SampleTest, CallingGetUserHeaderFromNonConstTypeReturnsCorrectAddress)

auto sut = iox::popo::Sample<DummyData, DummyHeader>(std::move(testSamplePtr), mockPublisherInterface);

// this line is actually not needed for the tests but if it is not present gmock raises a unused-function warning in
// gmock-spec-builders.h which breaks the build with -Werror
EXPECT_CALL(mockPublisherInterface, publishMock).Times(0);

// ===== Test ===== //
auto& userHeader = sut.getUserHeader();

Expand Down
6 changes: 3 additions & 3 deletions iceoryx_posh/test/moduletests/test_roudi_memory_block.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019, 2021 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,8 +33,8 @@ class MemoryBlock_Test : public Test
public:
void SetUp() override
{
EXPECT_CALL(sut, sizeMock()).WillRepeatedly(Return(MEMORY_SIZE));
EXPECT_CALL(sut, alignmentMock()).WillRepeatedly(Return(MEMORY_ALIGNMENT));
EXPECT_CALL(sut, size()).WillRepeatedly(Return(MEMORY_SIZE));
EXPECT_CALL(sut, alignment()).WillRepeatedly(Return(MEMORY_ALIGNMENT));
}

void TearDown() override
Expand Down
28 changes: 14 additions & 14 deletions iceoryx_posh/test/moduletests/test_roudi_memory_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2020, 2021 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,12 +78,12 @@ TEST_F(RouDiMemoryManager_Test, CallingCreateMemoryWithMemoryProviderSucceeds)
uint64_t MEMORY_ALIGNMENT_1{8};
uint64_t MEMORY_SIZE_2{32};
uint64_t MEMORY_ALIGNMENT_2{16};
EXPECT_CALL(memoryBlock1, sizeMock()).WillRepeatedly(Return(MEMORY_SIZE_1));
EXPECT_CALL(memoryBlock1, alignmentMock()).WillRepeatedly(Return(MEMORY_ALIGNMENT_1));
EXPECT_CALL(memoryBlock1, onMemoryAvailableMock(_));
EXPECT_CALL(memoryBlock2, sizeMock()).WillRepeatedly(Return(MEMORY_SIZE_2));
EXPECT_CALL(memoryBlock2, alignmentMock()).WillRepeatedly(Return(MEMORY_ALIGNMENT_2));
EXPECT_CALL(memoryBlock2, onMemoryAvailableMock(_));
EXPECT_CALL(memoryBlock1, size()).WillRepeatedly(Return(MEMORY_SIZE_1));
EXPECT_CALL(memoryBlock1, alignment()).WillRepeatedly(Return(MEMORY_ALIGNMENT_1));
EXPECT_CALL(memoryBlock1, onMemoryAvailable(_));
EXPECT_CALL(memoryBlock2, size()).WillRepeatedly(Return(MEMORY_SIZE_2));
EXPECT_CALL(memoryBlock2, alignment()).WillRepeatedly(Return(MEMORY_ALIGNMENT_2));
EXPECT_CALL(memoryBlock2, onMemoryAvailable(_));

IOX_DISCARD_RESULT(memoryProvider1.addMemoryBlock(&memoryBlock1));
IOX_DISCARD_RESULT(memoryProvider2.addMemoryBlock(&memoryBlock2));
Expand All @@ -93,8 +93,8 @@ TEST_F(RouDiMemoryManager_Test, CallingCreateMemoryWithMemoryProviderSucceeds)

EXPECT_THAT(sut.createAndAnnounceMemory().has_error(), Eq(false));

EXPECT_CALL(memoryBlock1, destroyMock());
EXPECT_CALL(memoryBlock2, destroyMock());
EXPECT_CALL(memoryBlock1, destroy());
EXPECT_CALL(memoryBlock2, destroy());
}

TEST_F(RouDiMemoryManager_Test, CallingCreateMemoryWithMemoryProviderError)
Expand All @@ -112,19 +112,19 @@ TEST_F(RouDiMemoryManager_Test, RouDiMemoryManagerDTorTriggersMemoryProviderDest
{
uint64_t MEMORY_SIZE_1{16};
uint64_t MEMORY_ALIGNMENT_1{8};
EXPECT_CALL(memoryBlock1, sizeMock()).WillRepeatedly(Return(MEMORY_SIZE_1));
EXPECT_CALL(memoryBlock1, alignmentMock()).WillRepeatedly(Return(MEMORY_ALIGNMENT_1));
EXPECT_CALL(memoryBlock1, onMemoryAvailableMock(_));
EXPECT_CALL(memoryBlock1, size()).WillRepeatedly(Return(MEMORY_SIZE_1));
EXPECT_CALL(memoryBlock1, alignment()).WillRepeatedly(Return(MEMORY_ALIGNMENT_1));
EXPECT_CALL(memoryBlock1, onMemoryAvailable(_));

IOX_DISCARD_RESULT(memoryProvider1.addMemoryBlock(&memoryBlock1));

{
RouDiMemoryManager sutDestroy;
ASSERT_FALSE(sutDestroy.addMemoryProvider(&memoryProvider1).has_error());
ASSERT_FALSE(sutDestroy.createAndAnnounceMemory().has_error());
EXPECT_CALL(memoryBlock1, destroyMock()).Times(1);
EXPECT_CALL(memoryBlock1, destroy()).Times(1);
}
EXPECT_CALL(memoryBlock1, destroyMock()).Times(0);
EXPECT_CALL(memoryBlock1, destroy()).Times(0);
}

TEST_F(RouDiMemoryManager_Test, AddMemoryProviderExceedsCapacity)
Expand Down
32 changes: 16 additions & 16 deletions iceoryx_posh/test/moduletests/test_roudi_memory_provider.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019, 2021 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -69,12 +69,12 @@ class MemoryProvider_Test : public Test
iox::cxx::expected<MemoryProviderError> commonSetup()
{
EXPECT_FALSE(sut.addMemoryBlock(&memoryBlock1).has_error());
EXPECT_CALL(memoryBlock1, sizeMock()).WillRepeatedly(Return(COMMON_SETUP_MEMORY_SIZE));
EXPECT_CALL(memoryBlock1, alignmentMock()).WillRepeatedly(Return(COMMON_SETUP_MEMORY_ALIGNMENT));
EXPECT_CALL(memoryBlock1, size()).WillRepeatedly(Return(COMMON_SETUP_MEMORY_SIZE));
EXPECT_CALL(memoryBlock1, alignment()).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());
EXPECT_CALL(memoryBlock1, destroy());

return sut.create();
}
Expand Down Expand Up @@ -178,8 +178,8 @@ TEST_F(MemoryProvider_Test, CreationFailed)
ASSERT_FALSE(sutFailure.addMemoryBlock(&memoryBlock1).has_error());
uint64_t MEMORY_SIZE{16};
uint64_t MEMORY_ALIGNMENT{8};
EXPECT_CALL(memoryBlock1, sizeMock()).WillRepeatedly(Return(MEMORY_SIZE));
EXPECT_CALL(memoryBlock1, alignmentMock()).WillRepeatedly(Return(MEMORY_ALIGNMENT));
EXPECT_CALL(memoryBlock1, size()).WillRepeatedly(Return(MEMORY_SIZE));
EXPECT_CALL(memoryBlock1, alignment()).WillRepeatedly(Return(MEMORY_ALIGNMENT));

auto expectError = sutFailure.create();
ASSERT_THAT(expectError.has_error(), Eq(true));
Expand All @@ -193,7 +193,7 @@ TEST_F(MemoryProvider_Test, CreateAndAnnounceWithOneMemoryBlock)
{
ASSERT_FALSE(commonSetup().has_error());

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

EXPECT_THAT(sut.isAvailableAnnounced(), Eq(true));
Expand All @@ -207,23 +207,23 @@ TEST_F(MemoryProvider_Test, CreateAndAnnounceWithMultipleMemoryBlocks)
uint64_t MEMORY_ALIGNMENT_1{8};
uint64_t MEMORY_SIZE_2{32};
uint64_t MEMORY_ALIGNMENT_2{16};
EXPECT_CALL(memoryBlock1, sizeMock()).WillRepeatedly(Return(MEMORY_SIZE_1));
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(memoryBlock1, size()).WillRepeatedly(Return(MEMORY_SIZE_1));
EXPECT_CALL(memoryBlock1, alignment()).WillRepeatedly(Return(MEMORY_ALIGNMENT_1));
EXPECT_CALL(memoryBlock2, size()).WillRepeatedly(Return(MEMORY_SIZE_2));
EXPECT_CALL(memoryBlock2, alignment()).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, onMemoryAvailableMock(_)).Times(1);
EXPECT_CALL(memoryBlock2, onMemoryAvailableMock(_)).Times(1);
EXPECT_CALL(memoryBlock1, onMemoryAvailable(_)).Times(1);
EXPECT_CALL(memoryBlock2, onMemoryAvailable(_)).Times(1);
sut.announceMemoryAvailable();

EXPECT_THAT(sut.isAvailableAnnounced(), Eq(true));

EXPECT_CALL(sut, destroyMemoryMock());
EXPECT_CALL(memoryBlock1, destroyMock());
EXPECT_CALL(memoryBlock2, destroyMock());
EXPECT_CALL(memoryBlock1, destroy());
EXPECT_CALL(memoryBlock2, destroy());
}

TEST_F(MemoryProvider_Test, AddMemoryBlockAfterCreation)
Expand All @@ -248,7 +248,7 @@ TEST_F(MemoryProvider_Test, MultipleAnnouncesAreSuppressed)
{
ASSERT_FALSE(commonSetup().has_error());

EXPECT_CALL(memoryBlock1, onMemoryAvailableMock(_)).Times(1);
EXPECT_CALL(memoryBlock1, onMemoryAvailable(_)).Times(1);
sut.announceMemoryAvailable();
sut.announceMemoryAvailable(); // this shouldn't trigger a second memoryAvailable call on memoryBlock1

Expand Down
Loading

0 comments on commit b02ba6b

Please sign in to comment.