Exemplo n.º 1
0
void
allTestsWithDeploy(const Ice::CommunicatorPtr& communicator)
{
    cout << "testing stringToProxy... " << flush;
    Ice::ObjectPrx base = communicator->stringToProxy("test @ TestAdapter");
    test(base);
    Ice::ObjectPrx base2 = communicator->stringToProxy("test");
    test(base2);
    cout << "ok" << endl;

    cout << "testing checked cast... " << flush;
    TestIntfPrx obj = TestIntfPrx::checkedCast(base);
    test(obj);
    test(obj == base);
    TestIntfPrx obj2 = TestIntfPrx::checkedCast(base2);
    test(obj2);
    test(obj2 == base2);
    cout << "ok" << endl;

    cout << "pinging server... " << flush;
    obj->ice_ping();
    obj2->ice_ping();
    cout << "ok" << endl;

    cout << "testing reference with unknown identity... " << flush;
    try
    {
        communicator->stringToProxy("unknown/unknown")->ice_ping();
        test(false);
    }
    catch (const Ice::NotRegisteredException& ex)
    {
        test(ex.kindOfObject == "object");
        test(ex.id == "unknown/unknown");
    }
    cout << "ok" << endl;

    cout << "testing reference with unknown adapter... " << flush;
    try
    {
        communicator->stringToProxy("test @ TestAdapterUnknown")->ice_ping();
        test(false);
    }
    catch (const Ice::NotRegisteredException& ex)
    {
        test(ex.kindOfObject == "object adapter");
        test(ex.id == "TestAdapterUnknown");
    }
    cout << "ok" << endl;

    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->enableServer("server", false);
    admin->stopServer("server");

    cout << "testing whether server is still reachable... " << flush;
    try
    {
        obj = TestIntfPrx::checkedCast(base);
        test(false);
    }
    catch(const Ice::NoEndpointException&)
    {
    }
    try
    {
        obj2 = TestIntfPrx::checkedCast(base2);
        test(false);
    }
    catch(const Ice::NoEndpointException&)
    {
    }
    
    admin->enableServer("server", true);

    try
    {
        obj = TestIntfPrx::checkedCast(base);
    }
    catch(const Ice::NoEndpointException&)
    {
        test(false);
    }
    try
    {
        obj2 = TestIntfPrx::checkedCast(base2);
    }
    catch(const Ice::NoEndpointException&)
    {
        test(false);
    }
    cout << "ok" << endl;

    admin->stopServer("server");

    keepAlive->destroy();
    keepAlive->getThreadControl().join();
    keepAlive = 0;

    session->destroy();
}
Exemplo n.º 2
0
void
allTests(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);
        }
        catch(const Ice::NoEndpointException&)
        {
        }
        try
        {
            admin->startServer("server-manual");
            test(false);
        }
        catch(const IceGrid::ServerStartException&)
        {
        }
        test(admin->getServerState("server-manual") == IceGrid::Inactive);

        test(admin->getServerState("server-always") == IceGrid::Active);
        admin->enableServer("server-always", false);
        admin->stopServer("server-always");
        test(admin->getServerState("server-always") == IceGrid::Inactive);
        try
        {
            communicator->stringToProxy("server-always")->ice_ping();
            test(false);
        }
        catch(const Ice::NoEndpointException&)
        {
        }
        try
        {
            admin->startServer("server-always");
            test(false);
        }
        catch(const IceGrid::ServerStartException&)
        {
        }
        test(admin->getServerState("server-always") == IceGrid::Inactive);
    }
    catch(const Ice::LocalException& ex)
    {
        cerr << ex << endl;
        test(false);
    }
    cout << "ok" << endl;
    

    cout << "testing server enable... " << flush;
    try
    {
        test(admin->getServerState("server") == IceGrid::Inactive);
        admin->enableServer("server", true);
        communicator->stringToProxy("server")->ice_ping();
        admin->stopServer("server");
        test(admin->getServerState("server") == IceGrid::Inactive);
        admin->startServer("server");
        test(admin->getServerState("server") == IceGrid::Active);
        admin->stopServer("server");
        test(admin->getServerState("server") == IceGrid::Inactive);

        test(admin->getServerState("server-manual") == IceGrid::Inactive);
        admin->enableServer("server-manual", true);
        try
        {
            communicator->stringToProxy("server-manual")->ice_ping();
            test(false);
        }
        catch(const Ice::NoEndpointException&)
        {
        }
        test(admin->getServerState("server-manual") == IceGrid::Inactive);
        admin->startServer("server-manual");
        test(admin->getServerState("server-manual") == IceGrid::Active);
        admin->stopServer("server-manual");
        test(admin->getServerState("server-manual") == IceGrid::Inactive);

        test(admin->getServerState("server-always") == IceGrid::Inactive);
        admin->enableServer("server-always", true);
        waitForServerState(admin, "server-always", IceGrid::Active);
        admin->stopServer("server-always");
        try
        {
            admin->startServer("server-always");
//          test(false);
        }
        catch(const IceGrid::ServerStartException&)
        {
        }
        test(admin->getServerState("server-always") == IceGrid::Active);
    }
    catch(const Ice::LocalException& ex)
    {
        cerr << ex << endl;
        test(false);
    }
    cout << "ok" << endl;       

    cout << "testing activation failure... " << flush;
    try
    {
        int i;
        const int nThreads = 3;
        Ice::ObjectPrx invalid = communicator->stringToProxy("invalid-exe");

        vector<PingThreadPtr> threads;
        threads.reserve(nThreads);
        vector<PingThreadPtr>::const_iterator p;
        for(i = 0; i < nThreads; i++)
        {
            threads.push_back(new PingThread(invalid, 10));
        }
        for(p = threads.begin(); p != threads.end(); ++p)
        {
            (*p)->start();
        }
        for(p = threads.begin(); p != threads.end(); ++p)
        {
            auto_ptr<Ice::LocalException> ex((*p)->waitUntilFinished());
            test(dynamic_cast<Ice::NoEndpointException*>(ex.get()));
        }
        threads.resize(0);

        invalid = communicator->stringToProxy("invalid-pwd");
        for(i = 0; i < nThreads; i++)
        {
            threads.push_back(new PingThread(invalid, 10));
        }
        for(p = threads.begin(); p != threads.end(); ++p)
        {
            (*p)->start();
        }
        for(p = threads.begin(); p != threads.end(); ++p)
        {
            auto_ptr<Ice::LocalException> ex((*p)->waitUntilFinished());
            test(dynamic_cast<Ice::NoEndpointException*>(ex.get()));
        }
        threads.resize(0);

        invalid = communicator->stringToProxy("fail-on-startup");
        for(i = 0; i < nThreads; i++)
        {
            threads.push_back(new PingThread(invalid, 5));
        }
        for(p = threads.begin(); p != threads.end(); ++p)
        {
            (*p)->start();
        }
        for(p = threads.begin(); p != threads.end(); ++p)
        {
            auto_ptr<Ice::LocalException> ex((*p)->waitUntilFinished());
            test(dynamic_cast<Ice::NoEndpointException*>(ex.get()));
        }
        threads.resize(0);

    }
    catch(const Ice::LocalException& ex)
    {
        cerr << ex << endl;
        test(false);
    }
    cout << "ok" << endl;

    cout << "testing activation timeout... " << flush;
    try
    {
        test(admin->getServerState("server-activation-timeout") == IceGrid::Inactive);
        const int nThreads = 5;
        Ice::ObjectPrx proxy = communicator->stringToProxy("server-activation-timeout");
        vector<PingThreadPtr> threads;
        threads.reserve(nThreads);
        vector<PingThreadPtr>::const_iterator p;
        int i;
        for(i = 0; i < nThreads; i++)
        {
            threads.push_back(new PingThread(proxy, 1));
        }
        for(p = threads.begin(); p != threads.end(); ++p)
        {
            (*p)->start();
        }
        for(p = threads.begin(); p != threads.end(); ++p)
        {
            auto_ptr<Ice::LocalException> ex((*p)->waitUntilFinished());
            test(dynamic_cast<Ice::NoEndpointException*>(ex.get()));
        }
        admin->stopServer("server-activation-timeout");
    }
    catch(const IceGrid::ServerStopException& ex)
    {
        cerr << ex << ": " << ex.reason << endl;
        test(false);
    }
    catch(const Ice::LocalException& ex)
    {
        cerr << ex << endl;
        test(false);
    }
    cout << "ok" << endl;       

    cout << "testing deactivation timeout... " << flush;
    try
    {
        test(admin->getServerState("server-deactivation-timeout") == IceGrid::Inactive);
        communicator->stringToProxy("server-deactivation-timeout")->ice_ping();
        admin->stopServer("server-deactivation-timeout");
    }
    catch(const Ice::LocalException& ex)
    {
        cerr << ex << endl;
        test(false);
    }
    cout << "ok" << endl;       

    cout << "testing permanent disable on failure... " << flush;
    try
    {
        test(admin->getServerState("server1") == IceGrid::Inactive);
        TestIntfPrx obj = TestIntfPrx::checkedCast(communicator->stringToProxy("server1"));
        waitForServerState(admin, "server1", IceGrid::Active);
        obj->fail();
        waitForServerState(admin, "server1", IceGrid::Inactive);
        try
        {
            obj->ice_ping();    
            test(false);
        }
        catch(const Ice::NoEndpointException&)
        {
        }       
        test(!admin->isServerEnabled("server1"));

        test(admin->getServerState("server1-manual") == IceGrid::Inactive);
        admin->startServer("server1-manual");
        test(admin->getServerState("server1-manual") == IceGrid::Active);
        obj = TestIntfPrx::checkedCast(communicator->stringToProxy("server1-manual"));  
        test(admin->getServerState("server1-manual") == IceGrid::Active);
        obj->fail();
        waitForServerState(admin, "server1-manual", IceGrid::Inactive);
        test(!admin->isServerEnabled("server1-manual"));

        test(admin->getServerState("server1-always") == IceGrid::Active);
        obj = TestIntfPrx::checkedCast(communicator->stringToProxy("server1-always"));
        obj->fail();
        waitForServerState(admin, "server1-always", IceGrid::Inactive);
        test(!admin->isServerEnabled("server1-always"));
    }
    catch(const Ice::LocalException& ex)
    {
        cerr << ex << endl;
        test(false);
    }
    cout << "ok" << endl;       


    cout << "testing temporary disable on failure... " << flush;
    try
    {
        test(admin->getServerState("server2") == IceGrid::Inactive);
        TestIntfPrx obj = TestIntfPrx::checkedCast(communicator->stringToProxy("server2"));
        waitForServerState(admin, "server2", IceGrid::Active);
        obj->fail();
        waitForServerState(admin, "server2", IceGrid::Inactive);
        try
        {
            obj->ice_ping();    
            test(false);
        }
        catch(const Ice::NoEndpointException&)
        {
        }       
        test(!admin->isServerEnabled("server2"));
        nRetry = 0;
        while(!admin->isServerEnabled("server2") && nRetry < 15)
        {
            IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500));
            ++nRetry;
            try
            {
                obj->ice_ping();    
            }
            catch(const Ice::NoEndpointException&)
            {
            }
        }
        test(admin->isServerEnabled("server2"));
        waitForServerState(admin, "server2", IceGrid::Active);
        obj->ice_ping();
        admin->stopServer("server2");

        test(admin->getServerState("server2-manual") == IceGrid::Inactive);
        admin->startServer("server2-manual");
        test(admin->getServerState("server2-manual") == IceGrid::Active);
        obj = TestIntfPrx::checkedCast(communicator->stringToProxy("server2-manual"));
        obj->fail();
        waitForServerState(admin, "server2-manual", IceGrid::Inactive);
        test(!admin->isServerEnabled("server2-manual"));
        admin->startServer("server2-manual");
        test(admin->isServerEnabled("server2-manual"));
        test(admin->getServerState("server2-manual") == IceGrid::Active);
        admin->stopServer("server2-manual");

        test(admin->getServerState("server2-always") == IceGrid::Active);
        obj = TestIntfPrx::checkedCast(communicator->stringToProxy("server2-always"));
        obj->fail();
        waitForServerState(admin, "server2-always", IceGrid::Inactive);
        test(!admin->isServerEnabled("server2-always"));
        nRetry = 0;
        while((!admin->isServerEnabled("server2-always") ||
               admin->getServerState("server2-always") != IceGrid::Active) &&
              nRetry < 15)
        {
            IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500));
            ++nRetry;
        }
        test(admin->isServerEnabled("server2-always") && admin->getServerState("server2-always") == IceGrid::Active);
        obj->ice_ping();
    }
    catch(const Ice::LocalException& ex)
    {
        cerr << ex << endl;
        test(false);
    }
    cout << "ok" << endl;

    admin->stopServer("node-1");
    admin->stopServer("node-2");
    
    keepAlive->destroy();
    keepAlive->getThreadControl().join();
    keepAlive = 0;

    session->destroy();
}
Exemplo n.º 3
0
int
HelloClient::run(int argc, char* argv[])
{
    if(argc > 1)
    {
        cerr << appName() << ": too many arguments" << endl;
        return EXIT_FAILURE;
    }

    int status = EXIT_SUCCESS;

    IceGrid::RegistryPrx registry =
        IceGrid::RegistryPrx::checkedCast(communicator()->stringToProxy("DemoIceGrid/Registry"));
    if(!registry)
    {
        cerr << argv[0] << ": could not contact registry" << endl;
        return EXIT_FAILURE;
    }

    IceGrid::SessionPrx session;
    while(true)
    {
        cout << "This demo accepts any user-id / password combination.\n";

        string id;
        cout << "user id: " << flush;
        getline(cin, id);
        id = trim(id);

        string password;
        cout << "password: "******"permission denied:\n" << ex.reason << endl;
        }
    }

    SessionKeepAliveThreadPtr keepAlive = new SessionKeepAliveThread(session, registry->getSessionTimeout() / 2);
    keepAlive->start();

    try
    {
        HelloPrx hello = HelloPrx::checkedCast(session->allocateObjectById(communicator()->stringToIdentity("hello")));

        menu();

        char c;
        do
        {
            try
            {
                cout << "==> ";
                cin >> c;
                if(c == 't')
                {
                    hello->sayHello();
                }
                else if(c == 'x')
                {
                    // Nothing to do
                }
                else if(c == '?')
                {
                    menu();
                }
                else
                {
                    cout << "unknown command `" << c << "'" << endl;
                    menu();
                }
            }
            catch(const Ice::Exception& ex)
            {
                cerr << ex << endl;
            }
        }
        while(cin.good() && c != 'x');
    }
    catch(const IceGrid::AllocationException& ex)
    {
        cerr << argv[0] << ": could not allocate object: " << ex.reason << endl;
        status = EXIT_FAILURE;
    }
    catch(const IceGrid::ObjectNotRegisteredException&)
    {
        cerr << argv[0] << ": object not registered with registry" << endl;
        status = EXIT_FAILURE;
    }
    catch(const Ice::Exception& ex)
    {
        cerr << ex << endl;
        status = EXIT_FAILURE;
    }
    catch(...)
    {
        cerr << "unexpected exception" << endl;
        status = EXIT_FAILURE;
    }

    //
    // Destroy the keepAlive thread and the sesion object otherwise
    // the session will be kept allocated until the timeout occurs.
    // Destroying the session will release all allocated objects.
    //
    keepAlive->destroy();
    keepAlive->getThreadControl().join();

    session->destroy();

    return status;
}