From 6b48f4492c8fb6579c87cf9d474cf973f78d3b18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 21 Sep 2017 19:26:38 +0200 Subject: [PATCH] Always use Resolver with default DNS to match Socket component --- src/Factory.php | 22 +++++++++++++++------- tests/FactoryTest.php | 19 +++++++++++-------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index 3c12228..1b2313c 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -2,20 +2,32 @@ namespace React\Datagram; -use React\EventLoop\LoopInterface; +use React\Datagram\Socket; +use React\Dns\Resolver\Factory as DnsFactory; use React\Dns\Resolver\Resolver; +use React\EventLoop\LoopInterface; use React\Promise; -use React\Datagram\Socket; -use \Exception; use React\Promise\CancellablePromiseInterface; +use \Exception; class Factory { protected $loop; protected $resolver; + /** + * + * @param LoopInterface $loop + * @param Resolver|null $resolver Resolver instance to use. Will otherwise + * default to using Google's public DNS 8.8.8.8 + */ public function __construct(LoopInterface $loop, Resolver $resolver = null) { + if ($resolver === null) { + $factory = new DnsFactory(); + $resolver = $factory->create('8.8.8.8', $loop); + } + $this->loop = $loop; $this->resolver = $resolver; } @@ -99,10 +111,6 @@ protected function resolveHost($host) return Promise\resolve($host); } - if ($this->resolver === null) { - return Promise\reject(new Exception('No resolver given in order to get IP address for given hostname')); - } - $promise = $this->resolver->resolve($host); // wrap DNS lookup in order to control cancellation behavior diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 595e549..6e20766 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -54,6 +54,17 @@ public function testCreateClientLocalhost() $capturedClient->close(); } + public function testCreateClientLocalhostWithDefaultResolver() + { + $this->resolver = null; + $this->factory = new Factory($this->loop); + + $promise = $this->factory->createClient('localhost:12345'); + + $capturedClient = Block\await($promise, $this->loop); + $capturedClient->close(); + } + public function testCreateClientIpv6() { $promise = $this->factory->createClient('[::1]:12345'); @@ -126,14 +137,6 @@ public function testCreateClientWithHostnameWillRejectIfResolverRejects() Block\await($this->factory->createClient('example.com:0'), $this->loop); } - public function testCreateClientWithHostnameWillRejectIfNoResolverIsGiven() - { - $this->factory = new Factory($this->loop); - - $this->setExpectedException('Exception'); - Block\await($this->factory->createClient('example.com:0'), $this->loop); - } - /** * @expectedException Exception * @expectedExceptionMessage Unable to create client socket