Skip to content

Commit

Permalink
Avoid using fixed ports with adapter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone committed Oct 16, 2024
1 parent c7d1db6 commit c907e6d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
42 changes: 27 additions & 15 deletions cpp/test/Ice/adapterDeactivation/AllTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,21 @@ allTests(Test::TestHelper* helper)
cout << "ok" << endl;
}

int firstPort = helper->getTestPort(3);
int secondPort = helper->getTestPort(4);
cout << "testing object adapter published endpoints... " << flush;
{
communicator->getProperties()->setProperty("PAdapter.PublishedEndpoints", "tcp -h localhost -p 12345 -t 30000");
communicator->getProperties()->setProperty(
"PAdapter.PublishedEndpoints",
"tcp -h localhost -p " + to_string(firstPort) + " -t 30000");
Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapter("PAdapter");
test(adapter->getPublishedEndpoints().size() == 1);
Ice::EndpointPtr endpt = adapter->getPublishedEndpoints()[0];
test(endpt->toString() == "tcp -h localhost -p 12345 -t 30000");
Ice::ObjectPrx prx(communicator, "dummy:tcp -h localhost -p 12346 -t 20000:tcp -h localhost -p 12347 -t 10000");
test(endpt->toString() == "tcp -h localhost -p " + to_string(firstPort) + " -t 30000");
Ice::ObjectPrx prx(
communicator,
"dummy:tcp -h localhost -p " + to_string(firstPort) + " -t 20000:tcp -h localhost -p " +
to_string(secondPort) + " -t 10000");
adapter->setPublishedEndpoints(prx->ice_getEndpoints());
test(adapter->getPublishedEndpoints().size() == 2);
Ice::Identity id;
Expand Down Expand Up @@ -135,7 +142,7 @@ allTests(Test::TestHelper* helper)
// Two loopback endpoints with different ports
communicator->getProperties()->setProperty(
"PHAdapter.Endpoints",
"default -h 127.0.0.1 -p 12345:default -h 127.0.0.1");
"default -h 127.0.0.1 -p " + to_string(firstPort) + ":default -h 127.0.0.1");

communicator->getProperties()->setProperty("PHAdapter.PublishedHost", "");
{
Expand All @@ -146,21 +153,23 @@ allTests(Test::TestHelper* helper)
dynamic_pointer_cast<IPEndpointInfo>(getUnderlying(publishedEndpoints[0]->getInfo()));
auto ipEndpointInfo1 =
dynamic_pointer_cast<IPEndpointInfo>(getUnderlying(publishedEndpoints[1]->getInfo()));
test(ipEndpointInfo0 && ipEndpointInfo0->host == "127.0.0.1" && ipEndpointInfo0->port == 12345);
test(ipEndpointInfo1 && ipEndpointInfo1->host == "127.0.0.1" && ipEndpointInfo1->port != 12345);
test(ipEndpointInfo0 && ipEndpointInfo0->host == "127.0.0.1" && ipEndpointInfo0->port == firstPort);
test(ipEndpointInfo1 && ipEndpointInfo1->host == "127.0.0.1" && ipEndpointInfo1->port != firstPort);
adapter->destroy();
}

// Two endpoints - one loopback, one not loopback
communicator->getProperties()->setProperty("PHAdapter.Endpoints", "default -h 127.0.0.1 -p 12345:default -h *");
communicator->getProperties()->setProperty(
"PHAdapter.Endpoints",
"default -h 127.0.0.1 -p " + to_string(firstPort) + ":default -h *");

communicator->getProperties()->setProperty("PHAdapter.PublishedHost", "");
{
ObjectAdapterPtr adapter = communicator->createObjectAdapter("PHAdapter");
auto publishedEndpoints = adapter->getPublishedEndpoints();
test(publishedEndpoints.size() == 1); // loopback filtered out
auto ipEndpointInfo = dynamic_pointer_cast<IPEndpointInfo>(getUnderlying(publishedEndpoints[0]->getInfo()));
test(ipEndpointInfo && !ipEndpointInfo->host.empty() && ipEndpointInfo->port != 12345);
test(ipEndpointInfo && !ipEndpointInfo->host.empty() && ipEndpointInfo->port != firstPort);
adapter->destroy();
}

Expand All @@ -170,12 +179,14 @@ allTests(Test::TestHelper* helper)
auto publishedEndpoints = adapter->getPublishedEndpoints();
test(publishedEndpoints.size() == 1); // loopback filtered out
auto ipEndpointInfo = dynamic_pointer_cast<IPEndpointInfo>(getUnderlying(publishedEndpoints[0]->getInfo()));
test(ipEndpointInfo && ipEndpointInfo->host == "test.zeroc.com" && ipEndpointInfo->port != 12345);
test(ipEndpointInfo && ipEndpointInfo->host == "test.zeroc.com" && ipEndpointInfo->port != firstPort);
adapter->destroy();
}

// Two non-loopback endpoints
communicator->getProperties()->setProperty("PHAdapter.Endpoints", "tcp -h * -p 12345:default -h *");
communicator->getProperties()->setProperty(
"PHAdapter.Endpoints",
"tcp -h * -p " + to_string(firstPort) +":default -h *");

communicator->getProperties()->setProperty("PHAdapter.PublishedHost", "");
{
Expand All @@ -186,8 +197,8 @@ allTests(Test::TestHelper* helper)
dynamic_pointer_cast<IPEndpointInfo>(getUnderlying(publishedEndpoints[0]->getInfo()));
auto ipEndpointInfo1 =
dynamic_pointer_cast<IPEndpointInfo>(getUnderlying(publishedEndpoints[1]->getInfo()));
test(ipEndpointInfo0 && !ipEndpointInfo0->host.empty() && ipEndpointInfo0->port == 12345);
test(ipEndpointInfo1 && !ipEndpointInfo1->host.empty() && ipEndpointInfo1->port != 12345);
test(ipEndpointInfo0 && !ipEndpointInfo0->host.empty() && ipEndpointInfo0->port == firstPort);
test(ipEndpointInfo1 && !ipEndpointInfo1->host.empty() && ipEndpointInfo1->port != firstPort);
adapter->destroy();
}

Expand All @@ -200,8 +211,8 @@ allTests(Test::TestHelper* helper)
dynamic_pointer_cast<IPEndpointInfo>(getUnderlying(publishedEndpoints[0]->getInfo()));
auto ipEndpointInfo1 =
dynamic_pointer_cast<IPEndpointInfo>(getUnderlying(publishedEndpoints[1]->getInfo()));
test(ipEndpointInfo0 && ipEndpointInfo0->host == "test.zeroc.com" && ipEndpointInfo0->port == 12345);
test(ipEndpointInfo1 && ipEndpointInfo1->host == "test.zeroc.com" && ipEndpointInfo1->port != 12345);
test(ipEndpointInfo0 && ipEndpointInfo0->host == "test.zeroc.com" && ipEndpointInfo0->port == firstPort);
test(ipEndpointInfo1 && ipEndpointInfo1->host == "test.zeroc.com" && ipEndpointInfo1->port != firstPort);
adapter->destroy();
}
}
Expand Down Expand Up @@ -249,12 +260,13 @@ allTests(Test::TestHelper* helper)

cout << "testing object adapter with router... " << flush;
{
int routerPort = helper->getTestPort(5);
Ice::Identity routerId;
routerId.name = "router";
auto router = obj->ice_identity<Ice::RouterPrx>(routerId)->ice_connectionId("rc");
Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapterWithRouter("", router);
test(adapter->getPublishedEndpoints().size() == 1);
test(adapter->getPublishedEndpoints()[0]->toString() == "tcp -h localhost -p 23456 -t 30000");
test(adapter->getPublishedEndpoints()[0]->toString() == "tcp -h localhost -p " + to_string(routerPort) + " -t 30000");
try
{
adapter->setPublishedEndpoints(router->ice_getEndpoints());
Expand Down
2 changes: 1 addition & 1 deletion cpp/test/Ice/adapterDeactivation/Collocated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Collocated::run(int argc, char** argv)
communicator->getProperties()->setProperty("TestAdapter.ThreadPool.Size", "2");

Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapter("TestAdapter");
ServantLocatorPtr locator = make_shared<ServantLocatorI>();
ServantLocatorPtr locator = make_shared<ServantLocatorI>(this);
adapter->addServantLocator(locator, "");

void allTests(TestHelper*);
Expand Down
10 changes: 8 additions & 2 deletions cpp/test/Ice/adapterDeactivation/ServantLocatorI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ namespace
class RouterI final : public Router
{
public:
RouterI(Test::TestHelper* helper) : _routerPort(helper->getTestPort(5)) {}
optional<ObjectPrx> getClientProxy(optional<bool>&, const Current&) const final { return nullopt; }

optional<ObjectPrx> getServerProxy(const Current& c) const final
{
return ObjectPrx(c.adapter->getCommunicator(), "dummy:tcp -h localhost -p 23456 -t 30000");
return ObjectPrx(c.adapter->getCommunicator(), "dummy:tcp -h localhost -p " + to_string(_routerPort) + " -t 30000");
}

ObjectProxySeq addProxies(ObjectProxySeq, const Current&) final { return ObjectProxySeq(); }

private:
int _routerPort;
};
}

ServantLocatorI::ServantLocatorI() : _deactivated(false), _router(make_shared<RouterI>()) {}
ServantLocatorI::ServantLocatorI(Test::TestHelper* helper) :
_deactivated(false),
_router(make_shared<RouterI>(helper)) {}

ServantLocatorI::~ServantLocatorI() { test(_deactivated); }

Expand Down
3 changes: 2 additions & 1 deletion cpp/test/Ice/adapterDeactivation/ServantLocatorI.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#define SERVANT_LOCATOR_I_H

#include "Ice/Ice.h"
#include "TestHelper.h"

class ServantLocatorI final : public Ice::ServantLocator
{
public:
ServantLocatorI();
ServantLocatorI(Test::TestHelper*);
~ServantLocatorI() final;

Ice::ObjectPtr locate(const Ice::Current&, std::shared_ptr<void>&) final;
Expand Down
2 changes: 1 addition & 1 deletion cpp/test/Ice/adapterDeactivation/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Server::run(int argc, char** argv)
Ice::CommunicatorHolder communicator = initialize(argc, argv);
communicator->getProperties()->setProperty("TestAdapter.Endpoints", getTestEndpoint());
Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapter("TestAdapter");
ServantLocatorPtr locator = make_shared<ServantLocatorI>();
ServantLocatorPtr locator = make_shared<ServantLocatorI>(this);
adapter->addServantLocator(locator, "");
adapter->activate();
serverReady();
Expand Down

0 comments on commit c907e6d

Please sign in to comment.