blob: 5266e9a9faca6052b24a2a3890dd80e1a15bdfe1 [file] [log] [blame]
mlumishb892a272014-12-09 16:28:23 -08001<?php
mlumish156e67d2015-01-02 14:59:16 -08002require_once realpath(dirname(__FILE__) . '/../../lib/autoload.php');
mlumishb892a272014-12-09 16:28:23 -08003require 'DrSlump/Protobuf.php';
4\DrSlump\Protobuf::autoload();
5require 'empty.php';
6require 'message_set.php';
7require 'messages.php';
8require '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 */
15function 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 */
27function emptyUnary($stub) {
murgatroid99f21eb252015-01-30 13:47:41 -080028 list($result, $status) = $stub->EmptyCall(new grpc\testing\EmptyMessage())->wait();
murgatroid9925e5f672015-02-02 11:05:01 -080029 hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
30 hardAssert($result !== null, 'Call completed with a null response');
mlumishb892a272014-12-09 16:28:23 -080031}
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 */
39function 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();
murgatroid9925e5f672015-02-02 11:05:01 -080052 hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
53 hardAssert($result !== null, 'Call returned a null response');
mlumishb892a272014-12-09 16:28:23 -080054 $payload = $result->getPayload();
murgatroid9925e5f672015-02-02 11:05:01 -080055 hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
mlumishb892a272014-12-09 16:28:23 -080056 'Payload had the wrong type');
murgatroid9925e5f672015-02-02 11:05:01 -080057 hardAssert(strlen($payload->getBody()) === $response_len,
mlumishb892a272014-12-09 16:28:23 -080058 'Payload had the wrong length');
murgatroid9925e5f672015-02-02 11:05:01 -080059 hardAssert($payload->getBody() === str_repeat("\0", $response_len),
mlumishb892a272014-12-09 16:28:23 -080060 '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 */
68function 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();
murgatroid9925e5f672015-02-02 11:05:01 -080081 hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully');
82 hardAssert($result->getAggregatedPayloadSize() === 74922,
mlumishb892a272014-12-09 16:28:23 -080083 '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 */
91function 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);
murgatroid9925e5f672015-02-02 11:05:01 -0800103 hardAssert($call->getStatus()->code === Grpc\STATUS_OK,
mlumishb892a272014-12-09 16:28:23 -0800104 '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();
murgatroid9925e5f672015-02-02 11:05:01 -0800109 hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
mlumishb892a272014-12-09 16:28:23 -0800110 'Payload ' . $i . ' had the wrong type');
murgatroid9925e5f672015-02-02 11:05:01 -0800111 hardAssert(strlen($payload->getBody()) === $sizes[$i],
mlumishb892a272014-12-09 16:28:23 -0800112 '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 */
121function 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
murgatroid9925e5f672015-02-02 11:05:01 -0800139 hardAssert($response !== null, 'Server returned too few responses');
mlumishb892a272014-12-09 16:28:23 -0800140 $payload = $response->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()) === $response_lengths[$i],
mlumishb892a272014-12-09 16:28:23 -0800144 'Payload ' . $i . ' had the wrong length');
145 }
146 $call->writesDone();
murgatroid9925e5f672015-02-02 11:05:01 -0800147 hardAssert($call->read() === null, 'Server returned too many responses');
148 hardAssert($call->getStatus()->code === Grpc\STATUS_OK,
mlumishb892a272014-12-09 16:28:23 -0800149 'Call did not complete successfully');
150}
151
murgatroid9975c9d2f2015-02-03 18:16:23 -0800152function cancelAfterFirstResponse($stub) {
murgatroid99554fe352015-02-03 18:18:28 -0800153 $call = $stub->FullDuplexCall();
murgatroid9975c9d2f2015-02-03 18:16:23 -0800154 $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
mlumishb892a272014-12-09 16:28:23 -0800171$args = getopt('', array('server_host:', 'server_port:', 'test_case:'));
172if (!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(
murgatroid9914d2ce22015-01-30 15:36:23 -0800183 new Grpc\BaseStub(
184 $server_address,
185 [
186 'grpc.ssl_target_name_override' => 'foo.test.google.com',
187 'credentials' => $credentials
188 ]));
mlumishb892a272014-12-09 16:28:23 -0800189
190echo "Connecting to $server_address\n";
191echo "Running test case $args[test_case]\n";
192
193switch($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;
murgatroid9975c9d2f2015-02-03 18:16:23 -0800209 case 'cancel_after_first_response':
210 cancelAfterFirstResponse($stub);
mlumishb892a272014-12-09 16:28:23 -0800211}