ICE 3.4.2
[php5-ice-freebsdport.git] / cpp / test / Ice / location / AllTests.cpp
blob6023616bcbd58073e51eaca4bef9807398812918
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 #include <Ice/Ice.h>
11 #include <Ice/Router.h>
12 #include <IceUtil/IceUtil.h>
13 #include <TestCommon.h>
14 #include <Test.h>
15 #include <list>
17 using namespace std;
18 using namespace Test;
20 class HelloI : virtual public Hello
22 public:
24 virtual void
25 sayHello(const Ice::Current& foo)
27 // Do nothing, this is just a dummy servant.
31 class AMICallback : public IceUtil::Shared
33 public:
34 AMICallback()
38 void
39 exception1(const Ice::Exception&)
41 test(false);
43 void
44 exception2(const Ice::Exception& ex)
46 test(dynamic_cast<const Ice::NotRegisteredException*>(&ex));
49 void
50 response1()
54 void
55 response2()
57 test(false);
60 typedef IceUtil::Handle<AMICallback> AMICallbackPtr;
62 void
63 allTests(const Ice::CommunicatorPtr& communicator, const string& ref)
65 ServerManagerPrx manager = ServerManagerPrx::checkedCast(communicator->stringToProxy(ref));
66 TestLocatorPrx locator = TestLocatorPrx::uncheckedCast(communicator->getDefaultLocator());
67 test(manager);
69 TestLocatorRegistryPrx registry = TestLocatorRegistryPrx::checkedCast(locator->getRegistry());
70 test(registry);
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");
79 cout << "ok" << endl;
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);
118 test(obj);
119 TestIntfPrx obj2 = TestIntfPrx::checkedCast(base2);
120 test(obj2);
121 TestIntfPrx obj3 = TestIntfPrx::checkedCast(base3);
122 test(obj3);
123 ServerManagerPrx obj4 = ServerManagerPrx::checkedCast(base4);
124 test(obj4);
125 TestIntfPrx obj5 = TestIntfPrx::checkedCast(base5);
126 test(obj5);
127 TestIntfPrx obj6 = TestIntfPrx::checkedCast(base6);
128 test(obj6);
129 cout << "ok" << endl;
131 cout << "testing id@AdapterId indirect proxy... " << flush;
132 obj->shutdown();
133 manager->startServer();
136 obj2->ice_ping();
138 catch(const Ice::LocalException& ex)
140 cerr << ex << endl;
141 test(false);
143 cout << "ok" << endl;
145 cout << "testing id@ReplicaGroupId indirect proxy... " << flush;
146 obj->shutdown();
147 manager->startServer();
150 obj6->ice_ping();
152 catch(const Ice::LocalException& ex)
154 cerr << ex << endl;
155 test(false);
157 cout << "ok" << endl;
159 cout << "testing identity indirect proxy... " << flush;
160 obj->shutdown();
161 manager->startServer();
164 obj3->ice_ping();
166 catch(const Ice::LocalException& ex)
168 cerr << ex << endl;
169 test(false);
173 obj2->ice_ping();
175 catch(const Ice::LocalException& ex)
177 cerr << ex << endl;
178 test(false);
180 obj->shutdown();
181 manager->startServer();
184 obj2->ice_ping();
186 catch(const Ice::LocalException& ex)
188 cerr << ex << endl;
189 test(false);
193 obj3->ice_ping();
195 catch(const Ice::LocalException& ex)
197 cerr << ex << endl;
198 test(false);
200 obj->shutdown();
201 manager->startServer();
205 obj2->ice_ping();
207 catch(const Ice::LocalException& ex)
209 cerr << ex << endl;
210 test(false);
212 obj->shutdown();
213 manager->startServer();
216 obj3->ice_ping();
218 catch(const Ice::LocalException& ex)
220 cerr << ex << endl;
221 test(false);
223 obj->shutdown();
224 manager->startServer();
227 obj2->ice_ping();
229 catch(const Ice::LocalException& ex)
231 cerr << ex << endl;
232 test(false);
234 obj->shutdown();
235 manager->startServer();
239 obj5->ice_ping();
241 catch(const Ice::LocalException& ex)
243 cerr << ex << endl;
244 test(false);
246 cout << "ok" << endl;
248 cout << "testing proxy with unknown identity... " << flush;
251 base = communicator->stringToProxy("unknown/unknown");
252 base->ice_ping();
253 test(false);
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");
266 base->ice_ping();
267 test(false);
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.
290 count += 2;
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
296 count += 2;
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");
316 hello->sayHello();
317 hello = obj->getReplicatedHello();
318 test(hello->ice_getAdapterId() == "ReplicatedAdapter");
319 hello->sayHello();
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();
325 hello->ice_ping();
326 test(++count == locator->getRequestCount());
327 int i;
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();
340 results.pop_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();
359 results.pop_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();
374 test(false);
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&)
390 test(false);
395 communicator->stringToProxy("test@TestAdapter3")->ice_locatorCacheTimeout(0)->ice_ping();
396 test(false);
398 catch(const Ice::LocalException&)
403 communicator->stringToProxy("test@TestAdapter3")->ice_ping();
404 test(false);
406 catch(const Ice::LocalException&)
409 registry->setAdapterDirectProxy("TestAdapter3", locator->findAdapterById("TestAdapter"));
412 communicator->stringToProxy("test@TestAdapter3")->ice_ping();
414 catch(const Ice::LocalException&)
416 test(false);
418 cout << "ok" <<endl;
420 cout << "testing well-known object locator cache... " << flush;
422 registry->addObject(communicator->stringToProxy("test3@TestUnknown"));
425 communicator->stringToProxy("test3")->ice_ping();
426 test(false);
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();
438 test(false);
440 catch(const Ice::LocalException&)
443 registry->setAdapterDirectProxy("TestAdapter4", locator->findAdapterById("TestAdapter"));
446 communicator->stringToProxy("test3")->ice_ping();
448 catch(const Ice::LocalException&)
450 test(false);
453 registry->setAdapterDirectProxy("TestAdapter4", communicator->stringToProxy("dummy:tcp"));
456 communicator->stringToProxy("test3")->ice_ping();
458 catch(const Ice::LocalException&)
460 test(false);
465 communicator->stringToProxy("test@TestAdapter4")->ice_locatorCacheTimeout(0)->ice_ping();
466 test(false);
468 catch(const Ice::LocalException&)
473 communicator->stringToProxy("test@TestAdapter4")->ice_ping();
474 test(false);
476 catch(const Ice::LocalException&)
481 communicator->stringToProxy("test3")->ice_ping();
482 test(false);
484 catch(const Ice::LocalException&)
487 registry->addObject(communicator->stringToProxy("test3@TestAdapter"));
490 communicator->stringToProxy("test3")->ice_ping();
492 catch(const Ice::LocalException&)
494 test(false);
497 registry->addObject(communicator->stringToProxy("test4"));
500 communicator->stringToProxy("test4")->ice_ping();
501 test(false);
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.
521 count += 3;
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.
537 while(true)
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.
549 while(true)
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.
559 ic->destroy();
561 cout << "ok" << endl;
563 cout << "testing proxy from server after shutdown... " << flush;
564 hello = obj->getReplicatedHello();
565 obj->shutdown();
566 manager->startServer();
567 hello->sayHello();
568 cout << "ok" << endl;
570 cout << "testing object migration... " << flush;
571 hello = HelloPrx::checkedCast(communicator->stringToProxy("hello"));
572 obj->migrateHello();
573 hello->sayHello();
574 obj->migrateHello();
575 hello->sayHello();
576 obj->migrateHello();
577 hello->sayHello();
578 cout << "ok" << endl;
580 cout << "shutdown server... " << flush;
581 obj->shutdown();
582 cout << "ok" << endl;
584 cout << "testing whether server is gone... " << flush;
587 obj2->ice_ping();
588 test(false);
590 catch(const Ice::LocalException&)
595 obj3->ice_ping();
596 test(false);
598 catch(const Ice::LocalException&)
603 obj5->ice_ping();
604 test(false);
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);
620 Ice::Identity id;
621 id.name = IceUtil::generateUUID();
622 registry->addObject(adapter->add(new HelloI, id));
623 adapter->activate();
627 HelloPrx helloPrx = HelloPrx::checkedCast(communicator->stringToProxy(communicator->identityToString(id)));
628 Ice::ConnectionPtr connection = helloPrx->ice_getConnection();
629 test(false);
631 catch(const Ice::CollocationOptimizationException&)
634 adapter->deactivate();
635 cout << "ok" << endl;
637 cout << "shutdown server manager... " << flush;
638 manager->shutdown();
639 cout << "ok" << endl;