blob: 82ca4381690e702aae5873f25d1a5731ed74be96 [file] [log] [blame]
mlumishb892a272014-12-09 16:28:23 -08001<?php
Craig Tiller2e498aa2015-02-16 12:09:31 -08002/*
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 */
mlumish156e67d2015-01-02 14:59:16 -080034require_once realpath(dirname(__FILE__) . '/../../lib/autoload.php');
mlumishb892a272014-12-09 16:28:23 -080035require 'DrSlump/Protobuf.php';
36\DrSlump\Protobuf::autoload();
37require 'empty.php';
38require 'message_set.php';
39require 'messages.php';
40require '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 */
47function hardAssert($value, $error_message) {
48 if(!$value) {
49 echo $error_message . "\n";
50 exit(1);
51 }
52}
53
54/**
55 * Run the empty_unary test.
56 * Currently not tested against any server as of 2014-12-04
57 * @param $stub Stub object that has service methods
58 */
59function emptyUnary($stub) {
murgatroid99f21eb252015-01-30 13:47:41 -080060 list($result, $status) = $stub->EmptyCall(new grpc\testing\EmptyMessage())->wait();
murgatroid9925e5f672015-02-02 11:05:01 -080061 hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
62 hardAssert($result !== null, 'Call completed with a null response');
mlumishb892a272014-12-09 16:28:23 -080063}
64
65/**
66 * Run the large_unary test.
67 * Passes when run against the C++ server as of 2014-12-04
68 * Not tested against any other server as of 2014-12-04
69 * @param $stub Stub object that has service methods
70 */
71function largeUnary($stub) {
72 $request_len = 271828;
73 $response_len = 314159;
74
75 $request = new grpc\testing\SimpleRequest();
76 $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
77 $request->setResponseSize($response_len);
78 $payload = new grpc\testing\Payload();
79 $payload->setType(grpc\testing\PayloadType::COMPRESSABLE);
80 $payload->setBody(str_repeat("\0", $request_len));
81 $request->setPayload($payload);
82
83 list($result, $status) = $stub->UnaryCall($request)->wait();
murgatroid9925e5f672015-02-02 11:05:01 -080084 hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
85 hardAssert($result !== null, 'Call returned a null response');
mlumishb892a272014-12-09 16:28:23 -080086 $payload = $result->getPayload();
murgatroid9925e5f672015-02-02 11:05:01 -080087 hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
mlumishb892a272014-12-09 16:28:23 -080088 'Payload had the wrong type');
murgatroid9925e5f672015-02-02 11:05:01 -080089 hardAssert(strlen($payload->getBody()) === $response_len,
mlumishb892a272014-12-09 16:28:23 -080090 'Payload had the wrong length');
murgatroid9925e5f672015-02-02 11:05:01 -080091 hardAssert($payload->getBody() === str_repeat("\0", $response_len),
mlumishb892a272014-12-09 16:28:23 -080092 'Payload had the wrong content');
93}
94
95/**
96 * Run the client_streaming test.
97 * Not tested against any server as of 2014-12-04.
98 * @param $stub Stub object that has service methods
99 */
100function clientStreaming($stub) {
101 $request_lengths = array(27182, 8, 1828, 45904);
102
103 $requests = array_map(
104 function($length) {
105 $request = new grpc\testing\StreamingInputCallRequest();
106 $payload = new grpc\testing\Payload();
107 $payload->setBody(str_repeat("\0", $length));
108 $request->setPayload($payload);
109 return $request;
110 }, $request_lengths);
111
112 list($result, $status) = $stub->StreamingInputCall($requests)->wait();
murgatroid9925e5f672015-02-02 11:05:01 -0800113 hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
114 hardAssert($result->getAggregatedPayloadSize() === 74922,
mlumishb892a272014-12-09 16:28:23 -0800115 'aggregated_payload_size was incorrect');
116}
117
118/**
119 * Run the server_streaming test.
120 * Not tested against any server as of 2014-12-04.
121 * @param $stub Stub object that has service methods.
122 */
123function serverStreaming($stub) {
124 $sizes = array(31415, 9, 2653, 58979);
125
126 $request = new grpc\testing\StreamingOutputCallRequest();
127 $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
128 foreach($sizes as $size) {
129 $response_parameters = new grpc\testing\ResponseParameters();
130 $response_parameters->setSize($size);
131 $request->addResponseParameters($response_parameters);
132 }
133
134 $call = $stub->StreamingOutputCall($request);
murgatroid9925e5f672015-02-02 11:05:01 -0800135 hardAssert($call->getStatus()->code === Grpc\STATUS_OK,
mlumishb892a272014-12-09 16:28:23 -0800136 'Call did not complete successfully');
137 $i = 0;
138 foreach($call->responses() as $value) {
139 hardAssert($i < 4, 'Too many responses');
140 $payload = $value->getPayload();
murgatroid9925e5f672015-02-02 11:05:01 -0800141 hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
mlumishb892a272014-12-09 16:28:23 -0800142 'Payload ' . $i . ' had the wrong type');
murgatroid9925e5f672015-02-02 11:05:01 -0800143 hardAssert(strlen($payload->getBody()) === $sizes[$i],
mlumishb892a272014-12-09 16:28:23 -0800144 'Response ' . $i . ' had the wrong length');
145 }
146}
147
148/**
149 * Run the ping_pong test.
150 * Not tested against any server as of 2014-12-04.
151 * @param $stub Stub object that has service methods.
152 */
153function pingPong($stub) {
154 $request_lengths = array(27182, 8, 1828, 45904);
155 $response_lengths = array(31415, 9, 2653, 58979);
156
157 $call = $stub->FullDuplexCall();
158 for($i = 0; $i < 4; $i++) {
159 $request = new grpc\testing\StreamingOutputCallRequest();
160 $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
161 $response_parameters = new grpc\testing\ResponseParameters();
162 $response_parameters->setSize($response_lengths[$i]);
163 $request->addResponseParameters($response_parameters);
164 $payload = new grpc\testing\Payload();
165 $payload->setBody(str_repeat("\0", $request_lengths[$i]));
166 $request->setPayload($payload);
167
168 $call->write($request);
169 $response = $call->read();
170
murgatroid9925e5f672015-02-02 11:05:01 -0800171 hardAssert($response !== null, 'Server returned too few responses');
mlumishb892a272014-12-09 16:28:23 -0800172 $payload = $response->getPayload();
murgatroid9925e5f672015-02-02 11:05:01 -0800173 hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
mlumishb892a272014-12-09 16:28:23 -0800174 'Payload ' . $i . ' had the wrong type');
murgatroid9925e5f672015-02-02 11:05:01 -0800175 hardAssert(strlen($payload->getBody()) === $response_lengths[$i],
mlumishb892a272014-12-09 16:28:23 -0800176 'Payload ' . $i . ' had the wrong length');
177 }
178 $call->writesDone();
murgatroid9925e5f672015-02-02 11:05:01 -0800179 hardAssert($call->read() === null, 'Server returned too many responses');
180 hardAssert($call->getStatus()->code === Grpc\STATUS_OK,
mlumishb892a272014-12-09 16:28:23 -0800181 'Call did not complete successfully');
182}
183
murgatroid9975c9d2f2015-02-03 18:16:23 -0800184function cancelAfterFirstResponse($stub) {
murgatroid99554fe352015-02-03 18:18:28 -0800185 $call = $stub->FullDuplexCall();
murgatroid9975c9d2f2015-02-03 18:16:23 -0800186 $request = new grpc\testing\StreamingOutputCallRequest();
187 $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
188 $response_parameters = new grpc\testing\ResponseParameters();
189 $response_parameters->setSize(31415);
190 $request->addResponseParameters($response_parameters);
191 $payload = new grpc\testing\Payload();
192 $payload->setBody(str_repeat("\0", 27182));
193 $request->setPayload($payload);
194
195 $call->write($request);
196 $response = $call->read();
197
198 $call->cancel();
199 hardAssert($call->getStatus()->code === Grpc\STATUS_CANCELLED,
200 'Call status was not CANCELLED');
201}
202
mlumishb892a272014-12-09 16:28:23 -0800203$args = getopt('', array('server_host:', 'server_port:', 'test_case:'));
204if (!array_key_exists('server_host', $args) ||
205 !array_key_exists('server_port', $args) ||
206 !array_key_exists('test_case', $args)) {
207 throw new Exception('Missing argument');
208}
209
210$server_address = $args['server_host'] . ':' . $args['server_port'];
211
212$credentials = Grpc\Credentials::createSsl(
213 file_get_contents(dirname(__FILE__) . '/../data/ca.pem'));
214$stub = new grpc\testing\TestServiceClient(
murgatroid9914d2ce22015-01-30 15:36:23 -0800215 new Grpc\BaseStub(
216 $server_address,
217 [
Julien Boeuf597a4f22015-02-23 15:57:14 -0800218 'grpc.ssl_target_name_override' => 'foo.test.google.fr',
murgatroid9914d2ce22015-01-30 15:36:23 -0800219 'credentials' => $credentials
220 ]));
mlumishb892a272014-12-09 16:28:23 -0800221
222echo "Connecting to $server_address\n";
223echo "Running test case $args[test_case]\n";
224
225switch($args['test_case']) {
226 case 'empty_unary':
227 emptyUnary($stub);
228 break;
229 case 'large_unary':
230 largeUnary($stub);
231 break;
232 case 'client_streaming':
233 clientStreaming($stub);
234 break;
235 case 'server_streaming':
236 serverStreaming($stub);
237 break;
238 case 'ping_pong':
239 pingPong($stub);
240 break;
murgatroid9975c9d2f2015-02-03 18:16:23 -0800241 case 'cancel_after_first_response':
242 cancelAfterFirstResponse($stub);
Craig Tiller2e498aa2015-02-16 12:09:31 -0800243}