Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1065 Introduce reference discovery for tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Killat <[email protected]>
  • Loading branch information
MatthiasKillat committed Feb 24, 2022
1 parent 7faa895 commit d64a3fd
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 5 deletions.
5 changes: 0 additions & 5 deletions iceoryx_posh/include/iceoryx_posh/iceoryx_posh_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ constexpr const char SERVICE_DISCOVERY_SERVICE_NAME[] = "ServiceDiscovery";
constexpr const char SERVICE_DISCOVERY_INSTANCE_NAME[] = "RouDi_ID";
constexpr const char SERVICE_DISCOVERY_EVENT_NAME[] = "ServiceRegistry";

namespace runtime
{
using ServiceContainer = iox::cxx::vector<capro::ServiceDescription, MAX_FINDSERVICE_RESULT_SIZE>;
}

// Nodes
constexpr uint32_t MAX_NODE_NUMBER = 1000U;
constexpr uint32_t MAX_NODE_PER_PROCESS = 50U;
Expand Down
135 changes: 135 additions & 0 deletions iceoryx_posh/test/integrationtests/test_service_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,4 +718,139 @@ TEST_F(ServiceDiscoveryPubSub_test, ServiceDiscoveryNotifiedbyListenerFindsSingl
EXPECT_THAT(serviceContainer.front(), Eq(serviceDescriptionToSearchFor));
}

struct Comparator
{
bool operator()(const ServiceDescription& a, const ServiceDescription& b)
{
if (a.getServiceIDString() < b.getServiceIDString())
{
return true;
}
if (b.getServiceIDString() < a.getServiceIDString())
{
return false;
}

if (a.getInstanceIDString() < b.getInstanceIDString())
{
return true;
}
if (b.getInstanceIDString() < a.getInstanceIDString())
{
return false;
}

if (a.getEventIDString() < b.getEventIDString())
{
return true;
}
return false;
}
};

bool sortAndCompare(ServiceContainer& a, ServiceContainer& b)
{
std::sort(a.begin(), a.end(), Comparator());
std::sort(b.begin(), b.end(), Comparator());
return a == b;
}

struct ReferenceDiscovery
{
ServiceContainer services;

ReferenceDiscovery()
{
services.emplace_back(iox::roudi::IntrospectionMempoolService);
services.emplace_back(iox::roudi::IntrospectionPortService);
services.emplace_back(iox::roudi::IntrospectionPortThroughputService);
services.emplace_back(iox::roudi::IntrospectionSubscriberPortChangingDataService);
services.emplace_back(iox::roudi::IntrospectionProcessService);
services.emplace_back(iox::SERVICE_DISCOVERY_SERVICE_NAME,
iox::SERVICE_DISCOVERY_INSTANCE_NAME,
iox::SERVICE_DISCOVERY_EVENT_NAME);
}

void add(const ServiceDescription& s)
{
services.emplace_back(s);
}

// brute force reference implementation
ServiceContainer findService(const optional<IdString_t>& service,
const optional<IdString_t>& instance,
const optional<IdString_t>& event)
{
ServiceContainer result;
for (auto& s : services)
{
bool match = (service) ? (s.getServiceIDString() == *service) : true;
match &= (instance) ? (s.getInstanceIDString() == *instance) : true;
match &= (event) ? (s.getEventIDString() == *event) : true;

if (match)
{
result.emplace_back(s);
}
}
return result;
}
};

#include <functional>

// generalize for REQ-RES
class ReferenceServiceDiscovery_test : public ServiceDiscoveryPubSub_test
{
public:
// std::vector<iox::popo::UntypedPublisher> publishers;
iox::cxx::vector<iox::popo::UntypedPublisher, 1024> publishers;
ReferenceDiscovery referenceDiscovery;
ServiceContainer expectedResult;

// variation point
std::function<void(optional<IdString_t>& service, optional<IdString_t>& instance, optional<IdString_t>& event)>
modifySearchArgs =
[](optional<IdString_t>&, optional<IdString_t>& instance, optional<IdString_t>&) { instance.reset(); };

void add(const ServiceDescription& s)
{
// does not work with std::vector, but probably with std::array
// probably because vector grows dynamically and may require copying (though the error is about move)
publishers.emplace_back(s);
referenceDiscovery.add(s);
}

void findService(const optional<IdString_t>& service,
const optional<IdString_t>& instance,
const optional<IdString_t>& event) noexcept
{
optional<IdString_t> s(service);
optional<IdString_t> i(instance);
optional<IdString_t> e(event);

modifySearchArgs(s, i, e);

ServiceDiscoveryPubSub_test::findService(s, i, e, MessagingPattern::PUB_SUB);
expectedResult = referenceDiscovery.findService(s, i, e);
EXPECT_TRUE(sortAndCompare(serviceContainer, expectedResult));
}

void clear()
{
expectedResult.clear();
}
};

TEST_F(ReferenceServiceDiscovery_test, FindInternalServices)
{
findService(iox::capro::Wildcard, iox::capro::Wildcard, iox::capro::Wildcard);
}

TEST_F(ReferenceServiceDiscovery_test, FindSingleService)
{
add({"a", "b", "c"});
findService({"a"}, {"b"}, {"c"});
}

} // namespace

0 comments on commit d64a3fd

Please sign in to comment.