PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/test/php/TestClient.php

http://github.com/dreiss/old-thrift
PHP | 398 lines | 265 code | 51 blank | 82 comment | 32 complexity | e4fc60a5f447d44c161989e6e3a13d19 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. <?php
  20. if (!isset($GEN_DIR)) {
  21. $GEN_DIR = 'gen-php';
  22. }
  23. if (!isset($MODE)) {
  24. $MODE = 'normal';
  25. }
  26. /** Set the Thrift root */
  27. $GLOBALS['THRIFT_ROOT'] = '../../lib/php/src';
  28. /** Include the Thrift base */
  29. require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
  30. /** Include the binary protocol */
  31. require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
  32. /** Include the socket layer */
  33. require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocketPool.php';
  34. /** Include the socket layer */
  35. require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
  36. echo '==============================='."\n";
  37. echo ' SAFE TO IGNORE THESE IN TEST'."\n";
  38. echo '==============================='."\n";
  39. /** Include the generated code */
  40. require_once $GEN_DIR.'/ThriftTest/ThriftTest.php';
  41. require_once $GEN_DIR.'/ThriftTest/ThriftTest_types.php';
  42. echo '==============================='."\n";
  43. echo ' END OF SAFE ERRORS SECTION'."\n";
  44. echo '==============================='."\n\n";
  45. $host = 'localhost';
  46. $port = 9090;
  47. if ($argc > 1) {
  48. $host = $argv[0];
  49. }
  50. if ($argc > 2) {
  51. $host = $argv[1];
  52. }
  53. $hosts = array('localhost');
  54. $socket = new TSocket($host, $port);
  55. $socket = new TSocketPool($hosts, $port);
  56. $socket->setDebug(TRUE);
  57. if ($MODE == 'inline') {
  58. $transport = $socket;
  59. $testClient = new ThriftTestClient($transport);
  60. } else {
  61. $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
  62. $transport = $bufferedSocket;
  63. $protocol = new TBinaryProtocol($transport);
  64. $testClient = new ThriftTestClient($protocol);
  65. }
  66. $transport->open();
  67. $start = microtime(true);
  68. /**
  69. * VOID TEST
  70. */
  71. print_r("testVoid()");
  72. $testClient->testVoid();
  73. print_r(" = void\n");
  74. /**
  75. * STRING TEST
  76. */
  77. print_r("testString(\"Test\")");
  78. $s = $testClient->testString("Test");
  79. print_r(" = \"$s\"\n");
  80. /**
  81. * BYTE TEST
  82. */
  83. print_r("testByte(1)");
  84. $u8 = $testClient->testByte(1);
  85. print_r(" = $u8\n");
  86. /**
  87. * I32 TEST
  88. */
  89. print_r("testI32(-1)");
  90. $i32 = $testClient->testI32(-1);
  91. print_r(" = $i32\n");
  92. /**
  93. * I64 TEST
  94. */
  95. print_r("testI64(-34359738368)");
  96. $i64 = $testClient->testI64(-34359738368);
  97. print_r(" = $i64\n");
  98. /**
  99. * DOUBLE TEST
  100. */
  101. print_r("testDouble(-852.234234234)");
  102. $dub = $testClient->testDouble(-852.234234234);
  103. print_r(" = $dub\n");
  104. /**
  105. * STRUCT TEST
  106. */
  107. print_r("testStruct({\"Zero\", 1, -3, -5})");
  108. $out = new Xtruct();
  109. $out->string_thing = "Zero";
  110. $out->byte_thing = 1;
  111. $out->i32_thing = -3;
  112. $out->i64_thing = -5;
  113. $in = $testClient->testStruct($out);
  114. print_r(" = {\"".$in->string_thing."\", ".
  115. $in->byte_thing.", ".
  116. $in->i32_thing.", ".
  117. $in->i64_thing."}\n");
  118. /**
  119. * NESTED STRUCT TEST
  120. */
  121. print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
  122. $out2 = new Xtruct2();
  123. $out2->byte_thing = 1;
  124. $out2->struct_thing = $out;
  125. $out2->i32_thing = 5;
  126. $in2 = $testClient->testNest($out2);
  127. $in = $in2->struct_thing;
  128. print_r(" = {".$in2->byte_thing.", {\"".
  129. $in->string_thing."\", ".
  130. $in->byte_thing.", ".
  131. $in->i32_thing.", ".
  132. $in->i64_thing."}, ".
  133. $in2->i32_thing."}\n");
  134. /**
  135. * MAP TEST
  136. */
  137. $mapout = array();
  138. for ($i = 0; $i < 5; ++$i) {
  139. $mapout[$i] = $i-10;
  140. }
  141. print_r("testMap({");
  142. $first = true;
  143. foreach ($mapout as $key => $val) {
  144. if ($first) {
  145. $first = false;
  146. } else {
  147. print_r(", ");
  148. }
  149. print_r("$key => $val");
  150. }
  151. print_r("})");
  152. $mapin = $testClient->testMap($mapout);
  153. print_r(" = {");
  154. $first = true;
  155. foreach ($mapin as $key => $val) {
  156. if ($first) {
  157. $first = false;
  158. } else {
  159. print_r(", ");
  160. }
  161. print_r("$key => $val");
  162. }
  163. print_r("}\n");
  164. /**
  165. * SET TEST
  166. */
  167. $setout = array();;
  168. for ($i = -2; $i < 3; ++$i) {
  169. $setout []= $i;
  170. }
  171. print_r("testSet({");
  172. $first = true;
  173. foreach ($setout as $val) {
  174. if ($first) {
  175. $first = false;
  176. } else {
  177. print_r(", ");
  178. }
  179. print_r($val);
  180. }
  181. print_r("})");
  182. $setin = $testClient->testSet($setout);
  183. print_r(" = {");
  184. $first = true;
  185. foreach ($setin as $val) {
  186. if ($first) {
  187. $first = false;
  188. } else {
  189. print_r(", ");
  190. }
  191. print_r($val);
  192. }
  193. print_r("}\n");
  194. /**
  195. * LIST TEST
  196. */
  197. $listout = array();
  198. for ($i = -2; $i < 3; ++$i) {
  199. $listout []= $i;
  200. }
  201. print_r("testList({");
  202. $first = true;
  203. foreach ($listout as $val) {
  204. if ($first) {
  205. $first = false;
  206. } else {
  207. print_r(", ");
  208. }
  209. print_r($val);
  210. }
  211. print_r("})");
  212. $listin = $testClient->testList($listout);
  213. print_r(" = {");
  214. $first = true;
  215. foreach ($listin as $val) {
  216. if ($first) {
  217. $first = false;
  218. } else {
  219. print_r(", ");
  220. }
  221. print_r($val);
  222. }
  223. print_r("}\n");
  224. /**
  225. * ENUM TEST
  226. */
  227. print_r("testEnum(ONE)");
  228. $ret = $testClient->testEnum(Numberz::ONE);
  229. print_r(" = $ret\n");
  230. print_r("testEnum(TWO)");
  231. $ret = $testClient->testEnum(Numberz::TWO);
  232. print_r(" = $ret\n");
  233. print_r("testEnum(THREE)");
  234. $ret = $testClient->testEnum(Numberz::THREE);
  235. print_r(" = $ret\n");
  236. print_r("testEnum(FIVE)");
  237. $ret = $testClient->testEnum(Numberz::FIVE);
  238. print_r(" = $ret\n");
  239. print_r("testEnum(EIGHT)");
  240. $ret = $testClient->testEnum(Numberz::EIGHT);
  241. print_r(" = $ret\n");
  242. /**
  243. * TYPEDEF TEST
  244. */
  245. print_r("testTypedef(309858235082523)");
  246. $uid = $testClient->testTypedef(309858235082523);
  247. print_r(" = $uid\n");
  248. /**
  249. * NESTED MAP TEST
  250. */
  251. print_r("testMapMap(1)");
  252. $mm = $testClient->testMapMap(1);
  253. print_r(" = {");
  254. foreach ($mm as $key => $val) {
  255. print_r("$key => {");
  256. foreach ($val as $k2 => $v2) {
  257. print_r("$k2 => $v2, ");
  258. }
  259. print_r("}, ");
  260. }
  261. print_r("}\n");
  262. /**
  263. * INSANITY TEST
  264. */
  265. $insane = new Insanity();
  266. $insane->userMap[Numberz::FIVE] = 5000;
  267. $truck = new Xtruct();
  268. $truck->string_thing = "Truck";
  269. $truck->byte_thing = 8;
  270. $truck->i32_thing = 8;
  271. $truck->i64_thing = 8;
  272. $insane->xtructs []= $truck;
  273. print_r("testInsanity()");
  274. $whoa = $testClient->testInsanity($insane);
  275. print_r(" = {");
  276. foreach ($whoa as $key => $val) {
  277. print_r("$key => {");
  278. foreach ($val as $k2 => $v2) {
  279. print_r("$k2 => {");
  280. $userMap = $v2->userMap;
  281. print_r("{");
  282. if (is_array($usermap)) {
  283. foreach ($userMap as $k3 => $v3) {
  284. print_r("$k3 => $v3, ");
  285. }
  286. }
  287. print_r("}, ");
  288. $xtructs = $v2->xtructs;
  289. print_r("{");
  290. if (is_array($xtructs)) {
  291. foreach ($xtructs as $x) {
  292. print_r("{\"".$x->string_thing."\", ".
  293. $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
  294. }
  295. }
  296. print_r("}");
  297. print_r("}, ");
  298. }
  299. print_r("}, ");
  300. }
  301. print_r("}\n");
  302. /**
  303. * EXCEPTION TEST
  304. */
  305. print_r("testException('Xception')");
  306. try {
  307. $testClient->testException('Xception');
  308. print_r(" void\nFAILURE\n");
  309. } catch (Xception $x) {
  310. print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
  311. }
  312. /**
  313. * Normal tests done.
  314. */
  315. $stop = microtime(true);
  316. $elp = round(1000*($stop - $start), 0);
  317. print_r("Total time: $elp ms\n");
  318. /**
  319. * Extraneous "I don't trust PHP to pack/unpack integer" tests
  320. */
  321. // Max I32
  322. $num = pow(2, 30) + (pow(2, 30) - 1);
  323. $num2 = $testClient->testI32($num);
  324. if ($num != $num2) {
  325. print "Missed $num = $num2\n";
  326. }
  327. // Min I32
  328. $num = 0 - pow(2, 31);
  329. $num2 = $testClient->testI32($num);
  330. if ($num != $num2) {
  331. print "Missed $num = $num2\n";
  332. }
  333. // Max I64
  334. $num = pow(2, 62) + (pow(2, 62) - 1);
  335. $num2 = $testClient->testI64($num);
  336. if ($num != $num2) {
  337. print "Missed $num = $num2\n";
  338. }
  339. // Min I64
  340. $num = 0 - pow(2, 63);
  341. $num2 = $testClient->testI64($num);
  342. if ($num != $num2) {
  343. print "Missed $num = $num2\n";
  344. }
  345. $transport->close();
  346. return;
  347. ?>