/php/test/Ice/binding/Client.php

https://bitbucket.org/cleto/zeroc-ice-package · PHP · 532 lines · 414 code · 78 blank · 40 comment · 91 complexity · 0f836e5aa8f97c2d5a6b17de905f9ae2 MD5 · raw file

  1. <?
  2. // **********************************************************************
  3. //
  4. // Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
  5. //
  6. // This copy of Ice is licensed to you under the terms described in the
  7. // ICE_LICENSE file included in this distribution.
  8. //
  9. // **********************************************************************
  10. error_reporting(E_ALL | E_STRICT);
  11. if(!extension_loaded("ice"))
  12. {
  13. echo "\nerror: Ice extension is not loaded.\n\n";
  14. exit(1);
  15. }
  16. $NS = function_exists("Ice\\initialize");
  17. require_once ($NS ? 'Ice_ns.php' : 'Ice.php');
  18. require_once 'Test.php';
  19. function test($b)
  20. {
  21. if(!$b)
  22. {
  23. $bt = debug_backtrace();
  24. die("\ntest failed in ".$bt[0]["file"]." line ".$bt[0]["line"]."\n");
  25. }
  26. }
  27. function createTestIntfPrx($adapters)
  28. {
  29. $endpoints = array();
  30. $test = null;
  31. foreach($adapters as $p)
  32. {
  33. $test = $p->getTestIntf();
  34. $edpts = $test->ice_getEndpoints();
  35. foreach($edpts as $e)
  36. {
  37. $endpoints[] = $e;
  38. }
  39. }
  40. $test = $test->ice_endpoints($endpoints);
  41. return $test->ice_uncheckedCast("::Test::TestIntf");
  42. }
  43. function deactivate($com, $adapters)
  44. {
  45. foreach($adapters as $p)
  46. {
  47. $com->deactivateObjectAdapter($p);
  48. }
  49. }
  50. function allTests($communicator)
  51. {
  52. global $NS;
  53. $random = $NS ? constant("Ice\\EndpointSelectionType::Random") : constant("Ice_EndpointSelectionType::Random");
  54. $ordered = $NS ? constant("Ice\\EndpointSelectionType::Ordered") : constant("Ice_EndpointSelectionType::Ordered");
  55. $ref = "communicator:default -p 12010";
  56. $com = $communicator->stringToProxy($ref)->ice_uncheckedCast("::Test::RemoteCommunicator");
  57. echo "testing binding with single endpoint... ";
  58. flush();
  59. {
  60. $adapter = $com->createObjectAdapter("Adapter", "default");
  61. $test1 = $adapter->getTestIntf();
  62. $test2 = $adapter->getTestIntf();
  63. test($test1->ice_getConnection() == $test2->ice_getConnection());
  64. $test1->ice_ping();
  65. $test2->ice_ping();
  66. $com->deactivateObjectAdapter($adapter);
  67. $test3 = $test1->ice_uncheckedCast("::Test::TestIntf");
  68. test($test3->ice_getConnection() == $test1->ice_getConnection());
  69. test($test3->ice_getConnection() == $test2->ice_getConnection());
  70. try
  71. {
  72. $test3->ice_ping();
  73. test(false);
  74. }
  75. catch(Exception $ex)
  76. {
  77. $cre = $NS ? "Ice\\ConnectionRefusedException" : "Ice_ConnectionRefusedException";
  78. if(!($ex instanceof $cre))
  79. {
  80. throw $ex;
  81. }
  82. }
  83. }
  84. echo "ok" . "\n";
  85. echo "testing binding with multiple endpoints... ";
  86. flush();
  87. {
  88. $adapters = array();
  89. $adapters[] = $com->createObjectAdapter("Adapter11", "default");
  90. $adapters[] = $com->createObjectAdapter("Adapter12", "default");
  91. $adapters[] = $com->createObjectAdapter("Adapter13", "default");
  92. //
  93. // Ensure that when a connection is opened it's reused for new
  94. // proxies and that all endpoints are eventually tried.
  95. //
  96. $names = array("Adapter11", "Adapter12", "Adapter13");
  97. while(count($names) > 0)
  98. {
  99. $adpts = $adapters;
  100. $test1 = createTestIntfPrx($adpts);
  101. shuffle($adpts);
  102. $test2 = createTestIntfPrx($adpts);
  103. shuffle($adpts);
  104. $test3 = createTestIntfPrx($adpts);
  105. test($test1->ice_getConnection() == $test2->ice_getConnection());
  106. test($test2->ice_getConnection() == $test3->ice_getConnection());
  107. $key = array_search($test1->getAdapterName(), $names);
  108. if($key !== false)
  109. {
  110. unset($names[$key]);
  111. }
  112. $test1->ice_getConnection()->close(false);
  113. }
  114. //
  115. // Ensure that the proxy correctly caches the connection (we
  116. // always send the request over the same connection.)
  117. //
  118. {
  119. foreach($adapters as $p)
  120. {
  121. $p->getTestIntf()->ice_ping();
  122. }
  123. $test = createTestIntfPrx($adapters);
  124. $name = $test->getAdapterName();
  125. $nRetry = 10;
  126. for($i = 0; $i < $nRetry && $test->getAdapterName() == $name; $i++);
  127. test($i == $nRetry);
  128. foreach($adapters as $p)
  129. {
  130. $p->getTestIntf()->ice_getConnection()->close(false);
  131. }
  132. }
  133. //
  134. // Deactivate an adapter and ensure that we can still
  135. // establish the connection to the remaining adapters.
  136. //
  137. $com->deactivateObjectAdapter($adapters[0]);
  138. $names = array("Adapter12", "Adapter13");
  139. while(count($names) > 0)
  140. {
  141. $adpts = $adapters;
  142. $test1 = createTestIntfPrx($adpts);
  143. shuffle($adpts);
  144. $test2 = createTestIntfPrx($adpts);
  145. shuffle($adpts);
  146. $test3 = createTestIntfPrx($adpts);
  147. test($test1->ice_getConnection() == $test2->ice_getConnection());
  148. test($test2->ice_getConnection() == $test3->ice_getConnection());
  149. $key = array_search($test1->getAdapterName(), $names);
  150. if($key !== false)
  151. {
  152. unset($names[$key]);
  153. }
  154. $test1->ice_getConnection()->close(false);
  155. }
  156. //
  157. // Deactivate an adapter and ensure that we can still
  158. // establish the connection to the remaining adapter.
  159. //
  160. $com->deactivateObjectAdapter($adapters[2]);
  161. $test = createTestIntfPrx($adapters);
  162. test($test->getAdapterName() == "Adapter12");
  163. deactivate($com, $adapters);
  164. }
  165. echo "ok" . "\n";
  166. echo "testing random endpoint selection... ";
  167. flush();
  168. {
  169. $adapters = array();
  170. $adapters[] = $com->createObjectAdapter("Adapter21", "default");
  171. $adapters[] = $com->createObjectAdapter("Adapter22", "default");
  172. $adapters[] = $com->createObjectAdapter("Adapter23", "default");
  173. $test = createTestIntfPrx($adapters);
  174. test($test->ice_getEndpointSelection() == $random);
  175. $names = array("Adapter21", "Adapter22", "Adapter23");
  176. while(count($names) > 0)
  177. {
  178. $key = array_search($test->getAdapterName(), $names);
  179. if($key !== false)
  180. {
  181. unset($names[$key]);
  182. }
  183. $test->ice_getConnection()->close(false);
  184. }
  185. $test = $test->ice_endpointSelection($random)->ice_uncheckedCast("::Test::TestIntf");
  186. test($test->ice_getEndpointSelection() == $random);
  187. $names = array("Adapter21", "Adapter22", "Adapter23");
  188. while(count($names) > 0)
  189. {
  190. $key = array_search($test->getAdapterName(), $names);
  191. if($key !== false)
  192. {
  193. unset($names[$key]);
  194. }
  195. $test->ice_getConnection()->close(false);
  196. }
  197. deactivate($com, $adapters);
  198. }
  199. echo "ok" . "\n";
  200. echo "testing ordered endpoint selection... ";
  201. flush();
  202. {
  203. $adapters = array();
  204. $adapters[] = $com->createObjectAdapter("Adapter31", "default");
  205. $adapters[] = $com->createObjectAdapter("Adapter32", "default");
  206. $adapters[] = $com->createObjectAdapter("Adapter33", "default");
  207. $test = createTestIntfPrx($adapters);
  208. $test = $test->ice_endpointSelection($ordered)->ice_uncheckedCast("::Test::TestIntf");
  209. test($test->ice_getEndpointSelection() == $ordered);
  210. $nRetry = 5;
  211. //
  212. // Ensure that endpoints are tried in order by deactiving the adapters
  213. // one after the other.
  214. //
  215. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter31"; $i++);
  216. test($i == $nRetry);
  217. $com->deactivateObjectAdapter($adapters[0]);
  218. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter32"; $i++);
  219. test($i == $nRetry);
  220. $com->deactivateObjectAdapter($adapters[1]);
  221. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter33"; $i++);
  222. test($i == $nRetry);
  223. $com->deactivateObjectAdapter($adapters[2]);
  224. try
  225. {
  226. $test->getAdapterName();
  227. }
  228. catch(Exception $ex)
  229. {
  230. $cre = $NS ? "Ice\\ConnectionRefusedException" : "Ice_ConnectionRefusedException";
  231. if(!($ex instanceof $cre))
  232. {
  233. throw $ex;
  234. }
  235. }
  236. $endpoints = $test->ice_getEndpoints();
  237. $adapters = array();
  238. //
  239. // Now, re-activate the adapters with the same endpoints in the opposite
  240. // order.
  241. //
  242. $adapters[] = $com->createObjectAdapter("Adapter36", $endpoints[2]->toString());
  243. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter36"; $i++);
  244. test($i == $nRetry);
  245. $test->ice_getConnection()->close(false);
  246. $adapters[] = $com->createObjectAdapter("Adapter35", $endpoints[1]->toString());
  247. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter35"; $i++);
  248. test($i == $nRetry);
  249. $test->ice_getConnection()->close(false);
  250. $adapters[] = $com->createObjectAdapter("Adapter34", $endpoints[0]->toString());
  251. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter34"; $i++);
  252. test($i == $nRetry);
  253. deactivate($com, $adapters);
  254. }
  255. echo "ok" . "\n";
  256. echo "testing per request binding with single endpoint... ";
  257. flush();
  258. {
  259. $adapter = $com->createObjectAdapter("Adapter41", "default");
  260. $test1 = $adapter->getTestIntf()->ice_connectionCached(false)->ice_uncheckedCast("::Test::TestIntf");
  261. $test2 = $adapter->getTestIntf()->ice_connectionCached(false)->ice_uncheckedCast("::Test::TestIntf");
  262. test(!$test1->ice_isConnectionCached());
  263. test(!$test2->ice_isConnectionCached());
  264. test($test1->ice_getConnection() == $test2->ice_getConnection());
  265. $test1->ice_ping();
  266. $com->deactivateObjectAdapter($adapter);
  267. $test3 = $test1->ice_uncheckedCast("::Test::TestIntf");
  268. try
  269. {
  270. test($test3->ice_getConnection() == $test1->ice_getConnection());
  271. test(false);
  272. }
  273. catch(Exception $ex)
  274. {
  275. $cre = $NS ? "Ice\\ConnectionRefusedException" : "Ice_ConnectionRefusedException";
  276. if(!($ex instanceof $cre))
  277. {
  278. throw $ex;
  279. }
  280. }
  281. }
  282. echo "ok" . "\n";
  283. echo "testing per request binding with multiple endpoints... ";
  284. flush();
  285. {
  286. $adapters = array();
  287. $adapters[] = $com->createObjectAdapter("Adapter51", "default");
  288. $adapters[] = $com->createObjectAdapter("Adapter52", "default");
  289. $adapters[] = $com->createObjectAdapter("Adapter53", "default");
  290. $test = createTestIntfPrx($adapters)->ice_connectionCached(false)->ice_uncheckedCast("::Test::TestIntf");
  291. test(!$test->ice_isConnectionCached());
  292. $names = array("Adapter51", "Adapter52", "Adapter53");
  293. while(count($names) > 0)
  294. {
  295. $key = array_search($test->getAdapterName(), $names);
  296. if($key !== false)
  297. {
  298. unset($names[$key]);
  299. }
  300. }
  301. $com->deactivateObjectAdapter($adapters[0]);
  302. $names = array("Adapter52", "Adapter53");
  303. while(count($names) > 0)
  304. {
  305. $key = array_search($test->getAdapterName(), $names);
  306. if($key !== false)
  307. {
  308. unset($names[$key]);
  309. }
  310. }
  311. $com->deactivateObjectAdapter($adapters[2]);
  312. test($test->getAdapterName() == "Adapter52");
  313. deactivate($com, $adapters);
  314. }
  315. echo "ok" . "\n";
  316. echo "testing per request binding and ordered endpoint selection... ";
  317. flush();
  318. {
  319. $adapters = array();
  320. $adapters[] = $com->createObjectAdapter("Adapter61", "default");
  321. $adapters[] = $com->createObjectAdapter("Adapter62", "default");
  322. $adapters[] = $com->createObjectAdapter("Adapter63", "default");
  323. $test = createTestIntfPrx($adapters);
  324. $test = $test->ice_endpointSelection($ordered)->ice_uncheckedCast("::Test::TestIntf");
  325. test($test->ice_getEndpointSelection() == $ordered);
  326. $test = $test->ice_connectionCached(false)->ice_uncheckedCast("::Test::TestIntf");
  327. test(!$test->ice_isConnectionCached());
  328. $nRetry = 5;
  329. //
  330. // Ensure that endpoints are tried in order by deactiving the adapters
  331. // one after the other.
  332. //
  333. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter61"; $i++);
  334. test($i == $nRetry);
  335. $com->deactivateObjectAdapter($adapters[0]);
  336. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter62"; $i++);
  337. test($i == $nRetry);
  338. $com->deactivateObjectAdapter($adapters[1]);
  339. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter63"; $i++);
  340. test($i == $nRetry);
  341. $com->deactivateObjectAdapter($adapters[2]);
  342. try
  343. {
  344. $test->getAdapterName();
  345. }
  346. catch(Exception $ex)
  347. {
  348. $cre = $NS ? "Ice\\ConnectionRefusedException" : "Ice_ConnectionRefusedException";
  349. if(!($ex instanceof $cre))
  350. {
  351. throw $ex;
  352. }
  353. }
  354. $endpoints = $test->ice_getEndpoints();
  355. $adapters = array();
  356. //
  357. // Now, re-activate the adapters with the same endpoints in the opposite
  358. // order.
  359. //
  360. $adapters[] = $com->createObjectAdapter("Adapter66", $endpoints[2]->toString());
  361. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter66"; $i++);
  362. test($i == $nRetry);
  363. $adapters[] = $com->createObjectAdapter("Adapter65", $endpoints[1]->toString());
  364. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter65"; $i++);
  365. test($i == $nRetry);
  366. $adapters[] = $com->createObjectAdapter("Adapter64", $endpoints[0]->toString());
  367. for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter64"; $i++);
  368. test($i == $nRetry);
  369. deactivate($com, $adapters);
  370. }
  371. echo "ok" . "\n";
  372. echo "testing endpoint mode filtering... ";
  373. flush();
  374. {
  375. $adapters = array();
  376. $adapters[] = $com->createObjectAdapter("Adapter71", "default");
  377. $adapters[] = $com->createObjectAdapter("Adapter72", "udp");
  378. $test = createTestIntfPrx($adapters);
  379. test($test->getAdapterName() == "Adapter71");
  380. $testUDP = $test->ice_datagram()->ice_uncheckedCast("::Test::TestIntf");
  381. test($test->ice_getConnection() != $testUDP->ice_getConnection());
  382. try
  383. {
  384. $testUDP->getAdapterName();
  385. }
  386. catch(Exception $ex)
  387. {
  388. $cre = $NS ? "Ice\\TwowayOnlyException" : "Ice_TwowayOnlyException";
  389. if(!($ex instanceof $cre))
  390. {
  391. throw $ex;
  392. }
  393. }
  394. }
  395. echo "ok" . "\n";
  396. if(strlen($communicator->getProperties()->getProperty("Ice.Plugin.IceSSL")) > 0)
  397. {
  398. echo "testing unsecure vs. secure endpoints... ";
  399. flush();
  400. {
  401. $adapters = array();
  402. $adapters[] = $com->createObjectAdapter("Adapter81", "ssl");
  403. $adapters[] = $com->createObjectAdapter("Adapter82", "tcp");
  404. $test = createTestIntfPrx($adapters);
  405. for($i = 0; $i < 5; $i++)
  406. {
  407. test($test->getAdapterName() == "Adapter82");
  408. $test->ice_getConnection()->close(false);
  409. }
  410. $testSecure = $test->ice_secure(true)->ice_uncheckedCast("::Test::TestIntf");
  411. test($testSecure->ice_isSecure());
  412. $testSecure = $test->ice_secure(false)->ice_uncheckedCast("::Test::TestIntf");
  413. test(!$testSecure->ice_isSecure());
  414. $testSecure = $test->ice_secure(true)->ice_uncheckedCast("::Test::TestIntf");
  415. test($testSecure->ice_isSecure());
  416. test($test->ice_getConnection() != $testSecure->ice_getConnection());
  417. $com->deactivateObjectAdapter($adapters[1]);
  418. for($i = 0; $i < 5; $i++)
  419. {
  420. test($test->getAdapterName() == "Adapter81");
  421. $test->ice_getConnection()->close(false);
  422. }
  423. $endpts = $test->ice_getEndpoints();
  424. $com->createObjectAdapter("Adapter83", $endpts[1]->toString()); // Reactive tcp OA.
  425. for($i = 0; $i < 5; $i++)
  426. {
  427. test($test->getAdapterName() == "Adapter83");
  428. $test->ice_getConnection()->close(false);
  429. }
  430. $com->deactivateObjectAdapter($adapters[0]);
  431. try
  432. {
  433. $testSecure->ice_ping();
  434. test(false);
  435. }
  436. catch(Exception $ex)
  437. {
  438. $cre = $NS ? "Ice\\ConnectionRefusedException" : "Ice_ConnectionRefusedException";
  439. if(!($ex instanceof $cre))
  440. {
  441. throw $ex;
  442. }
  443. }
  444. deactivate($com, $adapters);
  445. }
  446. echo "ok" . "\n";
  447. }
  448. $com->shutdown();
  449. }
  450. $communicator = Ice_initialize($argv);
  451. allTests($communicator);
  452. $communicator->destroy();
  453. exit();
  454. ?>