Skip to content

Commit 847780f

Browse files
committed
Merge pull request #13 from clue-labs/timers
Timed connectors now use $loop as last argument
2 parents e13bf3d + cecd24a commit 847780f

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,22 @@ $connectorRepeater->create('www.google.com', 80)->then(function ($stream) {
7373

7474
### Timeout
7575

76-
The `ConnectionManagerTimeout($connector, $timeout)` sets a maximum `$timeout` in seconds on when to give up
76+
The `ConnectionManagerTimeout($connector, $timeout, $loop)` sets a maximum `$timeout` in seconds on when to give up
7777
waiting for the connection to complete.
7878

79+
```php
80+
$connector = new ConnectionManagerTimeout($connector, 3.0, $loop);
81+
```
82+
7983
### Delay
8084

81-
The `ConnectionManagerDelay($connector, $delay)` sets a fixed initial `$delay` in seconds before actually
85+
The `ConnectionManagerDelay($connector, $delay, $loop)` sets a fixed initial `$delay` in seconds before actually
8286
trying to connect. (Not to be confused with [`ConnectionManagerTimeout`](#timeout) which sets a _maximum timeout_.)
8387

88+
```php
89+
$delayed = new ConnectionManagerDelayed($connector, 0.5, $loop);
90+
```
91+
8492
### Reject
8593

8694
The `ConnectionManagerReject()` simply rejects every single connection attempt.
@@ -167,11 +175,11 @@ retrying unreliable hosts:
167175

168176
```php
169177
// delay connection by 2 seconds
170-
$delayed = new ConnectionManagerDelay($connector, $loop, 2.0);
178+
$delayed = new ConnectionManagerDelay($connector, 2.0, $loop);
171179

172180
// maximum of 3 tries, each taking no longer than 3 seconds
173181
$retry = new ConnectionManagerRepeat(
174-
new ConnectionManagerTimeout($connector, $loop, 3.0),
182+
new ConnectionManagerTimeout($connector, 3.0, $loop),
175183
2
176184
);
177185

src/ConnectionManagerDelay.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
class ConnectionManagerDelay implements ConnectorInterface
1010
{
1111
private $connectionManager;
12-
private $loop;
1312
private $delay;
13+
private $loop;
1414

15-
public function __construct(ConnectorInterface $connectionManager, LoopInterface $loop, $delay)
15+
public function __construct(ConnectorInterface $connectionManager, $delay, LoopInterface $loop)
1616
{
1717
$this->connectionManager = $connectionManager;
18-
$this->loop = $loop;
1918
$this->delay = $delay;
19+
$this->loop = $loop;
2020
}
2121

2222
public function create($host, $port)

src/ConnectionManagerTimeout.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
class ConnectionManagerTimeout implements ConnectorInterface
1111
{
1212
private $connectionManager;
13-
private $loop;
1413
private $timeout;
14+
private $loop;
1515

16-
public function __construct(ConnectorInterface $connectionManager, LoopInterface $loop, $timeout)
16+
public function __construct(ConnectorInterface $connectionManager, $timeout, LoopInterface $loop)
1717
{
1818
$this->connectionManager = $connectionManager;
19-
$this->loop = $loop;
2019
$this->timeout = $timeout;
20+
$this->loop = $loop;
2121
}
2222

2323
public function create($host, $port)

tests/ConnectionManagerDelayTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function setUp()
1515
public function testDelayTenth()
1616
{
1717
$will = $this->createConnectionManagerMock(true);
18-
$cm = new ConnectionManagerDelay($will, $this->loop, 0.1);
18+
$cm = new ConnectionManagerDelay($will, 0.1, $this->loop);
1919

2020
$promise = $cm->create('www.google.com', 80);
2121
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
@@ -29,7 +29,7 @@ public function testCancellationOfPromiseBeforeDelayDoesNotStartConnection()
2929
$unused = $this->getMock('React\SocketClient\ConnectorInterface');
3030
$unused->expects($this->never())->method('create');
3131

32-
$cm = new ConnectionManagerDelay($unused, $this->loop, 1.0);
32+
$cm = new ConnectionManagerDelay($unused, 1.0, $this->loop);
3333

3434
$promise = $cm->create('www.google.com', 80);
3535
$promise->cancel();

tests/ConnectionManagerTimeoutTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
class ConnectionManagerTimeoutTest extends TestCase
1212
{
13+
private $loop;
14+
1315
public function setUp()
1416
{
1517
$this->loop = React\EventLoop\Factory::create();
@@ -18,7 +20,7 @@ public function setUp()
1820
public function testTimeoutOkay()
1921
{
2022
$will = $this->createConnectionManagerMock(true);
21-
$cm = new ConnectionManagerTimeout($will, $this->loop, 0.1);
23+
$cm = new ConnectionManagerTimeout($will, 0.1, $this->loop);
2224

2325
$promise = $cm->create('www.google.com', 80);
2426
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
@@ -30,9 +32,9 @@ public function testTimeoutOkay()
3032
public function testTimeoutExpire()
3133
{
3234
$will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r'), $this->loop));
33-
$wont = new ConnectionManagerDelay($will, $this->loop, 0.2);
35+
$wont = new ConnectionManagerDelay($will, 0.2, $this->loop);
3436

35-
$cm = new ConnectionManagerTimeout($wont, $this->loop, 0.1);
37+
$cm = new ConnectionManagerTimeout($wont, 0.1, $this->loop);
3638

3739
$promise = $cm->create('www.google.com', 80);
3840
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
@@ -45,7 +47,7 @@ public function testTimeoutAbort()
4547
{
4648
$wont = new ConnectionManagerReject();
4749

48-
$cm = new ConnectionManagerTimeout($wont, $this->loop, 0.1);
50+
$cm = new ConnectionManagerTimeout($wont, 0.1, $this->loop);
4951

5052
$promise = $cm->create('www.google.com', 80);
5153
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
@@ -69,7 +71,7 @@ public function testWillEndConnectionIfConnectionResolvesDespiteTimeout()
6971
$connector = $this->getMock('React\SocketClient\ConnectorInterface');
7072
$connector->expects($this->once())->method('create')->with('www.google.com', 80)->willReturn($promise);
7173

72-
$cm = new ConnectionManagerTimeout($connector, $this->loop, 0.001);
74+
$cm = new ConnectionManagerTimeout($connector, 0.001, $this->loop);
7375

7476
$promise = $cm->create('www.google.com', 80);
7577

@@ -87,7 +89,7 @@ public function testCancellationOfPromiseWillCancelConnectionAttempt()
8789
$connector = $this->getMock('React\SocketClient\ConnectorInterface');
8890
$connector->expects($this->once())->method('create')->with('www.google.com', 80)->willReturn($promise);
8991

90-
$cm = new ConnectionManagerTimeout($connector, $this->loop, 5.0);
92+
$cm = new ConnectionManagerTimeout($connector, 5.0, $this->loop);
9193

9294
$promise = $cm->create('www.google.com', 80);
9395
$promise->cancel();

0 commit comments

Comments
 (0)