EndToEndTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. /*
  3. *
  4. * Copyright 2015-2016, Google Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following disclaimer
  15. * in the documentation and/or other materials provided with the
  16. * distribution.
  17. * * Neither the name of Google Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. class EndToEndTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function setUp()
  37. {
  38. $this->server = new Grpc\Server([]);
  39. $this->port = $this->server->addHttp2Port('0.0.0.0:0');
  40. $this->channel = new Grpc\Channel('localhost:'.$this->port, []);
  41. $this->server->start();
  42. }
  43. public function tearDown()
  44. {
  45. unset($this->channel);
  46. unset($this->server);
  47. }
  48. public function testSimpleRequestBody()
  49. {
  50. $deadline = Grpc\Timeval::infFuture();
  51. $status_text = 'xyz';
  52. $call = new Grpc\Call($this->channel,
  53. 'dummy_method',
  54. $deadline);
  55. $event = $call->startBatch([
  56. Grpc\OP_SEND_INITIAL_METADATA => [],
  57. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  58. ]);
  59. $this->assertTrue($event->send_metadata);
  60. $this->assertTrue($event->send_close);
  61. $event = $this->server->requestCall();
  62. $this->assertSame('dummy_method', $event->method);
  63. $server_call = $event->call;
  64. $event = $server_call->startBatch([
  65. Grpc\OP_SEND_INITIAL_METADATA => [],
  66. Grpc\OP_SEND_STATUS_FROM_SERVER => [
  67. 'metadata' => [],
  68. 'code' => Grpc\STATUS_OK,
  69. 'details' => $status_text,
  70. ],
  71. Grpc\OP_RECV_CLOSE_ON_SERVER => true,
  72. ]);
  73. $this->assertTrue($event->send_metadata);
  74. $this->assertTrue($event->send_status);
  75. $this->assertFalse($event->cancelled);
  76. $event = $call->startBatch([
  77. Grpc\OP_RECV_INITIAL_METADATA => true,
  78. Grpc\OP_RECV_STATUS_ON_CLIENT => true,
  79. ]);
  80. $status = $event->status;
  81. $this->assertSame([], $status->metadata);
  82. $this->assertSame(Grpc\STATUS_OK, $status->code);
  83. $this->assertSame($status_text, $status->details);
  84. unset($call);
  85. unset($server_call);
  86. }
  87. public function testMessageWriteFlags()
  88. {
  89. $deadline = Grpc\Timeval::infFuture();
  90. $req_text = 'message_write_flags_test';
  91. $status_text = 'xyz';
  92. $call = new Grpc\Call($this->channel,
  93. 'dummy_method',
  94. $deadline);
  95. $event = $call->startBatch([
  96. Grpc\OP_SEND_INITIAL_METADATA => [],
  97. Grpc\OP_SEND_MESSAGE => ['message' => $req_text,
  98. 'flags' => Grpc\WRITE_NO_COMPRESS, ],
  99. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  100. ]);
  101. $this->assertTrue($event->send_metadata);
  102. $this->assertTrue($event->send_close);
  103. $event = $this->server->requestCall();
  104. $this->assertSame('dummy_method', $event->method);
  105. $server_call = $event->call;
  106. $event = $server_call->startBatch([
  107. Grpc\OP_SEND_INITIAL_METADATA => [],
  108. Grpc\OP_SEND_STATUS_FROM_SERVER => [
  109. 'metadata' => [],
  110. 'code' => Grpc\STATUS_OK,
  111. 'details' => $status_text,
  112. ],
  113. ]);
  114. $event = $call->startBatch([
  115. Grpc\OP_RECV_INITIAL_METADATA => true,
  116. Grpc\OP_RECV_STATUS_ON_CLIENT => true,
  117. ]);
  118. $status = $event->status;
  119. $this->assertSame([], $status->metadata);
  120. $this->assertSame(Grpc\STATUS_OK, $status->code);
  121. $this->assertSame($status_text, $status->details);
  122. unset($call);
  123. unset($server_call);
  124. }
  125. public function testClientServerFullRequestResponse()
  126. {
  127. $deadline = Grpc\Timeval::infFuture();
  128. $req_text = 'client_server_full_request_response';
  129. $reply_text = 'reply:client_server_full_request_response';
  130. $status_text = 'status:client_server_full_response_text';
  131. $call = new Grpc\Call($this->channel,
  132. 'dummy_method',
  133. $deadline);
  134. $event = $call->startBatch([
  135. Grpc\OP_SEND_INITIAL_METADATA => [],
  136. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  137. Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
  138. ]);
  139. $this->assertTrue($event->send_metadata);
  140. $this->assertTrue($event->send_close);
  141. $this->assertTrue($event->send_message);
  142. $event = $this->server->requestCall();
  143. $this->assertSame('dummy_method', $event->method);
  144. $server_call = $event->call;
  145. $event = $server_call->startBatch([
  146. Grpc\OP_SEND_INITIAL_METADATA => [],
  147. Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
  148. Grpc\OP_SEND_STATUS_FROM_SERVER => [
  149. 'metadata' => [],
  150. 'code' => Grpc\STATUS_OK,
  151. 'details' => $status_text,
  152. ],
  153. Grpc\OP_RECV_MESSAGE => true,
  154. Grpc\OP_RECV_CLOSE_ON_SERVER => true,
  155. ]);
  156. $this->assertTrue($event->send_metadata);
  157. $this->assertTrue($event->send_status);
  158. $this->assertTrue($event->send_message);
  159. $this->assertFalse($event->cancelled);
  160. $this->assertSame($req_text, $event->message);
  161. $event = $call->startBatch([
  162. Grpc\OP_RECV_INITIAL_METADATA => true,
  163. Grpc\OP_RECV_MESSAGE => true,
  164. Grpc\OP_RECV_STATUS_ON_CLIENT => true,
  165. ]);
  166. $this->assertSame([], $event->metadata);
  167. $this->assertSame($reply_text, $event->message);
  168. $status = $event->status;
  169. $this->assertSame([], $status->metadata);
  170. $this->assertSame(Grpc\STATUS_OK, $status->code);
  171. $this->assertSame($status_text, $status->details);
  172. unset($call);
  173. unset($server_call);
  174. }
  175. /**
  176. * @expectedException InvalidArgumentException
  177. */
  178. public function testInvalidClientMessageArray()
  179. {
  180. $deadline = Grpc\Timeval::infFuture();
  181. $req_text = 'client_server_full_request_response';
  182. $reply_text = 'reply:client_server_full_request_response';
  183. $status_text = 'status:client_server_full_response_text';
  184. $call = new Grpc\Call($this->channel,
  185. 'dummy_method',
  186. $deadline);
  187. $event = $call->startBatch([
  188. Grpc\OP_SEND_INITIAL_METADATA => [],
  189. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  190. Grpc\OP_SEND_MESSAGE => 'invalid',
  191. ]);
  192. }
  193. /**
  194. * @expectedException InvalidArgumentException
  195. */
  196. public function testInvalidClientMessageString()
  197. {
  198. $deadline = Grpc\Timeval::infFuture();
  199. $req_text = 'client_server_full_request_response';
  200. $reply_text = 'reply:client_server_full_request_response';
  201. $status_text = 'status:client_server_full_response_text';
  202. $call = new Grpc\Call($this->channel,
  203. 'dummy_method',
  204. $deadline);
  205. $event = $call->startBatch([
  206. Grpc\OP_SEND_INITIAL_METADATA => [],
  207. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  208. Grpc\OP_SEND_MESSAGE => ['message' => 0],
  209. ]);
  210. }
  211. /**
  212. * @expectedException InvalidArgumentException
  213. */
  214. public function testInvalidClientMessageFlags()
  215. {
  216. $deadline = Grpc\Timeval::infFuture();
  217. $req_text = 'client_server_full_request_response';
  218. $reply_text = 'reply:client_server_full_request_response';
  219. $status_text = 'status:client_server_full_response_text';
  220. $call = new Grpc\Call($this->channel,
  221. 'dummy_method',
  222. $deadline);
  223. $event = $call->startBatch([
  224. Grpc\OP_SEND_INITIAL_METADATA => [],
  225. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  226. Grpc\OP_SEND_MESSAGE => ['message' => 'abc',
  227. 'flags' => 'invalid'],
  228. ]);
  229. }
  230. /**
  231. * @expectedException InvalidArgumentException
  232. */
  233. public function testInvalidServerStatusMetadata()
  234. {
  235. $deadline = Grpc\Timeval::infFuture();
  236. $req_text = 'client_server_full_request_response';
  237. $reply_text = 'reply:client_server_full_request_response';
  238. $status_text = 'status:client_server_full_response_text';
  239. $call = new Grpc\Call($this->channel,
  240. 'dummy_method',
  241. $deadline);
  242. $event = $call->startBatch([
  243. Grpc\OP_SEND_INITIAL_METADATA => [],
  244. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  245. Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
  246. ]);
  247. $this->assertTrue($event->send_metadata);
  248. $this->assertTrue($event->send_close);
  249. $this->assertTrue($event->send_message);
  250. $event = $this->server->requestCall();
  251. $this->assertSame('dummy_method', $event->method);
  252. $server_call = $event->call;
  253. $event = $server_call->startBatch([
  254. Grpc\OP_SEND_INITIAL_METADATA => [],
  255. Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
  256. Grpc\OP_SEND_STATUS_FROM_SERVER => [
  257. 'metadata' => 'invalid',
  258. 'code' => Grpc\STATUS_OK,
  259. 'details' => $status_text,
  260. ],
  261. Grpc\OP_RECV_MESSAGE => true,
  262. Grpc\OP_RECV_CLOSE_ON_SERVER => true,
  263. ]);
  264. }
  265. /**
  266. * @expectedException InvalidArgumentException
  267. */
  268. public function testInvalidServerStatusCode()
  269. {
  270. $deadline = Grpc\Timeval::infFuture();
  271. $req_text = 'client_server_full_request_response';
  272. $reply_text = 'reply:client_server_full_request_response';
  273. $status_text = 'status:client_server_full_response_text';
  274. $call = new Grpc\Call($this->channel,
  275. 'dummy_method',
  276. $deadline);
  277. $event = $call->startBatch([
  278. Grpc\OP_SEND_INITIAL_METADATA => [],
  279. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  280. Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
  281. ]);
  282. $this->assertTrue($event->send_metadata);
  283. $this->assertTrue($event->send_close);
  284. $this->assertTrue($event->send_message);
  285. $event = $this->server->requestCall();
  286. $this->assertSame('dummy_method', $event->method);
  287. $server_call = $event->call;
  288. $event = $server_call->startBatch([
  289. Grpc\OP_SEND_INITIAL_METADATA => [],
  290. Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
  291. Grpc\OP_SEND_STATUS_FROM_SERVER => [
  292. 'metadata' => [],
  293. 'code' => 'invalid',
  294. 'details' => $status_text,
  295. ],
  296. Grpc\OP_RECV_MESSAGE => true,
  297. Grpc\OP_RECV_CLOSE_ON_SERVER => true,
  298. ]);
  299. }
  300. /**
  301. * @expectedException InvalidArgumentException
  302. */
  303. public function testMissingServerStatusCode()
  304. {
  305. $deadline = Grpc\Timeval::infFuture();
  306. $req_text = 'client_server_full_request_response';
  307. $reply_text = 'reply:client_server_full_request_response';
  308. $status_text = 'status:client_server_full_response_text';
  309. $call = new Grpc\Call($this->channel,
  310. 'dummy_method',
  311. $deadline);
  312. $event = $call->startBatch([
  313. Grpc\OP_SEND_INITIAL_METADATA => [],
  314. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  315. Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
  316. ]);
  317. $this->assertTrue($event->send_metadata);
  318. $this->assertTrue($event->send_close);
  319. $this->assertTrue($event->send_message);
  320. $event = $this->server->requestCall();
  321. $this->assertSame('dummy_method', $event->method);
  322. $server_call = $event->call;
  323. $event = $server_call->startBatch([
  324. Grpc\OP_SEND_INITIAL_METADATA => [],
  325. Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
  326. Grpc\OP_SEND_STATUS_FROM_SERVER => [
  327. 'metadata' => [],
  328. 'details' => $status_text,
  329. ],
  330. Grpc\OP_RECV_MESSAGE => true,
  331. Grpc\OP_RECV_CLOSE_ON_SERVER => true,
  332. ]);
  333. }
  334. /**
  335. * @expectedException InvalidArgumentException
  336. */
  337. public function testInvalidServerStatusDetails()
  338. {
  339. $deadline = Grpc\Timeval::infFuture();
  340. $req_text = 'client_server_full_request_response';
  341. $reply_text = 'reply:client_server_full_request_response';
  342. $status_text = 'status:client_server_full_response_text';
  343. $call = new Grpc\Call($this->channel,
  344. 'dummy_method',
  345. $deadline);
  346. $event = $call->startBatch([
  347. Grpc\OP_SEND_INITIAL_METADATA => [],
  348. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  349. Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
  350. ]);
  351. $this->assertTrue($event->send_metadata);
  352. $this->assertTrue($event->send_close);
  353. $this->assertTrue($event->send_message);
  354. $event = $this->server->requestCall();
  355. $this->assertSame('dummy_method', $event->method);
  356. $server_call = $event->call;
  357. $event = $server_call->startBatch([
  358. Grpc\OP_SEND_INITIAL_METADATA => [],
  359. Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
  360. Grpc\OP_SEND_STATUS_FROM_SERVER => [
  361. 'metadata' => [],
  362. 'code' => Grpc\STATUS_OK,
  363. 'details' => 0,
  364. ],
  365. Grpc\OP_RECV_MESSAGE => true,
  366. Grpc\OP_RECV_CLOSE_ON_SERVER => true,
  367. ]);
  368. }
  369. /**
  370. * @expectedException InvalidArgumentException
  371. */
  372. public function testMissingServerStatusDetails()
  373. {
  374. $deadline = Grpc\Timeval::infFuture();
  375. $req_text = 'client_server_full_request_response';
  376. $reply_text = 'reply:client_server_full_request_response';
  377. $status_text = 'status:client_server_full_response_text';
  378. $call = new Grpc\Call($this->channel,
  379. 'dummy_method',
  380. $deadline);
  381. $event = $call->startBatch([
  382. Grpc\OP_SEND_INITIAL_METADATA => [],
  383. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  384. Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
  385. ]);
  386. $this->assertTrue($event->send_metadata);
  387. $this->assertTrue($event->send_close);
  388. $this->assertTrue($event->send_message);
  389. $event = $this->server->requestCall();
  390. $this->assertSame('dummy_method', $event->method);
  391. $server_call = $event->call;
  392. $event = $server_call->startBatch([
  393. Grpc\OP_SEND_INITIAL_METADATA => [],
  394. Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
  395. Grpc\OP_SEND_STATUS_FROM_SERVER => [
  396. 'metadata' => [],
  397. 'code' => Grpc\STATUS_OK,
  398. ],
  399. Grpc\OP_RECV_MESSAGE => true,
  400. Grpc\OP_RECV_CLOSE_ON_SERVER => true,
  401. ]);
  402. }
  403. /**
  404. * @expectedException InvalidArgumentException
  405. */
  406. public function testInvalidStartBatchKey()
  407. {
  408. $deadline = Grpc\Timeval::infFuture();
  409. $req_text = 'client_server_full_request_response';
  410. $reply_text = 'reply:client_server_full_request_response';
  411. $status_text = 'status:client_server_full_response_text';
  412. $call = new Grpc\Call($this->channel,
  413. 'dummy_method',
  414. $deadline);
  415. $event = $call->startBatch([
  416. 9999999 => [],
  417. ]);
  418. }
  419. /**
  420. * @expectedException LogicException
  421. */
  422. public function testInvalidStartBatch()
  423. {
  424. $deadline = Grpc\Timeval::infFuture();
  425. $req_text = 'client_server_full_request_response';
  426. $reply_text = 'reply:client_server_full_request_response';
  427. $status_text = 'status:client_server_full_response_text';
  428. $call = new Grpc\Call($this->channel,
  429. 'dummy_method',
  430. $deadline);
  431. $event = $call->startBatch([
  432. Grpc\OP_SEND_INITIAL_METADATA => [],
  433. Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
  434. Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
  435. Grpc\OP_SEND_STATUS_FROM_SERVER => [
  436. 'metadata' => [],
  437. 'code' => Grpc\STATUS_OK,
  438. 'details' => 'abc',
  439. ],
  440. ]);
  441. }
  442. public function testGetTarget()
  443. {
  444. $this->assertTrue(is_string($this->channel->getTarget()));
  445. }
  446. public function testGetConnectivityState()
  447. {
  448. $this->assertTrue($this->channel->getConnectivityState() == Grpc\CHANNEL_IDLE);
  449. }
  450. public function testWatchConnectivityStateFailed()
  451. {
  452. $idle_state = $this->channel->getConnectivityState();
  453. $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE);
  454. $now = Grpc\Timeval::now();
  455. $delta = new Grpc\Timeval(500000); // should timeout
  456. $deadline = $now->add($delta);
  457. $this->assertFalse($this->channel->watchConnectivityState(
  458. $idle_state, $deadline));
  459. }
  460. public function testWatchConnectivityStateSuccess()
  461. {
  462. $idle_state = $this->channel->getConnectivityState(true);
  463. $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE);
  464. $now = Grpc\Timeval::now();
  465. $delta = new Grpc\Timeval(3000000); // should finish well before
  466. $deadline = $now->add($delta);
  467. $this->assertTrue($this->channel->watchConnectivityState(
  468. $idle_state, $deadline));
  469. $new_state = $this->channel->getConnectivityState();
  470. $this->assertTrue($idle_state != $new_state);
  471. }
  472. public function testWatchConnectivityStateDoNothing()
  473. {
  474. $idle_state = $this->channel->getConnectivityState();
  475. $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE);
  476. $now = Grpc\Timeval::now();
  477. $delta = new Grpc\Timeval(100000);
  478. $deadline = $now->add($delta);
  479. $this->assertFalse($this->channel->watchConnectivityState(
  480. $idle_state, $deadline));
  481. $new_state = $this->channel->getConnectivityState();
  482. $this->assertTrue($new_state == Grpc\CHANNEL_IDLE);
  483. }
  484. /**
  485. * @expectedException InvalidArgumentException
  486. */
  487. public function testGetConnectivityStateInvalidParam()
  488. {
  489. $this->assertTrue($this->channel->getConnectivityState(
  490. new Grpc\Timeval));
  491. }
  492. /**
  493. * @expectedException InvalidArgumentException
  494. */
  495. public function testWatchConnectivityStateInvalidParam()
  496. {
  497. $this->assertTrue($this->channel->watchConnectivityState(
  498. 0, 1000));
  499. }
  500. /**
  501. * @expectedException InvalidArgumentException
  502. */
  503. public function testChannelConstructorInvalidParam()
  504. {
  505. $this->channel = new Grpc\Channel('localhost:'.$this->port, NULL);
  506. }
  507. public function testClose()
  508. {
  509. $this->assertNull($this->channel->close());
  510. }
  511. }