Skip to content

Commit

Permalink
fix Utils\Worker\Server ConnectionCoroutineCount++ error when UdpConn…
Browse files Browse the repository at this point in the history
…ection
  • Loading branch information
chaz6chez committed Oct 21, 2024
1 parent d122611 commit 40ef425
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Utils/Worker/ServerMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ public function __init__serverMethods(): void
$waitGroup = new WaitGroup();
// 协程创建
$waitGroup->add();
static::$_connectionCoroutineCount[$connectionId]++;
// 计数 ++
self::$_connectionCoroutineCount[$connectionId] =
isset(self::$_connectionCoroutineCount[$connectionId])
? (self::$_connectionCoroutineCount[$connectionId] + 1)
: 1;
new Coroutine(function () use (&$res, $waitGroup, $params, $connectionId) {
try {
$res = call_user_func($this->getParentOnMessage(), ...$params);
Expand Down
32 changes: 32 additions & 0 deletions tests/UtilsCase/Worker/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,38 @@ public function testServerSetConnectionCoroutine()
call_user_func($worker->onClose, $connection);
}

public function testServerSetConnectionCoroutineUdpConnection()
{
Factory::init(Factory::WORKERMAN_DEFAULT);
$worker = new Server();
$worker->setConnectionCoroutine(true);
$worker->onMessage = $onMessage = function ($connection, $data) {
echo "testServerSetConnectionCoroutine->onMessage\n";
};

$this->assertNull($worker->getParentOnMessage());

// init
$reflection = new \ReflectionClass(AbstractWorker::class);
$init = $reflection->getMethod('initWorkers');
$init->setAccessible(true);
$init->invoke(null);
// onWorkerStart
$start = $reflection->getProperty('onWorkerStart');
call_user_func($start->getValue($worker), $worker);

$this->assertEquals($onMessage, $worker->getParentOnMessage());

$connection = Mockery::mock(ConnectionInterface::class);

$this->expectOutputString( "testServerSetConnectionCoroutine->onMessage\n");

$this->assertFalse(isset($worker::getConnectionCoroutineCount()[$id = spl_object_hash($connection)]));
call_user_func($worker->onMessage, $connection, 'aaa');
$this->assertEquals(0, $worker::getConnectionCoroutineCount($id));
$this->assertFalse(isset($worker::getConnectionCoroutineCount()[$id]));
}

public function testServerUnsetConnectionCoroutineByForce()
{
Factory::init(Factory::WORKERMAN_DEFAULT);
Expand Down

0 comments on commit 40ef425

Please sign in to comment.