-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace React\SocketClient; | ||
|
||
use React\SocketClient\ConnectorInterface; | ||
use React\EventLoop\LoopInterface; | ||
use React\Promise\Timer; | ||
|
||
class TimeoutConnector implements ConnectorInterface | ||
{ | ||
private $connector; | ||
private $timeout; | ||
private $loop; | ||
|
||
public function __construct(ConnectorInterface $connector, $timeout, LoopInterface $loop) | ||
{ | ||
$this->connector = $connector; | ||
$this->timeout = $timeout; | ||
$this->loop = $loop; | ||
} | ||
|
||
public function create($host, $port) | ||
{ | ||
return Timer\timeout($this->connector->create($host, $port), $this->timeout, $this->loop); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace React\Tests\SocketClient; | ||
|
||
use React\SocketClient\TimeoutConnector; | ||
use React\Promise; | ||
use React\EventLoop\Factory; | ||
|
||
class TimeoutConnectorTest extends TestCase | ||
{ | ||
public function testRejectsOnTimeout() | ||
{ | ||
$promise = new Promise\Promise(function () { }); | ||
|
||
$connector = $this->getMock('React\SocketClient\ConnectorInterface'); | ||
$connector->expects($this->once())->method('create')->with('google.com', 80)->will($this->returnValue($promise)); | ||
|
||
$loop = Factory::create(); | ||
|
||
$timeout = new TimeoutConnector($connector, 0.01, $loop); | ||
|
||
$timeout->create('google.com', 80)->then( | ||
$this->expectCallableNever(), | ||
$this->expectCallableOnce() | ||
); | ||
|
||
$loop->run(); | ||
} | ||
|
||
public function testRejectsWhenConnectorRejects() | ||
{ | ||
$promise = Promise\reject(); | ||
|
||
$connector = $this->getMock('React\SocketClient\ConnectorInterface'); | ||
$connector->expects($this->once())->method('create')->with('google.com', 80)->will($this->returnValue($promise)); | ||
|
||
$loop = Factory::create(); | ||
|
||
$timeout = new TimeoutConnector($connector, 5.0, $loop); | ||
|
||
$timeout->create('google.com', 80)->then( | ||
$this->expectCallableNever(), | ||
$this->expectCallableOnce() | ||
); | ||
|
||
$loop->run(); | ||
} | ||
|
||
public function testResolvesWhenConnectorResolves() | ||
{ | ||
$promise = Promise\resolve(); | ||
|
||
$connector = $this->getMock('React\SocketClient\ConnectorInterface'); | ||
$connector->expects($this->once())->method('create')->with('google.com', 80)->will($this->returnValue($promise)); | ||
|
||
$loop = Factory::create(); | ||
|
||
$timeout = new TimeoutConnector($connector, 5.0, $loop); | ||
|
||
$timeout->create('google.com', 80)->then( | ||
$this->expectCallableOnce(), | ||
$this->expectCallableNever() | ||
); | ||
|
||
$loop->run(); | ||
} | ||
|
||
public function testRejectsAndCancelsPendingPromiseOnTimeout() | ||
{ | ||
$promise = new Promise\Promise(function () { }, $this->expectCallableOnce()); | ||
|
||
$connector = $this->getMock('React\SocketClient\ConnectorInterface'); | ||
$connector->expects($this->once())->method('create')->with('google.com', 80)->will($this->returnValue($promise)); | ||
|
||
$loop = Factory::create(); | ||
|
||
$timeout = new TimeoutConnector($connector, 0.01, $loop); | ||
|
||
$timeout->create('google.com', 80)->then( | ||
$this->expectCallableNever(), | ||
$this->expectCallableOnce() | ||
); | ||
|
||
$loop->run(); | ||
} | ||
} |