From 6e9f5daaab5ba02a2266075b97fba97bef699837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 8 Jul 2021 11:21:25 +0200 Subject: [PATCH 1/2] Simplify usage by supporting new default loop --- README.md | 52 ++++++++---------------- composer.json | 2 +- examples/01-one.php | 6 +-- examples/02-concurrent.php | 6 +-- examples/03-cached.php | 13 +++--- examples/11-all-ips.php | 6 +-- examples/12-all-types.php | 6 +-- examples/13-reverse-dns.php | 6 +-- examples/91-query-a-and-aaaa.php | 5 +-- examples/92-query-any.php | 5 +-- src/Query/CoopExecutor.php | 5 +-- src/Query/TcpTransportExecutor.php | 24 +++++------ src/Query/TimeoutExecutor.php | 5 ++- src/Query/UdpTransportExecutor.php | 29 ++++++------- src/Resolver/Factory.php | 15 +++---- tests/Query/TcpTransportExecutorTest.php | 11 +++++ tests/Query/TimeoutExecutorTest.php | 15 +++++++ tests/Query/UdpTransportExecutorTest.php | 11 +++++ tests/Resolver/FactoryTest.php | 8 +--- 19 files changed, 103 insertions(+), 127 deletions(-) diff --git a/README.md b/README.md index 385992c1..40115637 100644 --- a/README.md +++ b/README.md @@ -33,21 +33,17 @@ factory. All you need to give it is a nameserver, then you can start resolving names, baby! ```php -$loop = React\EventLoop\Factory::create(); - $config = React\Dns\Config\Config::loadSystemConfigBlocking(); if (!$config->nameservers) { $config->nameservers[] = '8.8.8.8'; } $factory = new React\Dns\Resolver\Factory(); -$dns = $factory->create($config, $loop); +$dns = $factory->create($config); $dns->resolve('igor.io')->then(function ($ip) { echo "Host: $ip\n"; }); - -$loop->run(); ``` See also the [first example](examples). @@ -72,15 +68,13 @@ But there's more. You can cache results by configuring the resolver to use a `CachedExecutor`: ```php -$loop = React\EventLoop\Factory::create(); - $config = React\Dns\Config\Config::loadSystemConfigBlocking(); if (!$config->nameservers) { $config->nameservers[] = '8.8.8.8'; } $factory = new React\Dns\Resolver\Factory(); -$dns = $factory->createCached($config, $loop); +$dns = $factory->createCached($config); $dns->resolve('igor.io')->then(function ($ip) { echo "Host: $ip\n"; @@ -91,8 +85,6 @@ $dns->resolve('igor.io')->then(function ($ip) { $dns->resolve('igor.io')->then(function ($ip) { echo "Host: $ip\n"; }); - -$loop->run(); ``` If the first call returns before the second, only one query will be executed. @@ -110,9 +102,8 @@ You can also specify a custom cache implementing [`CacheInterface`](https://gith ```php $cache = new React\Cache\ArrayCache(); -$loop = React\EventLoop\Factory::create(); $factory = new React\Dns\Resolver\Factory(); -$dns = $factory->createCached('8.8.8.8', $loop, $cache); +$dns = $factory->createCached('8.8.8.8', null, $cache); ``` See also the wiki for possible [cache implementations](https://github.com/reactphp/react/wiki/Users#cache-implementations). @@ -215,8 +206,7 @@ For more advanced usages one can utilize this class directly. The following example looks up the `IPv6` address for `igor.io`. ```php -$loop = Factory::create(); -$executor = new UdpTransportExecutor('8.8.8.8:53', $loop); +$executor = new UdpTransportExecutor('8.8.8.8:53'); $executor->query( new Query($name, Message::TYPE_AAAA, Message::CLASS_IN) @@ -225,8 +215,6 @@ $executor->query( echo 'IPv6: ' . $answer->data . PHP_EOL; } }, 'printf'); - -$loop->run(); ``` See also the [fourth example](examples). @@ -236,9 +224,8 @@ want to use this in combination with a `TimeoutExecutor` like this: ```php $executor = new TimeoutExecutor( - new UdpTransportExecutor($nameserver, $loop), - 3.0, - $loop + new UdpTransportExecutor($nameserver), + 3.0 ); ``` @@ -249,9 +236,8 @@ combination with a `RetryExecutor` like this: ```php $executor = new RetryExecutor( new TimeoutExecutor( - new UdpTransportExecutor($nameserver, $loop), - 3.0, - $loop + new UdpTransportExecutor($nameserver), + 3.0 ) ); ``` @@ -268,9 +254,8 @@ a `CoopExecutor` like this: $executor = new CoopExecutor( new RetryExecutor( new TimeoutExecutor( - new UdpTransportExecutor($nameserver, $loop), - 3.0, - $loop + new UdpTransportExecutor($nameserver), + 3.0 ) ) ); @@ -293,8 +278,7 @@ For more advanced usages one can utilize this class directly. The following example looks up the `IPv6` address for `reactphp.org`. ```php -$loop = Factory::create(); -$executor = new TcpTransportExecutor('8.8.8.8:53', $loop); +$executor = new TcpTransportExecutor('8.8.8.8:53'); $executor->query( new Query($name, Message::TYPE_AAAA, Message::CLASS_IN) @@ -303,8 +287,6 @@ $executor->query( echo 'IPv6: ' . $answer->data . PHP_EOL; } }, 'printf'); - -$loop->run(); ``` See also [example #92](examples). @@ -314,9 +296,8 @@ want to use this in combination with a `TimeoutExecutor` like this: ```php $executor = new TimeoutExecutor( - new TcpTransportExecutor($nameserver, $loop), - 3.0, - $loop + new TcpTransportExecutor($nameserver), + 3.0 ); ``` @@ -342,9 +323,8 @@ combination with a `CoopExecutor` like this: ```php $executor = new CoopExecutor( new TimeoutExecutor( - new TcpTransportExecutor($nameserver, $loop), - 3.0, - $loop + new TcpTransportExecutor($nameserver), + 3.0 ) ); ``` @@ -412,7 +392,7 @@ use this code: ```php $hosts = \React\Dns\Config\HostsFile::loadFromPathBlocking(); -$executor = new UdpTransportExecutor('8.8.8.8:53', $loop); +$executor = new UdpTransportExecutor('8.8.8.8:53'); $executor = new HostsFileExecutor($hosts, $executor); $executor->query( diff --git a/composer.json b/composer.json index ea644ae3..0709f1dd 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "require": { "php": ">=5.3.0", "react/cache": "^1.0 || ^0.6 || ^0.5", - "react/event-loop": "^1.0 || ^0.5", + "react/event-loop": "dev-master#78f7f43 as 1.2.0", "react/promise": "^3.0 || ^2.7 || ^1.2.1", "react/promise-timer": "^1.2" }, diff --git a/examples/01-one.php b/examples/01-one.php index 509062c8..df8dda2a 100644 --- a/examples/01-one.php +++ b/examples/01-one.php @@ -5,20 +5,16 @@ require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); - $config = Config::loadSystemConfigBlocking(); if (!$config->nameservers) { $config->nameservers[] = '8.8.8.8'; } $factory = new Factory(); -$resolver = $factory->create($config, $loop); +$resolver = $factory->create($config); $name = isset($argv[1]) ? $argv[1] : 'www.google.com'; $resolver->resolve($name)->then(function ($ip) use ($name) { echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; }, 'printf'); - -$loop->run(); diff --git a/examples/02-concurrent.php b/examples/02-concurrent.php index 53a1c0e0..1810f0f7 100644 --- a/examples/02-concurrent.php +++ b/examples/02-concurrent.php @@ -5,15 +5,13 @@ require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); - $config = Config::loadSystemConfigBlocking(); if (!$config->nameservers) { $config->nameservers[] = '8.8.8.8'; } $factory = new Factory(); -$resolver = $factory->create($config, $loop); +$resolver = $factory->create($config); $names = array_slice($argv, 1); if (!$names) { @@ -25,5 +23,3 @@ echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; }, 'printf'); } - -$loop->run(); diff --git a/examples/03-cached.php b/examples/03-cached.php index 492b2497..fc800f54 100644 --- a/examples/03-cached.php +++ b/examples/03-cached.php @@ -2,18 +2,17 @@ use React\Dns\Config\Config; use React\Dns\Resolver\Factory; +use React\EventLoop\Loop; require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); - $config = Config::loadSystemConfigBlocking(); if (!$config->nameservers) { $config->nameservers[] = '8.8.8.8'; } $factory = new Factory(); -$resolver = $factory->createCached($config, $loop); +$resolver = $factory->createCached($config); $name = isset($argv[1]) ? $argv[1] : 'www.google.com'; @@ -21,22 +20,20 @@ echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; }, 'printf'); -$loop->addTimer(1.0, function() use ($name, $resolver) { +Loop::addTimer(1.0, function() use ($name, $resolver) { $resolver->resolve($name)->then(function ($ip) use ($name) { echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; }, 'printf'); }); -$loop->addTimer(2.0, function() use ($name, $resolver) { +Loop::addTimer(2.0, function() use ($name, $resolver) { $resolver->resolve($name)->then(function ($ip) use ($name) { echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; }, 'printf'); }); -$loop->addTimer(3.0, function() use ($name, $resolver) { +Loop::addTimer(3.0, function() use ($name, $resolver) { $resolver->resolve($name)->then(function ($ip) use ($name) { echo 'IP for ' . $name . ': ' . $ip . PHP_EOL; }, 'printf'); }); - -$loop->run(); diff --git a/examples/11-all-ips.php b/examples/11-all-ips.php index 042ec379..85a008c1 100644 --- a/examples/11-all-ips.php +++ b/examples/11-all-ips.php @@ -6,15 +6,13 @@ require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); - $config = Config::loadSystemConfigBlocking(); if (!$config->nameservers) { $config->nameservers[] = '8.8.8.8'; } $factory = new Factory(); -$resolver = $factory->create($config, $loop); +$resolver = $factory->create($config); $name = isset($argv[1]) ? $argv[1] : 'www.google.com'; @@ -29,5 +27,3 @@ }, function (Exception $e) use ($name) { echo 'No IPv6 addresses for ' . $name . ': ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/12-all-types.php b/examples/12-all-types.php index 7e2d5bf3..fbb75ca2 100644 --- a/examples/12-all-types.php +++ b/examples/12-all-types.php @@ -8,15 +8,13 @@ require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); - $config = Config::loadSystemConfigBlocking(); if (!$config->nameservers) { $config->nameservers[] = '8.8.8.8'; } $factory = new Factory(); -$resolver = $factory->create($config, $loop); +$resolver = $factory->create($config); $name = isset($argv[1]) ? $argv[1] : 'google.com'; $type = constant('React\Dns\Model\Message::TYPE_' . (isset($argv[2]) ? $argv[2] : 'TXT')); @@ -26,5 +24,3 @@ }, function (Exception $e) { echo $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/13-reverse-dns.php b/examples/13-reverse-dns.php index 015a9cf2..859e3011 100644 --- a/examples/13-reverse-dns.php +++ b/examples/13-reverse-dns.php @@ -6,15 +6,13 @@ require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); - $config = Config::loadSystemConfigBlocking(); if (!$config->nameservers) { $config->nameservers[] = '8.8.8.8'; } $factory = new Factory(); -$resolver = $factory->create($config, $loop); +$resolver = $factory->create($config); $ip = isset($argv[1]) ? $argv[1] : '8.8.8.8'; @@ -33,5 +31,3 @@ }, function (Exception $e) { echo $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/91-query-a-and-aaaa.php b/examples/91-query-a-and-aaaa.php index 185baa06..ccc322de 100644 --- a/examples/91-query-a-and-aaaa.php +++ b/examples/91-query-a-and-aaaa.php @@ -7,8 +7,7 @@ require __DIR__ . '/../vendor/autoload.php'; -$loop = Factory::create(); -$executor = new UdpTransportExecutor('8.8.8.8:53', $loop); +$executor = new UdpTransportExecutor('8.8.8.8:53'); $name = isset($argv[1]) ? $argv[1] : 'www.google.com'; @@ -25,5 +24,3 @@ echo 'IPv6: ' . $answer->data . PHP_EOL; } }, 'printf'); - -$loop->run(); diff --git a/examples/92-query-any.php b/examples/92-query-any.php index 17383cff..ed61a758 100644 --- a/examples/92-query-any.php +++ b/examples/92-query-any.php @@ -11,8 +11,7 @@ require __DIR__ . '/../vendor/autoload.php'; -$loop = Factory::create(); -$executor = new TcpTransportExecutor('8.8.8.8:53', $loop); +$executor = new TcpTransportExecutor('8.8.8.8:53'); $name = isset($argv[1]) ? $argv[1] : 'google.com'; @@ -80,5 +79,3 @@ echo $type . ': ' . $data . PHP_EOL; } }, 'printf'); - -$loop->run(); diff --git a/src/Query/CoopExecutor.php b/src/Query/CoopExecutor.php index 2c6653c3..e3f913ba 100644 --- a/src/Query/CoopExecutor.php +++ b/src/Query/CoopExecutor.php @@ -27,9 +27,8 @@ * $executor = new CoopExecutor( * new RetryExecutor( * new TimeoutExecutor( - * new UdpTransportExecutor($nameserver, $loop), - * 3.0, - * $loop + * new UdpTransportExecutor($nameserver), + * 3.0 * ) * ) * ); diff --git a/src/Query/TcpTransportExecutor.php b/src/Query/TcpTransportExecutor.php index b21e403c..7efeb90b 100644 --- a/src/Query/TcpTransportExecutor.php +++ b/src/Query/TcpTransportExecutor.php @@ -5,6 +5,7 @@ use React\Dns\Model\Message; use React\Dns\Protocol\BinaryDumper; use React\Dns\Protocol\Parser; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise\Deferred; @@ -17,8 +18,7 @@ * The following example looks up the `IPv6` address for `reactphp.org`. * * ```php - * $loop = Factory::create(); - * $executor = new TcpTransportExecutor('8.8.8.8:53', $loop); + * $executor = new TcpTransportExecutor('8.8.8.8:53'); * * $executor->query( * new Query($name, Message::TYPE_AAAA, Message::CLASS_IN) @@ -27,8 +27,6 @@ * echo 'IPv6: ' . $answer->data . PHP_EOL; * } * }, 'printf'); - * - * $loop->run(); * ``` * * See also [example #92](examples). @@ -38,9 +36,8 @@ * * ```php * $executor = new TimeoutExecutor( - * new TcpTransportExecutor($nameserver, $loop), - * 3.0, - * $loop + * new TcpTransportExecutor($nameserver), + * 3.0 * ); * ``` * @@ -66,9 +63,8 @@ * ```php * $executor = new CoopExecutor( * new TimeoutExecutor( - * new TcpTransportExecutor($nameserver, $loop), - * 3.0, - * $loop + * new TcpTransportExecutor($nameserver), + * 3.0 * ) * ); * ``` @@ -132,10 +128,10 @@ class TcpTransportExecutor implements ExecutorInterface private $readPending = false; /** - * @param string $nameserver - * @param LoopInterface $loop + * @param string $nameserver + * @param ?LoopInterface $loop */ - public function __construct($nameserver, LoopInterface $loop) + public function __construct($nameserver, LoopInterface $loop = null) { if (\strpos($nameserver, '[') === false && \substr_count($nameserver, ':') >= 2 && \strpos($nameserver, '://') === false) { // several colons, but not enclosed in square brackets => enclose IPv6 address in square brackets @@ -148,7 +144,7 @@ public function __construct($nameserver, LoopInterface $loop) } $this->nameserver = 'tcp://' . $parts['host'] . ':' . (isset($parts['port']) ? $parts['port'] : 53); - $this->loop = $loop; + $this->loop = $loop ?: Loop::get(); $this->parser = new Parser(); $this->dumper = new BinaryDumper(); } diff --git a/src/Query/TimeoutExecutor.php b/src/Query/TimeoutExecutor.php index e20ff596..15c8c22a 100644 --- a/src/Query/TimeoutExecutor.php +++ b/src/Query/TimeoutExecutor.php @@ -2,6 +2,7 @@ namespace React\Dns\Query; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise\Timer; @@ -11,10 +12,10 @@ final class TimeoutExecutor implements ExecutorInterface private $loop; private $timeout; - public function __construct(ExecutorInterface $executor, $timeout, LoopInterface $loop) + public function __construct(ExecutorInterface $executor, $timeout, LoopInterface $loop = null) { $this->executor = $executor; - $this->loop = $loop; + $this->loop = $loop ?: Loop::get(); $this->timeout = $timeout; } diff --git a/src/Query/UdpTransportExecutor.php b/src/Query/UdpTransportExecutor.php index 0b7e7669..4687109c 100644 --- a/src/Query/UdpTransportExecutor.php +++ b/src/Query/UdpTransportExecutor.php @@ -5,6 +5,7 @@ use React\Dns\Model\Message; use React\Dns\Protocol\BinaryDumper; use React\Dns\Protocol\Parser; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise\Deferred; @@ -18,8 +19,7 @@ * The following example looks up the `IPv6` address for `igor.io`. * * ```php - * $loop = Factory::create(); - * $executor = new UdpTransportExecutor('8.8.8.8:53', $loop); + * $executor = new UdpTransportExecutor('8.8.8.8:53'); * * $executor->query( * new Query($name, Message::TYPE_AAAA, Message::CLASS_IN) @@ -28,8 +28,6 @@ * echo 'IPv6: ' . $answer->data . PHP_EOL; * } * }, 'printf'); - * - * $loop->run(); * ``` * * See also the [fourth example](examples). @@ -39,9 +37,8 @@ * * ```php * $executor = new TimeoutExecutor( - * new UdpTransportExecutor($nameserver, $loop), - * 3.0, - * $loop + * new UdpTransportExecutor($nameserver), + * 3.0 * ); * ``` * @@ -52,9 +49,8 @@ * ```php * $executor = new RetryExecutor( * new TimeoutExecutor( - * new UdpTransportExecutor($nameserver, $loop), - * 3.0, - * $loop + * new UdpTransportExecutor($nameserver), + * 3.0 * ) * ); * ``` @@ -71,9 +67,8 @@ * $executor = new CoopExecutor( * new RetryExecutor( * new TimeoutExecutor( - * new UdpTransportExecutor($nameserver, $loop), - * 3.0, - * $loop + * new UdpTransportExecutor($nameserver), + * 3.0 * ) * ) * ); @@ -100,10 +95,10 @@ final class UdpTransportExecutor implements ExecutorInterface private $maxPacketSize = 512; /** - * @param string $nameserver - * @param LoopInterface $loop + * @param string $nameserver + * @param ?LoopInterface $loop */ - public function __construct($nameserver, LoopInterface $loop) + public function __construct($nameserver, LoopInterface $loop = null) { if (\strpos($nameserver, '[') === false && \substr_count($nameserver, ':') >= 2 && \strpos($nameserver, '://') === false) { // several colons, but not enclosed in square brackets => enclose IPv6 address in square brackets @@ -116,7 +111,7 @@ public function __construct($nameserver, LoopInterface $loop) } $this->nameserver = 'udp://' . $parts['host'] . ':' . (isset($parts['port']) ? $parts['port'] : 53); - $this->loop = $loop; + $this->loop = $loop ?: Loop::get(); $this->parser = new Parser(); $this->dumper = new BinaryDumper(); } diff --git a/src/Resolver/Factory.php b/src/Resolver/Factory.php index 589eb4ae..5fe608cb 100644 --- a/src/Resolver/Factory.php +++ b/src/Resolver/Factory.php @@ -16,6 +16,7 @@ use React\Dns\Query\TcpTransportExecutor; use React\Dns\Query\TimeoutExecutor; use React\Dns\Query\UdpTransportExecutor; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; final class Factory @@ -29,15 +30,15 @@ final class Factory * server will always be used first before falling back to the secondary or * tertiary DNS server. * - * @param Config|string $config DNS Config object (recommended) or single nameserver address - * @param LoopInterface $loop + * @param Config|string $config DNS Config object (recommended) or single nameserver address + * @param ?LoopInterface $loop * @return \React\Dns\Resolver\ResolverInterface * @throws \InvalidArgumentException for invalid DNS server address * @throws \UnderflowException when given DNS Config object has an empty list of nameservers */ - public function create($config, LoopInterface $loop) + public function create($config, LoopInterface $loop = null) { - $executor = $this->decorateHostsFileExecutor($this->createExecutor($config, $loop)); + $executor = $this->decorateHostsFileExecutor($this->createExecutor($config, $loop ?: Loop::get())); return new Resolver($executor); } @@ -52,20 +53,20 @@ public function create($config, LoopInterface $loop) * tertiary DNS server. * * @param Config|string $config DNS Config object (recommended) or single nameserver address - * @param LoopInterface $loop + * @param ?LoopInterface $loop * @param ?CacheInterface $cache * @return \React\Dns\Resolver\ResolverInterface * @throws \InvalidArgumentException for invalid DNS server address * @throws \UnderflowException when given DNS Config object has an empty list of nameservers */ - public function createCached($config, LoopInterface $loop, CacheInterface $cache = null) + public function createCached($config, LoopInterface $loop = null, CacheInterface $cache = null) { // default to keeping maximum of 256 responses in cache unless explicitly given if (!($cache instanceof CacheInterface)) { $cache = new ArrayCache(256); } - $executor = $this->createExecutor($config, $loop); + $executor = $this->createExecutor($config, $loop ?: Loop::get()); $executor = new CachingExecutor($executor, $cache); $executor = $this->decorateHostsFileExecutor($executor); diff --git a/tests/Query/TcpTransportExecutorTest.php b/tests/Query/TcpTransportExecutorTest.php index 322815a8..3315d80f 100644 --- a/tests/Query/TcpTransportExecutorTest.php +++ b/tests/Query/TcpTransportExecutorTest.php @@ -60,6 +60,17 @@ public static function provideDefaultPortProvider() ); } + public function testCtorWithoutLoopShouldAssignDefaultLoop() + { + $executor = new TcpTransportExecutor('127.0.0.1'); + + $ref = new \ReflectionProperty($executor, 'loop'); + $ref->setAccessible(true); + $loop = $ref->getValue($executor); + + $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + } + public function testCtorShouldThrowWhenNameserverAddressIsInvalid() { $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); diff --git a/tests/Query/TimeoutExecutorTest.php b/tests/Query/TimeoutExecutorTest.php index 0df5aa78..a7537053 100644 --- a/tests/Query/TimeoutExecutorTest.php +++ b/tests/Query/TimeoutExecutorTest.php @@ -14,6 +14,10 @@ class TimeoutExecutorTest extends TestCase { + private $loop; + private $wrapped; + private $executor; + /** * @before */ @@ -26,6 +30,17 @@ public function setUpExecutor() $this->executor = new TimeoutExecutor($this->wrapped, 5.0, $this->loop); } + public function testCtorWithoutLoopShouldAssignDefaultLoop() + { + $executor = new TimeoutExecutor($this->executor, 5.0); + + $ref = new \ReflectionProperty($executor, 'loop'); + $ref->setAccessible(true); + $loop = $ref->getValue($executor); + + $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + } + public function testCancellingPromiseWillCancelWrapped() { $cancelled = 0; diff --git a/tests/Query/UdpTransportExecutorTest.php b/tests/Query/UdpTransportExecutorTest.php index 08a4a960..fed613c4 100644 --- a/tests/Query/UdpTransportExecutorTest.php +++ b/tests/Query/UdpTransportExecutorTest.php @@ -60,6 +60,17 @@ public static function provideDefaultPortProvider() ); } + public function testCtorWithoutLoopShouldAssignDefaultLoop() + { + $executor = new UdpTransportExecutor('127.0.0.1'); + + $ref = new \ReflectionProperty($executor, 'loop'); + $ref->setAccessible(true); + $loop = $ref->getValue($executor); + + $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + } + public function testCtorShouldThrowWhenNameserverAddressIsInvalid() { $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); diff --git a/tests/Resolver/FactoryTest.php b/tests/Resolver/FactoryTest.php index 04ae4769..af758b51 100644 --- a/tests/Resolver/FactoryTest.php +++ b/tests/Resolver/FactoryTest.php @@ -12,10 +12,8 @@ class FactoryTest extends TestCase /** @test */ public function createShouldCreateResolver() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); - $factory = new Factory(); - $resolver = $factory->create('8.8.8.8:53', $loop); + $resolver = $factory->create('8.8.8.8:53'); $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); } @@ -375,10 +373,8 @@ public function createShouldThrowWhenConfigHasInvalidNameserver() /** @test */ public function createCachedShouldCreateResolverWithCachingExecutor() { - $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); - $factory = new Factory(); - $resolver = $factory->createCached('8.8.8.8:53', $loop); + $resolver = $factory->createCached('8.8.8.8:53'); $this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver); $executor = $this->getResolverPrivateExecutor($resolver); From 28e5df183f02a5abe939bb4843efbddb775eb4a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 8 Jul 2021 11:27:30 +0200 Subject: [PATCH 2/2] Update to stable reactphp/event-loop v1.2.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0709f1dd..28482f50 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "require": { "php": ">=5.3.0", "react/cache": "^1.0 || ^0.6 || ^0.5", - "react/event-loop": "dev-master#78f7f43 as 1.2.0", + "react/event-loop": "^1.2", "react/promise": "^3.0 || ^2.7 || ^1.2.1", "react/promise-timer": "^1.2" },