⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alltests.cpp

📁 ICE-3.2 一个开源的中间件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// **********************************************************************//// 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 <Ice/BuiltinSequences.h>#include <Ice/IdentityUtil.h>#include <IceGrid/Query.h>#include <IceGrid/Registry.h>#include <IceGrid/Admin.h>#include <IceUtil/Thread.h>#include <TestCommon.h>#include <Test.h>#include <set>using namespace std;using namespace Test;using namespace IceGrid;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;voidinstantiateServer(const AdminPrx& admin, const string& templ, const string& node, const map<string, string>& params){    ServerInstanceDescriptor desc;    desc._cpp_template = templ;    desc.parameterValues = params;    NodeUpdateDescriptor nodeUpdate;    nodeUpdate.name = node;    nodeUpdate.serverInstances.push_back(desc);    ApplicationUpdateDescriptor update;    update.name = "Test";    update.nodes.push_back(nodeUpdate);    try    {        admin->updateApplication(update);    }    catch(DeploymentException& ex)    {        cerr << ex.reason << endl;        test(false);    }}voidremoveServer(const AdminPrx& admin, const string& id){    try    {        admin->stopServer(id);    }    catch(const ServerStopException&)    {    }    catch(const NodeUnreachableException&)    {    }    catch(const Ice::UserException& ex)    {        cerr << ex << endl;        test(false);    }    NodeUpdateDescriptor nodeUpdate;    nodeUpdate.name = "localnode";    nodeUpdate.removeServers.push_back(id);    ApplicationUpdateDescriptor update;    update.name = "Test";    update.nodes.push_back(nodeUpdate);    try    {        admin->updateApplication(update);    }    catch(DeploymentException& ex)    {        cerr << ex.reason << endl;        test(false);    }}voidallTests(const Ice::CommunicatorPtr& comm){    RegistryPrx registry = IceGrid::RegistryPrx::checkedCast(comm->stringToProxy("IceGrid/Registry"));    test(registry);    AdminSessionPrx session = registry->createAdminSession("foo", "bar");    SessionKeepAliveThreadPtr keepAlive = new SessionKeepAliveThread(session, registry->getSessionTimeout()/2);    keepAlive->start();    AdminPrx admin = session->getAdmin();    test(admin);    set<string> serverReplicaIds;    serverReplicaIds.insert("Server1.ReplicatedAdapter");    serverReplicaIds.insert("Server2.ReplicatedAdapter");    serverReplicaIds.insert("Server3.ReplicatedAdapter");    set<string> svcReplicaIds;    svcReplicaIds.insert("IceBox1.Service1.Service1");    svcReplicaIds.insert("IceBox1.Service2.Service2");    svcReplicaIds.insert("IceBox1.Service3.Service3");    cout << "testing Query::findAllReplicas... " << flush;    {        map<string, string> params;        params["replicaGroup"] = "RoundRobin";        params["id"] = "Server1";        instantiateServer(admin, "Server", "localnode", params);        params["id"] = "Server2";        instantiateServer(admin, "Server", "localnode", params);        params["id"] = "Server3";        instantiateServer(admin, "Server", "localnode", params);        QueryPrx query = IceGrid::QueryPrx::checkedCast(comm->stringToProxy("IceGrid/Query"));        test(query);                TestIntfPrx obj = TestIntfPrx::uncheckedCast(comm->stringToProxy("dummy@RoundRobin"));        Ice::ObjectProxySeq objs = query->findAllReplicas(obj);        test(objs.size() == 3);        test(serverReplicaIds.find(objs[0]->ice_getAdapterId()) != serverReplicaIds.end());        test(serverReplicaIds.find(objs[1]->ice_getAdapterId()) != serverReplicaIds.end());        test(serverReplicaIds.find(objs[2]->ice_getAdapterId()) != serverReplicaIds.end());                obj = TestIntfPrx::uncheckedCast(comm->stringToProxy("dummy@dummy"));        objs = query->findAllReplicas(obj);        test(objs.empty());        obj = TestIntfPrx::uncheckedCast(comm->stringToProxy("dummy@Server1.ReplicatedAdapter"));        objs = query->findAllReplicas(obj);        test(objs.empty());        obj = TestIntfPrx::uncheckedCast(comm->stringToProxy("dummy:tcp"));        objs = query->findAllReplicas(obj);        test(objs.empty());                removeServer(admin, "Server1");        removeServer(admin, "Server2");        removeServer(admin, "Server3");    }    cout << "ok" << endl;    cout << "testing replication with round-robin load balancing... " << flush;    {        map<string, string> params;        params["replicaGroup"] = "RoundRobin";        params["id"] = "Server1";        instantiateServer(admin, "Server", "localnode", params);        params["id"] = "Server2";        instantiateServer(admin, "Server", "localnode", params);        params["id"] = "Server3";        instantiateServer(admin, "Server", "localnode", params);        TestIntfPrx obj = TestIntfPrx::uncheckedCast(comm->stringToProxy("RoundRobin"));        try        {            test(obj->getReplicaIdAndShutdown() == "Server1.ReplicatedAdapter");            test(obj->getReplicaIdAndShutdown() == "Server2.ReplicatedAdapter");            test(obj->getReplicaIdAndShutdown() == "Server3.ReplicatedAdapter");                }        catch(const Ice::LocalException& ex)        {            cerr << ex << endl;            test(false);        }        removeServer(admin, "Server1");        removeServer(admin, "Server2");        removeServer(admin, "Server3");    }    {        map<string, string> params;        params["replicaGroup"] = "RoundRobin";        params["id"] = "IceBox1";        instantiateServer(admin, "IceBox", "localnode", params);        TestIntfPrx obj = TestIntfPrx::uncheckedCast(comm->stringToProxy("RoundRobin"));        try        {            test(obj->getReplicaIdAndShutdown() == "IceBox1.Service1.Service1");            test(obj->getReplicaIdAndShutdown() == "IceBox1.Service2.Service2");            test(obj->getReplicaIdAndShutdown() == "IceBox1.Service3.Service3");        }        catch(const Ice::LocalException& ex)        {            cerr << ex << endl;            test(false);        }        removeServer(admin, "IceBox1");    }    cout << "ok" << endl;    cout << "testing replication with ordered load balancing... " << flush;    {        map<string, string> params;        params["replicaGroup"] = "Ordered";        params["id"] = "Server1";        params["priority"] = "3";        instantiateServer(admin, "Server", "localnode", params);        params["id"] = "Server2";        params["priority"] = "1";        instantiateServer(admin, "Server", "localnode", params);        params["id"] = "Server3";        params["priority"] = "2";        instantiateServer(admin, "Server", "localnode", params);        TestIntfPrx obj = TestIntfPrx::uncheckedCast(comm->stringToProxy("Ordered"));        try        {            test(obj->getReplicaIdAndShutdown() == "Server2.ReplicatedAdapter");            admin->enableServer("Server2", false);            test(obj->getReplicaIdAndShutdown() == "Server3.ReplicatedAdapter");            admin->enableServer("Server3", false);            test(obj->getReplicaIdAndShutdown() == "Server1.ReplicatedAdapter");        }        catch(const Ice::LocalException& ex)        {            cerr << ex << endl;            test(false);        }        removeServer(admin, "Server1");        removeServer(admin, "Server2");        removeServer(admin, "Server3");    }    {        map<string, string> params;        params["replicaGroup"] = "Ordered";        params["id"] = "IceBox1";        instantiateServer(admin, "IceBox", "localnode", params);        TestIntfPrx obj = TestIntfPrx::uncheckedCast(comm->stringToProxy("Ordered"));        try        {            test(obj->getReplicaIdAndShutdown() == "IceBox1.Service3.Service3");            test(obj->getReplicaIdAndShutdown() == "IceBox1.Service2.Service2");            test(obj->getReplicaIdAndShutdown() == "IceBox1.Service1.Service1");        }        catch(const Ice::LocalException& ex)        {            cerr << ex << endl;            test(false);        }        removeServer(admin, "IceBox1");    }    cout << "ok" << endl;    cout << "testing replication with random load balancing... " << flush;    {        map<string, string> params;        params["replicaGroup"] = "Random";        params["id"] = "Server1";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -