4 * Copyright 2015 gRPC authors.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 class EndToEndTest extends \PHPUnit\Framework\TestCase
21 public function setUp(): void
23 $this->server = new Grpc\Server([]);
24 $this->port = $this->server->addHttp2Port('0.0.0.0:0');
25 $this->channel = new Grpc\Channel('localhost:'.$this->port, [
28 $this->server->start();
31 public function tearDown(): void
33 $this->channel->close();
37 public function testSimpleRequestBody()
39 $deadline = Grpc\Timeval::infFuture();
41 $call = new Grpc\Call($this->channel,
45 $event = $call->startBatch([
46 Grpc\OP_SEND_INITIAL_METADATA => [],
47 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
50 $this->assertTrue($event->send_metadata);
51 $this->assertTrue($event->send_close);
53 $event = $this->server->requestCall();
54 $this->assertSame('dummy_method', $event->method);
55 $server_call = $event->call;
57 $event = $server_call->startBatch([
58 Grpc\OP_SEND_INITIAL_METADATA => [],
59 Grpc\OP_SEND_STATUS_FROM_SERVER => [
61 'code' => Grpc\STATUS_OK,
62 'details' => $status_text,
64 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
67 $this->assertTrue($event->send_metadata);
68 $this->assertTrue($event->send_status);
69 $this->assertFalse($event->cancelled);
71 $event = $call->startBatch([
72 Grpc\OP_RECV_INITIAL_METADATA => true,
73 Grpc\OP_RECV_STATUS_ON_CLIENT => true,
76 $status = $event->status;
77 $this->assertSame([], $status->metadata);
78 $this->assertSame(Grpc\STATUS_OK, $status->code);
79 $this->assertSame($status_text, $status->details);
85 public function testMessageWriteFlags()
87 $deadline = Grpc\Timeval::infFuture();
88 $req_text = 'message_write_flags_test';
90 $call = new Grpc\Call($this->channel,
94 $event = $call->startBatch([
95 Grpc\OP_SEND_INITIAL_METADATA => [],
96 Grpc\OP_SEND_MESSAGE => ['message' => $req_text,
97 'flags' => Grpc\WRITE_NO_COMPRESS, ],
98 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
101 $this->assertTrue($event->send_metadata);
102 $this->assertTrue($event->send_close);
104 $event = $this->server->requestCall();
105 $this->assertSame('dummy_method', $event->method);
106 $server_call = $event->call;
108 $event = $server_call->startBatch([
109 Grpc\OP_SEND_INITIAL_METADATA => [],
110 Grpc\OP_SEND_STATUS_FROM_SERVER => [
112 'code' => Grpc\STATUS_OK,
113 'details' => $status_text,
117 $event = $call->startBatch([
118 Grpc\OP_RECV_INITIAL_METADATA => true,
119 Grpc\OP_RECV_STATUS_ON_CLIENT => true,
122 $status = $event->status;
123 $this->assertSame([], $status->metadata);
124 $this->assertSame(Grpc\STATUS_OK, $status->code);
125 $this->assertSame($status_text, $status->details);
131 public function testClientServerFullRequestResponse()
133 $deadline = Grpc\Timeval::infFuture();
134 $req_text = 'client_server_full_request_response';
135 $reply_text = 'reply:client_server_full_request_response';
136 $status_text = 'status:client_server_full_response_text';
138 $call = new Grpc\Call($this->channel,
142 $event = $call->startBatch([
143 Grpc\OP_SEND_INITIAL_METADATA => [],
144 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
145 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
148 $this->assertTrue($event->send_metadata);
149 $this->assertTrue($event->send_close);
150 $this->assertTrue($event->send_message);
152 $event = $this->server->requestCall();
153 $this->assertSame('dummy_method', $event->method);
154 $server_call = $event->call;
156 $event = $server_call->startBatch([
157 Grpc\OP_SEND_INITIAL_METADATA => [],
158 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
159 Grpc\OP_SEND_STATUS_FROM_SERVER => [
161 'code' => Grpc\STATUS_OK,
162 'details' => $status_text,
164 Grpc\OP_RECV_MESSAGE => true,
165 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
168 $this->assertTrue($event->send_metadata);
169 $this->assertTrue($event->send_status);
170 $this->assertTrue($event->send_message);
171 $this->assertFalse($event->cancelled);
172 $this->assertSame($req_text, $event->message);
174 $event = $call->startBatch([
175 Grpc\OP_RECV_INITIAL_METADATA => true,
176 Grpc\OP_RECV_MESSAGE => true,
177 Grpc\OP_RECV_STATUS_ON_CLIENT => true,
180 $this->assertSame([], $event->metadata);
181 $this->assertSame($reply_text, $event->message);
182 $status = $event->status;
183 $this->assertSame([], $status->metadata);
184 $this->assertSame(Grpc\STATUS_OK, $status->code);
185 $this->assertSame($status_text, $status->details);
192 * @expectedException InvalidArgumentException
194 public function testInvalidClientMessageArray()
196 $deadline = Grpc\Timeval::infFuture();
197 $req_text = 'client_server_full_request_response';
198 $reply_text = 'reply:client_server_full_request_response';
199 $status_text = 'status:client_server_full_response_text';
201 $call = new Grpc\Call($this->channel,
205 $event = $call->startBatch([
206 Grpc\OP_SEND_INITIAL_METADATA => [],
207 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
208 Grpc\OP_SEND_MESSAGE => 'invalid',
213 * @expectedException InvalidArgumentException
215 public function testInvalidClientMessageString()
217 $deadline = Grpc\Timeval::infFuture();
218 $req_text = 'client_server_full_request_response';
219 $reply_text = 'reply:client_server_full_request_response';
220 $status_text = 'status:client_server_full_response_text';
222 $call = new Grpc\Call($this->channel,
226 $event = $call->startBatch([
227 Grpc\OP_SEND_INITIAL_METADATA => [],
228 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
229 Grpc\OP_SEND_MESSAGE => ['message' => 0],
234 * @expectedException InvalidArgumentException
236 public function testInvalidClientMessageFlags()
238 $deadline = Grpc\Timeval::infFuture();
239 $req_text = 'client_server_full_request_response';
240 $reply_text = 'reply:client_server_full_request_response';
241 $status_text = 'status:client_server_full_response_text';
243 $call = new Grpc\Call($this->channel,
247 $event = $call->startBatch([
248 Grpc\OP_SEND_INITIAL_METADATA => [],
249 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
250 Grpc\OP_SEND_MESSAGE => ['message' => 'abc',
251 'flags' => 'invalid',
257 * @expectedException InvalidArgumentException
259 public function testInvalidServerStatusMetadata()
261 $deadline = Grpc\Timeval::infFuture();
262 $req_text = 'client_server_full_request_response';
263 $reply_text = 'reply:client_server_full_request_response';
264 $status_text = 'status:client_server_full_response_text';
266 $call = new Grpc\Call($this->channel,
270 $event = $call->startBatch([
271 Grpc\OP_SEND_INITIAL_METADATA => [],
272 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
273 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
276 $this->assertTrue($event->send_metadata);
277 $this->assertTrue($event->send_close);
278 $this->assertTrue($event->send_message);
280 $event = $this->server->requestCall();
281 $this->assertSame('dummy_method', $event->method);
282 $server_call = $event->call;
284 $event = $server_call->startBatch([
285 Grpc\OP_SEND_INITIAL_METADATA => [],
286 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
287 Grpc\OP_SEND_STATUS_FROM_SERVER => [
288 'metadata' => 'invalid',
289 'code' => Grpc\STATUS_OK,
290 'details' => $status_text,
292 Grpc\OP_RECV_MESSAGE => true,
293 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
298 * @expectedException InvalidArgumentException
300 public function testInvalidServerStatusCode()
302 $deadline = Grpc\Timeval::infFuture();
303 $req_text = 'client_server_full_request_response';
304 $reply_text = 'reply:client_server_full_request_response';
305 $status_text = 'status:client_server_full_response_text';
307 $call = new Grpc\Call($this->channel,
311 $event = $call->startBatch([
312 Grpc\OP_SEND_INITIAL_METADATA => [],
313 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
314 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
317 $this->assertTrue($event->send_metadata);
318 $this->assertTrue($event->send_close);
319 $this->assertTrue($event->send_message);
321 $event = $this->server->requestCall();
322 $this->assertSame('dummy_method', $event->method);
323 $server_call = $event->call;
325 $event = $server_call->startBatch([
326 Grpc\OP_SEND_INITIAL_METADATA => [],
327 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
328 Grpc\OP_SEND_STATUS_FROM_SERVER => [
331 'details' => $status_text,
333 Grpc\OP_RECV_MESSAGE => true,
334 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
339 * @expectedException InvalidArgumentException
341 public function testMissingServerStatusCode()
343 $deadline = Grpc\Timeval::infFuture();
344 $req_text = 'client_server_full_request_response';
345 $reply_text = 'reply:client_server_full_request_response';
346 $status_text = 'status:client_server_full_response_text';
348 $call = new Grpc\Call($this->channel,
352 $event = $call->startBatch([
353 Grpc\OP_SEND_INITIAL_METADATA => [],
354 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
355 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
358 $this->assertTrue($event->send_metadata);
359 $this->assertTrue($event->send_close);
360 $this->assertTrue($event->send_message);
362 $event = $this->server->requestCall();
363 $this->assertSame('dummy_method', $event->method);
364 $server_call = $event->call;
366 $event = $server_call->startBatch([
367 Grpc\OP_SEND_INITIAL_METADATA => [],
368 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
369 Grpc\OP_SEND_STATUS_FROM_SERVER => [
371 'details' => $status_text,
373 Grpc\OP_RECV_MESSAGE => true,
374 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
379 * @expectedException InvalidArgumentException
381 public function testInvalidServerStatusDetails()
383 $deadline = Grpc\Timeval::infFuture();
384 $req_text = 'client_server_full_request_response';
385 $reply_text = 'reply:client_server_full_request_response';
386 $status_text = 'status:client_server_full_response_text';
388 $call = new Grpc\Call($this->channel,
392 $event = $call->startBatch([
393 Grpc\OP_SEND_INITIAL_METADATA => [],
394 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
395 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
398 $this->assertTrue($event->send_metadata);
399 $this->assertTrue($event->send_close);
400 $this->assertTrue($event->send_message);
402 $event = $this->server->requestCall();
403 $this->assertSame('dummy_method', $event->method);
404 $server_call = $event->call;
406 $event = $server_call->startBatch([
407 Grpc\OP_SEND_INITIAL_METADATA => [],
408 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
409 Grpc\OP_SEND_STATUS_FROM_SERVER => [
411 'code' => Grpc\STATUS_OK,
414 Grpc\OP_RECV_MESSAGE => true,
415 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
420 * @expectedException InvalidArgumentException
422 public function testMissingServerStatusDetails()
424 $deadline = Grpc\Timeval::infFuture();
425 $req_text = 'client_server_full_request_response';
426 $reply_text = 'reply:client_server_full_request_response';
427 $status_text = 'status:client_server_full_response_text';
429 $call = new Grpc\Call($this->channel,
433 $event = $call->startBatch([
434 Grpc\OP_SEND_INITIAL_METADATA => [],
435 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
436 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
439 $this->assertTrue($event->send_metadata);
440 $this->assertTrue($event->send_close);
441 $this->assertTrue($event->send_message);
443 $event = $this->server->requestCall();
444 $this->assertSame('dummy_method', $event->method);
445 $server_call = $event->call;
447 $event = $server_call->startBatch([
448 Grpc\OP_SEND_INITIAL_METADATA => [],
449 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
450 Grpc\OP_SEND_STATUS_FROM_SERVER => [
452 'code' => Grpc\STATUS_OK,
454 Grpc\OP_RECV_MESSAGE => true,
455 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
460 * @expectedException InvalidArgumentException
462 public function testInvalidStartBatchKey()
464 $deadline = Grpc\Timeval::infFuture();
465 $req_text = 'client_server_full_request_response';
466 $reply_text = 'reply:client_server_full_request_response';
467 $status_text = 'status:client_server_full_response_text';
469 $call = new Grpc\Call($this->channel,
473 $event = $call->startBatch([
479 * @expectedException LogicException
481 public function testInvalidStartBatch()
483 $deadline = Grpc\Timeval::infFuture();
484 $req_text = 'client_server_full_request_response';
485 $reply_text = 'reply:client_server_full_request_response';
486 $status_text = 'status:client_server_full_response_text';
488 $call = new Grpc\Call($this->channel,
492 $event = $call->startBatch([
493 Grpc\OP_SEND_INITIAL_METADATA => [],
494 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
495 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
496 Grpc\OP_SEND_STATUS_FROM_SERVER => [
498 'code' => Grpc\STATUS_OK,
504 public function testGetTarget()
506 $this->assertTrue(is_string($this->channel->getTarget()));
509 public function testGetConnectivityState()
511 $this->assertTrue($this->channel->getConnectivityState() ==
515 public function testWatchConnectivityStateFailed()
517 $idle_state = $this->channel->getConnectivityState();
518 $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE);
520 $now = Grpc\Timeval::now();
521 $delta = new Grpc\Timeval(50000); // should timeout
522 $deadline = $now->add($delta);
524 $this->assertFalse($this->channel->watchConnectivityState(
525 $idle_state, $deadline));
528 public function testWatchConnectivityStateSuccess()
530 $idle_state = $this->channel->getConnectivityState(true);
531 $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE);
533 $now = Grpc\Timeval::now();
534 $delta = new Grpc\Timeval(3000000); // should finish well before
535 $deadline = $now->add($delta);
537 $this->assertTrue($this->channel->watchConnectivityState(
538 $idle_state, $deadline));
540 $new_state = $this->channel->getConnectivityState();
541 $this->assertTrue($idle_state != $new_state);
544 public function testWatchConnectivityStateDoNothing()
546 $idle_state = $this->channel->getConnectivityState();
547 $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE);
549 $now = Grpc\Timeval::now();
550 $delta = new Grpc\Timeval(50000);
551 $deadline = $now->add($delta);
553 $this->assertFalse($this->channel->watchConnectivityState(
554 $idle_state, $deadline));
556 $new_state = $this->channel->getConnectivityState();
557 $this->assertTrue($new_state == Grpc\CHANNEL_IDLE);
561 * @expectedException InvalidArgumentException
563 public function testGetConnectivityStateInvalidParam()
565 $this->assertTrue($this->channel->getConnectivityState(
566 new Grpc\Timeval()));
570 * @expectedException InvalidArgumentException
572 public function testWatchConnectivityStateInvalidParam()
574 $this->assertTrue($this->channel->watchConnectivityState(
579 * @expectedException InvalidArgumentException
581 public function testChannelConstructorInvalidParam()
583 $this->channel = new Grpc\Channel('localhost:'.$this->port, null);
586 public function testClose()
588 $this->assertNull($this->channel->close());