Skip to content

Commit 512c838

Browse files
authored
Merge pull request #20 from clue-labs/resolver
Always use Resolver with default DNS to match Socket component
2 parents a2d305b + 6b48f44 commit 512c838

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/Factory.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@
22

33
namespace React\Datagram;
44

5-
use React\EventLoop\LoopInterface;
5+
use React\Datagram\Socket;
6+
use React\Dns\Resolver\Factory as DnsFactory;
67
use React\Dns\Resolver\Resolver;
8+
use React\EventLoop\LoopInterface;
79
use React\Promise;
8-
use React\Datagram\Socket;
9-
use \Exception;
1010
use React\Promise\CancellablePromiseInterface;
11+
use \Exception;
1112

1213
class Factory
1314
{
1415
protected $loop;
1516
protected $resolver;
1617

18+
/**
19+
*
20+
* @param LoopInterface $loop
21+
* @param Resolver|null $resolver Resolver instance to use. Will otherwise
22+
* default to using Google's public DNS 8.8.8.8
23+
*/
1724
public function __construct(LoopInterface $loop, Resolver $resolver = null)
1825
{
26+
if ($resolver === null) {
27+
$factory = new DnsFactory();
28+
$resolver = $factory->create('8.8.8.8', $loop);
29+
}
30+
1931
$this->loop = $loop;
2032
$this->resolver = $resolver;
2133
}
@@ -99,10 +111,6 @@ protected function resolveHost($host)
99111
return Promise\resolve($host);
100112
}
101113

102-
if ($this->resolver === null) {
103-
return Promise\reject(new Exception('No resolver given in order to get IP address for given hostname'));
104-
}
105-
106114
$promise = $this->resolver->resolve($host);
107115

108116
// wrap DNS lookup in order to control cancellation behavior

tests/FactoryTest.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ public function testCreateClientLocalhost()
5454
$capturedClient->close();
5555
}
5656

57+
public function testCreateClientLocalhostWithDefaultResolver()
58+
{
59+
$this->resolver = null;
60+
$this->factory = new Factory($this->loop);
61+
62+
$promise = $this->factory->createClient('localhost:12345');
63+
64+
$capturedClient = Block\await($promise, $this->loop);
65+
$capturedClient->close();
66+
}
67+
5768
public function testCreateClientIpv6()
5869
{
5970
$promise = $this->factory->createClient('[::1]:12345');
@@ -126,14 +137,6 @@ public function testCreateClientWithHostnameWillRejectIfResolverRejects()
126137
Block\await($this->factory->createClient('example.com:0'), $this->loop);
127138
}
128139

129-
public function testCreateClientWithHostnameWillRejectIfNoResolverIsGiven()
130-
{
131-
$this->factory = new Factory($this->loop);
132-
133-
$this->setExpectedException('Exception');
134-
Block\await($this->factory->createClient('example.com:0'), $this->loop);
135-
}
136-
137140
/**
138141
* @expectedException Exception
139142
* @expectedExceptionMessage Unable to create client socket

0 commit comments

Comments
 (0)