| 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\Server; |
| 23 |
|
| 24 |
use phpmock\phpunit\PHPMock; |
| 25 |
use PHPUnit\Framework\TestCase; |
| 26 |
use Test\Thrift\Unit\Lib\ReflectionHelper; |
| 27 |
use Thrift\Exception\TTransportException; |
| 28 |
use Thrift\Server\TServerSocket; |
| 29 |
use Thrift\Transport\TSocket; |
| 30 |
|
| 31 |
class TServerSocketTest extends TestCase |
| 32 |
{ |
| 33 |
use PHPMock; |
| 34 |
use ReflectionHelper; |
| 35 |
|
| 36 |
public function testSetAcceptTimeout(): void |
| 37 |
{ |
| 38 |
$socket = new TServerSocket(); |
| 39 |
$socket->setAcceptTimeout(1000); |
| 40 |
|
| 41 |
$this->assertEquals(1000, $this->getPropertyValue($socket, 'acceptTimeout_')); |
| 42 |
} |
| 43 |
|
| 44 |
public function testListenAndClose(): void |
| 45 |
{ |
| 46 |
$socket = new TServerSocket('somehost', 999); |
| 47 |
|
| 48 |
$listener = tmpfile(); |
| 49 |
$this->getFunctionMock('Thrift\Server', 'stream_socket_server') |
| 50 |
->expects($this->once()) |
| 51 |
->with('tcp://somehost:999') |
| 52 |
->willReturn($listener); |
| 53 |
|
| 54 |
$socket->listen(); |
| 55 |
|
| 56 |
$this->assertIsResource($this->getPropertyValue($socket, 'listener_')); |
| 57 |
|
| 58 |
$this->getFunctionMock('Thrift\Server', 'fclose') |
| 59 |
->expects($this->once()) |
| 60 |
->with($this->equalTo($listener)) |
| 61 |
->willReturn(true); |
| 62 |
|
| 63 |
$socket->close(); |
| 64 |
$this->assertNull($this->getPropertyValue($socket, 'listener_')); |
| 65 |
} |
| 66 |
|
| 67 |
public function testAccept() |
| 68 |
{ |
| 69 |
$socket = new TServerSocket('somehost', 999); |
| 70 |
$socket->setAcceptTimeout(1000); |
| 71 |
|
| 72 |
$listener = tmpfile(); |
| 73 |
|
| 74 |
$this->getFunctionMock('Thrift\Server', 'stream_socket_server') |
| 75 |
->expects($this->once()) |
| 76 |
->with('tcp://somehost:999') |
| 77 |
->willReturn($listener); |
| 78 |
|
| 79 |
$transportHandle = tmpfile(); |
| 80 |
$this->getFunctionMock('Thrift\Server', 'stream_socket_accept') |
| 81 |
->expects($this->once()) |
| 82 |
->with( |
| 83 |
$this->equalTo($listener), |
| 84 |
1 |
| 85 |
)->willReturn($transportHandle); |
| 86 |
|
| 87 |
$socket->listen(); |
| 88 |
$result = $socket->accept(); |
| 89 |
$this->assertInstanceOf(TSocket::class, $result); |
| 90 |
|
| 91 |
$this->assertEquals($transportHandle, $this->getPropertyValue($result, 'handle_')); |
| 92 |
} |
| 93 |
|
| 94 |
public function testAcceptFailed() |
| 95 |
{ |
| 96 |
$socket = new TServerSocket('somehost', 999); |
| 97 |
$socket->setAcceptTimeout(1000); |
| 98 |
|
| 99 |
$listener = tmpfile(); |
| 100 |
|
| 101 |
$this->getFunctionMock('Thrift\Server', 'stream_socket_server') |
| 102 |
->expects($this->once()) |
| 103 |
->with('tcp://somehost:999') |
| 104 |
->willReturn($listener); |
| 105 |
|
| 106 |
$this->getFunctionMock('Thrift\Server', 'stream_socket_accept') |
| 107 |
->expects($this->once()) |
| 108 |
->with( |
| 109 |
$this->equalTo($listener), |
| 110 |
1 |
| 111 |
)->willReturn(null); |
| 112 |
|
| 113 |
$this->expectException(TTransportException::class); |
| 114 |
$this->expectExceptionMessage('accept() may not return NULL'); |
| 115 |
|
| 116 |
$socket->listen(); |
| 117 |
$socket->accept(); |
| 118 |
} |
| 119 |
} |