📄 alltests.cpp
字号:
// **********************************************************************//// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.//// This copy of Ice is licensed to you under the terms described in the// ICE_LICENSE file included in this distribution.//// **********************************************************************#include <Ice/Ice.h>#include <IceGrid/Admin.h>#include <IceGrid/Registry.h>#include <IceUtil/Thread.h>#include <TestCommon.h>#include <Test.h>using namespace std;using namespace Test;voidwaitForServerState(const IceGrid::AdminPrx& admin, const std::string& server, IceGrid::ServerState state){ int nRetry = 0; while(admin->getServerState(server) != state && nRetry < 15) { IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500)); ++nRetry; } if(admin->getServerState(server) != state) { cerr << "server state change timed out:" << endl; cerr << "server: " << server << endl; cerr << "state: " << state << endl; }}class PingThread : public IceUtil::Thread, IceUtil::Monitor<IceUtil::Mutex>{public: PingThread(const Ice::ObjectPrx& proxy, int nRepetitions) : _proxy(proxy), _finished(false), _nRepetitions(nRepetitions) { } virtual void run() { for(int i = 0; i < _nRepetitions; ++i) { try { _proxy->ice_ping(); } catch(const Ice::LocalException& ex) { _exception.reset(dynamic_cast<Ice::LocalException*>(ex.ice_clone())); } catch(...) { assert(false); } } Lock sync(*this); _finished = true; notifyAll(); } auto_ptr<Ice::LocalException> waitUntilFinished() { Lock sync(*this); while(!_finished) { wait(); } return _exception; }private: Ice::ObjectPrx _proxy; auto_ptr<Ice::LocalException> _exception; bool _finished; int _nRepetitions;};typedef IceUtil::Handle<PingThread> PingThreadPtr;class SessionKeepAliveThread : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex>{public: SessionKeepAliveThread(const IceGrid::AdminSessionPrx& session, long timeout) : _session(session), _timeout(IceUtil::Time::seconds(timeout)), _destroy(false) { } virtual void run() { Lock sync(*this); while(!_destroy) { timedWait(_timeout); if(_destroy) { break; } try { _session->keepAlive(); } catch(const Ice::Exception&) { break; } } } void destroy() { Lock sync(*this); _destroy = true; notify(); }private: IceGrid::AdminSessionPrx _session; const IceUtil::Time _timeout; bool _destroy;};typedef IceUtil::Handle<SessionKeepAliveThread> SessionKeepAliveThreadPtr;voidallTests(const Ice::CommunicatorPtr& communicator){ IceGrid::RegistryPrx registry = IceGrid::RegistryPrx::checkedCast(communicator->stringToProxy("IceGrid/Registry")); test(registry); IceGrid::AdminSessionPrx session = registry->createAdminSession("foo", "bar"); SessionKeepAliveThreadPtr keepAlive = new SessionKeepAliveThread(session, registry->getSessionTimeout()/2); keepAlive->start(); IceGrid::AdminPrx admin = session->getAdmin(); test(admin); admin->startServer("node-1"); admin->startServer("node-2"); int nRetry = 0; while(!admin->pingNode("node-1") && nRetry < 15) { IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(200)); ++nRetry; } nRetry = 0; while(!admin->pingNode("node-2") && nRetry < 15) { IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(200)); ++nRetry; } cout << "testing on-demand activation... " << flush; try { test(admin->getServerState("server") == IceGrid::Inactive); TestIntfPrx obj = TestIntfPrx::checkedCast(communicator->stringToProxy("server")); waitForServerState(admin, "server", IceGrid::Active); obj->shutdown(); waitForServerState(admin, "server", IceGrid::Inactive); nRetry = 4; while(--nRetry > 0) { obj->shutdown(); } waitForServerState(admin, "server", IceGrid::Inactive); } catch(const Ice::LocalException& ex) { cerr << ex << endl; test(false); } cout << "ok" << endl; cout << "testing manual activation... " << flush; try { test(admin->getServerState("server-manual") == IceGrid::Inactive); TestIntfPrx obj; try { obj = TestIntfPrx::checkedCast(communicator->stringToProxy("server-manual")); test(false); } catch(const Ice::NoEndpointException&) { } test(admin->getServerState("server-manual") == IceGrid::Inactive); admin->startServer("server-manual"); test(admin->getServerState("server-manual") == IceGrid::Active); obj = TestIntfPrx::checkedCast(communicator->stringToProxy("server-manual")); test(admin->getServerState("server-manual") == IceGrid::Active); obj->shutdown(); waitForServerState(admin, "server-manual", IceGrid::Inactive); } catch(const Ice::LocalException& ex) { cerr << ex << endl; test(false); } cout << "ok" << endl; cout << "testing always activation... " << flush; try { waitForServerState(admin, "server-always", IceGrid::Active); TestIntfPrx obj = TestIntfPrx::checkedCast(communicator->stringToProxy("server-always")); admin->stopServer("server-always"); waitForServerState(admin, "server-always", IceGrid::Active); obj->shutdown(); waitForServerState(admin, "server-always", IceGrid::Active); nRetry = 4; while(--nRetry > 0) { obj->shutdown(); } waitForServerState(admin, "server-always", IceGrid::Active); } catch(const Ice::LocalException& ex) { cerr << ex << endl; test(false); } cout << "ok" << endl; cout << "testing session activation... " << flush; try { IceGrid::SessionPrx session = registry->createSession("test", ""); test(admin->getServerState("server-session") == IceGrid::Inactive); TestIntfPrx obj = TestIntfPrx::uncheckedCast(communicator->stringToProxy("server-session")); try { obj->ice_ping(); test(false); } catch(const Ice::NoEndpointException&) { } session->allocateObjectById(obj->ice_getIdentity()); obj->ice_ping(); waitForServerState(admin, "server-session", IceGrid::Active); obj->shutdown(); waitForServerState(admin, "server-session", IceGrid::Inactive); obj->ice_ping(); waitForServerState(admin, "server-session", IceGrid::Active); nRetry = 4; while(--nRetry > 0) { obj->shutdown(); } obj->ice_ping(); waitForServerState(admin, "server-session", IceGrid::Active); session->releaseObject(obj->ice_getIdentity()); try { obj->ice_ping(); test(false); } catch(const Ice::NoEndpointException&) { } waitForServerState(admin, "server-session", IceGrid::Inactive); session->allocateObjectById(obj->ice_getIdentity()); obj->ice_ping(); waitForServerState(admin, "server-session", IceGrid::Active); session->destroy(); try { obj->ice_ping(); test(false); } catch(const Ice::NoEndpointException&) { } waitForServerState(admin, "server-session", IceGrid::Inactive); } catch(const Ice::LocalException& ex) { cerr << ex << endl; test(false); } cout << "ok" << endl; cout << "testing server disable... " << flush; try { test(admin->getServerState("server") == IceGrid::Inactive); admin->enableServer("server", false); try { communicator->stringToProxy("server")->ice_ping(); test(false); } catch(const Ice::NoEndpointException&) { } try { admin->startServer("server"); test(false); } catch(const IceGrid::ServerStartException&) { } test(admin->getServerState("server") == IceGrid::Inactive); test(admin->getServerState("server-manual") == IceGrid::Inactive); admin->enableServer("server-manual", false); try { communicator->stringToProxy("server-manual")->ice_ping(); test(false); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -