Skip to content

Commit

Permalink
Update to SocketClient v0.6+ and use connect($uri)
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Apr 7, 2017
1 parent 5170c27 commit a7614ee
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 101 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ In order to be able to establish several connections at the same time, [react/so
API to establish simple connections in an async (non-blocking) way.

This project includes several classes that extend this base functionality by implementing the same simple `ConnectorInterface`.
This interface provides a single promise-based method `create($host, $ip)` which can be used to easily notify
This interface provides a single promise-based method `connect($uri)` which can be used to easily notify
when the connection is successfully established or the `Connector` gives up and the connection fails.

```php
$connector->create('www.google.com', 80)->then(function ($stream) {
$connector->connect('www.google.com:80')->then(function ($stream) {
echo 'connection successfully established';
$stream->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
$stream->end();
Expand Down Expand Up @@ -70,7 +70,7 @@ and then retry up to 2 times if the connection attempt fails:
```php
$connectorRepeater = new ConnectionManagerRepeat($connector, 3);

$connectorRepeater->create('www.google.com', 80)->then(function ($stream) {
$connectorRepeater->connect('www.google.com:80')->then(function ($stream) {
echo 'connection successfully established';
$stream->close();
});
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"require": {
"php": ">=5.3",
"react/socket-client": "^0.5 || ^0.4 || ^0.3",
"react/socket-client": "^0.7 || ^0.6",
"react/event-loop": "^0.4 || ^0.3",
"react/promise": "^2.1 || ^1.2",
"react/promise-timer": "^1.1"
Expand Down
6 changes: 3 additions & 3 deletions src/ConnectionManagerDelay.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public function __construct(ConnectorInterface $connectionManager, $delay, LoopI
$this->loop = $loop;
}

public function create($host, $port)
public function connect($uri)
{
$connectionManager = $this->connectionManager;

return Timer\resolve($this->delay, $this->loop)->then(function () use ($connectionManager, $host, $port) {
return $connectionManager->create($host, $port);
return Timer\resolve($this->delay, $this->loop)->then(function () use ($connectionManager, $uri) {
return $connectionManager->connect($uri);
});
}
}
2 changes: 1 addition & 1 deletion src/ConnectionManagerReject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// a simple connection manager that rejects every single connection attempt
class ConnectionManagerReject implements ConnectorInterface
{
public function create($host, $port)
public function connect($_)
{
return Promise\reject(new Exception('Connection rejected'));
}
Expand Down
8 changes: 4 additions & 4 deletions src/ConnectionManagerRepeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public function __construct(ConnectorInterface $connectionManager, $maximumTries
$this->maximumTries = $maximumTries;
}

public function create($host, $port)
public function connect($uri)
{
$tries = $this->maximumTries;
$connector = $this->connectionManager;

return new Promise(function ($resolve, $reject) use ($host, $port, &$pending, &$tries, $connector) {
$try = function ($error = null) use (&$try, &$pending, &$tries, $host, $port, $connector, $resolve, $reject) {
return new Promise(function ($resolve, $reject) use ($uri, &$pending, &$tries, $connector) {
$try = function ($error = null) use (&$try, &$pending, &$tries, $uri, $connector, $resolve, $reject) {
if ($tries > 0) {
--$tries;
$pending = $connector->create($host, $port);
$pending = $connector->connect($uri);
$pending->then($resolve, $try);
} else {
$reject(new Exception('Connection still fails even after retrying', 0, $error));
Expand Down
4 changes: 2 additions & 2 deletions src/ConnectionManagerSwappable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public function __construct(ConnectorInterface $connectionManager)
$this->connectionManager = $connectionManager;
}

public function create($host, $port)
public function connect($uri)
{
return $this->connectionManager->create($host, $port);
return $this->connectionManager->connect($uri);
}

public function setConnectionManager(ConnectorInterface $connectionManager)
Expand Down
4 changes: 2 additions & 2 deletions src/ConnectionManagerTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function __construct(ConnectorInterface $connectionManager, $timeout, Loo
$this->loop = $loop;
}

public function create($host, $port)
public function connect($uri)
{
$promise = $this->connectionManager->create($host, $port);
$promise = $this->connectionManager->connect($uri);

return Timer\timeout($promise, $this->timeout, $this->loop)->then(null, function ($e) use ($promise) {
// connection successfully established but timeout already expired => close successful connection
Expand Down
4 changes: 2 additions & 2 deletions src/Multiple/ConnectionManagerConcurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

class ConnectionManagerConcurrent extends ConnectionManagerConsecutive
{
public function create($host, $port)
public function connect($uri)
{
$all = array();
foreach ($this->managers as $connector) {
/* @var $connection Connector */
$all []= $connector->create($host, $port);
$all []= $connector->connect($uri);
}
return Promise\any($all)->then(function ($conn) use ($all) {
// a connection attempt succeeded
Expand Down
15 changes: 7 additions & 8 deletions src/Multiple/ConnectionManagerConsecutive.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,28 @@ public function __construct(array $managers)
$this->managers = $managers;
}

public function create($host, $port)
public function connect($uri)
{
return $this->tryConnection($this->managers, $host, $port);
return $this->tryConnection($this->managers, $uri);
}

/**
*
* @param ConnectorInterface[] $managers
* @param string $host
* @param int $port
* @param string $uri
* @return Promise
* @internal
*/
public function tryConnection(array $managers, $host, $port)
public function tryConnection(array $managers, $uri)
{
return new Promise\Promise(function ($resolve, $reject) use (&$managers, &$pending, $host, $port) {
$try = function () use (&$try, &$managers, $host, $port, $resolve, $reject, &$pending) {
return new Promise\Promise(function ($resolve, $reject) use (&$managers, &$pending, $uri) {
$try = function () use (&$try, &$managers, $uri, $resolve, $reject, &$pending) {
if (!$managers) {
return $reject(new UnderflowException('No more managers to try to connect through'));
}

$manager = array_shift($managers);
$pending = $manager->create($host, $port);
$pending = $manager->connect($uri);
$pending->then($resolve, $try);
};

Expand Down
6 changes: 3 additions & 3 deletions src/Multiple/ConnectionManagerRandom.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

class ConnectionManagerRandom extends ConnectionManagerConsecutive
{
public function create($host, $port)
public function connect($uri)
{
$managers = $this->managers;
shuffle($managers);
return $this->tryConnection($managers, $host, $port);

return $this->tryConnection($managers, $uri);
}
}
14 changes: 11 additions & 3 deletions src/Multiple/ConnectionManagerSelective.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,22 @@ public function __construct(array $managers)
$this->managers = $managers;
}

public function create($host, $port)
public function connect($uri)
{
try {
$connector = $this->getConnectorForTarget($host, $port);
$parts = parse_url('tcp://' . $uri);
if (!isset($parts) || !isset($parts['scheme'], $parts['host'], $parts['port'])) {
throw new InvalidArgumentException('Invalid URI');
}

$connector = $this->getConnectorForTarget(
trim($parts['host'], '[]'),
$parts['port']
);
} catch (UnderflowException $e) {
return Promise\reject($e);
}
return $connector->create($host, $port);
return $connector->connect($uri);
}

private function getConnectorForTarget($targetHost, $targetPort)
Expand Down
6 changes: 3 additions & 3 deletions tests/ConnectionManagerDelayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testDelayTenth()
$will = $this->createConnectionManagerMock(true);
$cm = new ConnectionManagerDelay($will, 0.1, $this->loop);

$promise = $cm->create('www.google.com', 80);
$promise = $cm->connect('www.google.com:80');
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);

$this->loop->run();
Expand All @@ -27,11 +27,11 @@ public function testDelayTenth()
public function testCancellationOfPromiseBeforeDelayDoesNotStartConnection()
{
$unused = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();
$unused->expects($this->never())->method('create');
$unused->expects($this->never())->method('connect');

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

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

$this->loop->run();
Expand Down
2 changes: 1 addition & 1 deletion tests/ConnectionManagerRejectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ConnectionManagerRejectTest extends TestCase
public function testReject()
{
$cm = new ConnectionManagerReject();
$promise = $cm->create('www.google.com', 80);
$promise = $cm->connect('www.google.com:80');

$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);

Expand Down
14 changes: 7 additions & 7 deletions tests/ConnectionManagerRepeatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function testRepeatRejected()
{
$wont = new ConnectionManagerReject();
$cm = new ConnectionManagerRepeat($wont, 3);
$promise = $cm->create('www.google.com', 80);
$promise = $cm->connect('www.google.com:80');

$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);

Expand All @@ -22,11 +22,11 @@ public function testTwoTriesWillStartTwoConnectionAttempts()
$promise = Promise\reject(new \RuntimeException('nope'));

$connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();
$connector->expects($this->exactly(2))->method('create')->with('google.com', 80)->willReturn($promise);
$connector->expects($this->exactly(2))->method('connect')->with('google.com:80')->willReturn($promise);

$cm = new ConnectionManagerRepeat($connector, 2);

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

$this->assertPromiseReject($promise);
}
Expand All @@ -45,11 +45,11 @@ public function testCancellationWillNotStartAnyFurtherConnections()
$pending = new Promise\Promise(function () { }, $this->expectCallableOnce());

$connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();
$connector->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending);
$connector->expects($this->once())->method('connect')->with('google.com:80')->willReturn($pending);

$cm = new ConnectionManagerRepeat($connector, 3);

$promise = $cm->create('google.com', 80);
$promise = $cm->connect('google.com:80');
$promise->cancel();
}

Expand All @@ -60,11 +60,11 @@ public function testCancellationWillNotStartAnyFurtherConnectionsIfPromiseReject
});

$connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();
$connector->expects($this->once())->method('create')->with('google.com', 80)->willReturn($pending);
$connector->expects($this->once())->method('connect')->with('google.com:80')->willReturn($pending);

$cm = new ConnectionManagerRepeat($connector, 3);

$promise = $cm->create('google.com', 80);
$promise = $cm->connect('google.com:80');
$promise->cancel();
}
}
4 changes: 2 additions & 2 deletions tests/ConnectionManagerSwappableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public function testSwap()
$wont = new ConnectionManagerReject();
$cm = new ConnectionManagerSwappable($wont);

$promise = $cm->create('www.google.com', 80);
$promise = $cm->connect('www.google.com:80');
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());

$will = $this->createConnectionManagerMock(true);
$cm->setConnectionManager($will);

$promise = $cm->create('www.google.com', 80);
$promise = $cm->connect('www.google.com:80');
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
}
Expand Down
18 changes: 9 additions & 9 deletions tests/ConnectionManagerTimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testTimeoutOkay()
$will = $this->createConnectionManagerMock(true);
$cm = new ConnectionManagerTimeout($will, 0.1, $this->loop);

$promise = $cm->create('www.google.com', 80);
$promise = $cm->connect('www.google.com:80');
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);

$this->loop->run();
Expand All @@ -31,12 +31,12 @@ public function testTimeoutOkay()

public function testTimeoutExpire()
{
$will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r'), $this->loop));
$will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r+'), $this->loop));
$wont = new ConnectionManagerDelay($will, 0.2, $this->loop);

$cm = new ConnectionManagerTimeout($wont, 0.1, $this->loop);

$promise = $cm->create('www.google.com', 80);
$promise = $cm->connect('www.google.com:80');
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);

$this->loop->run();
Expand All @@ -49,7 +49,7 @@ public function testTimeoutAbort()

$cm = new ConnectionManagerTimeout($wont, 0.1, $this->loop);

$promise = $cm->create('www.google.com', 80);
$promise = $cm->connect('www.google.com:80');
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);

$this->loop->run();
Expand All @@ -58,7 +58,7 @@ public function testTimeoutAbort()

public function testWillEndConnectionIfConnectionResolvesDespiteTimeout()
{
$stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock();
$stream = $this->getMockBuilder('React\Stream\DuplexStreamInterface')->disableOriginalConstructor()->getMock();
$stream->expects($this->once())->method('end');

$loop = $this->loop;
Expand All @@ -69,11 +69,11 @@ public function testWillEndConnectionIfConnectionResolvesDespiteTimeout()
});

$connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();
$connector->expects($this->once())->method('create')->with('www.google.com', 80)->willReturn($promise);
$connector->expects($this->once())->method('connect')->with('www.google.com:80')->willReturn($promise);

$cm = new ConnectionManagerTimeout($connector, 0.001, $this->loop);

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

$this->loop->run();

Expand All @@ -87,11 +87,11 @@ public function testCancellationOfPromiseWillCancelConnectionAttempt()
});

$connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();
$connector->expects($this->once())->method('create')->with('www.google.com', 80)->willReturn($promise);
$connector->expects($this->once())->method('connect')->with('www.google.com:80')->willReturn($promise);

$cm = new ConnectionManagerTimeout($connector, 5.0, $this->loop);

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

$this->loop->run();
Expand Down
Loading

0 comments on commit a7614ee

Please sign in to comment.