Skip to content

Commit

Permalink
Merge pull request #70 from clue-labs/compat
Browse files Browse the repository at this point in the history
Forward compatibility with EventLoop v1.0 and v0.5
  • Loading branch information
jsor authored Aug 8, 2017
2 parents ca68364 + 5566f21 commit d54ae9a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"require": {
"php": ">=5.3.0",
"react/cache": "~0.4.0|~0.3.0",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
"react/socket": "^1.0 || ^0.8 || ^0.7 || ^0.6 || ^0.5 || ^0.4.4",
"react/stream": "^1.0 || ^0.7 || ^0.6 || ^0.5 || ^0.4.5",
"react/promise": "~2.1|~1.2",
Expand Down
10 changes: 5 additions & 5 deletions src/Query/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public function doQuery($nameserver, $transport, $queryData, $name)
$parser = $this->parser;
$loop = $this->loop;

$deferred = new Deferred(function ($resolve, $reject) use (&$timer, &$conn, $name) {
$deferred = new Deferred(function ($resolve, $reject) use (&$timer, $loop, &$conn, $name) {
$reject(new CancellationException(sprintf('DNS query for %s has been cancelled', $name)));

if ($timer !== null) {
$timer->cancel();
$loop->cancelTimer($timer);
}
$conn->close();
});
Expand Down Expand Up @@ -97,16 +97,16 @@ public function doQuery($nameserver, $transport, $queryData, $name)
} catch (\Exception $e) {
// both UDP and TCP failed => reject
if ($timer !== null) {
$timer->cancel();
$loop->cancelTimer($timer);
}
$deferred->reject(new \RuntimeException('Unable to connect to DNS server: ' . $e->getMessage(), 0, $e));

return $deferred->promise();
}

$conn->on('data', function ($data) use ($retryWithTcp, $conn, $parser, $transport, $deferred, $timer) {
$conn->on('data', function ($data) use ($retryWithTcp, $conn, $parser, $transport, $deferred, $timer, $loop) {
if ($timer !== null) {
$timer->cancel();
$loop->cancelTimer($timer);
}

try {
Expand Down
8 changes: 5 additions & 3 deletions tests/Query/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,18 @@ public function resolveShouldCancelTimerWhenFullResponseIsReceived()


$timer = $this->getMockBuilder('React\EventLoop\Timer\TimerInterface')->getMock();
$timer
->expects($this->once())
->method('cancel');

$this->loop
->expects($this->once())
->method('addTimer')
->with(5, $this->isInstanceOf('Closure'))
->will($this->returnValue($timer));

$this->loop
->expects($this->once())
->method('cancelTimer')
->with($timer);

$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451);
$this->executor->query('8.8.8.8:53', $query, function () {}, function () {});
}
Expand Down

0 comments on commit d54ae9a

Please sign in to comment.