1 // **********************************************************************
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
8 // **********************************************************************
11 #include <Ice/Router.h>
12 #include <IceUtil/IceUtil.h>
13 #include <TestCommon.h>
20 class HelloI
: virtual public Hello
25 sayHello(const Ice::Current
& foo
)
27 // Do nothing, this is just a dummy servant.
31 class AMICallback
: public IceUtil::Shared
39 exception1(const Ice::Exception
&)
44 exception2(const Ice::Exception
& ex
)
46 test(dynamic_cast<const Ice::NotRegisteredException
*>(&ex
));
60 typedef IceUtil::Handle
<AMICallback
> AMICallbackPtr
;
63 allTests(const Ice::CommunicatorPtr
& communicator
, const string
& ref
)
65 ServerManagerPrx manager
= ServerManagerPrx::checkedCast(communicator
->stringToProxy(ref
));
66 TestLocatorPrx locator
= TestLocatorPrx::uncheckedCast(communicator
->getDefaultLocator());
69 TestLocatorRegistryPrx registry
= TestLocatorRegistryPrx::checkedCast(locator
->getRegistry());
72 cout
<< "testing stringToProxy... " << flush
;
73 Ice::ObjectPrx base
= communicator
->stringToProxy("test @ TestAdapter");
74 Ice::ObjectPrx base2
= communicator
->stringToProxy("test @ TestAdapter");
75 Ice::ObjectPrx base3
= communicator
->stringToProxy("test");
76 Ice::ObjectPrx base4
= communicator
->stringToProxy("ServerManager");
77 Ice::ObjectPrx base5
= communicator
->stringToProxy("test2");
78 Ice::ObjectPrx base6
= communicator
->stringToProxy("test @ ReplicatedAdapter");
81 cout
<< "testing ice_locator and ice_getLocator... " << flush
;
82 test(Ice::proxyIdentityEqual(base
->ice_getLocator(), communicator
->getDefaultLocator()));
83 Ice::LocatorPrx anotherLocator
= Ice::LocatorPrx::uncheckedCast(communicator
->stringToProxy("anotherLocator"));
84 base
= base
->ice_locator(anotherLocator
);
85 test(Ice::proxyIdentityEqual(base
->ice_getLocator(), anotherLocator
));
86 communicator
->setDefaultLocator(0);
87 base
= communicator
->stringToProxy("test @ TestAdapter");
88 test(!base
->ice_getLocator());
89 base
= base
->ice_locator(anotherLocator
);
90 test(Ice::proxyIdentityEqual(base
->ice_getLocator(), anotherLocator
));
91 communicator
->setDefaultLocator(locator
);
92 base
= communicator
->stringToProxy("test @ TestAdapter");
93 test(Ice::proxyIdentityEqual(base
->ice_getLocator(), communicator
->getDefaultLocator()));
96 // We also test ice_router/ice_getRouter (perhaps we should add a
97 // test/Ice/router test?)
99 test(!base
->ice_getRouter());
100 Ice::RouterPrx anotherRouter
= Ice::RouterPrx::uncheckedCast(communicator
->stringToProxy("anotherRouter"));
101 base
= base
->ice_router(anotherRouter
);
102 test(Ice::proxyIdentityEqual(base
->ice_getRouter(), anotherRouter
));
103 Ice::RouterPrx router
= Ice::RouterPrx::uncheckedCast(communicator
->stringToProxy("dummyrouter"));
104 communicator
->setDefaultRouter(router
);
105 base
= communicator
->stringToProxy("test @ TestAdapter");
106 test(Ice::proxyIdentityEqual(base
->ice_getRouter(), communicator
->getDefaultRouter()));
107 communicator
->setDefaultRouter(0);
108 base
= communicator
->stringToProxy("test @ TestAdapter");
109 test(!base
->ice_getRouter());
110 cout
<< "ok" << endl
;
112 cout
<< "starting server... " << flush
;
113 manager
->startServer();
114 cout
<< "ok" << endl
;
116 cout
<< "testing checked cast... " << flush
;
117 TestIntfPrx obj
= TestIntfPrx::checkedCast(base
);
119 TestIntfPrx obj2
= TestIntfPrx::checkedCast(base2
);
121 TestIntfPrx obj3
= TestIntfPrx::checkedCast(base3
);
123 ServerManagerPrx obj4
= ServerManagerPrx::checkedCast(base4
);
125 TestIntfPrx obj5
= TestIntfPrx::checkedCast(base5
);
127 TestIntfPrx obj6
= TestIntfPrx::checkedCast(base6
);
129 cout
<< "ok" << endl
;
131 cout
<< "testing id@AdapterId indirect proxy... " << flush
;
133 manager
->startServer();
138 catch(const Ice::LocalException
& ex
)
143 cout
<< "ok" << endl
;
145 cout
<< "testing id@ReplicaGroupId indirect proxy... " << flush
;
147 manager
->startServer();
152 catch(const Ice::LocalException
& ex
)
157 cout
<< "ok" << endl
;
159 cout
<< "testing identity indirect proxy... " << flush
;
161 manager
->startServer();
166 catch(const Ice::LocalException
& ex
)
175 catch(const Ice::LocalException
& ex
)
181 manager
->startServer();
186 catch(const Ice::LocalException
& ex
)
195 catch(const Ice::LocalException
& ex
)
201 manager
->startServer();
207 catch(const Ice::LocalException
& ex
)
213 manager
->startServer();
218 catch(const Ice::LocalException
& ex
)
224 manager
->startServer();
229 catch(const Ice::LocalException
& ex
)
235 manager
->startServer();
241 catch(const Ice::LocalException
& ex
)
246 cout
<< "ok" << endl
;
248 cout
<< "testing proxy with unknown identity... " << flush
;
251 base
= communicator
->stringToProxy("unknown/unknown");
255 catch (const Ice::NotRegisteredException
& ex
)
257 test(ex
.kindOfObject
== "object");
258 test(ex
.id
== "unknown/unknown");
260 cout
<< "ok" << endl
;
262 cout
<< "testing proxy with unknown adapter... " << flush
;
265 base
= communicator
->stringToProxy("test @ TestAdapterUnknown");
269 catch (const Ice::NotRegisteredException
& ex
)
271 test(ex
.kindOfObject
== "object adapter");
272 test(ex
.id
== "TestAdapterUnknown");
274 cout
<< "ok" << endl
;
276 cout
<< "testing locator cache timeout... " << flush
;
278 int count
= locator
->getRequestCount();
279 communicator
->stringToProxy("test@TestAdapter")->ice_locatorCacheTimeout(0)->ice_ping(); // No locator cache.
280 test(++count
== locator
->getRequestCount());
281 communicator
->stringToProxy("test@TestAdapter")->ice_locatorCacheTimeout(0)->ice_ping(); // No locator cache.
282 test(++count
== locator
->getRequestCount());
283 communicator
->stringToProxy("test@TestAdapter")->ice_locatorCacheTimeout(1)->ice_ping(); // 1s timeout.
284 test(count
== locator
->getRequestCount());
285 IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(1300));
286 communicator
->stringToProxy("test@TestAdapter")->ice_locatorCacheTimeout(1)->ice_ping(); // 1s timeout.
287 test(++count
== locator
->getRequestCount());
289 communicator
->stringToProxy("test")->ice_locatorCacheTimeout(0)->ice_ping(); // No locator cache.
291 test(count
== locator
->getRequestCount());
292 communicator
->stringToProxy("test")->ice_locatorCacheTimeout(1)->ice_ping(); // 1s timeout
293 test(count
== locator
->getRequestCount());
294 IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(1300));
295 communicator
->stringToProxy("test")->ice_locatorCacheTimeout(1)->ice_ping(); // 1s timeout
297 test(count
== locator
->getRequestCount());
299 communicator
->stringToProxy("test@TestAdapter")->ice_locatorCacheTimeout(-1)->ice_ping();
300 test(count
== locator
->getRequestCount());
301 communicator
->stringToProxy("test")->ice_locatorCacheTimeout(-1)->ice_ping();
302 test(count
== locator
->getRequestCount());
303 communicator
->stringToProxy("test@TestAdapter")->ice_ping();
304 test(count
== locator
->getRequestCount());
305 communicator
->stringToProxy("test")->ice_ping();
306 test(count
== locator
->getRequestCount());
308 test(communicator
->stringToProxy("test")->ice_locatorCacheTimeout(99)->ice_getLocatorCacheTimeout() == 99);
310 cout
<< "ok" << endl
;
312 cout
<< "testing proxy from server... " << flush
;
313 obj
= TestIntfPrx::checkedCast(communicator
->stringToProxy("test@TestAdapter"));
314 HelloPrx hello
= obj
->getHello();
315 test(hello
->ice_getAdapterId() == "TestAdapter");
317 hello
= obj
->getReplicatedHello();
318 test(hello
->ice_getAdapterId() == "ReplicatedAdapter");
320 cout
<< "ok" << endl
;
322 cout
<< "testing locator request queuing... " << flush
;
323 hello
= obj
->getReplicatedHello()->ice_locatorCacheTimeout(0)->ice_connectionCached(false);
324 count
= locator
->getRequestCount();
326 test(++count
== locator
->getRequestCount());
329 list
<Ice::AsyncResultPtr
> results
;
330 AMICallbackPtr cb
= new AMICallback
;
331 for(i
= 0; i
< 1000; i
++)
333 Ice::AsyncResultPtr result
= hello
->begin_sayHello(
334 newCallback_Hello_sayHello(cb
, &AMICallback::response1
, &AMICallback::exception1
));
335 results
.push_back(result
);
337 while(!results
.empty())
339 Ice::AsyncResultPtr result
= results
.front();
341 result
->waitForCompleted();
343 test(locator
->getRequestCount() > count
&& locator
->getRequestCount() < count
+ 999);
344 if(locator
->getRequestCount() > count
+ 800)
346 cout
<< "queuing = " << locator
->getRequestCount() - count
;
348 count
= locator
->getRequestCount();
349 hello
= hello
->ice_adapterId("unknown");
350 for(i
= 0; i
< 1000; i
++)
352 Ice::AsyncResultPtr result
= hello
->begin_sayHello(
353 newCallback_Hello_sayHello(cb
, &AMICallback::response2
, &AMICallback::exception2
));
354 results
.push_back(result
);
356 while(!results
.empty())
358 Ice::AsyncResultPtr result
= results
.front();
360 result
->waitForCompleted();
362 // Take into account the retries.
363 test(locator
->getRequestCount() > count
&& locator
->getRequestCount() < count
+ 1999);
364 if(locator
->getRequestCount() > count
+ 800)
366 cout
<< "queuing = " << locator
->getRequestCount() - count
;
368 cout
<< "ok" << endl
;
370 cout
<< "testing adapter locator cache... " << flush
;
373 communicator
->stringToProxy("test@TestAdapter3")->ice_ping();
376 catch(const Ice::NotRegisteredException
& ex
)
378 test(ex
.kindOfObject
== "object adapter");
379 test(ex
.id
== "TestAdapter3");
381 registry
->setAdapterDirectProxy("TestAdapter3", locator
->findAdapterById("TestAdapter"));
384 communicator
->stringToProxy("test@TestAdapter3")->ice_ping();
385 registry
->setAdapterDirectProxy("TestAdapter3", communicator
->stringToProxy("dummy:tcp"));
386 communicator
->stringToProxy("test@TestAdapter3")->ice_ping();
388 catch(const Ice::LocalException
&)
395 communicator
->stringToProxy("test@TestAdapter3")->ice_locatorCacheTimeout(0)->ice_ping();
398 catch(const Ice::LocalException
&)
403 communicator
->stringToProxy("test@TestAdapter3")->ice_ping();
406 catch(const Ice::LocalException
&)
409 registry
->setAdapterDirectProxy("TestAdapter3", locator
->findAdapterById("TestAdapter"));
412 communicator
->stringToProxy("test@TestAdapter3")->ice_ping();
414 catch(const Ice::LocalException
&)
420 cout
<< "testing well-known object locator cache... " << flush
;
422 registry
->addObject(communicator
->stringToProxy("test3@TestUnknown"));
425 communicator
->stringToProxy("test3")->ice_ping();
428 catch(const Ice::NotRegisteredException
& ex
)
430 test(ex
.kindOfObject
== "object adapter");
431 test(ex
.id
== "TestUnknown");
433 registry
->addObject(communicator
->stringToProxy("test3@TestAdapter4")); // Update
434 registry
->setAdapterDirectProxy("TestAdapter4", communicator
->stringToProxy("dummy:tcp"));
437 communicator
->stringToProxy("test3")->ice_ping();
440 catch(const Ice::LocalException
&)
443 registry
->setAdapterDirectProxy("TestAdapter4", locator
->findAdapterById("TestAdapter"));
446 communicator
->stringToProxy("test3")->ice_ping();
448 catch(const Ice::LocalException
&)
453 registry
->setAdapterDirectProxy("TestAdapter4", communicator
->stringToProxy("dummy:tcp"));
456 communicator
->stringToProxy("test3")->ice_ping();
458 catch(const Ice::LocalException
&)
465 communicator
->stringToProxy("test@TestAdapter4")->ice_locatorCacheTimeout(0)->ice_ping();
468 catch(const Ice::LocalException
&)
473 communicator
->stringToProxy("test@TestAdapter4")->ice_ping();
476 catch(const Ice::LocalException
&)
481 communicator
->stringToProxy("test3")->ice_ping();
484 catch(const Ice::LocalException
&)
487 registry
->addObject(communicator
->stringToProxy("test3@TestAdapter"));
490 communicator
->stringToProxy("test3")->ice_ping();
492 catch(const Ice::LocalException
&)
497 registry
->addObject(communicator
->stringToProxy("test4"));
500 communicator
->stringToProxy("test4")->ice_ping();
503 catch(const Ice::NoEndpointException
&)
506 cout
<< "ok" << endl
;
508 cout
<< "testing locator cache background updates... " << flush
;
510 Ice::InitializationData initData
;
511 initData
.properties
= communicator
->getProperties()->clone();
512 initData
.properties
->setProperty("Ice.BackgroundLocatorCacheUpdates", "1");
513 Ice::CommunicatorPtr ic
= Ice::initialize(initData
);
515 registry
->setAdapterDirectProxy("TestAdapter5", locator
->findAdapterById("TestAdapter"));
516 registry
->addObject(communicator
->stringToProxy("test3@TestAdapter"));
518 int count
= locator
->getRequestCount();
519 ic
->stringToProxy("test@TestAdapter5")->ice_locatorCacheTimeout(0)->ice_ping(); // No locator cache.
520 ic
->stringToProxy("test3")->ice_locatorCacheTimeout(0)->ice_ping(); // No locator cache.
522 test(count
== locator
->getRequestCount());
523 registry
->setAdapterDirectProxy("TestAdapter5", 0);
524 registry
->addObject(communicator
->stringToProxy("test3:tcp"));
525 ic
->stringToProxy("test@TestAdapter5")->ice_locatorCacheTimeout(10)->ice_ping(); // 10s timeout.
526 ic
->stringToProxy("test3")->ice_locatorCacheTimeout(10)->ice_ping(); // 10s timeout.
527 test(count
== locator
->getRequestCount());
528 IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(1200));
530 // The following request should trigger the background updates but still use the cached endpoints
531 // and therefore succeed.
532 ic
->stringToProxy("test@TestAdapter5")->ice_locatorCacheTimeout(1)->ice_ping(); // 1s timeout.
533 ic
->stringToProxy("test3")->ice_locatorCacheTimeout(1)->ice_ping(); // 1s timeout.
539 ic
->stringToProxy("test@TestAdapter5")->ice_locatorCacheTimeout(1)->ice_ping(); // 1s timeout.
540 IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(10));
543 catch(const Ice::LocalException
&)
545 // Expected to fail once they endpoints have been updated in the background.
551 ic
->stringToProxy("test3")->ice_locatorCacheTimeout(1)->ice_ping(); // 1s timeout.
552 IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(10));
555 catch(const Ice::LocalException
&)
557 // Expected to fail once they endpoints have been updated in the background.
561 cout
<< "ok" << endl
;
563 cout
<< "testing proxy from server after shutdown... " << flush
;
564 hello
= obj
->getReplicatedHello();
566 manager
->startServer();
568 cout
<< "ok" << endl
;
570 cout
<< "testing object migration... " << flush
;
571 hello
= HelloPrx::checkedCast(communicator
->stringToProxy("hello"));
578 cout
<< "ok" << endl
;
580 cout
<< "shutdown server... " << flush
;
582 cout
<< "ok" << endl
;
584 cout
<< "testing whether server is gone... " << flush
;
590 catch(const Ice::LocalException
&)
598 catch(const Ice::LocalException
&)
606 catch(const Ice::LocalException
&)
609 cout
<< "ok" << endl
;
611 cout
<< "testing indirect proxies to collocated objects... " << flush
;
613 // Set up test for calling a collocated object through an indirect, adapterless reference.
615 Ice::PropertiesPtr properties
= communicator
->getProperties();
616 properties
->setProperty("Ice.PrintAdapterReady", "0");
617 Ice::ObjectAdapterPtr adapter
= communicator
->createObjectAdapterWithEndpoints("Hello", "default");
618 adapter
->setLocator(locator
);
621 id
.name
= IceUtil::generateUUID();
622 registry
->addObject(adapter
->add(new HelloI
, id
));
627 HelloPrx helloPrx
= HelloPrx::checkedCast(communicator
->stringToProxy(communicator
->identityToString(id
)));
628 Ice::ConnectionPtr connection
= helloPrx
->ice_getConnection();
631 catch(const Ice::CollocationOptimizationException
&)
634 adapter
->deactivate();
635 cout
<< "ok" << endl
;
637 cout
<< "shutdown server manager... " << flush
;
639 cout
<< "ok" << endl
;