Skip to content

Commit

Permalink
Simplify usage by supporting new default loop
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulRotmann authored and SimonFrings committed Sep 3, 2021
1 parent 05b1e69 commit 1ccc50f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 18 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
17 changes: 14 additions & 3 deletions src/ConnectionManagerDelay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions src/ConnectionManagerTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 16 additions & 3 deletions tests/ConnectionManagerDelayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ConnectionManager\Tests\Extra;

use ConnectionManager\Extra\ConnectionManagerDelay;
use React\EventLoop\Loop;

class ConnectionManagerDelayTest extends TestCase
{
Expand All @@ -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()
Expand All @@ -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());
}

Expand All @@ -38,6 +51,6 @@ public function testCancellationOfPromiseBeforeDelayDoesNotStartConnection()
$promise = $cm->connect('www.google.com:80');
$promise->cancel();

$this->loop->run();
Loop::run();
}
}
14 changes: 13 additions & 1 deletion tests/ConnectionManagerTimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1ccc50f

Please sign in to comment.