1
0
mirror of https://github.com/danog/dns.git synced 2025-01-23 05:51:11 +01:00

Update for amphp/amp v0.15.0

This commit is contained in:
Daniel Lowrey 2014-12-08 10:01:38 -05:00
parent 56fc89cca8
commit aaf718e128
6 changed files with 49 additions and 36 deletions

View File

@ -6,7 +6,7 @@ Asynchronous DNS resolution built on the [Amp](https://github.com/amphp/amp) con
## Examples
**Synchronous Resolution Via `wait()`**
**Synchronous Resolution Via `Amp\wait()`**
```php
<?php
@ -14,7 +14,7 @@ require __DIR__ . '/vendor/autoload.php';
$resolver = new Amp\Dns\Resolver;
$name = 'google.com';
list($address, $type) = $resolver->resolve($name)->wait();
list($address, $type) = Amp\wait($resolver->resolve($name));
printf("%s resolved to %s\n", $name, $address);
```
@ -34,7 +34,8 @@ $promises = [];
foreach ($names as $name) {
$promises[$name] = $resolver->resolve($name);
}
$results = Amp\all($promises)->wait();
$comboPromise = Amp\all($promises);
$results = Amp\wait($comboPromise);
foreach ($results as $name => $resultArray) {
list($addr, $type) = $resultArray;
printf("%s => %s\n", $name, $addr);
@ -65,11 +66,8 @@ Amp\run(function() {
$promises[$name] = $promise;
}
// Flatten multiple promises into a single promise
$comboPromise = Amp\some($promises);
// Yield control until the combo promise resolves
list($errors, $successes) = (yield $comboPromise);
list($errors, $successes) = (yield 'some' => $promises);
foreach ($names as $name) {
echo isset($errors[$name])

View File

@ -13,7 +13,7 @@
],
"require": {
"php": ">=5.4.0",
"amphp/amp": "~0.12",
"amphp/amp": "~0.15.0",
"daverandom/libdns": "~0.2"
},
"require-dev": {

View File

@ -98,7 +98,7 @@ class Client {
ResponseInterpreter $responseInterpreter = null,
Cache $cache = null
) {
$this->reactor = $reactor ?: \Amp\reactor();
$this->reactor = $reactor ?: \Amp\getReactor();
$this->requestBuilder = $requestBuilder ?: new RequestBuilder;
$this->responseInterpreter = $responseInterpreter ?: new ResponseInterpreter;
$this->cache = $cache ?: (new CacheFactory)->select();
@ -129,7 +129,7 @@ class Client {
$this->reactor->enable($this->readWatcherId);
}
$promisor = new Future($this->reactor);
$promisor = new Future;
$id = $this->getNextFreeLookupId();
$this->pendingLookups[$id] = [
'name' => $name,

View File

@ -2,20 +2,21 @@
namespace Amp\Dns\Test;
use Amp\NativeReactor;
use Amp\Dns\AddressModes;
use Amp\Dns\Client;
class ClientTest extends \PHPUnit_Framework_TestCase {
/**
* @expectedException \Amp\Dns\ResolutionException
*/
public function testFailToConnect() {
$client = new Client();
$reactor = new NativeReactor;
$client = new Client($reactor);
$client->setOption(Client::OP_SERVER_ADDRESS, '260.260.260.260');
$promise = $client->resolve('example.com', AddressModes::INET4_ADDR);
$promise->wait();
$result = \Amp\wait($promise, $reactor);
}
/**
@ -23,7 +24,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase {
* but it makes it easier to detect missing important coverage.
*/
public function testSetRequestTime() {
$client = new Client();
$reactor = new NativeReactor;
$client = new Client($reactor);
$client->setOption(Client::OP_MS_REQUEST_TIMEOUT, 1000);
$client->setOption(Client::OP_SERVER_PORT, 53);
}
@ -32,7 +34,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase {
* @expectedException \DomainException
*/
public function testUnknownOptionThrowsException() {
$client = new Client();
$reactor = new NativeReactor;
$client = new Client($reactor);
$client->setOption('foo', 1000);
}
@ -41,9 +44,10 @@ class ClientTest extends \PHPUnit_Framework_TestCase {
* @group internet
*/
public function testSetAddressAfterConnectException() {
$client = new Client();
$reactor = new NativeReactor;
$client = new Client($reactor);
$promise = $client->resolve('google.com', AddressModes::INET4_ADDR);
$promise->wait();
$result = \Amp\wait($promise, $reactor);
$client->setOption(Client::OP_SERVER_ADDRESS, '260.260.260.260');
}
@ -52,9 +56,10 @@ class ClientTest extends \PHPUnit_Framework_TestCase {
* @group internet
*/
public function testSetPortAfterConnectException() {
$client = new Client();
$reactor = new NativeReactor;
$client = new Client($reactor);
$promise = $client->resolve('google.com', AddressModes::INET4_ADDR);
$promise->wait();
$result = \Amp\wait($promise, $reactor);
$client->setOption(Client::OP_SERVER_PORT, 53);
}
@ -63,9 +68,10 @@ class ClientTest extends \PHPUnit_Framework_TestCase {
* @expectedException \Amp\Dns\ResolutionException
*/
public function testNoAnswers() {
$client = new Client();
$reactor = new NativeReactor;
$client = new Client($reactor);
$promise = $client->resolve('googleaiusdisuhdihas.apsidjpasjdisjdajsoidaugiug.com', AddressModes::INET4_ADDR);
$promise->wait();
$result = \Amp\wait($promise, $reactor);
}
/**
@ -73,19 +79,20 @@ class ClientTest extends \PHPUnit_Framework_TestCase {
* zero occurs.
*/
public function testPendingIdOverflow() {
$reactor = new NativeReactor;
$class = new \ReflectionClass('Amp\Dns\Client');
$lookupIdProperty = $class->getProperty("lookupIdCounter");
$requestIdCounterProperty = $class->getProperty("requestIdCounter");
/** @var Client $client */
$client = $class->newInstance();
$client = $class->newInstance($reactor);
$lookupIdProperty->setAccessible(true);
$lookupIdProperty->setValue($client, PHP_INT_MAX);
$requestIdCounterProperty->setAccessible(true);
$requestIdCounterProperty->setValue($client, 65535);
$promise = $client->resolve('google.com', AddressModes::INET4_ADDR);
$promise->wait();
$result = \Amp\wait($promise, $reactor);
$lookupIdCounter = $lookupIdProperty->getValue($client);
$this->assertEquals(0, $lookupIdCounter);

View File

@ -3,7 +3,6 @@
namespace Amp\Dns\Test;
use Amp\NativeReactor;
use Amp\Combinator;
use Amp\Dns\Cache;
use Amp\Dns\Client;
use Amp\Dns\Resolver;
@ -81,7 +80,6 @@ class IntegrationTest extends \PHPUnit_Framework_TestCase {
];
$reactor = new NativeReactor;
$combinator = new Combinator($reactor);
$client = new Client($reactor, null, null, $cache);
$resolver = new Resolver($client);
@ -90,7 +88,9 @@ class IntegrationTest extends \PHPUnit_Framework_TestCase {
$promises[$name] = $resolver->resolve($name);
}
$results = $combinator->all($promises)->wait();
$comboPromise = \Amp\all($promises);
$results = \Amp\wait($comboPromise, $reactor);
foreach ($results as $name => $addrStruct) {
list($addr, $type) = $addrStruct;
$validIP = @inet_pton($addr);
@ -132,7 +132,6 @@ class IntegrationTest extends \PHPUnit_Framework_TestCase {
$mockedCache->makePartial();
$reactor = new NativeReactor;
$combinator = new Combinator($reactor);
$client = new Client($reactor, null, null, $mockedCache);
$resolver = new Resolver($client);
@ -141,7 +140,9 @@ class IntegrationTest extends \PHPUnit_Framework_TestCase {
$promises[$name] = $resolver->resolve($name);
}
$results = $combinator->all($promises)->wait();
$comboPromise = \Amp\all($promises);
$results = \Amp\wait($comboPromise, $reactor);
foreach ($results as $name => $addrStruct) {
list($addr, $type) = $addrStruct;
$validIP = @inet_pton($addr);
@ -156,7 +157,8 @@ class IntegrationTest extends \PHPUnit_Framework_TestCase {
$promises[$name] = $resolver->resolve($name);
}
$results = $combinator->all($promises)->wait();
$comboPromise = \Amp\all($promises);
$results = \Amp\wait($comboPromise, $reactor);
foreach ($results as $name => $addrStruct) {
list($addr, $type) = $addrStruct;
$validIP = @inet_pton($addr);

View File

@ -34,7 +34,8 @@ class ResolverTest extends \PHPUnit_Framework_TestCase {
$tooLongName = $tooLongName.$alphabet; //234
$tooLongName = $tooLongName.$alphabet; //260
$resolver->resolve($tooLongName, AddressModes::PREFER_INET6)->wait();
$promise = $resolver->resolve($tooLongName, AddressModes::PREFER_INET6);
$addrStruct = \Amp\wait($promise, $reactor);
}
/**
@ -44,17 +45,20 @@ class ResolverTest extends \PHPUnit_Framework_TestCase {
*/
public function testUnknownName() {
list($reactor, $resolver) = $this->createResolver();
$resolver->resolve("doesntexist", AddressModes::PREFER_INET6)->wait();
$promise = $resolver->resolve("doesntexist", AddressModes::PREFER_INET6);
$addrStruct = \Amp\wait($promise, $reactor);
}
public function testLocalHostResolution() {
list($reactor, $resolver) = $this->createResolver();
list($addr, $type) = $resolver->resolve("localhost", AddressModes::INET4_ADDR)->wait();
$promise = $resolver->resolve("localhost", AddressModes::INET4_ADDR);
list($addr, $type) = \Amp\wait($promise, $reactor);
$this->assertSame('127.0.0.1', $addr);
$this->assertSame(AddressModes::INET4_ADDR, $type, "Wrong result type - should be INET4_ADDR but got $type");
list($addr, $type) = $resolver->resolve("localhost", AddressModes::PREFER_INET6)->wait();
$promise = $resolver->resolve("localhost", AddressModes::PREFER_INET6);
list($addr, $type) = \Amp\wait($promise, $reactor);
$this->assertSame('::1', $addr);
$this->assertSame(AddressModes::INET6_ADDR, $type, "Wrong result type - should be INET6_ADDR but got $type");
}
@ -63,11 +67,13 @@ class ResolverTest extends \PHPUnit_Framework_TestCase {
$hostsFile = __DIR__ . '/fixtures/resolverTest.txt';
list($reactor, $resolver) = $this->createResolver($hostsFile);
list($addr, $type) = $resolver->resolve("host1.example.com", AddressModes::INET4_ADDR)->wait();
$promise = $resolver->resolve("host1.example.com", AddressModes::INET4_ADDR);
list($addr, $type) = \Amp\wait($promise, $reactor);
$this->assertSame('192.168.1.1', $addr);
$this->assertSame(AddressModes::INET4_ADDR, $type);
list($addr, $type) = $resolver->resolve("resolvertest", AddressModes::INET4_ADDR)->wait();
$promise = $resolver->resolve("resolvertest", AddressModes::INET4_ADDR);
list($addr, $type) = \Amp\wait($promise, $reactor);
$this->assertSame('192.168.1.3', $addr);
$this->assertSame(AddressModes::INET4_ADDR, $type);
}