Debian Codegraph @

lib/php/test/Unit/Lib/Transport/TSocketPoolTest.php

raw
1 <?php
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 namespace Test\Thrift\Unit\Lib\Transport;
23
24 use phpmock\phpunit\PHPMock;
25 use PHPUnit\Framework\TestCase;
26 use Test\Thrift\Unit\Lib\ReflectionHelper;
27 use Thrift\Exception\TException;
28 use Thrift\Transport\TSocketPool;
29
30 class TSocketPoolTest extends TestCase
31 {
32 use PHPMock;
33 use ReflectionHelper;
34
35 protected function setUp(): void
36 {
37 #need to be defined before the TSocketPool class definition
38 self::defineFunctionMock('Thrift\Transport', 'function_exists');
39 }
40
41 /**
42 * @dataProvider constructDataProvider
43 */
44 public function testConstruct(
45 $hosts,
46 $ports,
47 $persist,
48 $debugHandler,
49 $expectedServers
50 ) {
51 $socketPool = new TSocketPool($hosts, $ports, $persist, $debugHandler);
52
53 $this->assertEquals($expectedServers, $this->getPropertyValue($socketPool, 'servers_'));
54 }
55
56
57 public function constructDataProvider()
58 {
59 yield 'one server' => [
60 ['localhost'],
61 [9090],
62 false,
63 null,
64 [
65 ['host' => 'localhost', 'port' => 9090],
66 ],
67 ];
68 yield 'two servers' => [
69 ['localhost1', 'localhost2'],
70 [9090, 9091],
71 false,
72 null,
73 [
74 ['host' => 'localhost1', 'port' => 9090],
75 ['host' => 'localhost2', 'port' => 9091],
76 ],
77 ];
78 yield 'one server with one port' => [
79 ['localhost'],
80 9090,
81 false,
82 null,
83 [
84 ['host' => 'localhost', 'port' => 9090],
85 ],
86 ];
87 yield 'two servers with one port' => [
88 ['localhost1', 'localhost2'],
89 9090,
90 false,
91 null,
92 [
93 ['host' => 'localhost1', 'port' => 9090],
94 ['host' => 'localhost2', 'port' => 9090],
95 ],
96 ];
97 }
98
99 public function testAddServer(): void
100 {
101 $socketPool = new TSocketPool([], []);
102 $socketPool->addServer('localhost', 9090);
103
104 $this->assertEquals([['host' => 'localhost', 'port' => 9090]], $this->getPropertyValue($socketPool, 'servers_'));
105 }
106
107 public function testSetNumRetries(): void
108 {
109 $socketPool = new TSocketPool([], []);
110 $socketPool->setNumRetries(5);
111
112 $this->assertEquals(5, $this->getPropertyValue($socketPool, 'numRetries_'));
113 }
114
115 public function testrSetRetryInterval(): void
116 {
117 $socketPool = new TSocketPool([], []);
118 $socketPool->setRetryInterval(5);
119
120 $this->assertEquals(5, $this->getPropertyValue($socketPool, 'retryInterval_'));
121 }
122
123 public function testrSetMaxConsecutiveFailures(): void
124 {
125 $socketPool = new TSocketPool([], []);
126 $socketPool->setMaxConsecutiveFailures(5);
127
128 $this->assertEquals(5, $this->getPropertyValue($socketPool, 'maxConsecutiveFailures_'));
129 }
130
131 public function testrSetRandomize(): void
132 {
133 $socketPool = new TSocketPool([], []);
134 $socketPool->setRandomize(false);
135
136 $this->assertEquals(false, $this->getPropertyValue($socketPool, 'randomize_'));
137 }
138
139 public function testrSetAlwaysTryLast(): void
140 {
141 $socketPool = new TSocketPool([], []);
142 $socketPool->setAlwaysTryLast(false);
143
144 $this->assertEquals(false, $this->getPropertyValue($socketPool, 'alwaysTryLast_'));
145 }
146
147 /**
148 * @dataProvider openDataProvider
149 */
150 public function testOpen(
151 $hosts,
152 $ports,
153 $persist,
154 $debugHandler,
155 $randomize,
156 $retryInterval,
157 $numRetries,
158 $maxConsecutiveFailures,
159 $debug,
160 $servers,
161 $functionExistCallParams,
162 $functionExistResult,
163 $apcuFetchCallParams,
164 $apcuFetchResult,
165 $timeResult,
166 $debugHandlerCall,
167 $apcuStoreCallParams,
168 $fsockopenCallParams,
169 $fsockopenResult,
170 $expectedException,
171 $expectedExceptionMessage
172 ) {
173 $this->getFunctionMock('Thrift\Transport', 'function_exists')
174 ->expects($this->exactly(count($functionExistCallParams)))
175 ->withConsecutive(...$functionExistCallParams)
176 ->willReturnOnConsecutiveCalls(...$functionExistResult);
177
178 $this->getFunctionMock('Thrift\Transport', 'shuffle')
179 ->expects($randomize ? $this->once() : $this->never())
180 ->with($servers)
181 ->willReturnCallback(function (array &$servers) {
182 $servers = array_reverse($servers);
183
184 return true;
185 });
186
187 $this->getFunctionMock('Thrift\Transport', 'apcu_fetch')
188 ->expects($this->exactly(count($apcuFetchCallParams)))
189 ->withConsecutive(...$apcuFetchCallParams)
190 ->willReturnOnConsecutiveCalls(...$apcuFetchResult);
191
192 $this->getFunctionMock('Thrift\Transport', 'call_user_func')
193 ->expects($this->exactly(count($debugHandlerCall)))
194 ->withConsecutive(...$debugHandlerCall)
195 ->willReturn(true);
196
197 $this->getFunctionMock('Thrift\Transport', 'apcu_store')
198 ->expects($this->exactly(count($apcuStoreCallParams)))
199 ->withConsecutive(...$apcuStoreCallParams)
200 ->willReturn(true);
201
202 $this->getFunctionMock('Thrift\Transport', 'time')
203 ->expects($this->exactly(count($timeResult)))
204 ->willReturnOnConsecutiveCalls(...$timeResult);
205
206 #due to the running tests in separate process we could not open stream in data provider, so we need to do it here
207 foreach ($fsockopenResult as $num => $result) {
208 $fsockopenResult[$num] = $result ? fopen(...$result) : $result;
209 }
210
211 $this->getFunctionMock('Thrift\Transport', $persist ? 'pfsockopen' : 'fsockopen')
212 ->expects($this->exactly(count($fsockopenCallParams)))
213 ->withConsecutive(...$fsockopenCallParams)
214 ->willReturnOnConsecutiveCalls(...$fsockopenResult);
215
216 $this->getFunctionMock('Thrift\Transport', 'socket_import_stream')
217 ->expects(is_null($expectedException) ? $this->once() : $this->never())
218 ->with(
219 $this->callback(function ($stream) {
220 return is_resource($stream);
221 })
222 )
223 ->willReturn(true);
224
225 $this->getFunctionMock('Thrift\Transport', 'socket_set_option')
226 ->expects(is_null($expectedException) ? $this->once() : $this->never())
227 ->with(
228 $this->anything(), #$socket,
229 SOL_TCP, #$level
230 TCP_NODELAY, #$option
231 1 #$value
232 )
233 ->willReturn(true);
234
235 if ($expectedException) {
236 $this->expectException($expectedException);
237 $this->expectExceptionMessage($expectedExceptionMessage);
238 }
239
240 $socketPool = new TSocketPool($hosts, $ports, $persist, $debugHandler);
241 $socketPool->setRandomize($randomize);
242 $socketPool->setRetryInterval($retryInterval);
243 $socketPool->setNumRetries($numRetries);
244 $socketPool->setMaxConsecutiveFailures($maxConsecutiveFailures);
245 $socketPool->setDebug($debug);
246
247 $this->assertNull($socketPool->open());
248 }
249
250 public function openDataProvider()
251 {
252 $default = [
253 'hosts' => ['localhost'],
254 'ports' => [9090],
255 'persist' => false,
256 'debugHandler' => null,
257 'randomize' => true,
258 'retryInterval' => 5,
259 'numRetries' => 1,
260 'maxConsecutiveFailures' => 1,
261 'debug' => false,
262 'servers' => [
263 ['host' => 'localhost', 'port' => 9090],
264 ],
265 'functionExistCallParams' => [
266 ['apcu_fetch'],
267 ['socket_import_stream'],
268 ['socket_set_option'],
269 ],
270 'functionExistResult' => [
271 true,
272 true,
273 true,
274 ],
275 'apcuFetchCallParams' => [
276 ['thrift_failtime:localhost:9090~', $this->anything()],
277 ],
278 'apcuFetchResult' => [
279 false,
280 ],
281 'timeResult' => [],
282 'debugHandlerCall' => [],
283 'apcuStoreCallParams' => [],
284 'fsockopenCallParams' => [
285 [
286 'localhost',
287 9090,
288 $this->anything(), #$errno,
289 $this->anything(), #$errstr,
290 $this->anything(), #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
291 ],
292 ],
293 'fsockopenResult' => [
294 ['php://temp', 'r'],
295 ],
296 'expectedException' => null,
297 'expectedExceptionMessage' => null,
298 ];
299
300 yield 'one server ready' => $default;
301 yield 'one server failed' => array_merge(
302 $default,
303 [
304 'functionExistCallParams' => [
305 ['apcu_fetch'],
306 ],
307 'fsockopenResult' => [
308 false,
309 ],
310 'apcuFetchCallParams' => [
311 ['thrift_failtime:localhost:9090~', $this->anything()],
312 ['thrift_consecfails:localhost:9090~', $this->anything()],
313 ],
314 'apcuStoreCallParams' => [
315 ['thrift_failtime:localhost:9090~', $this->anything()],
316 ['thrift_consecfails:localhost:9090~', $this->anything(), 0],
317 ],
318 'timeResult' => [
319 1,
320 ],
321 'expectedException' => TException::class,
322 'expectedExceptionMessage' => 'TSocketPool: All hosts in pool are down. (localhost:9090)',
323 ]
324 );
325 yield 'connect to one server on second attempt' => array_merge(
326 $default,
327 [
328 'numRetries' => 2,
329 'fsockopenCallParams' => [
330 [
331 'localhost',
332 9090,
333 $this->anything(), #$errno,
334 $this->anything(), #$errstr,
335 $this->anything(), #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
336 ],
337 [
338 'localhost',
339 9090,
340 $this->anything(), #$errno,
341 $this->anything(), #$errstr,
342 $this->anything(), #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
343 ],
344 ],
345 'fsockopenResult' => [
346 false,
347 ['php://temp', 'r'],
348 ],
349 'apcuStoreCallParams' => [],
350 ]
351 );
352 yield 'last time fail time is not expired' => array_merge(
353 $default,
354 [
355 'retryInterval' => 5,
356 'apcuFetchResult' => [
357 99,
358 ],
359 'apcuStoreCallParams' => [
360 ['thrift_failtime:localhost:9090~', $this->anything()],
361 ],
362 'timeResult' => [
363 100,
364 ],
365 ]
366 );
367 yield 'last time fail time is expired, store info to debug' => array_merge(
368 $default,
369 [
370 'retryInterval' => 5,
371 'apcuFetchResult' => [
372 90,
373 ],
374 'apcuStoreCallParams' => [
375 ['thrift_failtime:localhost:9090~', $this->anything()],
376 ],
377 'timeResult' => [
378 100,
379 ],
380 'debug' => true,
381 'debugHandlerCall' => [
382 ['error_log', 'TSocketPool: retryInterval (5) has passed for host localhost:9090'],
383 ],
384 ]
385 );
386 yield 'not accessible server, store info to debug' => array_merge(
387 $default,
388 [
389 'retryInterval' => 5,
390 'functionExistCallParams' => [
391 ['apcu_fetch'],
392 ],
393 'functionExistResult' => [
394 true,
395 ],
396 'apcuFetchCallParams' => [
397 ['thrift_failtime:localhost:9090~', $this->anything()],
398 ['thrift_consecfails:localhost:9090~', $this->anything()],
399 ],
400 'apcuFetchResult' => [
401 90,
402 ],
403 'apcuStoreCallParams' => [
404 ['thrift_failtime:localhost:9090~', $this->anything()],
405 ['thrift_consecfails:localhost:9090~', 0],
406 ],
407 'timeResult' => [
408 100,
409 101,
410 ],
411 'fsockopenResult' => [
412 false,
413 ],
414 'debug' => true,
415 'debugHandlerCall' => [
416 ['error_log', 'TSocketPool: retryInterval (5) has passed for host localhost:9090'],
417 ['error_log', 'TSocket: Could not connect to localhost:9090 ( [])'],
418 ['error_log', 'TSocketPool: marking localhost:9090 as down for 5 secs after 1 failed attempts.'],
419 ['error_log', 'TSocketPool: All hosts in pool are down. (localhost:9090)'],
420 ],
421 'expectedException' => TException::class,
422 'expectedExceptionMessage' => 'TSocketPool: All hosts in pool are down. (localhost:9090)',
423 ]
424 );
425 yield 'max consecutive failures' => array_merge(
426 $default,
427 [
428 'maxConsecutiveFailures' => 5,
429 'functionExistCallParams' => [
430 ['apcu_fetch'],
431 ],
432 'functionExistResult' => [
433 true,
434 ],
435 'apcuFetchCallParams' => [
436 ['thrift_failtime:localhost:9090~', $this->anything()],
437 ['thrift_consecfails:localhost:9090~', $this->anything()],
438 ],
439 'apcuStoreCallParams' => [
440 ['thrift_consecfails:localhost:9090~', 1],
441 ],
442 'timeResult' => [],
443 'fsockopenResult' => [
444 false,
445 ],
446 'expectedException' => TException::class,
447 'expectedExceptionMessage' => 'TSocketPool: All hosts in pool are down. (localhost:9090)',
448 ]
449 );
450 yield 'apcu disabled' => array_merge(
451 $default,
452 [
453 'functionExistCallParams' => [
454 ['apcu_fetch'],
455 ],
456 'functionExistResult' => [
457 false,
458 ],
459 'fsockopenResult' => [
460 false,
461 ],
462 'timeResult' => [
463 1,
464 ],
465 'apcuFetchCallParams' => [],
466 'apcuStoreCallParams' => [],
467 'expectedException' => TException::class,
468 'expectedExceptionMessage' => 'TSocketPool: All hosts in pool are down. (localhost:9090)',
469 ]
470 );
471 yield 'second host accessible' => array_merge(
472 $default,
473 [
474 'hosts' => ['host1', 'host2'],
475 'ports' => [9090, 9091],
476 'servers' => [
477 ['host' => 'host1', 'port' => 9090],
478 ['host' => 'host2', 'port' => 9091],
479 ],
480 'fsockopenCallParams' => [
481 [
482 'host2',
483 9091,
484 $this->anything(), #$errno,
485 $this->anything(), #$errstr,
486 $this->anything(), #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
487 ],
488 [
489 'host1',
490 9090,
491 $this->anything(), #$errno,
492 $this->anything(), #$errstr,
493 $this->anything(), #$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000),
494 ],
495 ],
496 'fsockopenResult' => [
497 false,
498 ['php://temp', 'r'],
499 ],
500 'apcuFetchCallParams' => [
501 ['thrift_failtime:host2:9091~', $this->anything()],
502 ['thrift_consecfails:host2:9091~', $this->anything()],
503 ['thrift_failtime:host1:9090~', $this->anything()],
504 ],
505 'apcuStoreCallParams' => [
506 ['thrift_failtime:host2:9091~', $this->anything()],
507 ['thrift_consecfails:host2:9091~', $this->anything(), 0],
508 ],
509 'timeResult' => [
510 1,
511 ],
512 ]
513 );
514 }
515 }