From 1ccc50fe2403cbda56d5d190691d3e1dc9ffe39a Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 10 Aug 2021 16:50:21 +0200 Subject: [PATCH] Simplify usage by supporting new default loop --- README.md | 12 ++++++------ composer.json | 4 ++-- src/ConnectionManagerDelay.php | 17 ++++++++++++++--- src/ConnectionManagerTimeout.php | 17 ++++++++++++++--- tests/ConnectionManagerDelayTest.php | 19 ++++++++++++++++--- tests/ConnectionManagerTimeoutTest.php | 14 +++++++++++++- 6 files changed, 65 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a26030c..dd87316 100644 --- a/README.md +++ b/README.md @@ -92,20 +92,20 @@ $connectorRepeater->connect('www.google.com:80')->then(function ($stream) { ### Timeout -The `ConnectionManagerTimeout($connector, $timeout, $loop)` sets a maximum `$timeout` in seconds on when to give up +The `ConnectionManagerTimeout($connector, $timeout, $loop = null)` sets a maximum `$timeout` in seconds on when to give up waiting for the connection to complete. ```php -$connector = new ConnectionManagerTimeout($connector, 3.0, $loop); +$connector = new ConnectionManagerTimeout($connector, 3.0); ``` ### Delay -The `ConnectionManagerDelay($connector, $delay, $loop)` sets a fixed initial `$delay` in seconds before actually +The `ConnectionManagerDelay($connector, $delay, $loop = null)` sets a fixed initial `$delay` in seconds before actually trying to connect. (Not to be confused with [`ConnectionManagerTimeout`](#timeout) which sets a _maximum timeout_.) ```php -$delayed = new ConnectionManagerDelayed($connector, 0.5, $loop); +$delayed = new ConnectionManagerDelayed($connector, 0.5); ``` ### Reject @@ -221,11 +221,11 @@ retrying unreliable hosts: ```php // delay connection by 2 seconds -$delayed = new ConnectionManagerDelay($connector, 2.0, $loop); +$delayed = new ConnectionManagerDelay($connector, 2.0); // maximum of 3 tries, each taking no longer than 2.0 seconds $retry = new ConnectionManagerRepeat( - new ConnectionManagerTimeout($connector, 2.0, $loop), + new ConnectionManagerTimeout($connector, 2.0), 3 ); diff --git a/composer.json b/composer.json index 7534f80..fa62c43 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,8 @@ }, "require": { "php": ">=5.3", - "react/socket": "^1.0 || ^0.8 || ^0.7", - "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5", + "react/socket": "^1.9", + "react/event-loop": "^1.2", "react/promise": "^2.1 || ^1.2.1", "react/promise-timer": "^1.1" }, diff --git a/src/ConnectionManagerDelay.php b/src/ConnectionManagerDelay.php index b5112c6..a8a85c5 100644 --- a/src/ConnectionManagerDelay.php +++ b/src/ConnectionManagerDelay.php @@ -2,21 +2,32 @@ namespace ConnectionManager\Extra; -use React\Socket\ConnectorInterface; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise\Timer; +use React\Socket\ConnectorInterface; class ConnectionManagerDelay implements ConnectorInterface { + /** @var ConnectorInterface */ private $connectionManager; + + /** @var float */ private $delay; + + /** @var LoopInterface */ private $loop; - public function __construct(ConnectorInterface $connectionManager, $delay, LoopInterface $loop) + /** + * @param ConnectorInterface $connectionManager + * @param float $delay + * @param ?LoopInterface $loop + */ + public function __construct(ConnectorInterface $connectionManager, $delay, LoopInterface $loop = null) { $this->connectionManager = $connectionManager; $this->delay = $delay; - $this->loop = $loop; + $this->loop = $loop ?: Loop::get(); } public function connect($uri) diff --git a/src/ConnectionManagerTimeout.php b/src/ConnectionManagerTimeout.php index 5ec0872..5ec23a5 100644 --- a/src/ConnectionManagerTimeout.php +++ b/src/ConnectionManagerTimeout.php @@ -2,21 +2,32 @@ namespace ConnectionManager\Extra; -use React\Socket\ConnectorInterface; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise\Timer; +use React\Socket\ConnectorInterface; class ConnectionManagerTimeout implements ConnectorInterface { + /** @var ConnectorInterface */ private $connectionManager; + + /** @var float */ private $timeout; + + /** @var LoopInterface */ private $loop; - public function __construct(ConnectorInterface $connectionManager, $timeout, LoopInterface $loop) + /** + * @param ConnectorInterface $connectionManager + * @param float $timeout + * @param ?LoopInterface $loop + */ + public function __construct(ConnectorInterface $connectionManager, $timeout, LoopInterface $loop = null) { $this->connectionManager = $connectionManager; $this->timeout = $timeout; - $this->loop = $loop; + $this->loop = $loop ?: Loop::get(); } public function connect($uri) diff --git a/tests/ConnectionManagerDelayTest.php b/tests/ConnectionManagerDelayTest.php index 9b4e8d8..fc64150 100644 --- a/tests/ConnectionManagerDelayTest.php +++ b/tests/ConnectionManagerDelayTest.php @@ -3,6 +3,7 @@ namespace ConnectionManager\Tests\Extra; use ConnectionManager\Extra\ConnectionManagerDelay; +use React\EventLoop\Loop; class ConnectionManagerDelayTest extends TestCase { @@ -13,7 +14,19 @@ class ConnectionManagerDelayTest extends TestCase */ public function setUpLoop() { - $this->loop = \React\EventLoop\Factory::create(); + $this->loop = Loop::get(); + } + + public function testConstructWithoutLoopAssignsLoopAutomatically() + { + $unused = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $cm = new ConnectionManagerDelay($unused, 0); + + $ref = new \ReflectionProperty($cm, 'loop'); + $ref->setAccessible(true); + $loop = $ref->getValue($cm); + + $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); } public function testDelayTenth() @@ -24,7 +37,7 @@ public function testDelayTenth() $promise = $cm->connect('www.google.com:80'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); - $this->loop->run(); + Loop::run(); $promise->then($this->expectCallableOnce(), $this->expectCallableNever()); } @@ -38,6 +51,6 @@ public function testCancellationOfPromiseBeforeDelayDoesNotStartConnection() $promise = $cm->connect('www.google.com:80'); $promise->cancel(); - $this->loop->run(); + Loop::run(); } } diff --git a/tests/ConnectionManagerTimeoutTest.php b/tests/ConnectionManagerTimeoutTest.php index a719694..4b82ecb 100644 --- a/tests/ConnectionManagerTimeoutTest.php +++ b/tests/ConnectionManagerTimeoutTest.php @@ -16,7 +16,19 @@ class ConnectionManagerTimeoutTest extends TestCase */ public function setUpLoop() { - $this->loop = \React\EventLoop\Factory::create(); + $this->loop = \React\EventLoop\Loop::get(); + } + + public function testConstructWithoutLoopAssignsLoopAutomatically() + { + $unused = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $cm = new ConnectionManagerTimeout($unused, 0); + + $ref = new \ReflectionProperty($cm, 'loop'); + $ref->setAccessible(true); + $loop = $ref->getValue($cm); + + $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); } public function testTimeoutOkay()