Skip to content

Commit 972f2b0

Browse files
committed
Merge branch 'master' into 2.2-merge
# Conflicts: # composer.json # phpunit.xml
2 parents d4abad9 + b9a8028 commit 972f2b0

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ protected function deferOnOpen(SwooleRequest $request, string $class, $server)
302302
};
303303

304304
if ($server instanceof SwooleResponse) {
305-
$onOpen();
305+
wait($onOpen);
306306
} else {
307307
defer($onOpen);
308308
}

tests/ServerTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\WebSocketServer;
13+
14+
use Hyperf\Contract\StdoutLoggerInterface;
15+
use Hyperf\Dispatcher\HttpDispatcher;
16+
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
17+
use Hyperf\HttpServer\ResponseEmitter;
18+
use Hyperf\Utils\ApplicationContext;
19+
use Hyperf\Utils\Coroutine;
20+
use Hyperf\Utils\Reflection\ClassInvoker;
21+
use Hyperf\Utils\Waiter;
22+
use Hyperf\WebSocketServer\Server;
23+
use HyperfTest\WebSocketServer\Stub\WebSocketStub;
24+
use Mockery;
25+
use PHPUnit\Framework\TestCase;
26+
use Psr\Container\ContainerInterface;
27+
use Swoole\Http\Request as SwooleRequest;
28+
use Swoole\Http\Response as SwooleResponse;
29+
30+
/**
31+
* @internal
32+
* @coversNothing
33+
*/
34+
class ServerTest extends TestCase
35+
{
36+
protected function tearDown(): void
37+
{
38+
Mockery::close();
39+
}
40+
41+
public function testDeferOnOpenInCoroutineStyleServer()
42+
{
43+
$container = Mockery::mock(ContainerInterface::class);
44+
ApplicationContext::setContainer($container);
45+
$container->shouldReceive('get')->with(WebSocketStub::class)->andReturn(new WebSocketStub());
46+
$container->shouldReceive('get')->with(Waiter::class)->andReturn(new Waiter());
47+
48+
$server = new Server(
49+
$container,
50+
Mockery::mock(HttpDispatcher::class),
51+
Mockery::mock(ExceptionHandlerDispatcher::class),
52+
Mockery::mock(ResponseEmitter::class),
53+
Mockery::mock(StdoutLoggerInterface::class),
54+
);
55+
56+
$server = new ClassInvoker($server);
57+
$server->deferOnOpen(new SwooleRequest(), WebSocketStub::class, new SwooleResponse());
58+
$this->assertNotEquals(Coroutine::id(), WebSocketStub::$coroutineId);
59+
$this->assertFalse(\Swoole\Coroutine::exists(WebSocketStub::$coroutineId));
60+
}
61+
}

tests/Stub/WebSocketStub.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\WebSocketServer\Stub;
13+
14+
use Hyperf\Contract\OnOpenInterface;
15+
use Hyperf\Utils\Coroutine;
16+
use Swoole\Http\Request;
17+
18+
class WebSocketStub implements OnOpenInterface
19+
{
20+
public static $coroutineId = 0;
21+
22+
public function onOpen($server, Request $request): void
23+
{
24+
static::$coroutineId = Coroutine::id();
25+
}
26+
}

0 commit comments

Comments
 (0)