Skip to content

Commit 315396e

Browse files
authored
Added some test cases and optimized the code comments. (#5845)
1 parent 5950c14 commit 315396e

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests/Cases/TcpServerTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@
1414
use Hyperf\Config\Config;
1515
use Hyperf\Contract\ConfigInterface;
1616
use Hyperf\Contract\StdoutLoggerInterface;
17+
use Hyperf\Engine\Channel;
1718
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
1819
use Hyperf\Rpc\ProtocolManager;
1920
use Hyperf\RpcMultiplex\TcpServer;
2021
use Hyperf\RpcServer\RequestDispatcher;
22+
use Hyperf\Server\Connection as HyperfConnection;
2123
use Hyperf\Server\Event;
2224
use Hyperf\Server\Exception\InvalidArgumentException;
2325
use Hyperf\Server\Server;
2426
use Hyperf\Support\Reflection\ClassInvoker;
2527
use HyperfTest\RpcMultiplex\Stub\ContainerStub;
2628
use Mockery;
29+
use Multiplex\Contract\PackerInterface;
30+
use Multiplex\Packet;
2731
use PHPUnit\Framework\Attributes\CoversNothing;
2832

2933
/**
@@ -71,6 +75,56 @@ public function testInitServerConfig()
7175
$this->assertSame($settings, $invoker->initServerConfig('rpc')['settings']);
7276
}
7377

78+
public function testHeartbeat()
79+
{
80+
$container = ContainerStub::mockContainer();
81+
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([
82+
'server' => [
83+
'servers' => [
84+
[
85+
'name' => 'rpc',
86+
'type' => Server::SERVER_BASE,
87+
'host' => '0.0.0.0',
88+
'port' => 9502,
89+
'sock_type' => SWOOLE_SOCK_TCP,
90+
'callbacks' => [
91+
Event::ON_RECEIVE => [TcpServer::class, 'onReceive'],
92+
],
93+
'settings' => $settings = [
94+
'open_length_check' => true,
95+
'package_max_length' => 1024 * 1024 * 2,
96+
'package_length_type' => 'N',
97+
'package_length_offset' => 0,
98+
'package_body_offset' => 4,
99+
],
100+
],
101+
],
102+
],
103+
]));
104+
105+
/** @var PackerInterface $packer */
106+
$packer = $container->get(PackerInterface::class);
107+
$server = new TcpServer(
108+
$container,
109+
Mockery::mock(RequestDispatcher::class),
110+
Mockery::mock(ExceptionHandlerDispatcher::class),
111+
Mockery::mock(ProtocolManager::class),
112+
Mockery::mock(StdoutLoggerInterface::class),
113+
);
114+
115+
$connection = Mockery::mock(HyperfConnection::class);
116+
$chan = new Channel(1);
117+
$connection->shouldReceive('send')->once()->withAnyArgs()->andReturnUsing(function ($args) use ($chan) {
118+
$chan->push($args);
119+
});
120+
$server->onReceive($connection, 1, 1, $packer->pack(new Packet(0, Packet::PING)));
121+
122+
$packet = $packer->unpack($chan->pop());
123+
$this->assertSame(0, $packet->getId());
124+
$this->assertSame(Packet::PONG, $packet->getBody());
125+
$this->assertTrue($packet->isHeartbeat());
126+
}
127+
74128
public function testInitServerConfigFailed()
75129
{
76130
$container = ContainerStub::mockContainer();

0 commit comments

Comments
 (0)