diff options
Diffstat (limited to 'cpp/test/IceGrid/allocation')
-rw-r--r-- | cpp/test/IceGrid/allocation/AllTests.cpp | 424 | ||||
-rw-r--r-- | cpp/test/IceGrid/allocation/PermissionsVerifier.cpp | 12 | ||||
-rw-r--r-- | cpp/test/IceGrid/allocation/Server.cpp | 26 | ||||
-rw-r--r-- | cpp/test/IceGrid/allocation/TestI.cpp | 6 | ||||
-rw-r--r-- | cpp/test/IceGrid/allocation/TestI.h | 10 |
5 files changed, 246 insertions, 232 deletions
diff --git a/cpp/test/IceGrid/allocation/AllTests.cpp b/cpp/test/IceGrid/allocation/AllTests.cpp index 3543e07e67c..268660f8120 100644 --- a/cpp/test/IceGrid/allocation/AllTests.cpp +++ b/cpp/test/IceGrid/allocation/AllTests.cpp @@ -2,19 +2,19 @@ // Copyright (c) ZeroC, Inc. All rights reserved. // -#include <IceUtil/Thread.h> -#include <IceUtil/Random.h> #include <Ice/Ice.h> #include <IceGrid/IceGrid.h> #include <Glacier2/Router.h> #include <TestHelper.h> #include <Test.h> +#include <random> + using namespace std; using namespace Test; using namespace IceGrid; -class Callback : public IceUtil::Monitor<IceUtil::Mutex>, public virtual IceUtil::Shared +class Callback { public: @@ -23,36 +23,38 @@ public: } void - response(const Ice::ObjectPrx& obj) + response(shared_ptr<Ice::ObjectPrx> obj) { - Lock sync(*this); - _response = true; - _obj = obj; - notify(); + { + lock_guard<mutex> lg(_mutex); + _response = true; + _obj = move(obj); + } + + _condVar.notify_one(); } void - exception(const Ice::Exception&) + exception(exception_ptr) { - Lock sync(*this); - _exception = true; - notify(); + { + lock_guard<mutex> lg(_mutex); + _exception = true; + } + _condVar.notify_one(); } void waitResponse(const char*, int) { - Lock sync(*this); - while(!_response && !_exception) - { - wait(); - } + unique_lock<mutex> lock(_mutex); + _condVar.wait(lock, [&] { return _response || _exception; }); } bool - hasResponse(Ice::ObjectPrx& obj) + hasResponse(shared_ptr<Ice::ObjectPrx>& obj) { - Lock sync(*this); + lock_guard<mutex> lg(_mutex); obj = _obj; return _response; } @@ -60,7 +62,7 @@ public: bool hasException() { - Lock sync(*this); + lock_guard<mutex> lg(_mutex); return _exception; } @@ -68,50 +70,45 @@ private: bool _response; bool _exception; - Ice::ObjectPrx _obj; + mutex _mutex; + condition_variable _condVar; + shared_ptr<Ice::ObjectPrx> _obj; }; -typedef IceUtil::Handle<Callback> CallbackPtr; -class StressClient : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex> +class StressClient { public: - StressClient(int id, const RegistryPrx& registry, bool destroySession) : - _communicator(registry->ice_getCommunicator()), + StressClient(int id, shared_ptr<RegistryPrx> registry, bool destroySession) : _id(id), - _registry(registry), + _registry(move(registry)), _notified(false), _terminated(false), _destroySession(destroySession) { } - StressClient(int id, const SessionPrx& session) : - _communicator(session->ice_getCommunicator()), + StressClient(int id, shared_ptr<SessionPrx> session) : _id(id), - _session(session), + _session(move(session)), _notified(false), _terminated(false), _destroySession(false) { } - virtual void run() { { - Lock sync(*this); - while(!_notified) - { - wait(); - } + unique_lock<mutex> lock(_mutex); + _condVar.wait(lock, [&] { return _notified == true; }); } - SessionPrx session; + shared_ptr<SessionPrx> session; while(true) { { - Lock sync(*this); + lock_guard<mutex> lg(_mutex); if(_terminated) { if(!_session && session) @@ -133,15 +130,15 @@ public: else { session = _registry->createSession(os.str(), ""); - session->setAllocationTimeout(static_cast<Ice::Int>(IceUtilInternal::random(200))); // 200ms timeout + session->setAllocationTimeout(static_cast<int>(_rd() % 200)); // 200ms timeout } } assert(session); session->keepAlive(); - Ice::ObjectPrx object; - switch(IceUtilInternal::random(_destroySession ? 4 : 2)) + shared_ptr<Ice::ObjectPrx> object; + switch( _rd() % (_destroySession ? 4 : 2)) { case 0: object = allocate(session); @@ -163,8 +160,8 @@ public: if(object) { - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(IceUtilInternal::random(20))); - switch(IceUtilInternal::random(_destroySession ? 2 : 1)) + this_thread::sleep_for(20ms); + switch(_rd() % (_destroySession ? 2 : 1)) { case 0: session->releaseObject(object->ice_getIdentity()); @@ -179,11 +176,11 @@ public: } } - Ice::ObjectPrx - allocate(const SessionPrx& session) + shared_ptr<Ice::ObjectPrx> + allocate(const shared_ptr<SessionPrx>& session) { ostringstream os; - os << "stress-" << IceUtilInternal::random(6) + 1; + os << "stress-" << (_rd() % 6) + 1; try { return session->allocateObjectById(Ice::stringToIdentity(os.str())); @@ -200,8 +197,8 @@ public: return 0; } - Ice::ObjectPrx - allocateByType(const SessionPrx& session) + shared_ptr<Ice::ObjectPrx> + allocateByType(const shared_ptr<SessionPrx>& session) { try { @@ -214,68 +211,74 @@ public: } void - allocateAndDestroy(const SessionPrx& session) + allocateAndDestroy(const shared_ptr<SessionPrx>& session) { ostringstream os; - os << "stress-" << IceUtilInternal::random(3); - CallbackPtr asyncCB = new Callback(); - IceGrid::Callback_Session_allocateObjectByIdPtr cb = - IceGrid::newCallback_Session_allocateObjectById(asyncCB, &Callback::response, &Callback::exception); - session->begin_allocateObjectById(Ice::stringToIdentity(os.str()), cb); + os << "stress-" << (_rd() % 3); + + auto asyncCB = make_shared<Callback>(); + session->allocateObjectByIdAsync(Ice::stringToIdentity(os.str()), + [asyncCB](shared_ptr<Ice::ObjectPrx> o) { asyncCB->response(move(o)); }, + [asyncCB](exception_ptr e) { asyncCB->exception(e); }); session->destroy(); } void - allocateByTypeAndDestroy(const SessionPrx& session) + allocateByTypeAndDestroy(const shared_ptr<SessionPrx>& session) { - CallbackPtr asyncCB = new Callback(); - IceGrid::Callback_Session_allocateObjectByTypePtr cb = - IceGrid::newCallback_Session_allocateObjectByType(asyncCB, &Callback::response, &Callback::exception); - session->begin_allocateObjectByType("::StressTest", cb); + auto asyncCB = make_shared<Callback>(); + session->allocateObjectByTypeAsync("::StressTest", + [asyncCB](shared_ptr<Ice::ObjectPrx> o) { asyncCB->response(move(o)); }, + [asyncCB](exception_ptr e) { asyncCB->exception(e); }); session->destroy(); } void notifyThread() { - Lock sync(*this); - _notified = true; - notify(); + { + lock_guard<mutex> lg(_mutex); + _notified = true; + } + _condVar.notify_one(); } void terminate() { - Lock sync(*this); - _terminated = true; - notify(); + { + lock_guard<mutex> lg(_mutex); + _terminated = true; + } + _condVar.notify_one(); } protected: - const Ice::CommunicatorPtr _communicator; const int _id; - const RegistryPrx _registry; - const SessionPrx _session; + const shared_ptr<RegistryPrx> _registry; + const shared_ptr<SessionPrx> _session; bool _notified; bool _terminated; const bool _destroySession; + mutex _mutex; + condition_variable _condVar; + random_device _rd; }; -typedef IceUtil::Handle<StressClient> StressClientPtr; void allTests(Test::TestHelper* helper) { - Ice::CommunicatorPtr communicator = helper->communicator(); - IceGrid::RegistryPrx registry = IceGrid::RegistryPrx::checkedCast( + auto communicator = helper->communicator(); + auto registry = Ice::checkedCast<RegistryPrx>( communicator->stringToProxy(communicator->getDefaultLocator()->ice_getIdentity().category + "/Registry")); test(registry); - AdminSessionPrx session = registry->createAdminSession("foo", "bar"); + auto session = registry->createAdminSession("foo", "bar"); session->ice_getConnection()->setACM(registry->getACMTimeout(), - IceUtil::None, - Ice::ICE_ENUM(ACMHeartbeat, HeartbeatAlways)); + Ice::nullopt, + Ice::ACMHeartbeat::HeartbeatAlways); - AdminPrx admin = session->getAdmin(); + auto admin = session->getAdmin(); test(admin); cout << "starting router... " << flush; @@ -292,20 +295,20 @@ allTests(Test::TestHelper* helper) const int allocationTimeout = 5000; - Ice::ObjectPrx obj; - Ice::ObjectPrx dummy; + shared_ptr<Ice::ObjectPrx> obj; + shared_ptr<Ice::ObjectPrx> dummy; try { cout << "testing create session... " << flush; - SessionPrx session1 = registry->createSession("Client1", ""); - SessionPrx session2 = registry->createSession("Client2", ""); + auto session1 = registry->createSession("Client1", ""); + auto session2 = registry->createSession("Client2", ""); cout << "ok" << endl; cout << "testing allocate object by identity... " << flush; - Ice::Identity allocatable = Ice::stringToIdentity("allocatable"); - Ice::Identity allocatablebis = Ice::stringToIdentity("allocatablebis"); + auto allocatable = Ice::stringToIdentity("allocatable"); + auto allocatablebis = Ice::stringToIdentity("allocatablebis"); try { @@ -427,13 +430,11 @@ allTests(Test::TestHelper* helper) session2->setAllocationTimeout(allocationTimeout); - CallbackPtr asyncCB1 = new Callback(); - IceGrid::Callback_Session_allocateObjectByIdPtr cb1 = IceGrid::newCallback_Session_allocateObjectById(asyncCB1, - &Callback::response, - &Callback::exception); - - session2->begin_allocateObjectById(allocatable, cb1); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + auto asyncCB1 = make_shared<Callback>(); + session2->allocateObjectByIdAsync(allocatable, + [&asyncCB1](shared_ptr<Ice::ObjectPrx> o) { asyncCB1->response(o); }, + [&asyncCB1](exception_ptr e) { asyncCB1->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB1->hasResponse(dummy)); session1->releaseObject(allocatable); asyncCB1->waitResponse(__FILE__, __LINE__); @@ -457,12 +458,11 @@ allTests(Test::TestHelper* helper) { } session1->setAllocationTimeout(allocationTimeout); - asyncCB1 = new Callback(); - cb1 = IceGrid::newCallback_Session_allocateObjectById(asyncCB1, - &Callback::response, - &Callback::exception); - session1->begin_allocateObjectById(allocatable, cb1); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + asyncCB1 = make_shared<Callback>(); + session1->allocateObjectByIdAsync(allocatable, + [&asyncCB1](shared_ptr<Ice::ObjectPrx> o) { asyncCB1->response(o); }, + [&asyncCB1](exception_ptr e) { asyncCB1->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB1->hasResponse(dummy)); session2->releaseObject(allocatable); asyncCB1->waitResponse(__FILE__, __LINE__); @@ -578,12 +578,11 @@ allTests(Test::TestHelper* helper) session2->releaseObject(allocatablebis); session1->setAllocationTimeout(allocationTimeout); - CallbackPtr asyncCB3 = new Callback(); - IceGrid::Callback_Session_allocateObjectByTypePtr cb3 = - IceGrid::newCallback_Session_allocateObjectByType(asyncCB3, &Callback::response, &Callback::exception); - - session1->begin_allocateObjectByType("::Test", cb3); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + auto asyncCB3 = make_shared<Callback>(); + session1->allocateObjectByTypeAsync("::Test", + [&asyncCB3](shared_ptr<Ice::ObjectPrx> o) { asyncCB3->response(o); }, + [&asyncCB3](exception_ptr e) { asyncCB3->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB3->hasResponse(dummy)); session2->releaseObject(obj->ice_getIdentity()); asyncCB3->waitResponse(__FILE__, __LINE__); @@ -600,7 +599,7 @@ allTests(Test::TestHelper* helper) // not be visible to the allocation system immediately. session1->allocateObjectByType("::Test"); session1->releaseObject(obj->ice_getIdentity()); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(100)); + this_thread::sleep_for(100ms); } test(false); } @@ -614,7 +613,7 @@ allTests(Test::TestHelper* helper) cout << "testing object allocation timeout... " << flush; session1->allocateObjectById(allocatable); - IceUtil::Time time = IceUtil::Time::now(); + auto time = chrono::system_clock::now(); session2->setAllocationTimeout(500); try { @@ -623,9 +622,9 @@ allTests(Test::TestHelper* helper) } catch(const AllocationTimeoutException&) { - test(time + IceUtil::Time::milliSeconds(100) < IceUtil::Time::now()); + test(time + 100ms < chrono::system_clock::now()); } - time = IceUtil::Time::now(); + time = chrono::system_clock::now(); try { session2->allocateObjectById(allocatable); @@ -634,8 +633,8 @@ allTests(Test::TestHelper* helper) catch(const AllocationTimeoutException&) { } - test(time + IceUtil::Time::milliSeconds(100) < IceUtil::Time::now()); - time = IceUtil::Time::now(); + test(time + 100ms < chrono::system_clock::now()); + time = chrono::system_clock::now(); try { session2->allocateObjectByType("::Test"); @@ -644,7 +643,7 @@ allTests(Test::TestHelper* helper) catch(const AllocationTimeoutException&) { } - test(time + IceUtil::Time::milliSeconds(100) < IceUtil::Time::now()); + test(time + 100ms < chrono::system_clock::now()); session1->releaseObject(allocatable); session2->setAllocationTimeout(0); @@ -755,10 +754,11 @@ allTests(Test::TestHelper* helper) session1->allocateObjectById(allocatable4); session2->setAllocationTimeout(allocationTimeout); - asyncCB1 = new Callback(); - cb1 = IceGrid::newCallback_Session_allocateObjectById(asyncCB1, &Callback::response, &Callback::exception); - session2->begin_allocateObjectById(allocatable3, cb1); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + asyncCB1 = make_shared<Callback>(); + session2->allocateObjectByIdAsync(allocatable3, + [&asyncCB1](shared_ptr<Ice::ObjectPrx> o) { asyncCB1->response(o); }, + [&asyncCB1](exception_ptr e) { asyncCB1->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB1->hasResponse(dummy)); session1->releaseObject(allocatable3); test(!asyncCB1->hasResponse(dummy)); @@ -769,10 +769,11 @@ allTests(Test::TestHelper* helper) session1->setAllocationTimeout(allocationTimeout); test(session2->allocateObjectByType("::TestServer1")); - asyncCB3 = new Callback(); - cb3 = IceGrid::newCallback_Session_allocateObjectByType(asyncCB3, &Callback::response, &Callback::exception); - session1->begin_allocateObjectByType("::TestServer2", cb3); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + asyncCB3 = make_shared<Callback>(); + session1->allocateObjectByTypeAsync("::TestServer2", + [&asyncCB3](shared_ptr<Ice::ObjectPrx> o) { asyncCB3->response(o); }, + [&asyncCB3](exception_ptr e) { asyncCB3->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB3->hasResponse(dummy)); session2->releaseObject(allocatable3); asyncCB3->waitResponse(__FILE__, __LINE__); @@ -806,9 +807,9 @@ allTests(Test::TestHelper* helper) session2->releaseObject(Ice::stringToIdentity("allocatable31")); session2->releaseObject(Ice::stringToIdentity("allocatable41")); - Ice::ObjectPrx obj1 = session1->allocateObjectByType("::TestMultipleServer"); + auto obj1 = session1->allocateObjectByType("::TestMultipleServer"); test(obj1); - Ice::ObjectPrx obj2 = session2->allocateObjectByType("::TestMultipleServer"); + auto obj2 = session2->allocateObjectByType("::TestMultipleServer"); test(obj2); try { @@ -840,7 +841,7 @@ allTests(Test::TestHelper* helper) // not be visible to the allocation system immediately. session1->allocateObjectByType("::TestServer1"); session1->releaseObject(allocatable3); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(100)); + this_thread::sleep_for(100ms); } test(false); } @@ -857,15 +858,15 @@ allTests(Test::TestHelper* helper) session2->setAllocationTimeout(allocationTimeout); session2->allocateObjectById(allocatable); - CallbackPtr asyncCB11 = new Callback(); - IceGrid::Callback_Session_allocateObjectByIdPtr cb11 = - IceGrid::newCallback_Session_allocateObjectById(asyncCB11, &Callback::response, &Callback::exception); - CallbackPtr asyncCB12 = new Callback(); - IceGrid::Callback_Session_allocateObjectByIdPtr cb12 = - IceGrid::newCallback_Session_allocateObjectById(asyncCB12, &Callback::response, &Callback::exception); - session1->begin_allocateObjectById(allocatable, cb11); - session1->begin_allocateObjectById(allocatable, cb12); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + auto asyncCB11 = make_shared<Callback>(); + auto asyncCB12 = make_shared<Callback>(); + session1->allocateObjectByIdAsync(allocatable, + [&asyncCB11](shared_ptr<Ice::ObjectPrx> o) { asyncCB11->response(o); }, + [&asyncCB11](exception_ptr e) { asyncCB11->exception(e); }); + session1->allocateObjectByIdAsync(allocatable, + [&asyncCB12](shared_ptr<Ice::ObjectPrx> o) { asyncCB12->response(o); }, + [&asyncCB12](exception_ptr e) { asyncCB12->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB11->hasResponse(dummy)); test(!asyncCB12->hasResponse(dummy)); session2->releaseObject(allocatable); @@ -876,41 +877,43 @@ allTests(Test::TestHelper* helper) session1->releaseObject(allocatable); session2->allocateObjectById(allocatable); - CallbackPtr asyncCB31 = new Callback(); - IceGrid::Callback_Session_allocateObjectByTypePtr cb31 = - IceGrid::newCallback_Session_allocateObjectByType(asyncCB31, &Callback::response, &Callback::exception); - CallbackPtr asyncCB32 = new Callback(); - IceGrid::Callback_Session_allocateObjectByTypePtr cb32 = - IceGrid::newCallback_Session_allocateObjectByType(asyncCB32, &Callback::response, &Callback::exception); - session1->begin_allocateObjectByType("::Test", cb31); - session1->begin_allocateObjectByType("::Test", cb32); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + auto asyncCB31 = make_shared<Callback>(); + auto asyncCB32 = make_shared<Callback>(); + session1->allocateObjectByTypeAsync("::Test", + [&asyncCB31](shared_ptr<Ice::ObjectPrx> o) { asyncCB31->response(o); }, + [&asyncCB31](exception_ptr e) { asyncCB31->exception(e); }); + session1->allocateObjectByTypeAsync("::Test", + [&asyncCB32](shared_ptr<Ice::ObjectPrx> o) { asyncCB32->response(o); }, + [&asyncCB32](exception_ptr e) { asyncCB32->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB31->hasResponse(dummy)); test(!asyncCB32->hasResponse(dummy)); session2->releaseObject(allocatable); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(300)); + this_thread::sleep_for(300ms); do { - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(200)); + this_thread::sleep_for(200ms); } while(!asyncCB31->hasResponse(dummy) && !asyncCB32->hasResponse(dummy)); test((asyncCB31->hasResponse(dummy) && dummy && !asyncCB32->hasResponse(dummy)) || (asyncCB32->hasResponse(dummy) && dummy && !asyncCB31->hasResponse(dummy))); session1->releaseObject(allocatable); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(300)); - CallbackPtr asyncCB33 = asyncCB31->hasResponse(dummy) ? asyncCB32 : asyncCB31; + this_thread::sleep_for(300ms); + auto asyncCB33 = asyncCB31->hasResponse(dummy) ? asyncCB32 : asyncCB31; asyncCB33->waitResponse(__FILE__, __LINE__); test(asyncCB33->hasResponse(dummy) && dummy); session1->releaseObject(allocatable); session2->allocateObjectById(allocatable3); - asyncCB11 = new Callback(); - cb11 = IceGrid::newCallback_Session_allocateObjectById(asyncCB11, &Callback::response, &Callback::exception); - asyncCB12 = new Callback(); - cb12 = IceGrid::newCallback_Session_allocateObjectById(asyncCB12, &Callback::response, &Callback::exception); - session1->begin_allocateObjectById(allocatable3, cb11); - session1->begin_allocateObjectById(allocatable3, cb12); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + asyncCB11 = make_shared<Callback>(); + asyncCB12 = make_shared<Callback>(); + session1->allocateObjectByIdAsync(allocatable3, + [&asyncCB11](shared_ptr<Ice::ObjectPrx> o) { asyncCB11->response(o); }, + [&asyncCB11](exception_ptr e) { asyncCB11->exception(e); }); + session1->allocateObjectByIdAsync(allocatable3, + [&asyncCB12](shared_ptr<Ice::ObjectPrx> o) { asyncCB12->response(o); }, + [&asyncCB12](exception_ptr e) { asyncCB12->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB11->hasResponse(dummy)); test(!asyncCB12->hasResponse(dummy)); session2->releaseObject(allocatable3); @@ -921,52 +924,56 @@ allTests(Test::TestHelper* helper) session1->releaseObject(allocatable3); session2->allocateObjectById(allocatable3); - asyncCB31 = new Callback(); - cb31 = IceGrid::newCallback_Session_allocateObjectByType(asyncCB31, &Callback::response, &Callback::exception); - asyncCB32 = new Callback(); - cb32 = IceGrid::newCallback_Session_allocateObjectByType(asyncCB32, &Callback::response, &Callback::exception); - session1->begin_allocateObjectByType("::TestServer1", cb31); - session1->begin_allocateObjectByType("::TestServer1", cb32); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + asyncCB31 = make_shared<Callback>(); + asyncCB32 = make_shared<Callback>(); + session1->allocateObjectByTypeAsync("::TestServer1", + [&asyncCB31](shared_ptr<Ice::ObjectPrx> o) { asyncCB31->response(o); }, + [&asyncCB31](exception_ptr e) { asyncCB31->exception(e); }); + session1->allocateObjectByTypeAsync("::TestServer1", + [&asyncCB32](shared_ptr<Ice::ObjectPrx> o) { asyncCB32->response(o); }, + [&asyncCB32](exception_ptr e) { asyncCB32->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB31->hasResponse(dummy)); test(!asyncCB32->hasResponse(dummy)); session2->releaseObject(allocatable3); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(300)); + this_thread::sleep_for(300ms); do { - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(200)); + this_thread::sleep_for(200ms); } while(!asyncCB31->hasResponse(dummy) && !asyncCB32->hasResponse(dummy)); test((asyncCB31->hasResponse(dummy) && dummy && !asyncCB32->hasResponse(dummy)) || (asyncCB32->hasResponse(dummy) && dummy && !asyncCB31->hasResponse(dummy))); session1->releaseObject(allocatable3); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(300)); + this_thread::sleep_for(300ms); asyncCB33 = asyncCB31->hasResponse(dummy) ? asyncCB32 : asyncCB31; asyncCB33->waitResponse(__FILE__, __LINE__); test(asyncCB33->hasResponse(dummy) && dummy); session1->releaseObject(allocatable3); session1->allocateObjectById(allocatable3); - asyncCB31 = new Callback(); - cb31 = IceGrid::newCallback_Session_allocateObjectByType(asyncCB31, &Callback::response, &Callback::exception); - asyncCB32 = new Callback(); - cb32 = IceGrid::newCallback_Session_allocateObjectByType(asyncCB32, &Callback::response, &Callback::exception); - session1->begin_allocateObjectByType("::TestServer1", cb31); - session1->begin_allocateObjectByType("::TestServer1", cb32); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + asyncCB31 = make_shared<Callback>(); + asyncCB32 = make_shared<Callback>(); + session1->allocateObjectByTypeAsync("::TestServer1", + [&asyncCB31](shared_ptr<Ice::ObjectPrx> o) { asyncCB31->response(o); }, + [&asyncCB31](exception_ptr e) { asyncCB31->exception(e); }); + session1->allocateObjectByTypeAsync("::TestServer1", + [&asyncCB32](shared_ptr<Ice::ObjectPrx> o) { asyncCB32->response(o); }, + [&asyncCB32](exception_ptr e) { asyncCB32->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB31->hasResponse(dummy)); test(!asyncCB32->hasResponse(dummy)); session1->releaseObject(allocatable3); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(300)); + this_thread::sleep_for(300ms); do { - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(200)); + this_thread::sleep_for(200ms); } while(!asyncCB31->hasResponse(dummy) && !asyncCB32->hasResponse(dummy)); test((asyncCB31->hasResponse(dummy) && dummy && !asyncCB32->hasResponse(dummy)) || (asyncCB32->hasResponse(dummy) && dummy && !asyncCB31->hasResponse(dummy))); session1->releaseObject(allocatable3); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(300)); + this_thread::sleep_for(300ms); asyncCB33 = asyncCB31->hasResponse(dummy) ? asyncCB32 : asyncCB31; asyncCB33->waitResponse(__FILE__, __LINE__); test(asyncCB33->hasResponse(dummy) && dummy); @@ -980,17 +987,18 @@ allTests(Test::TestHelper* helper) test(obj && obj->ice_getIdentity().name == "allocatable"); session1->setAllocationTimeout(allocationTimeout); - asyncCB3 = new Callback(); - cb3 = IceGrid::newCallback_Session_allocateObjectByType(asyncCB3, &Callback::response, &Callback::exception); - session1->begin_allocateObjectByType("::Test", cb3); - IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); + asyncCB3 = make_shared<Callback>(); + session1->allocateObjectByTypeAsync("::Test", + [&asyncCB3](shared_ptr<Ice::ObjectPrx> o) { asyncCB3->response(o); }, + [&asyncCB3](exception_ptr e) { asyncCB3->exception(e); }); + this_thread::sleep_for(500ms); test(!asyncCB3->hasResponse(dummy)); session2->destroy(); asyncCB3->waitResponse(__FILE__, __LINE__); test(asyncCB3->hasResponse(obj)); session1->destroy(); - session2 = SessionPrx::uncheckedCast(registry->createSession("Client2", "")); + session2 = Ice::uncheckedCast<SessionPrx>(registry->createSession("Client2", "")); session2->setAllocationTimeout(0); session2->allocateObjectById(allocatable); session2->destroy(); @@ -1001,11 +1009,11 @@ allTests(Test::TestHelper* helper) session1 = registry->createSession("Client1", ""); session2 = registry->createSession("Client2", ""); - ServerDescriptorPtr objectAllocOriginal = admin->getServerInfo("ObjectAllocation").descriptor; - ServerDescriptorPtr objectAllocUpdate = ServerDescriptorPtr::dynamicCast(objectAllocOriginal->ice_clone()); + auto objectAllocOriginal = admin->getServerInfo("ObjectAllocation").descriptor; + auto objectAllocUpdate = dynamic_pointer_cast<ServerDescriptor>(objectAllocOriginal->ice_clone()); - ServerDescriptorPtr serverAllocOriginal = admin->getServerInfo("ServerAllocation").descriptor; - ServerDescriptorPtr serverAllocUpdate = ServerDescriptorPtr::dynamicCast(serverAllocOriginal->ice_clone()); + auto serverAllocOriginal = admin->getServerInfo("ServerAllocation").descriptor; + auto serverAllocUpdate = dynamic_pointer_cast<ServerDescriptor>(serverAllocOriginal->ice_clone()); NodeUpdateDescriptor nodeUpdate; nodeUpdate.name = "localnode"; @@ -1018,19 +1026,19 @@ allTests(Test::TestHelper* helper) { session1->allocateObjectById(allocatable3); - Ice::AsyncResultPtr r2 = session2->begin_allocateObjectById(allocatable4); + auto r2 = session2->allocateObjectByIdAsync(allocatable4); session1->allocateObjectById(allocatable4); session1->releaseObject(allocatable4); - test(!r2->isCompleted()); + test(r2.wait_for(chrono::milliseconds(0)) != future_status::ready); serverAllocUpdate->allocatable = false; admin->updateApplication(appUpdate); - test(!r2->isCompleted()); + test(r2.wait_for(chrono::milliseconds(0)) != future_status::ready); session1->releaseObject(allocatable3); - session2->end_allocateObjectById(r2); + r2.get(); session2->releaseObject(allocatable4); serverAllocUpdate->allocatable = true; @@ -1039,19 +1047,19 @@ allTests(Test::TestHelper* helper) { session1->allocateObjectById(allocatable); - Ice::AsyncResultPtr r2 = session2->begin_allocateObjectById(allocatable); + auto r2 = session2->allocateObjectByIdAsync(allocatable); objectAllocUpdate->deactivationTimeout = "23"; admin->updateApplication(appUpdate); session1->releaseObject(allocatable); - session2->end_allocateObjectById(r2); + r2.get(); session2->releaseObject(allocatable); } { session1->allocateObjectById(allocatable); - Ice::AsyncResultPtr r2 = session2->begin_allocateObjectById(allocatable); + auto r2 = session2->allocateObjectByIdAsync(allocatable); vector<ObjectDescriptor> allocatables = objectAllocUpdate->adapters[0].allocatables; objectAllocUpdate->adapters[0].allocatables.clear(); // Remove the allocatable object @@ -1059,7 +1067,7 @@ allTests(Test::TestHelper* helper) try { - session2->end_allocateObjectById(r2); + r2.get(); test(false); } catch(const ObjectNotRegisteredException&) @@ -1084,14 +1092,14 @@ allTests(Test::TestHelper* helper) cout << "ok" << endl; cout << "testing allocation with Glacier2 session... " << flush; - Ice::ObjectPrx routerBase = communicator->stringToProxy("Glacier2/router:default -p 12347"); - Glacier2::RouterPrx router1 = Glacier2::RouterPrx::checkedCast(routerBase->ice_connectionId("client1")); + auto routerBase = communicator->stringToProxy("Glacier2/router:default -p 12347"); + auto router1 = Ice::checkedCast<Glacier2::RouterPrx>(routerBase->ice_connectionId("client1")); test(router1); - Glacier2::SessionPrx sessionBase = router1->createSession("test1", "abc123"); + auto sessionBase = router1->createSession("test1", "abc123"); try { - session1 = IceGrid::SessionPrx::checkedCast(sessionBase->ice_connectionId("client1")->ice_router(router1)); + session1 = Ice::checkedCast<SessionPrx>(sessionBase->ice_connectionId("client1")->ice_router(router1)); test(session1); session1->ice_ping(); @@ -1136,45 +1144,51 @@ allTests(Test::TestHelper* helper) cout << "stress test... " << flush; - SessionPrx stressSession = registry->createSession("StressSession", ""); + auto stressSession = registry->createSession("StressSession", ""); const int nClients = 10; - int i; - vector<StressClientPtr> clients; - for(i = 0; i < nClients - 2; ++i) + map<shared_ptr<StressClient>, future<void>> clients; + random_device rd; + + for(int i = 0; i < nClients - 2; ++i) { - if(IceUtilInternal::random(2) == 1) + shared_ptr<StressClient> client; + if(rd() % 2 == 1) { - clients.push_back(new StressClient(i, registry, false)); + client = make_shared<StressClient>(i, registry, false); } else { - clients.push_back(new StressClient(i, stressSession)); + client = make_shared<StressClient>(i, stressSession); } - clients.back()->start(); + + clients.insert(make_pair(client, async(launch::async, [=] { client->run(); }))); + } + + { + auto client8 = make_shared<StressClient>(8, registry, true); + clients.insert(make_pair(client8, async(launch::async, [=] { client8->run(); }))); + auto client9 = make_shared<StressClient>(9, registry, true); + clients.insert(make_pair(client9, async(launch::async, [=] { client9->run(); }))); } - clients.push_back(new StressClient(i++, registry, true)); - clients.back()->start(); - clients.push_back(new StressClient(i++, registry, true)); - clients.back()->start(); - for(vector<StressClientPtr>::const_iterator p = clients.begin(); p != clients.end(); ++p) + for(const auto& c : clients) { - (*p)->notifyThread(); + c.first->notifyThread(); } // // Let the stress client run for a bit. // - IceUtil::ThreadControl::sleep(IceUtil::Time::seconds(8)); + this_thread::sleep_for(8s); // // Terminate the stress clients. // - for(vector<StressClientPtr>::const_iterator q = clients.begin(); q != clients.end(); ++q) + for(auto& c : clients) { - (*q)->terminate(); - (*q)->getThreadControl().join(); + c.first->terminate(); + c.second.get(); } stressSession->destroy(); diff --git a/cpp/test/IceGrid/allocation/PermissionsVerifier.cpp b/cpp/test/IceGrid/allocation/PermissionsVerifier.cpp index 0651250f7dd..2358458e256 100644 --- a/cpp/test/IceGrid/allocation/PermissionsVerifier.cpp +++ b/cpp/test/IceGrid/allocation/PermissionsVerifier.cpp @@ -8,12 +8,12 @@ using namespace std; -class PermissionsVerifierI : public Glacier2::PermissionsVerifier +class PermissionsVerifierI final : public Glacier2::PermissionsVerifier { public: - virtual bool - checkPermissions(const string&, const string&, string&, const Ice::Current&) const + bool + checkPermissions(string, string, string&, const Ice::Current&) const override { return true; } @@ -23,11 +23,11 @@ class Server : public Test::TestHelper { public: - virtual void run(int argc, char** argv) + void run(int argc, char** argv) override { Ice::CommunicatorHolder communicator = initialize(argc, argv); - Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapter("PermissionsVerifier"); - adapter->add(new PermissionsVerifierI, Ice::stringToIdentity("PermissionsVerifier")); + auto adapter = communicator->createObjectAdapter("PermissionsVerifier"); + adapter->add(make_shared<PermissionsVerifierI>(), Ice::stringToIdentity("PermissionsVerifier")); adapter->activate(); communicator->waitForShutdown(); } diff --git a/cpp/test/IceGrid/allocation/Server.cpp b/cpp/test/IceGrid/allocation/Server.cpp index 5b45a7e7468..b3fb5db6296 100644 --- a/cpp/test/IceGrid/allocation/Server.cpp +++ b/cpp/test/IceGrid/allocation/Server.cpp @@ -12,28 +12,28 @@ class Server : public Test::TestHelper { public: - void run(int, char**); + void run(int, char**) override; }; void Server::run(int argc, char** argv) { Ice::CommunicatorHolder communicator = initialize(argc, argv); - Ice::PropertiesPtr properties = communicator->getProperties(); + auto properties = communicator->getProperties(); string name = properties->getProperty("Ice.ProgramName"); - Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapter("Server"); - adapter->add(new TestI(properties), Ice::stringToIdentity("allocatable")); - adapter->add(new TestI(properties), Ice::stringToIdentity("nonallocatable")); - adapter->add(new TestI(properties), Ice::stringToIdentity("allocatable1")); - adapter->add(new TestI(properties), Ice::stringToIdentity("allocatable2")); - adapter->add(new TestI(properties), Ice::stringToIdentity("allocatable3")); - adapter->add(new TestI(properties), Ice::stringToIdentity("allocatable4")); - adapter->add(new TestI(properties), Ice::stringToIdentity("allocatable11")); - adapter->add(new TestI(properties), Ice::stringToIdentity("allocatable21")); - adapter->add(new TestI(properties), Ice::stringToIdentity("allocatable31")); - adapter->add(new TestI(properties), Ice::stringToIdentity("allocatable41")); + auto adapter = communicator->createObjectAdapter("Server"); + adapter->add(make_shared<TestI>(properties), Ice::stringToIdentity("allocatable")); + adapter->add(make_shared<TestI>(properties), Ice::stringToIdentity("nonallocatable")); + adapter->add(make_shared<TestI>(properties), Ice::stringToIdentity("allocatable1")); + adapter->add(make_shared<TestI>(properties), Ice::stringToIdentity("allocatable2")); + adapter->add(make_shared<TestI>(properties), Ice::stringToIdentity("allocatable3")); + adapter->add(make_shared<TestI>(properties), Ice::stringToIdentity("allocatable4")); + adapter->add(make_shared<TestI>(properties), Ice::stringToIdentity("allocatable11")); + adapter->add(make_shared<TestI>(properties), Ice::stringToIdentity("allocatable21")); + adapter->add(make_shared<TestI>(properties), Ice::stringToIdentity("allocatable31")); + adapter->add(make_shared<TestI>(properties), Ice::stringToIdentity("allocatable41")); try { diff --git a/cpp/test/IceGrid/allocation/TestI.cpp b/cpp/test/IceGrid/allocation/TestI.cpp index f91a7ac44b2..dfa2b6c6bbb 100644 --- a/cpp/test/IceGrid/allocation/TestI.cpp +++ b/cpp/test/IceGrid/allocation/TestI.cpp @@ -5,8 +5,8 @@ #include <Ice/Ice.h> #include <TestI.h> -TestI::TestI(const Ice::PropertiesPtr& properties) : - _properties(properties) +TestI::TestI(std::shared_ptr<Ice::Properties> properties) : + _properties(move(properties)) { } @@ -17,7 +17,7 @@ TestI::shutdown(const Ice::Current& current) } std::string -TestI::getProperty(const std::string& name, const Ice::Current&) +TestI::getProperty(std::string name, const Ice::Current&) { return _properties->getProperty(name); } diff --git a/cpp/test/IceGrid/allocation/TestI.h b/cpp/test/IceGrid/allocation/TestI.h index 1fcf402f93d..e6edeef7abd 100644 --- a/cpp/test/IceGrid/allocation/TestI.h +++ b/cpp/test/IceGrid/allocation/TestI.h @@ -7,18 +7,18 @@ #include <Test.h> -class TestI : public ::Test::TestIntf +class TestI final : public Test::TestIntf { public: - TestI(const Ice::PropertiesPtr&); + TestI(std::shared_ptr<Ice::Properties>); - virtual void shutdown(const Ice::Current&); - virtual std::string getProperty(const std::string&, const Ice::Current&); + void shutdown(const Ice::Current&) override; + std::string getProperty(std::string, const ::Ice::Current&) override; private: - Ice::PropertiesPtr _properties; + std::shared_ptr<Ice::Properties> _properties; }; #endif |