Skip to content

Commit e13bf3d

Browse files
committed
Merge pull request #12 from clue-labs/lists
Move all connector lists to the constructor
2 parents 8b6bd9c + 11bb9eb commit e13bf3d

File tree

6 files changed

+55
-53
lines changed

6 files changed

+55
-53
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,37 @@ simplify exchanging the actual `ConnectionManager` during runtime (`->setConnect
9797
The `ConnectionManagerConsecutive($connectors)` establishs connections by trying to connect through
9898
any of the given `ConnectionManager`s in consecutive order until the first one succeeds.
9999

100+
```php
101+
$consecutive = new ConnectionManagerConsecutive(array(
102+
$connector1,
103+
$connector2
104+
));
105+
```
106+
100107
### Random
101108

102109
The `ConnectionManagerRandom($connectors)` works much like `ConnectionManagerConsecutive` but instead
103110
of using a fixed order, it always uses a randomly shuffled order.
104111

112+
```php
113+
$random = new ConnectionManagerRandom(array(
114+
$connector1,
115+
$connector2
116+
));
117+
```
118+
105119
### Concurrent
106120

107121
The `ConnectionManagerConcurrent($connectors)` establishes connections by trying to connect through
108122
ALL of the given `ConnectionManager`s at once, until the first one succeeds.
109123

124+
```php
125+
$concurrent = new ConnectionManagerConcurrent(array(
126+
$connector1,
127+
$connector2
128+
));
129+
```
130+
110131
### Selective
111132

112133
The `ConnectionManagerSelective($connectors)` manages a list of `Connector`s and

src/Multiple/ConnectionManagerConcurrent.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ class ConnectionManagerConcurrent extends ConnectionManagerConsecutive
1010
{
1111
public function create($host, $port)
1212
{
13-
if (!$this->managers) {
14-
return Promise\reject(new \UnderflowException('No managers to try to connect through'));
15-
}
16-
1713
$all = array();
1814
foreach ($this->managers as $connector) {
1915
/* @var $connection Connector */

src/Multiple/ConnectionManagerConsecutive.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99

1010
class ConnectionManagerConsecutive implements ConnectorInterface
1111
{
12-
protected $managers = array();
12+
protected $managers;
1313

14-
public function addConnectionManager(ConnectorInterface $connectionManager)
14+
/**
15+
*
16+
* @param ConnectorInterface[] $managers
17+
*/
18+
public function __construct(array $managers)
1519
{
16-
$this->managers []= $connectionManager;
20+
if (!$managers) {
21+
throw new \InvalidArgumentException('List of connectors must not be empty');
22+
}
23+
$this->managers = $managers;
1724
}
1825

1926
public function create($host, $port)

tests/Multiple/ConnectionManagerConcurrentTest.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55

66
class ConnectionManagerConcurrentTest extends TestCase
77
{
8-
public function testEmptyRejects()
8+
/**
9+
* @expectedException InvalidArgumentException
10+
*/
11+
public function testEmptyListsThrows()
912
{
10-
$connector = new ConnectionManagerConcurrent();
11-
12-
$promise = $connector->create('google.com', 80);
13-
14-
$this->assertPromiseReject($promise);
13+
new ConnectionManagerConcurrent(array());
1514
}
1615

1716
public function testWillForwardToInnerConnector()
@@ -21,8 +20,7 @@ public function testWillForwardToInnerConnector()
2120
$only = $this->getMock('React\SocketClient\ConnectorInterface');
2221
$only->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending);
2322

24-
$connector = new ConnectionManagerConcurrent();
25-
$connector->addConnectionManager($only);
23+
$connector = new ConnectionManagerConcurrent(array($only));
2624

2725
$promise = $connector->create('google.com', 80);
2826

@@ -39,9 +37,7 @@ public function testWillCancelOtherIfOneResolves()
3937
$second = $this->getMock('React\SocketClient\ConnectorInterface');
4038
$second->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending);
4139

42-
$connector = new ConnectionManagerConcurrent();
43-
$connector->addConnectionManager($first);
44-
$connector->addConnectionManager($second);
40+
$connector = new ConnectionManagerConcurrent(array($first, $second));
4541

4642
$promise = $connector->create('google.com', 80);
4743

@@ -59,9 +55,7 @@ public function testWillCloseOtherIfOneResolves()
5955
$second = $this->getMock('React\SocketClient\ConnectorInterface');
6056
$second->expects($this->once())->method('create')->with('google.com', 80)->willReturn(Promise\resolve($slower));
6157

62-
$connector = new ConnectionManagerConcurrent();
63-
$connector->addConnectionManager($first);
64-
$connector->addConnectionManager($second);
58+
$connector = new ConnectionManagerConcurrent(array($first, $second));
6559

6660
$promise = $connector->create('google.com', 80);
6761

tests/Multiple/ConnectionManagerConsecutiveTest.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@
66

77
class ConnectionManagerConsecutiveTest extends TestCase
88
{
9-
public function testEmpty()
9+
/**
10+
* @expectedException InvalidArgumentException
11+
*/
12+
public function testEmptyListThrows()
1013
{
11-
$cm = new ConnectionManagerConsecutive();
12-
13-
$promise = $cm->create('www.google.com', 80);
14-
15-
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
16-
17-
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
14+
new ConnectionManagerConsecutive(array());
1815
}
1916

2017
public function testReject()
2118
{
2219
$wont = new ConnectionManagerReject();
2320

24-
$cm = new ConnectionManagerConsecutive();
25-
$cm->addConnectionManager($wont);
21+
$cm = new ConnectionManagerConsecutive(array($wont));
2622

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

@@ -38,9 +34,7 @@ public function testWillTryAllIfEachRejects()
3834
$connector = $this->getMock('React\SocketClient\ConnectorInterface');
3935
$connector->expects($this->exactly(2))->method('create')->with('google.com', 80)->willReturn($rejected);
4036

41-
$cm = new ConnectionManagerConsecutive();
42-
$cm->addConnectionManager($connector);
43-
$cm->addConnectionManager($connector);
37+
$cm = new ConnectionManagerConsecutive(array($connector, $connector));
4438

4539
$promise = $cm->create('google.com', 80);
4640

@@ -54,9 +48,7 @@ public function testCancellationWillNotStartAnyFurtherConnections()
5448
$connector = $this->getMock('React\SocketClient\ConnectorInterface');
5549
$connector->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending);
5650

57-
$cm = new ConnectionManagerConsecutive();
58-
$cm->addConnectionManager($connector);
59-
$cm->addConnectionManager($connector);
51+
$cm = new ConnectionManagerConsecutive(array($connector, $connector));
6052

6153
$promise = $cm->create('google.com', 80);
6254
$promise->cancel();

tests/Multiple/ConnectionManagerRandomTest.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@
66

77
class ConnectionManagerRandomTest extends TestCase
88
{
9-
public function testEmpty()
9+
/**
10+
* @expectedException InvalidArgumentException
11+
*/
12+
public function testEmptyListThrows()
1013
{
11-
$cm = new ConnectionManagerRandom();
12-
13-
$promise = $cm->create('www.google.com', 80);
14-
15-
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
16-
17-
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
14+
new ConnectionManagerRandom(array());
1815
}
1916

2017
public function testReject()
2118
{
2219
$wont = new ConnectionManagerReject();
2320

24-
$cm = new ConnectionManagerRandom();
25-
$cm->addConnectionManager($wont);
21+
$cm = new ConnectionManagerRandom(array($wont));
2622

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

@@ -38,9 +34,7 @@ public function testWillTryAllIfEachRejects()
3834
$connector = $this->getMock('React\SocketClient\ConnectorInterface');
3935
$connector->expects($this->exactly(2))->method('create')->with('google.com', 80)->willReturn($rejected);
4036

41-
$cm = new ConnectionManagerRandom();
42-
$cm->addConnectionManager($connector);
43-
$cm->addConnectionManager($connector);
37+
$cm = new ConnectionManagerRandom(array($connector, $connector));
4438

4539
$promise = $cm->create('google.com', 80);
4640

@@ -54,9 +48,7 @@ public function testCancellationWillNotStartAnyFurtherConnections()
5448
$connector = $this->getMock('React\SocketClient\ConnectorInterface');
5549
$connector->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending);
5650

57-
$cm = new ConnectionManagerRandom();
58-
$cm->addConnectionManager($connector);
59-
$cm->addConnectionManager($connector);
51+
$cm = new ConnectionManagerRandom(array($connector, $connector));
6052

6153
$promise = $cm->create('google.com', 80);
6254
$promise->cancel();

0 commit comments

Comments
 (0)