blob: 43da47fd5318100bee9653ad239ea80744428ac5 [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) {
28 list($result, $status) = $stub->EmptyCall(new proto2\EmptyMessage())->wait();
29 hardAssert($status->code == Grpc\STATUS_OK, 'Call did not complete successfully');
30 hardAssert($result != null, 'Call completed with a null response');
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 */
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();
52 hardAssert($status->code == Grpc\STATUS_OK, 'Call did not complete successfully');
53 hardAssert($result != null, 'Call returned a null response');
54 $payload = $result->getPayload();
55 hardAssert($payload->getType() == grpc\testing\PayloadType::COMPRESSABLE,
56 'Payload had the wrong type');
57 hardAssert(strlen($payload->getBody()) == $response_len,
58 'Payload had the wrong length');
59 hardAssert($payload->getBody() == str_repeat("\0", $response_len),
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 */
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();
81 hardAssert($status->code == Grpc\STATUS_OK, 'Call did not complete successfully');
82 hardAssert($result->getAggregatedPayloadSize() == 74922,
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 */
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);
103 hardAssert($call->getStatus()->code == Grpc\STATUS_OK,
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();
109 hardAssert($payload->getType() == grpc\testing\PayloadType::COMPRESSABLE,
110 'Payload ' . $i . ' had the wrong type');
111 hardAssert(strlen($payload->getBody()) == $sizes[$i],
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 */
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
139 hardAssert($response != null, 'Server returned too few responses');
140 $payload = $response->getPayload();
141 hardAssert($payload->getType() == grpc\testing\PayloadType::COMPRESSABLE,
142 'Payload ' . $i . ' had the wrong type');
143 hardAssert(strlen($payload->getBody()) == $response_lengths[$i],
144 'Payload ' . $i . ' had the wrong length');
145 }
146 $call->writesDone();
147 hardAssert($call->read() == null, 'Server returned too many responses');
148 hardAssert($call->getStatus()->code == Grpc\STATUS_OK,
149 'Call did not complete successfully');
150}
151
152$args = getopt('', array('server_host:', 'server_port:', 'test_case:'));
153if (!array_key_exists('server_host', $args) ||
154 !array_key_exists('server_port', $args) ||
155 !array_key_exists('test_case', $args)) {
156 throw new Exception('Missing argument');
157}
158
159$server_address = $args['server_host'] . ':' . $args['server_port'];
160
161$credentials = Grpc\Credentials::createSsl(
162 file_get_contents(dirname(__FILE__) . '/../data/ca.pem'));
163$stub = new grpc\testing\TestServiceClient(
164 $server_address,
165 [
166 'grpc.ssl_target_name_override' => 'foo.test.google.com',
167 'credentials' => $credentials
168 ]);
169
170echo "Connecting to $server_address\n";
171echo "Running test case $args[test_case]\n";
172
173switch($args['test_case']) {
174 case 'empty_unary':
175 emptyUnary($stub);
176 break;
177 case 'large_unary':
178 largeUnary($stub);
179 break;
180 case 'client_streaming':
181 clientStreaming($stub);
182 break;
183 case 'server_streaming':
184 serverStreaming($stub);
185 break;
186 case 'ping_pong':
187 pingPong($stub);
188 break;
189}