Debian Codegraph @

src/php/tests/unit_tests/EndToEndTest.php

raw
1 <?php
2 /*
3 *
4 * Copyright 2015 gRPC authors.
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 */
19 class EndToEndTest extends \PHPUnit\Framework\TestCase
20 {
21 private $server;
22 private $port;
23 private $channel;
24
25 public function setUp(): void
26 {
27 $this->server = new Grpc\Server([]);
28 $this->port = $this->server->addHttp2Port('0.0.0.0:0');
29 $this->channel = new Grpc\Channel('localhost:'.$this->port, [
30 "force_new" => true,
31 ]);
32 $this->server->start();
33 }
34
35 public function tearDown(): void
36 {
37 $this->channel->close();
38 unset($this->server);
39 }
40
41 public function testSimpleRequestBody()
42 {
43 $deadline = Grpc\Timeval::infFuture();
44 $status_text = 'xyz';
45 $call = new Grpc\Call($this->channel,
46 'phony_method',
47 $deadline);
48
49 $event = $call->startBatch([
50 Grpc\OP_SEND_INITIAL_METADATA => [],
51 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
52 ]);
53
54 $this->assertTrue($event->send_metadata);
55 $this->assertTrue($event->send_close);
56
57 $event = $this->server->requestCall();
58 $this->assertSame('phony_method', $event->method);
59 $server_call = $event->call;
60
61 $event = $server_call->startBatch([
62 Grpc\OP_SEND_INITIAL_METADATA => [],
63 Grpc\OP_SEND_STATUS_FROM_SERVER => [
64 'metadata' => [],
65 'code' => Grpc\STATUS_OK,
66 'details' => $status_text,
67 ],
68 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
69 ]);
70
71 $this->assertTrue($event->send_metadata);
72 $this->assertTrue($event->send_status);
73 $this->assertFalse($event->cancelled);
74
75 $event = $call->startBatch([
76 Grpc\OP_RECV_INITIAL_METADATA => true,
77 Grpc\OP_RECV_STATUS_ON_CLIENT => true,
78 ]);
79
80 $status = $event->status;
81 $this->assertSame([], $status->metadata);
82 $this->assertSame(Grpc\STATUS_OK, $status->code);
83 $this->assertSame($status_text, $status->details);
84
85 unset($call);
86 unset($server_call);
87 }
88
89 public function testMessageWriteFlags()
90 {
91 $deadline = Grpc\Timeval::infFuture();
92 $req_text = 'message_write_flags_test';
93 $status_text = 'xyz';
94 $call = new Grpc\Call($this->channel,
95 'phony_method',
96 $deadline);
97
98 $event = $call->startBatch([
99 Grpc\OP_SEND_INITIAL_METADATA => [],
100 Grpc\OP_SEND_MESSAGE => ['message' => $req_text,
101 'flags' => Grpc\WRITE_NO_COMPRESS, ],
102 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
103 ]);
104
105 $this->assertTrue($event->send_metadata);
106 $this->assertTrue($event->send_close);
107
108 $event = $this->server->requestCall();
109 $this->assertSame('phony_method', $event->method);
110 $server_call = $event->call;
111
112 $event = $server_call->startBatch([
113 Grpc\OP_SEND_INITIAL_METADATA => [],
114 Grpc\OP_SEND_STATUS_FROM_SERVER => [
115 'metadata' => [],
116 'code' => Grpc\STATUS_OK,
117 'details' => $status_text,
118 ],
119 ]);
120
121 $event = $call->startBatch([
122 Grpc\OP_RECV_INITIAL_METADATA => true,
123 Grpc\OP_RECV_STATUS_ON_CLIENT => true,
124 ]);
125
126 $status = $event->status;
127 $this->assertSame([], $status->metadata);
128 $this->assertSame(Grpc\STATUS_OK, $status->code);
129 $this->assertSame($status_text, $status->details);
130
131 unset($call);
132 unset($server_call);
133 }
134
135 public function testClientServerFullRequestResponse()
136 {
137 $deadline = Grpc\Timeval::infFuture();
138 $req_text = 'client_server_full_request_response';
139 $reply_text = 'reply:client_server_full_request_response';
140 $status_text = 'status:client_server_full_response_text';
141
142 $call = new Grpc\Call($this->channel,
143 'phony_method',
144 $deadline);
145
146 $event = $call->startBatch([
147 Grpc\OP_SEND_INITIAL_METADATA => [],
148 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
149 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
150 ]);
151
152 $this->assertTrue($event->send_metadata);
153 $this->assertTrue($event->send_close);
154 $this->assertTrue($event->send_message);
155
156 $event = $this->server->requestCall();
157 $this->assertSame('phony_method', $event->method);
158 $server_call = $event->call;
159
160 $event = $server_call->startBatch([
161 Grpc\OP_SEND_INITIAL_METADATA => [],
162 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
163 Grpc\OP_SEND_STATUS_FROM_SERVER => [
164 'metadata' => [],
165 'code' => Grpc\STATUS_OK,
166 'details' => $status_text,
167 ],
168 Grpc\OP_RECV_MESSAGE => true,
169 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
170 ]);
171
172 $this->assertTrue($event->send_metadata);
173 $this->assertTrue($event->send_status);
174 $this->assertTrue($event->send_message);
175 $this->assertFalse($event->cancelled);
176 $this->assertSame($req_text, $event->message);
177
178 $event = $call->startBatch([
179 Grpc\OP_RECV_INITIAL_METADATA => true,
180 Grpc\OP_RECV_MESSAGE => true,
181 Grpc\OP_RECV_STATUS_ON_CLIENT => true,
182 ]);
183
184 $this->assertSame([], $event->metadata);
185 $this->assertSame($reply_text, $event->message);
186 $status = $event->status;
187 $this->assertSame([], $status->metadata);
188 $this->assertSame(Grpc\STATUS_OK, $status->code);
189 $this->assertSame($status_text, $status->details);
190
191 unset($call);
192 unset($server_call);
193 }
194
195 public function testInvalidClientMessageArray()
196 {
197 $this->expectException(\InvalidArgumentException::class);
198 $deadline = Grpc\Timeval::infFuture();
199 $req_text = 'client_server_full_request_response';
200 $reply_text = 'reply:client_server_full_request_response';
201 $status_text = 'status:client_server_full_response_text';
202
203 $call = new Grpc\Call($this->channel,
204 'phony_method',
205 $deadline);
206
207 $event = $call->startBatch([
208 Grpc\OP_SEND_INITIAL_METADATA => [],
209 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
210 Grpc\OP_SEND_MESSAGE => 'invalid',
211 ]);
212 }
213
214 public function testInvalidClientMessageString()
215 {
216 $this->expectException(\InvalidArgumentException::class);
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';
221
222 $call = new Grpc\Call($this->channel,
223 'phony_method',
224 $deadline);
225
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],
230 ]);
231 }
232
233 public function testInvalidClientMessageFlags()
234 {
235 $this->expectException(\InvalidArgumentException::class);
236 $deadline = Grpc\Timeval::infFuture();
237 $req_text = 'client_server_full_request_response';
238 $reply_text = 'reply:client_server_full_request_response';
239 $status_text = 'status:client_server_full_response_text';
240
241 $call = new Grpc\Call($this->channel,
242 'phony_method',
243 $deadline);
244
245 $event = $call->startBatch([
246 Grpc\OP_SEND_INITIAL_METADATA => [],
247 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
248 Grpc\OP_SEND_MESSAGE => ['message' => 'abc',
249 'flags' => 'invalid',
250 ],
251 ]);
252 }
253
254 public function testInvalidServerStatusMetadata()
255 {
256 $this->expectException(\InvalidArgumentException::class);
257 $deadline = Grpc\Timeval::infFuture();
258 $req_text = 'client_server_full_request_response';
259 $reply_text = 'reply:client_server_full_request_response';
260 $status_text = 'status:client_server_full_response_text';
261
262 $call = new Grpc\Call($this->channel,
263 'phony_method',
264 $deadline);
265
266 $event = $call->startBatch([
267 Grpc\OP_SEND_INITIAL_METADATA => [],
268 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
269 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
270 ]);
271
272 $this->assertTrue($event->send_metadata);
273 $this->assertTrue($event->send_close);
274 $this->assertTrue($event->send_message);
275
276 $event = $this->server->requestCall();
277 $this->assertSame('phony_method', $event->method);
278 $server_call = $event->call;
279
280 $event = $server_call->startBatch([
281 Grpc\OP_SEND_INITIAL_METADATA => [],
282 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
283 Grpc\OP_SEND_STATUS_FROM_SERVER => [
284 'metadata' => 'invalid',
285 'code' => Grpc\STATUS_OK,
286 'details' => $status_text,
287 ],
288 Grpc\OP_RECV_MESSAGE => true,
289 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
290 ]);
291 }
292
293 public function testInvalidServerStatusCode()
294 {
295 $this->expectException(\InvalidArgumentException::class);
296 $deadline = Grpc\Timeval::infFuture();
297 $req_text = 'client_server_full_request_response';
298 $reply_text = 'reply:client_server_full_request_response';
299 $status_text = 'status:client_server_full_response_text';
300
301 $call = new Grpc\Call($this->channel,
302 'phony_method',
303 $deadline);
304
305 $event = $call->startBatch([
306 Grpc\OP_SEND_INITIAL_METADATA => [],
307 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
308 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
309 ]);
310
311 $this->assertTrue($event->send_metadata);
312 $this->assertTrue($event->send_close);
313 $this->assertTrue($event->send_message);
314
315 $event = $this->server->requestCall();
316 $this->assertSame('phony_method', $event->method);
317 $server_call = $event->call;
318
319 $event = $server_call->startBatch([
320 Grpc\OP_SEND_INITIAL_METADATA => [],
321 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
322 Grpc\OP_SEND_STATUS_FROM_SERVER => [
323 'metadata' => [],
324 'code' => 'invalid',
325 'details' => $status_text,
326 ],
327 Grpc\OP_RECV_MESSAGE => true,
328 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
329 ]);
330 }
331
332 public function testMissingServerStatusCode()
333 {
334 $this->expectException(\InvalidArgumentException::class);
335 $deadline = Grpc\Timeval::infFuture();
336 $req_text = 'client_server_full_request_response';
337 $reply_text = 'reply:client_server_full_request_response';
338 $status_text = 'status:client_server_full_response_text';
339
340 $call = new Grpc\Call($this->channel,
341 'phony_method',
342 $deadline);
343
344 $event = $call->startBatch([
345 Grpc\OP_SEND_INITIAL_METADATA => [],
346 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
347 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
348 ]);
349
350 $this->assertTrue($event->send_metadata);
351 $this->assertTrue($event->send_close);
352 $this->assertTrue($event->send_message);
353
354 $event = $this->server->requestCall();
355 $this->assertSame('phony_method', $event->method);
356 $server_call = $event->call;
357
358 $event = $server_call->startBatch([
359 Grpc\OP_SEND_INITIAL_METADATA => [],
360 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
361 Grpc\OP_SEND_STATUS_FROM_SERVER => [
362 'metadata' => [],
363 'details' => $status_text,
364 ],
365 Grpc\OP_RECV_MESSAGE => true,
366 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
367 ]);
368 }
369
370 public function testInvalidServerStatusDetails()
371 {
372 $this->expectException(\InvalidArgumentException::class);
373 $deadline = Grpc\Timeval::infFuture();
374 $req_text = 'client_server_full_request_response';
375 $reply_text = 'reply:client_server_full_request_response';
376 $status_text = 'status:client_server_full_response_text';
377
378 $call = new Grpc\Call($this->channel,
379 'phony_method',
380 $deadline);
381
382 $event = $call->startBatch([
383 Grpc\OP_SEND_INITIAL_METADATA => [],
384 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
385 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
386 ]);
387
388 $this->assertTrue($event->send_metadata);
389 $this->assertTrue($event->send_close);
390 $this->assertTrue($event->send_message);
391
392 $event = $this->server->requestCall();
393 $this->assertSame('phony_method', $event->method);
394 $server_call = $event->call;
395
396 $event = $server_call->startBatch([
397 Grpc\OP_SEND_INITIAL_METADATA => [],
398 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
399 Grpc\OP_SEND_STATUS_FROM_SERVER => [
400 'metadata' => [],
401 'code' => Grpc\STATUS_OK,
402 'details' => 0,
403 ],
404 Grpc\OP_RECV_MESSAGE => true,
405 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
406 ]);
407 }
408
409 public function testMissingServerStatusDetails()
410 {
411 $this->expectException(\InvalidArgumentException::class);
412 $deadline = Grpc\Timeval::infFuture();
413 $req_text = 'client_server_full_request_response';
414 $reply_text = 'reply:client_server_full_request_response';
415 $status_text = 'status:client_server_full_response_text';
416
417 $call = new Grpc\Call($this->channel,
418 'phony_method',
419 $deadline);
420
421 $event = $call->startBatch([
422 Grpc\OP_SEND_INITIAL_METADATA => [],
423 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
424 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
425 ]);
426
427 $this->assertTrue($event->send_metadata);
428 $this->assertTrue($event->send_close);
429 $this->assertTrue($event->send_message);
430
431 $event = $this->server->requestCall();
432 $this->assertSame('phony_method', $event->method);
433 $server_call = $event->call;
434
435 $event = $server_call->startBatch([
436 Grpc\OP_SEND_INITIAL_METADATA => [],
437 Grpc\OP_SEND_MESSAGE => ['message' => $reply_text],
438 Grpc\OP_SEND_STATUS_FROM_SERVER => [
439 'metadata' => [],
440 'code' => Grpc\STATUS_OK,
441 ],
442 Grpc\OP_RECV_MESSAGE => true,
443 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
444 ]);
445 }
446
447 public function testInvalidStartBatchKey()
448 {
449 $this->expectException(\InvalidArgumentException::class);
450 $deadline = Grpc\Timeval::infFuture();
451 $req_text = 'client_server_full_request_response';
452 $reply_text = 'reply:client_server_full_request_response';
453 $status_text = 'status:client_server_full_response_text';
454
455 $call = new Grpc\Call($this->channel,
456 'phony_method',
457 $deadline);
458
459 $event = $call->startBatch([
460 9999999 => [],
461 ]);
462 }
463
464 public function testInvalidStartBatch()
465 {
466 $this->expectException(\LogicException::class);
467 $deadline = Grpc\Timeval::infFuture();
468 $req_text = 'client_server_full_request_response';
469 $reply_text = 'reply:client_server_full_request_response';
470 $status_text = 'status:client_server_full_response_text';
471
472 $call = new Grpc\Call($this->channel,
473 'phony_method',
474 $deadline);
475
476 $event = $call->startBatch([
477 Grpc\OP_SEND_INITIAL_METADATA => [],
478 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true,
479 Grpc\OP_SEND_MESSAGE => ['message' => $req_text],
480 Grpc\OP_SEND_STATUS_FROM_SERVER => [
481 'metadata' => [],
482 'code' => Grpc\STATUS_OK,
483 'details' => 'abc',
484 ],
485 ]);
486 }
487
488 public function testGetTarget()
489 {
490 $this->assertTrue(is_string($this->channel->getTarget()));
491 }
492
493 public function testGetConnectivityState()
494 {
495 $this->assertTrue($this->channel->getConnectivityState() ==
496 Grpc\CHANNEL_IDLE);
497 }
498
499 public function testWatchConnectivityStateFailed()
500 {
501 $idle_state = $this->channel->getConnectivityState();
502 $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE);
503
504 $now = Grpc\Timeval::now();
505 $delta = new Grpc\Timeval(50000); // should timeout
506 $deadline = $now->add($delta);
507
508 $this->assertFalse($this->channel->watchConnectivityState(
509 $idle_state, $deadline));
510 }
511
512 public function testWatchConnectivityStateSuccess()
513 {
514 $idle_state = $this->channel->getConnectivityState(true);
515 $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE);
516
517 $now = Grpc\Timeval::now();
518 $delta = new Grpc\Timeval(3000000); // should finish well before
519 $deadline = $now->add($delta);
520
521 $this->assertTrue($this->channel->watchConnectivityState(
522 $idle_state, $deadline));
523
524 $new_state = $this->channel->getConnectivityState();
525 $this->assertTrue($idle_state != $new_state);
526 }
527
528 public function testWatchConnectivityStateDoNothing()
529 {
530 $idle_state = $this->channel->getConnectivityState();
531 $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE);
532
533 $now = Grpc\Timeval::now();
534 $delta = new Grpc\Timeval(50000);
535 $deadline = $now->add($delta);
536
537 $this->assertFalse($this->channel->watchConnectivityState(
538 $idle_state, $deadline));
539
540 $new_state = $this->channel->getConnectivityState();
541 $this->assertTrue($new_state == Grpc\CHANNEL_IDLE);
542 }
543
544 public function testGetConnectivityStateInvalidParam()
545 {
546 $this->expectException(\InvalidArgumentException::class);
547 $this->assertTrue($this->channel->getConnectivityState(
548 new Grpc\Timeval()));
549 }
550
551 public function testWatchConnectivityStateInvalidParam()
552 {
553 $this->expectException(\InvalidArgumentException::class);
554 $this->assertTrue($this->channel->watchConnectivityState(
555 0, 1000));
556 }
557
558 public function testChannelConstructorInvalidParam()
559 {
560 $this->expectException(\InvalidArgumentException::class);
561 $this->channel = new Grpc\Channel('localhost:'.$this->port, null);
562 }
563
564 public function testClose()
565 {
566 $this->assertNull($this->channel->close());
567 }
568 }