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