interop_client.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /*
  3. *
  4. * Copyright 2015, 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. require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php');
  35. require 'DrSlump/Protobuf.php';
  36. \DrSlump\Protobuf::autoload();
  37. require 'empty.php';
  38. require 'message_set.php';
  39. require 'messages.php';
  40. require 'test.php';
  41. /**
  42. * Assertion function that always exits with an error code if the assertion is
  43. * falsy
  44. * @param $value Assertion value. Should be true.
  45. * @param $error_message Message to display if the assertion is false
  46. */
  47. function hardAssert($value, $error_message) {
  48. if(!$value) {
  49. echo $error_message . "\n";
  50. exit(1);
  51. }
  52. }
  53. /**
  54. * Run the empty_unary test.
  55. * Currently not tested against any server as of 2014-12-04
  56. * @param $stub Stub object that has service methods
  57. */
  58. function emptyUnary($stub) {
  59. list($result, $status) = $stub->EmptyCall(new grpc\testing\EmptyMessage())->wait();
  60. hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
  61. hardAssert($result !== null, 'Call completed with a null response');
  62. }
  63. /**
  64. * Run the large_unary test.
  65. * Passes when run against the C++ server as of 2014-12-04
  66. * Not tested against any other server as of 2014-12-04
  67. * @param $stub Stub object that has service methods
  68. */
  69. function largeUnary($stub) {
  70. $request_len = 271828;
  71. $response_len = 314159;
  72. $request = new grpc\testing\SimpleRequest();
  73. $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
  74. $request->setResponseSize($response_len);
  75. $payload = new grpc\testing\Payload();
  76. $payload->setType(grpc\testing\PayloadType::COMPRESSABLE);
  77. $payload->setBody(str_repeat("\0", $request_len));
  78. $request->setPayload($payload);
  79. list($result, $status) = $stub->UnaryCall($request)->wait();
  80. hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
  81. hardAssert($result !== null, 'Call returned a null response');
  82. $payload = $result->getPayload();
  83. hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
  84. 'Payload had the wrong type');
  85. hardAssert(strlen($payload->getBody()) === $response_len,
  86. 'Payload had the wrong length');
  87. hardAssert($payload->getBody() === str_repeat("\0", $response_len),
  88. 'Payload had the wrong content');
  89. }
  90. /**
  91. * Run the client_streaming test.
  92. * Not tested against any server as of 2014-12-04.
  93. * @param $stub Stub object that has service methods
  94. */
  95. function clientStreaming($stub) {
  96. $request_lengths = array(27182, 8, 1828, 45904);
  97. $requests = array_map(
  98. function($length) {
  99. $request = new grpc\testing\StreamingInputCallRequest();
  100. $payload = new grpc\testing\Payload();
  101. $payload->setBody(str_repeat("\0", $length));
  102. $request->setPayload($payload);
  103. return $request;
  104. }, $request_lengths);
  105. list($result, $status) = $stub->StreamingInputCall($requests)->wait();
  106. hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
  107. hardAssert($result->getAggregatedPayloadSize() === 74922,
  108. 'aggregated_payload_size was incorrect');
  109. }
  110. /**
  111. * Run the server_streaming test.
  112. * Not tested against any server as of 2014-12-04.
  113. * @param $stub Stub object that has service methods.
  114. */
  115. function serverStreaming($stub) {
  116. $sizes = array(31415, 9, 2653, 58979);
  117. $request = new grpc\testing\StreamingOutputCallRequest();
  118. $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
  119. foreach($sizes as $size) {
  120. $response_parameters = new grpc\testing\ResponseParameters();
  121. $response_parameters->setSize($size);
  122. $request->addResponseParameters($response_parameters);
  123. }
  124. $call = $stub->StreamingOutputCall($request);
  125. $i = 0;
  126. foreach($call->responses() as $value) {
  127. hardAssert($i < 4, 'Too many responses');
  128. $payload = $value->getPayload();
  129. hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
  130. 'Payload ' . $i . ' had the wrong type');
  131. hardAssert(strlen($payload->getBody()) === $sizes[$i],
  132. 'Response ' . $i . ' had the wrong length');
  133. $i += 1;
  134. }
  135. hardAssert($call->getStatus()->code === Grpc\STATUS_OK,
  136. 'Call did not complete successfully');
  137. }
  138. /**
  139. * Run the ping_pong test.
  140. * Not tested against any server as of 2014-12-04.
  141. * @param $stub Stub object that has service methods.
  142. */
  143. function pingPong($stub) {
  144. $request_lengths = array(27182, 8, 1828, 45904);
  145. $response_lengths = array(31415, 9, 2653, 58979);
  146. $call = $stub->FullDuplexCall();
  147. for($i = 0; $i < 4; $i++) {
  148. $request = new grpc\testing\StreamingOutputCallRequest();
  149. $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
  150. $response_parameters = new grpc\testing\ResponseParameters();
  151. $response_parameters->setSize($response_lengths[$i]);
  152. $request->addResponseParameters($response_parameters);
  153. $payload = new grpc\testing\Payload();
  154. $payload->setBody(str_repeat("\0", $request_lengths[$i]));
  155. $request->setPayload($payload);
  156. $call->write($request);
  157. $response = $call->read();
  158. hardAssert($response !== null, 'Server returned too few responses');
  159. $payload = $response->getPayload();
  160. hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
  161. 'Payload ' . $i . ' had the wrong type');
  162. hardAssert(strlen($payload->getBody()) === $response_lengths[$i],
  163. 'Payload ' . $i . ' had the wrong length');
  164. }
  165. $call->writesDone();
  166. hardAssert($call->read() === null, 'Server returned too many responses');
  167. hardAssert($call->getStatus()->code === Grpc\STATUS_OK,
  168. 'Call did not complete successfully');
  169. }
  170. function cancelAfterFirstResponse($stub) {
  171. $call = $stub->FullDuplexCall();
  172. $request = new grpc\testing\StreamingOutputCallRequest();
  173. $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
  174. $response_parameters = new grpc\testing\ResponseParameters();
  175. $response_parameters->setSize(31415);
  176. $request->addResponseParameters($response_parameters);
  177. $payload = new grpc\testing\Payload();
  178. $payload->setBody(str_repeat("\0", 27182));
  179. $request->setPayload($payload);
  180. $call->write($request);
  181. $response = $call->read();
  182. $call->cancel();
  183. hardAssert($call->getStatus()->code === Grpc\STATUS_CANCELLED,
  184. 'Call status was not CANCELLED');
  185. }
  186. $args = getopt('', array('server_host:', 'server_port:', 'test_case:'));
  187. if (!array_key_exists('server_host', $args) ||
  188. !array_key_exists('server_port', $args) ||
  189. !array_key_exists('test_case', $args)) {
  190. throw new Exception('Missing argument');
  191. }
  192. $server_address = $args['server_host'] . ':' . $args['server_port'];
  193. $credentials = Grpc\Credentials::createSsl(
  194. file_get_contents(dirname(__FILE__) . '/../data/ca.pem'));
  195. $stub = new grpc\testing\TestServiceClient(
  196. new Grpc\BaseStub(
  197. $server_address,
  198. [
  199. 'grpc.ssl_target_name_override' => 'foo.test.google.fr',
  200. 'credentials' => $credentials
  201. ]));
  202. echo "Connecting to $server_address\n";
  203. echo "Running test case $args[test_case]\n";
  204. switch($args['test_case']) {
  205. case 'empty_unary':
  206. emptyUnary($stub);
  207. break;
  208. case 'large_unary':
  209. largeUnary($stub);
  210. break;
  211. case 'client_streaming':
  212. clientStreaming($stub);
  213. break;
  214. case 'server_streaming':
  215. serverStreaming($stub);
  216. break;
  217. case 'ping_pong':
  218. pingPong($stub);
  219. break;
  220. case 'cancel_after_first_response':
  221. cancelAfterFirstResponse($stub);
  222. default:
  223. exit(1);
  224. }