From 4638e28b158ffd8fa170399c1935c6aa60868585 Mon Sep 17 00:00:00 2001 From: Savio Resende Date: Mon, 8 Apr 2024 22:15:06 -0500 Subject: [PATCH] Fixed abstract action pushing message before checking connection. --- composer.json | 1 - composer.lock | 217 +------------------- phpunit.xml | 4 +- src/Actions/Abstractions/AbstractAction.php | 4 +- src/Actions/Traits/HasAcknowledgment.php | 7 +- tests/Unit/MessageRouterTest.php | 12 +- 6 files changed, 17 insertions(+), 228 deletions(-) diff --git a/composer.json b/composer.json index 3d1f70c..af4a766 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,6 @@ "symfony/var-dumper": "^6.1", "phpunit/phpunit": "^9.2", "mockery/mockery": "^1.4", - "codedungeon/phpunit-result-printer": "^0.30.0", "openswoole/ide-helper": "^22.0", "phpro/grumphp": "^2.3", "phpstan/phpstan": "^1.10", diff --git a/composer.lock b/composer.lock index 7007851..f88ad26 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "be5fb900a53207c7f04ddf9b8487294e", + "content-hash": "d8c428eaaef585d33461dcd97566dcf3", "packages": [ { "name": "amphp/amp", @@ -4869,159 +4869,6 @@ } ], "packages-dev": [ - { - "name": "2bj/phanybar", - "version": "v1.0.0", - "source": { - "type": "git", - "url": "https://github.com/2bj/Phanybar.git", - "reference": "88ff671e18f30c2047a34f8cf2465a7ff93c819b" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/2bj/Phanybar/zipball/88ff671e18f30c2047a34f8cf2465a7ff93c819b", - "reference": "88ff671e18f30c2047a34f8cf2465a7ff93c819b", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "bin": [ - "bin/phanybar" - ], - "type": "library", - "autoload": { - "psr-4": { - "Bakyt\\": [ - "src/" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Bakyt Turgumbaev", - "email": "dev2bj@gmail.com" - } - ], - "description": "Control AnyBar from your php", - "keywords": [ - "anybar", - "phanybar" - ], - "support": { - "issues": "https://github.com/2bj/Phanybar/issues", - "source": "https://github.com/2bj/Phanybar/tree/master" - }, - "time": "2015-03-06T12:14:28+00:00" - }, - { - "name": "codedungeon/php-cli-colors", - "version": "1.12.2", - "source": { - "type": "git", - "url": "https://github.com/mikeerickson/php-cli-colors.git", - "reference": "e346156f75717140a3dd622124d2ec686aa7ff8e" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/mikeerickson/php-cli-colors/zipball/e346156f75717140a3dd622124d2ec686aa7ff8e", - "reference": "e346156f75717140a3dd622124d2ec686aa7ff8e", - "shasum": "" - }, - "require-dev": { - "phpunit/phpunit": ">=5.2" - }, - "type": "library", - "autoload": { - "psr-4": { - "Codedungeon\\PHPCliColors\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike Erickson", - "email": "codedungeon@gmail.com" - } - ], - "description": "Liven up you PHP Console Apps with standard colors", - "homepage": "https://github.com/mikeerickson/php-cli-colors", - "keywords": [ - "color", - "colors", - "composer", - "package", - "php" - ], - "support": { - "issues": "https://github.com/mikeerickson/php-cli-colors/issues", - "source": "https://github.com/mikeerickson/php-cli-colors/tree/1.12.2" - }, - "time": "2021-01-05T04:48:27+00:00" - }, - { - "name": "codedungeon/phpunit-result-printer", - "version": "0.30.1", - "source": { - "type": "git", - "url": "https://github.com/mikeerickson/phpunit-pretty-result-printer.git", - "reference": "a361009eeb7078c1478ba835976f7722a4952870" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/mikeerickson/phpunit-pretty-result-printer/zipball/a361009eeb7078c1478ba835976f7722a4952870", - "reference": "a361009eeb7078c1478ba835976f7722a4952870", - "shasum": "" - }, - "require": { - "2bj/phanybar": "^1.0", - "codedungeon/php-cli-colors": "^1.10.2", - "hassankhan/config": "^0.11.2|^1.0|^2.0", - "php": "^7.1 | ^8.0", - "symfony/yaml": "^2.7|^3.0|^4.0|^5.0" - }, - "require-dev": { - "spatie/phpunit-watcher": "^1.6" - }, - "type": "library", - "autoload": { - "psr-4": { - "Codedungeon\\PHPUnitPrettyResultPrinter\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike Erickson", - "email": "codedungeon@gmail.com" - } - ], - "description": "PHPUnit Pretty Result Printer", - "keywords": [ - "TDD", - "composer", - "package", - "phpunit", - "printer", - "result-printer", - "testing" - ], - "support": { - "issues": "https://github.com/mikeerickson/phpunit-pretty-result-printer/issues", - "source": "https://github.com/mikeerickson/phpunit-pretty-result-printer/tree/0.30.1" - }, - "time": "2020-12-24T20:19:03+00:00" - }, { "name": "composer/pcre", "version": "3.1.2", @@ -5453,68 +5300,6 @@ }, "time": "2020-07-09T08:09:16+00:00" }, - { - "name": "hassankhan/config", - "version": "2.2.0", - "source": { - "type": "git", - "url": "https://github.com/hassankhan/config.git", - "reference": "62b0fd17540136efa94ab6b39f04044c6dc5e4a7" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/hassankhan/config/zipball/62b0fd17540136efa94ab6b39f04044c6dc5e4a7", - "reference": "62b0fd17540136efa94ab6b39f04044c6dc5e4a7", - "shasum": "" - }, - "require": { - "php": ">=5.5.9" - }, - "require-dev": { - "phpunit/phpunit": "~4.8.36 || ~5.7 || ~6.5 || ~7.5", - "scrutinizer/ocular": "~1.1", - "squizlabs/php_codesniffer": "~2.2", - "symfony/yaml": "~3.4" - }, - "suggest": { - "symfony/yaml": "~3.4" - }, - "type": "library", - "autoload": { - "psr-4": { - "Noodlehaus\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Hassan Khan", - "homepage": "http://hassankhan.me/", - "role": "Developer" - } - ], - "description": "Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files", - "homepage": "http://hassankhan.me/config/", - "keywords": [ - "config", - "configuration", - "ini", - "json", - "microphp", - "unframework", - "xml", - "yaml", - "yml" - ], - "support": { - "issues": "https://github.com/hassankhan/config/issues", - "source": "https://github.com/hassankhan/config/tree/2.2.0" - }, - "time": "2020-12-07T16:04:15+00:00" - }, { "name": "kanata-php/conveyor-server-client", "version": "0.1.2", diff --git a/phpunit.xml b/phpunit.xml index 78c96b2..d745c2c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,5 @@ - + tests/Unit/MessageRouterTest.php diff --git a/src/Actions/Abstractions/AbstractAction.php b/src/Actions/Abstractions/AbstractAction.php index af359cf..56917c0 100644 --- a/src/Actions/Abstractions/AbstractAction.php +++ b/src/Actions/Abstractions/AbstractAction.php @@ -323,7 +323,9 @@ public function push(int $fd, string $data): void $this->server, ); - $this->server->push($fd, $data); + if ($this->server->isEstablished($fd)) { + $this->server->push($fd, $data); + } $this->checkAcknowledgment($fd, $data); } diff --git a/src/Actions/Traits/HasAcknowledgment.php b/src/Actions/Traits/HasAcknowledgment.php index 77e073a..070a9e2 100644 --- a/src/Actions/Traits/HasAcknowledgment.php +++ b/src/Actions/Traits/HasAcknowledgment.php @@ -4,6 +4,7 @@ use Conveyor\Actions\AcknowledgeAction; use Conveyor\Constants; +use Conveyor\Helpers\Arr; trait HasAcknowledgment { @@ -43,7 +44,7 @@ public function checkAcknowledgment(int $fd, string $data): void $parsedData = json_decode($data, true); if ( - $parsedData['action'] !== AcknowledgeAction::NAME + Arr::get($parsedData, 'action') !== AcknowledgeAction::NAME || !isset($parsedData['id']) ) { return; @@ -64,7 +65,9 @@ public function checkAcknowledgment(int $fd, string $data): void function () use ($fd, $data, $messageHash, &$timers) { if ($this->messageAcknowledmentPersistence->has($messageHash)) { $this->messageAcknowledmentPersistence->subtract($messageHash); - $this->server->push($fd, $data); + if ($this->server->isEstablished($fd)) { + $this->server->push($fd, $data); + } return; } diff --git a/tests/Unit/MessageRouterTest.php b/tests/Unit/MessageRouterTest.php index f216f1a..e3f28b8 100644 --- a/tests/Unit/MessageRouterTest.php +++ b/tests/Unit/MessageRouterTest.php @@ -47,6 +47,7 @@ public function testCanSendBaseAction() ]); $server = Mockery::mock(Server::class); + $server->shouldReceive('isEstablished')->andReturnTrue(); $server->shouldReceive('push') ->andReturnUsing(function ($fd, $data) use ($expectedResponse) { $this->assertEquals($expectedResponse, $data); @@ -252,6 +253,7 @@ public function testCantSendPlainText() $expectedTrue = false; $server = Mockery::mock(Server::class); + $server->shouldReceive('isEstablished')->andReturnTrue(); $server->shouldReceive('push') ->andReturnUsing(function ($fd, $data) use (&$expectedTrue, $expectedFd, $expectedMessage) { $this->assertEquals($expectedFd, $fd); @@ -287,7 +289,7 @@ public function testCanCallRefreshPersistence() Conveyor::refresh([$channelPersistence]); $server = Mockery::mock(Server::class); - $server->shouldReceive('isEstablished')->andReturn(true); + $server->shouldReceive('isEstablished')->andReturnTrue(); $server->shouldReceive('push'); Conveyor::init() @@ -325,6 +327,7 @@ function (string $data, int $fd, Server $server) { ]); $server = Mockery::mock(Server::class); + $server->shouldReceive('isEstablished')->andReturnTrue(); $server->shouldReceive('push') ->andReturnUsing(function ($fd, $data) use ($expectedResponse) { $this->assertEquals($expectedResponse, $data); @@ -365,12 +368,12 @@ public function testChannelAcknowledgmentAlone() ]); $server = Mockery::mock(Server::class); + $server->shouldReceive('isEstablished')->andReturnTrue(); $server->shouldReceive('push') ->andReturnUsing(function ($fd, $data) use ($expectedResponse) { $this->assertEquals($expectedResponse, $data); return true; }); - $server->shouldReceive('isEstablished')->andReturnTrue(); $clearVerification = false; @@ -401,18 +404,17 @@ public function testChannelPresenceAlone() ]); $server = Mockery::mock(Server::class); + $server->shouldReceive('isEstablished')->andReturnTrue(); $server->shouldReceive('push') ->andReturnUsing(function ($fd, $data) { // presence assertion (after acknowledgment) $parsedData = json_decode($data, true); $parsedMessage = json_decode($parsedData['data'], true); $this->assertEquals(ChannelConnectAction::NAME, $parsedData['action']); - $this->assertTrue(isset($parsedData['id'])); $this->assertEquals(Constants::ACTION_EVENT_CHANNEL_PRESENCE, $parsedMessage['event']); $this->assertCount(1, $parsedMessage['fds']); return true; }); - $server->shouldReceive('isEstablished')->andReturnTrue(); $clearVerification = false; @@ -448,6 +450,7 @@ public function testChannelAcknowledgmentAndPresence() $counter = 0; $server = Mockery::mock(Server::class); + $server->shouldReceive('isEstablished')->andReturnTrue(); $server->shouldReceive('push') ->andReturnUsing(function ($fd, $data) use ($expectedResponse, &$counter) { if ($counter === 0) { @@ -465,7 +468,6 @@ public function testChannelAcknowledgmentAndPresence() $counter++; return true; }); - $server->shouldReceive('isEstablished')->andReturnTrue(); $clearVerification = false;