From eaf450757a3bae218e3c6c4397d6b2fedc5376c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Dronga?= Date: Thu, 18 Feb 2021 22:41:33 +0200 Subject: [PATCH] since PHP8.0 stream_select() throws exception instead of warning --- PhpAmqpLib/Wire/IO/AbstractIO.php | 2 ++ PhpAmqpLib/Wire/IO/SocketIO.php | 5 +++++ PhpAmqpLib/Wire/IO/StreamIO.php | 5 +++++ tests/Unit/Wire/IO/SocketIOTest.php | 21 +++++++++++++++++++++ tests/Unit/Wire/IO/StreamIOTest.php | 3 ++- 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/PhpAmqpLib/Wire/IO/AbstractIO.php b/PhpAmqpLib/Wire/IO/AbstractIO.php index d074c6e0a..85d2be471 100644 --- a/PhpAmqpLib/Wire/IO/AbstractIO.php +++ b/PhpAmqpLib/Wire/IO/AbstractIO.php @@ -2,6 +2,7 @@ namespace PhpAmqpLib\Wire\IO; +use PhpAmqpLib\Exception\AMQPConnectionClosedException; use PhpAmqpLib\Exception\AMQPHeartbeatMissedException; use PhpAmqpLib\Exception\AMQPIOWaitException; use PhpAmqpLib\Wire\AMQPWriter; @@ -105,6 +106,7 @@ public function select($sec, $usec) * @param int|null $sec * @param int|null $usec * @return int|bool + * @throws AMQPConnectionClosedException */ abstract protected function do_select($sec, $usec); diff --git a/PhpAmqpLib/Wire/IO/SocketIO.php b/PhpAmqpLib/Wire/IO/SocketIO.php index 0f480803a..13bb10078 100644 --- a/PhpAmqpLib/Wire/IO/SocketIO.php +++ b/PhpAmqpLib/Wire/IO/SocketIO.php @@ -241,6 +241,11 @@ public function close() */ protected function do_select($sec, $usec) { + if ($this->sock === null || !is_resource($this->sock)) { + $this->sock = null; + throw new AMQPConnectionClosedException('Broken pipe or closed connection', 0); + } + $read = array($this->sock); $write = null; $except = null; diff --git a/PhpAmqpLib/Wire/IO/StreamIO.php b/PhpAmqpLib/Wire/IO/StreamIO.php index ca48182e3..b258e2029 100644 --- a/PhpAmqpLib/Wire/IO/StreamIO.php +++ b/PhpAmqpLib/Wire/IO/StreamIO.php @@ -335,6 +335,11 @@ public function getSocket() */ protected function do_select($sec, $usec) { + if ($this->sock === null || !is_resource($this->sock)) { + $this->sock = null; + throw new AMQPConnectionClosedException('Broken pipe or closed connection', 0); + } + $read = array($this->sock); $write = null; $except = null; diff --git a/tests/Unit/Wire/IO/SocketIOTest.php b/tests/Unit/Wire/IO/SocketIOTest.php index 808ef0355..92d9a1feb 100644 --- a/tests/Unit/Wire/IO/SocketIOTest.php +++ b/tests/Unit/Wire/IO/SocketIOTest.php @@ -2,6 +2,7 @@ namespace PhpAmqpLib\Tests\Unit\Wire\IO; +use PhpAmqpLib\Exception\AMQPConnectionClosedException; use PhpAmqpLib\Wire\IO\SocketIO; use PHPUnit\Framework\TestCase; @@ -75,4 +76,24 @@ public function write_when_closed(SocketIO $socketIO) { $socketIO->write('data'); } + + /** + * @test + * @group linux + * @requires OS Linux + */ + public function select_must_throw_io_exception() + { + $this->expectException(AMQPConnectionClosedException::class); + $property = new \ReflectionProperty(SocketIO::class, 'sock'); + $property->setAccessible(true); + + $resource = fopen('php://temp', 'r'); + fclose($resource); + + $socket = new SocketIO('0.0.0.0', PORT, 0.1, 0.1, null, false, 0); + $property->setValue($socket, $resource); + + $socket->select(0, 0); + } } diff --git a/tests/Unit/Wire/IO/StreamIOTest.php b/tests/Unit/Wire/IO/StreamIOTest.php index ca50bdd88..fbe1ecdc9 100644 --- a/tests/Unit/Wire/IO/StreamIOTest.php +++ b/tests/Unit/Wire/IO/StreamIOTest.php @@ -2,6 +2,7 @@ namespace PhpAmqpLib\Tests\Unit\Wire\IO; +use PhpAmqpLib\Exception\AMQPConnectionClosedException; use PhpAmqpLib\Wire\IO\StreamIO; use PHPUnit\Framework\TestCase; @@ -32,11 +33,11 @@ public function read_write_timeout_must_be_at_least_2x_the_heartbeat() /** * @test * @group linux - * @expectedException \PhpAmqpLib\Exception\AMQPIOWaitException * @requires OS Linux */ public function select_must_throw_io_exception() { + $this->expectException(AMQPConnectionClosedException::class); $property = new \ReflectionProperty(StreamIO::class, 'sock'); $property->setAccessible(true);