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