From c907e6d4c1b58d37a5dddcd1b4dfe01de0a54be8 Mon Sep 17 00:00:00 2001 From: Jose Date: Wed, 16 Oct 2024 17:39:25 +0200 Subject: [PATCH] Avoid using fixed ports with adapter tests --- cpp/test/Ice/adapterDeactivation/AllTests.cpp | 42 ++++++++++++------- .../Ice/adapterDeactivation/Collocated.cpp | 2 +- .../adapterDeactivation/ServantLocatorI.cpp | 10 ++++- .../Ice/adapterDeactivation/ServantLocatorI.h | 3 +- cpp/test/Ice/adapterDeactivation/Server.cpp | 2 +- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/cpp/test/Ice/adapterDeactivation/AllTests.cpp b/cpp/test/Ice/adapterDeactivation/AllTests.cpp index c4429efa897..83571736dc9 100644 --- a/cpp/test/Ice/adapterDeactivation/AllTests.cpp +++ b/cpp/test/Ice/adapterDeactivation/AllTests.cpp @@ -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; @@ -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", ""); { @@ -146,13 +153,15 @@ allTests(Test::TestHelper* helper) dynamic_pointer_cast(getUnderlying(publishedEndpoints[0]->getInfo())); auto ipEndpointInfo1 = dynamic_pointer_cast(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", ""); { @@ -160,7 +169,7 @@ allTests(Test::TestHelper* helper) auto publishedEndpoints = adapter->getPublishedEndpoints(); test(publishedEndpoints.size() == 1); // loopback filtered out auto ipEndpointInfo = dynamic_pointer_cast(getUnderlying(publishedEndpoints[0]->getInfo())); - test(ipEndpointInfo && !ipEndpointInfo->host.empty() && ipEndpointInfo->port != 12345); + test(ipEndpointInfo && !ipEndpointInfo->host.empty() && ipEndpointInfo->port != firstPort); adapter->destroy(); } @@ -170,12 +179,14 @@ allTests(Test::TestHelper* helper) auto publishedEndpoints = adapter->getPublishedEndpoints(); test(publishedEndpoints.size() == 1); // loopback filtered out auto ipEndpointInfo = dynamic_pointer_cast(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", ""); { @@ -186,8 +197,8 @@ allTests(Test::TestHelper* helper) dynamic_pointer_cast(getUnderlying(publishedEndpoints[0]->getInfo())); auto ipEndpointInfo1 = dynamic_pointer_cast(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(); } @@ -200,8 +211,8 @@ allTests(Test::TestHelper* helper) dynamic_pointer_cast(getUnderlying(publishedEndpoints[0]->getInfo())); auto ipEndpointInfo1 = dynamic_pointer_cast(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(); } } @@ -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(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()); diff --git a/cpp/test/Ice/adapterDeactivation/Collocated.cpp b/cpp/test/Ice/adapterDeactivation/Collocated.cpp index 7c47a9d2d18..57d46150cfe 100644 --- a/cpp/test/Ice/adapterDeactivation/Collocated.cpp +++ b/cpp/test/Ice/adapterDeactivation/Collocated.cpp @@ -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(); + ServantLocatorPtr locator = make_shared(this); adapter->addServantLocator(locator, ""); void allTests(TestHelper*); diff --git a/cpp/test/Ice/adapterDeactivation/ServantLocatorI.cpp b/cpp/test/Ice/adapterDeactivation/ServantLocatorI.cpp index eaec146cace..bc807513b17 100644 --- a/cpp/test/Ice/adapterDeactivation/ServantLocatorI.cpp +++ b/cpp/test/Ice/adapterDeactivation/ServantLocatorI.cpp @@ -15,18 +15,24 @@ namespace class RouterI final : public Router { public: + RouterI(Test::TestHelper* helper) : _routerPort(helper->getTestPort(5)) {} optional getClientProxy(optional&, const Current&) const final { return nullopt; } optional 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()) {} +ServantLocatorI::ServantLocatorI(Test::TestHelper* helper) : + _deactivated(false), + _router(make_shared(helper)) {} ServantLocatorI::~ServantLocatorI() { test(_deactivated); } diff --git a/cpp/test/Ice/adapterDeactivation/ServantLocatorI.h b/cpp/test/Ice/adapterDeactivation/ServantLocatorI.h index 08775902f62..bbc25a6e917 100644 --- a/cpp/test/Ice/adapterDeactivation/ServantLocatorI.h +++ b/cpp/test/Ice/adapterDeactivation/ServantLocatorI.h @@ -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&) final; diff --git a/cpp/test/Ice/adapterDeactivation/Server.cpp b/cpp/test/Ice/adapterDeactivation/Server.cpp index acf5facaa32..ee4af6d7128 100644 --- a/cpp/test/Ice/adapterDeactivation/Server.cpp +++ b/cpp/test/Ice/adapterDeactivation/Server.cpp @@ -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(); + ServantLocatorPtr locator = make_shared(this); adapter->addServantLocator(locator, ""); adapter->activate(); serverReady();