Debian Codegraph @

src/php/tests/unit_tests/CallInvokerTest.php

raw
1 <?php
2 /*
3 *
4 * Copyright 2018 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 /**
20 * Interface exported by the server.
21 */
22 require_once(dirname(__FILE__).'/../../lib/Grpc/BaseStub.php');
23 require_once(dirname(__FILE__).'/../../lib/Grpc/AbstractCall.php');
24 require_once(dirname(__FILE__).'/../../lib/Grpc/UnaryCall.php');
25 require_once(dirname(__FILE__).'/../../lib/Grpc/ClientStreamingCall.php');
26 require_once(dirname(__FILE__).'/../../lib/Grpc/Interceptor.php');
27 require_once(dirname(__FILE__).'/../../lib/Grpc/CallInvoker.php');
28 require_once(dirname(__FILE__).'/../../lib/Grpc/DefaultCallInvoker.php');
29 require_once(dirname(__FILE__).'/../../lib/Grpc/Internal/InterceptorChannel.php');
30
31 class CallInvokerSimpleRequest
32 {
33 private $data;
34 public function __construct($data)
35 {
36 $this->data = $data;
37 }
38 public function setData($data)
39 {
40 $this->data = $data;
41 }
42 public function serializeToString()
43 {
44 return $this->data;
45 }
46 }
47
48 class CallInvokerClient extends Grpc\BaseStub
49 {
50
51 /**
52 * @param string $hostname hostname
53 * @param array $opts channel options
54 * @param Channel|InterceptorChannel $channel (optional) re-use channel object
55 */
56 public function __construct($hostname, $opts, $channel = null)
57 {
58 parent::__construct($hostname, $opts, $channel);
59 }
60
61 /**
62 * A simple RPC.
63 * @param SimpleRequest $argument input argument
64 * @param array $metadata metadata
65 * @param array $options call options
66 */
67 public function UnaryCall(
68 CallInvokerSimpleRequest $argument,
69 $metadata = [],
70 $options = []
71 ) {
72 return $this->_simpleRequest(
73 '/phony_method',
74 $argument,
75 [],
76 $metadata,
77 $options
78 );
79 }
80 }
81
82 class CallInvokerUpdateChannel implements \Grpc\CallInvoker
83 {
84 private $channel;
85
86 public function getChannel() {
87 return $this->channel;
88 }
89
90 public function createChannelFactory($hostname, $opts) {
91 $this->channel = new \Grpc\Channel('localhost:50050', $opts);
92 return $this->channel;
93 }
94
95 public function UnaryCall($channel, $method, $deserialize, $options) {
96 return new UnaryCall($channel, $method, $deserialize, $options);
97 }
98
99 public function ClientStreamingCall($channel, $method, $deserialize, $options) {
100 return new ClientStreamingCall($channel, $method, $deserialize, $options);
101 }
102
103 public function ServerStreamingCall($channel, $method, $deserialize, $options) {
104 return new ServerStreamingCall($channel, $method, $deserialize, $options);
105 }
106
107 public function BidiStreamingCall($channel, $method, $deserialize, $options) {
108 return new BidiStreamingCall($channel, $method, $deserialize, $options);
109 }
110 }
111
112
113 class CallInvokerChangeRequest implements \Grpc\CallInvoker
114 {
115 private $channel;
116
117 public function getChannel() {
118 return $this->channel;
119 }
120 public function createChannelFactory($hostname, $opts) {
121 $this->channel = new \Grpc\Channel($hostname, $opts);
122 return $this->channel;
123 }
124
125 public function UnaryCall($channel, $method, $deserialize, $options) {
126 return new CallInvokerChangeRequestCall($channel, $method, $deserialize, $options);
127 }
128
129 public function ClientStreamingCall($channel, $method, $deserialize, $options) {
130 return new ClientStreamingCall($channel, $method, $deserialize, $options);
131 }
132
133 public function ServerStreamingCall($channel, $method, $deserialize, $options) {
134 return new ServerStreamingCall($channel, $method, $deserialize, $options);
135 }
136
137 public function BidiStreamingCall($channel, $method, $deserialize, $options) {
138 return new BidiStreamingCall($channel, $method, $deserialize, $options);
139 }
140 }
141
142 class CallInvokerChangeRequestCall
143 {
144 private $call;
145
146 public function __construct($channel, $method, $deserialize, $options)
147 {
148 $this->call = new \Grpc\UnaryCall($channel, $method, $deserialize, $options);
149 }
150
151 public function start($argument, $metadata, $options) {
152 $argument->setData('intercepted_unary_request');
153 $this->call->start($argument, $metadata, $options);
154 }
155
156 public function wait()
157 {
158 return $this->call->wait();
159 }
160 }
161
162 class CallInvokerTest extends \PHPUnit\Framework\TestCase
163 {
164 private $server;
165 private $port;
166
167 public function setUp(): void
168 {
169 $this->server = new Grpc\Server([]);
170 $this->port = $this->server->addHttp2Port('0.0.0.0:0');
171 $this->server->start();
172 }
173
174 public function tearDown(): void
175 {
176 unset($this->server);
177 }
178
179 public function testCreateDefaultCallInvoker()
180 {
181 $call_invoker = new \Grpc\DefaultCallInvoker();
182 $this->assertNotNull($call_invoker);
183 }
184
185 public function testCreateCallInvoker()
186 {
187 $call_invoker = new CallInvokerUpdateChannel();
188 $this->assertNotNull($call_invoker);
189 }
190
191 public function testCallInvokerAccessChannel()
192 {
193 $call_invoker = new CallInvokerUpdateChannel();
194 $stub = new \Grpc\BaseStub('localhost:50051',
195 ['credentials' => \Grpc\ChannelCredentials::createInsecure(),
196 'grpc_call_invoker' => $call_invoker]);
197 $this->assertEquals($call_invoker->getChannel()->getTarget(), 'localhost:50050');
198 $call_invoker->getChannel()->close();
199 }
200
201 public function testClientChangeRequestCallInvoker()
202 {
203 $req_text = 'client_request';
204 $call_invoker = new CallInvokerChangeRequest();
205 $client = new CallInvokerClient('localhost:'.$this->port, [
206 'force_new' => true,
207 'credentials' => Grpc\ChannelCredentials::createInsecure(),
208 'grpc_call_invoker' => $call_invoker,
209 ]);
210
211 $req = new CallInvokerSimpleRequest($req_text);
212 $unary_call = $client->UnaryCall($req);
213
214 $event = $this->server->requestCall();
215 $this->assertSame('/phony_method', $event->method);
216 $server_call = $event->call;
217 $event = $server_call->startBatch([
218 Grpc\OP_SEND_INITIAL_METADATA => [],
219 Grpc\OP_SEND_STATUS_FROM_SERVER => [
220 'metadata' => [],
221 'code' => Grpc\STATUS_OK,
222 'details' => '',
223 ],
224 Grpc\OP_RECV_MESSAGE => true,
225 Grpc\OP_RECV_CLOSE_ON_SERVER => true,
226 ]);
227 $this->assertSame('intercepted_unary_request', $event->message);
228 $call_invoker->getChannel()->close();
229 unset($unary_call);
230 unset($server_call);
231 }
232 }