AbstractGeneratedCodeTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /*
  3. *
  4. * Copyright 2015 gRPC authors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * 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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. require_once realpath(dirname(__FILE__).'/../../vendor/autoload.php');
  20. // The following includes are needed when using protobuf 3.1.0
  21. // and will suppress warnings when using protobuf 3.2.0+
  22. @include_once dirname(__FILE__).'/math.pb.php';
  23. @include_once dirname(__FILE__).'/math_grpc_pb.php';
  24. abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase
  25. {
  26. /**
  27. * These tests require that a server exporting the math service must be
  28. * running on $GRPC_TEST_HOST.
  29. */
  30. protected static $client;
  31. public function testWaitForNotReady()
  32. {
  33. $this->assertFalse(self::$client->waitForReady(1));
  34. }
  35. public function testWaitForReady()
  36. {
  37. $this->assertTrue(self::$client->waitForReady(250000));
  38. }
  39. public function testAlreadyReady()
  40. {
  41. $this->assertTrue(self::$client->waitForReady(250000));
  42. $this->assertTrue(self::$client->waitForReady(100));
  43. }
  44. public function testGetTarget()
  45. {
  46. $this->assertTrue(is_string(self::$client->getTarget()));
  47. }
  48. /**
  49. * @expectedException InvalidArgumentException
  50. */
  51. public function testClose()
  52. {
  53. self::$client->close();
  54. $div_arg = new Math\DivArgs();
  55. $call = self::$client->Div($div_arg);
  56. }
  57. /**
  58. * @expectedException InvalidArgumentException
  59. */
  60. public function testInvalidMetadata()
  61. {
  62. $div_arg = new Math\DivArgs();
  63. $call = self::$client->Div($div_arg, [' ' => 'abc123']);
  64. }
  65. public function testGetCallMetadata()
  66. {
  67. $div_arg = new Math\DivArgs();
  68. $call = self::$client->Div($div_arg);
  69. $this->assertTrue(is_array($call->getMetadata()));
  70. }
  71. public function testTimeout()
  72. {
  73. $div_arg = new Math\DivArgs();
  74. $call = self::$client->Div($div_arg, [], ['timeout' => 1]);
  75. list($response, $status) = $call->wait();
  76. $this->assertSame(\Grpc\STATUS_DEADLINE_EXCEEDED, $status->code);
  77. }
  78. public function testCancel()
  79. {
  80. $div_arg = new Math\DivArgs();
  81. $call = self::$client->Div($div_arg);
  82. $call->cancel();
  83. list($response, $status) = $call->wait();
  84. $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
  85. }
  86. public function testCallCredentialsCallback()
  87. {
  88. $div_arg = new Math\DivArgs();
  89. $call = self::$client->Div($div_arg, array(), array(
  90. 'call_credentials_callback' => function ($context) {
  91. return array();
  92. },
  93. ));
  94. $call->cancel();
  95. list($response, $status) = $call->wait();
  96. $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
  97. }
  98. public function testCallCredentialsCallback2()
  99. {
  100. $div_arg = new Math\DivArgs();
  101. $call = self::$client->Div($div_arg);
  102. $call_credentials = Grpc\CallCredentials::createFromPlugin(
  103. function ($context) {
  104. return array();
  105. }
  106. );
  107. $call->setCallCredentials($call_credentials);
  108. $call->cancel();
  109. list($response, $status) = $call->wait();
  110. $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
  111. }
  112. /**
  113. * @expectedException InvalidArgumentException
  114. */
  115. public function testInvalidMethodName()
  116. {
  117. $invalid_client = new DummyInvalidClient('host', [
  118. 'credentials' => Grpc\ChannelCredentials::createInsecure(),
  119. ]);
  120. $div_arg = new Math\DivArgs();
  121. $invalid_client->InvalidUnaryCall($div_arg);
  122. }
  123. /**
  124. * @expectedException Exception
  125. */
  126. public function testMissingCredentials()
  127. {
  128. $invalid_client = new DummyInvalidClient('host', [
  129. ]);
  130. }
  131. public function testPrimaryUserAgentString()
  132. {
  133. $invalid_client = new DummyInvalidClient('host', [
  134. 'credentials' => Grpc\ChannelCredentials::createInsecure(),
  135. 'grpc.primary_user_agent' => 'testUserAgent',
  136. ]);
  137. }
  138. public function testWriteFlags()
  139. {
  140. $div_arg = new Math\DivArgs();
  141. $div_arg->setDividend(7);
  142. $div_arg->setDivisor(4);
  143. $call = self::$client->Div($div_arg, [],
  144. ['flags' => Grpc\WRITE_NO_COMPRESS]);
  145. $this->assertTrue(is_string($call->getPeer()));
  146. list($response, $status) = $call->wait();
  147. $this->assertSame(1, $response->getQuotient());
  148. $this->assertSame(3, $response->getRemainder());
  149. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  150. }
  151. public function testWriteFlagsServerStreaming()
  152. {
  153. $fib_arg = new Math\FibArgs();
  154. $fib_arg->setLimit(7);
  155. $call = self::$client->Fib($fib_arg, [],
  156. ['flags' => Grpc\WRITE_NO_COMPRESS]);
  157. $result_array = iterator_to_array($call->responses());
  158. $status = $call->getStatus();
  159. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  160. }
  161. public function testWriteFlagsClientStreaming()
  162. {
  163. $call = self::$client->Sum();
  164. $num = new Math\Num();
  165. $num->setNum(1);
  166. $call->write($num, ['flags' => Grpc\WRITE_NO_COMPRESS]);
  167. list($response, $status) = $call->wait();
  168. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  169. }
  170. public function testWriteFlagsBidiStreaming()
  171. {
  172. $call = self::$client->DivMany();
  173. $div_arg = new Math\DivArgs();
  174. $div_arg->setDividend(7);
  175. $div_arg->setDivisor(4);
  176. $call->write($div_arg, ['flags' => Grpc\WRITE_NO_COMPRESS]);
  177. $response = $call->read();
  178. $call->writesDone();
  179. $status = $call->getStatus();
  180. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  181. }
  182. public function testSimpleRequest()
  183. {
  184. $div_arg = new Math\DivArgs();
  185. $div_arg->setDividend(7);
  186. $div_arg->setDivisor(4);
  187. $call = self::$client->Div($div_arg);
  188. $this->assertTrue(is_string($call->getPeer()));
  189. list($response, $status) = $call->wait();
  190. $this->assertSame(1, $response->getQuotient());
  191. $this->assertSame(3, $response->getRemainder());
  192. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  193. }
  194. public function testServerStreaming()
  195. {
  196. $fib_arg = new Math\FibArgs();
  197. $fib_arg->setLimit(7);
  198. $call = self::$client->Fib($fib_arg);
  199. $this->assertTrue(is_string($call->getPeer()));
  200. $result_array = iterator_to_array($call->responses());
  201. $extract_num = function ($num) {
  202. return $num->getNum();
  203. };
  204. $values = array_map($extract_num, $result_array);
  205. $this->assertSame([1, 1, 2, 3, 5, 8, 13], $values);
  206. $status = $call->getStatus();
  207. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  208. }
  209. public function testClientStreaming()
  210. {
  211. $call = self::$client->Sum();
  212. $this->assertTrue(is_string($call->getPeer()));
  213. for ($i = 0; $i < 7; ++$i) {
  214. $num = new Math\Num();
  215. $num->setNum($i);
  216. $call->write($num);
  217. }
  218. list($response, $status) = $call->wait();
  219. $this->assertSame(21, $response->getNum());
  220. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  221. }
  222. public function testBidiStreaming()
  223. {
  224. $call = self::$client->DivMany();
  225. $this->assertTrue(is_string($call->getPeer()));
  226. for ($i = 0; $i < 7; ++$i) {
  227. $div_arg = new Math\DivArgs();
  228. $div_arg->setDividend(2 * $i + 1);
  229. $div_arg->setDivisor(2);
  230. $call->write($div_arg);
  231. $response = $call->read();
  232. $this->assertSame($i, $response->getQuotient());
  233. $this->assertSame(1, $response->getRemainder());
  234. }
  235. $call->writesDone();
  236. $status = $call->getStatus();
  237. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  238. }
  239. }
  240. class DummyInvalidClient extends \Grpc\BaseStub
  241. {
  242. public function InvalidUnaryCall(\Math\DivArgs $argument,
  243. $metadata = [],
  244. $options = [])
  245. {
  246. return $this->_simpleRequest('invalidMethodName',
  247. $argument,
  248. function () {},
  249. $metadata,
  250. $options);
  251. }
  252. }