Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"evenement/evenement": "~1.0|~2.0",
"react/event-loop": ">=0.2, <0.5",
"react/dns": ">=0.2, <0.5",
"react/promise": "~2.0|~1.1"
"react/promise": "~2.1|~1.2"
},
"require-dev": {
"clue/block-react": "~1.0"
Expand Down
20 changes: 19 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use React\Promise;
use React\Datagram\Socket;
use \Exception;
use React\Promise\CancellablePromiseInterface;

class Factory
{
Expand Down Expand Up @@ -108,6 +109,23 @@ protected function resolveHost($host)
return Promise\reject(new Exception('No resolver given in order to get IP address for given hostname'));
}

return $this->resolver->resolve($host);
$promise = $this->resolver->resolve($host);

// wrap DNS lookup in order to control cancellation behavior
return new Promise\Promise(
function ($resolve, $reject) use ($promise) {
// forward promise resolution
$promise->then($resolve, $reject);
},
function ($_, $reject) use ($promise) {
// reject with custom message once cancelled
$reject(new \RuntimeException('Cancelled creating socket during DNS lookup'));

// (try to) cancel pending DNS lookup, otherwise ignoring its results
if ($promise instanceof CancellablePromiseInterface) {
$promise->cancel();
}
}
);
}
}
24 changes: 24 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,28 @@ public function testCreateServerWithInvalidHostnameWillReject()
{
Block\await($this->factory->createServer('/////'), $this->loop);
}

public function testCancelCreateClientWithCancellableHostnameResolver()
{
$promise = new Promise\Promise(function () { }, $this->expectCallableOnce());
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn($promise);

$promise = $this->factory->createClient('example.com:0');
$promise->cancel();

$this->setExpectedException('RuntimeException');
Block\await($promise, $this->loop);
}

public function testCancelCreateClientWithUncancellableHostnameResolver()
{
$promise = $this->getMock('React\Promise\PromiseInterface');
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn($promise);

$promise = $this->factory->createClient('example.com:0');
$promise->cancel();

$this->setExpectedException('RuntimeException');
Block\await($promise, $this->loop);
}
}