mirror of
https://github.com/danog/dns.git
synced 2025-01-22 21:41:11 +01:00
Changed to project standard bracket placement. Moved AddrCache to Addr\Cache. Refactored to use 'use', added not about running tests to readme. Moved travis badge to point at main repo.
This commit is contained in:
parent
a50369e71e
commit
ca150f4093
13
README.md
13
README.md
@ -4,4 +4,15 @@ Addr
|
||||
Asynchronous DNS resolver using [Alert](https://github.com/rdlowrey/Alert).
|
||||
|
||||
|
||||
[![Build Status](https://travis-ci.org/Danack/Addr.svg?branch=AddingTestsAndActualCaches)](https://travis-ci.org/Danack/Addr)
|
||||
Tests
|
||||
=====
|
||||
|
||||
[![Build Status](https://travis-ci.org/DaveRandom/Addr.svg?branch=master)](https://travis-ci.org/DaveRandom/Addr)
|
||||
|
||||
Tests can be run from the command line using:
|
||||
|
||||
`php vendor/bin/phpunit -c test/phpunit.xml`
|
||||
|
||||
or to exlude tests that require a working internet connection:
|
||||
|
||||
`php vendor/bin/phpunit -c test/phpunit.xml --exclude-group internet`
|
||||
|
@ -24,8 +24,7 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Addr\\": "lib/Addr/",
|
||||
"AddrCache\\": "lib/AddrCache/"
|
||||
"Addr\\": "lib/Addr/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
2
composer.lock
generated
2
composer.lock
generated
@ -3,7 +3,7 @@
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
|
||||
],
|
||||
"hash": "6ac53a76066ea6f5b5a4b6621ca23adb",
|
||||
"hash": "7abdbee16c1b23c04cf43ef2bcde99fe",
|
||||
"packages": [
|
||||
{
|
||||
"name": "daverandom/libdns",
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace AddrCache;
|
||||
namespace Addr;
|
||||
|
||||
interface Cache {
|
||||
|
@ -1,14 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace AddrCache;
|
||||
namespace Addr\Cache;
|
||||
|
||||
use Addr\Cache;
|
||||
|
||||
class APCCache implements \AddrCache\Cache {
|
||||
class APCCache implements Cache {
|
||||
|
||||
/**
|
||||
* @param string $prefix A prefix to prepend to all keys.
|
||||
*/
|
||||
function __construct($prefix = 'AddrCache\Cache\APCCache') {
|
||||
function __construct($prefix = 'AddrCache\Cache\APCCache')
|
||||
{
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
@ -22,7 +24,8 @@ class APCCache implements \AddrCache\Cache {
|
||||
* @param $key
|
||||
* @return array
|
||||
*/
|
||||
public function get($key) {
|
||||
public function get($key)
|
||||
{
|
||||
$key = $this->prefix.$key;
|
||||
$value = apc_fetch($key, $success);
|
||||
|
||||
@ -39,7 +42,8 @@ class APCCache implements \AddrCache\Cache {
|
||||
* @param $value
|
||||
* @param null $ttl
|
||||
*/
|
||||
public function store($key, $value, $ttl = null) {
|
||||
public function store($key, $value, $ttl = null)
|
||||
{
|
||||
$key = $this->prefix.$key;
|
||||
apc_store($key, $value, $ttl);
|
||||
}
|
||||
@ -48,7 +52,8 @@ class APCCache implements \AddrCache\Cache {
|
||||
* Deletes an entry from the cache.
|
||||
* @param $key
|
||||
*/
|
||||
public function delete($key) {
|
||||
public function delete($key)
|
||||
{
|
||||
$key = $this->prefix.$key;
|
||||
apc_delete($key);
|
||||
}
|
@ -1,10 +1,11 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace AddrCache;
|
||||
namespace Addr\Cache;
|
||||
|
||||
use Addr\Cache;
|
||||
|
||||
class MemoryCache implements \AddrCache\Cache {
|
||||
class MemoryCache implements Cache {
|
||||
|
||||
const MAX_TTL = 31536000;//1 year
|
||||
|
||||
@ -20,7 +21,8 @@ class MemoryCache implements \AddrCache\Cache {
|
||||
* @param $key
|
||||
* @return array
|
||||
*/
|
||||
public function get($key) {
|
||||
public function get($key)
|
||||
{
|
||||
if (array_key_exists($key, $this->valueAndTTLArray) == false) {
|
||||
return [false, null];
|
||||
}
|
||||
@ -41,7 +43,8 @@ class MemoryCache implements \AddrCache\Cache {
|
||||
* @param $value
|
||||
* @param null $ttl
|
||||
*/
|
||||
public function store($key, $value, $ttl = null) {
|
||||
public function store($key, $value, $ttl = null)
|
||||
{
|
||||
if ($ttl === null) {
|
||||
$ttl = self::MAX_TTL;
|
||||
}
|
||||
@ -53,7 +56,8 @@ class MemoryCache implements \AddrCache\Cache {
|
||||
* Deletes an entry from the cache.
|
||||
* @param $key
|
||||
*/
|
||||
public function delete($key) {
|
||||
public function delete($key)
|
||||
{
|
||||
unset($this->valueAndTTLArray[$key]);
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace AddrCache;
|
||||
namespace Addr\Cache;
|
||||
|
||||
use Addr\Cache;
|
||||
use Predis\Client as RedisClient;
|
||||
|
||||
class RedisCache implements \AddrCache\Cache {
|
||||
|
||||
class RedisCache implements Cache {
|
||||
|
||||
/**
|
||||
* @var RedisClient
|
||||
@ -32,7 +34,8 @@ END;
|
||||
* set via the redis client, in which case you may wish to pass an empty string
|
||||
* as $prefixKey
|
||||
*/
|
||||
function __construct(RedisClient $redisClient, $prefixKey = 'AddrCache\Cache\RedisCache') {
|
||||
function __construct(RedisClient $redisClient, $prefixKey = 'AddrCache\Cache\RedisCache')
|
||||
{
|
||||
$this->redisClient = $redisClient;
|
||||
$this->prefix = $prefixKey;
|
||||
}
|
||||
@ -44,7 +47,8 @@ END;
|
||||
* @param $value
|
||||
* @param null $ttl
|
||||
*/
|
||||
public function store($key, $value, $ttl = null) {
|
||||
public function store($key, $value, $ttl = null)
|
||||
{
|
||||
$key = $this->prefix.$key;
|
||||
$ttl = intval($ttl);
|
||||
if ($ttl > 0) {
|
||||
@ -66,7 +70,8 @@ END;
|
||||
* @param $key
|
||||
* @return array
|
||||
*/
|
||||
public function get($key) {
|
||||
public function get($key)
|
||||
{
|
||||
$key = $this->prefix.$key;
|
||||
list($wasHit, $value) = $this->redisClient->eval(self::getLuaScript, 1, $key);
|
||||
if ($wasHit) {
|
||||
@ -80,7 +85,8 @@ END;
|
||||
/**
|
||||
* @param $key
|
||||
*/
|
||||
public function delete($key) {
|
||||
public function delete($key)
|
||||
{
|
||||
$key = $this->prefix.$key;
|
||||
$this->redisClient->del([$key]);
|
||||
}
|
@ -22,7 +22,7 @@ class Client
|
||||
private $responseInterpreter;
|
||||
|
||||
/**
|
||||
* @var \AddrCache\Cache
|
||||
* @var \Addr\Cache
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
@ -72,7 +72,7 @@ class Client
|
||||
* @param Reactor $reactor
|
||||
* @param RequestBuilder $requestBuilder
|
||||
* @param ResponseInterpreter $responseInterpreter
|
||||
* @param \AddrCache\Cache $cache
|
||||
* @param Cache $cache
|
||||
* @param string $serverAddress
|
||||
* @param int $serverPort
|
||||
* @param int $requestTimeout
|
||||
@ -82,7 +82,7 @@ class Client
|
||||
Reactor $reactor,
|
||||
RequestBuilder $requestBuilder,
|
||||
ResponseInterpreter $responseInterpreter,
|
||||
\AddrCache\Cache $cache = null,
|
||||
Cache $cache = null,
|
||||
$serverAddress = null,
|
||||
$serverPort = null,
|
||||
$requestTimeout = null
|
||||
|
@ -17,7 +17,7 @@ class ResolverFactory
|
||||
* @param string $serverAddr
|
||||
* @param int $serverPort
|
||||
* @param int $requestTimeout
|
||||
* @param \AddrCache\Cache $cache
|
||||
* @param Cache $cache
|
||||
* @param string $hostsFilePath
|
||||
* @return Resolver
|
||||
*/
|
||||
@ -26,11 +26,11 @@ class ResolverFactory
|
||||
$serverAddr = null,
|
||||
$serverPort = null,
|
||||
$requestTimeout = null,
|
||||
\AddrCache\Cache $cache = null,
|
||||
Cache $cache = null,
|
||||
$hostsFilePath = null
|
||||
) {
|
||||
$nameValidator = new NameValidator;
|
||||
$cache = $cache ?: new \AddrCache\MemoryCache;
|
||||
$cache = $cache ?: new Cache\MemoryCache;
|
||||
|
||||
$client = new Client(
|
||||
$reactor,
|
||||
|
@ -5,6 +5,14 @@ namespace AddrTest;
|
||||
use Addr\ResolverFactory,
|
||||
Alert\ReactorFactory;
|
||||
|
||||
use Addr\Cache;
|
||||
use Addr\Cache\APCCache;
|
||||
use Addr\Cache\MemoryCache;
|
||||
use Addr\Cache\RedisCache;
|
||||
use Predis\Client as RedisClient;
|
||||
use Predis\Connection\ConnectionException as RedisConnectionException;
|
||||
|
||||
|
||||
class AddrTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
|
||||
@ -17,11 +25,11 @@ class AddrTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
public static function setUpBeforeClass() {
|
||||
try {
|
||||
$predisClient = new \Predis\Client(self::$redisParameters, []);
|
||||
$predisClient = new RedisClient(self::$redisParameters, []);
|
||||
$predisClient->ping();
|
||||
//It's connected
|
||||
}
|
||||
catch (\Predis\Connection\ConnectionException $ce) {
|
||||
catch (RedisConnectionException $rce) {
|
||||
self::$redisEnabled = false;
|
||||
}
|
||||
}
|
||||
@ -31,7 +39,7 @@ class AddrTest extends \PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
function testWithMemoryCache() {
|
||||
$memoryCache = new \AddrCache\MemoryCache();
|
||||
$memoryCache = new MemoryCache();
|
||||
$this->basicRun($memoryCache);
|
||||
}
|
||||
|
||||
@ -40,7 +48,7 @@ class AddrTest extends \PHPUnit_Framework_TestCase {
|
||||
*/
|
||||
function testWithApcCache() {
|
||||
$prefix = time().uniqid('CacheTest');
|
||||
$apcCache = new \AddrCache\APCCache($prefix);
|
||||
$apcCache = new APCCache($prefix);
|
||||
$this->basicRun($apcCache);
|
||||
}
|
||||
|
||||
@ -53,14 +61,14 @@ class AddrTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
$prefix = time().'_'.uniqid('CacheTest');
|
||||
try {
|
||||
$predisClient = new \Predis\Client(self::$redisParameters, []);
|
||||
$redisClient = new RedisClient(self::$redisParameters, []);
|
||||
}
|
||||
catch (\Predis\Connection\ConnectionException $ce) {
|
||||
catch (RedisConnectionException $rce) {
|
||||
$this->markTestIncomplete("Could not connect to Redis server, cannot test redis cache.");
|
||||
return;
|
||||
}
|
||||
|
||||
$redisCache = new \AddrCache\RedisCache($predisClient, $prefix);
|
||||
$redisCache = new RedisCache($redisClient, $prefix);
|
||||
$this->basicRun($redisCache);
|
||||
}
|
||||
|
||||
@ -68,7 +76,7 @@ class AddrTest extends \PHPUnit_Framework_TestCase {
|
||||
/**
|
||||
* @group internet
|
||||
*/
|
||||
function basicRun(\AddrCache\Cache $cache = null) {
|
||||
function basicRun(\Addr\Cache $cache = null) {
|
||||
$names = [
|
||||
'google.com',
|
||||
'github.com',
|
||||
@ -114,3 +122,8 @@ class AddrTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -2,6 +2,14 @@
|
||||
|
||||
namespace AddrTest;
|
||||
|
||||
|
||||
use Addr\Cache;
|
||||
use Addr\Cache\APCCache;
|
||||
use Addr\Cache\MemoryCache;
|
||||
use Addr\Cache\RedisCache;
|
||||
use Predis\Client as RedisClient;
|
||||
use Predis\Connection\ConnectionException as RedisConnectionException;
|
||||
|
||||
class CacheTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
static private $redisEnabled = true;
|
||||
@ -13,11 +21,11 @@ class CacheTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
public static function setUpBeforeClass() {
|
||||
try {
|
||||
$predisClient = new \Predis\Client(self::$redisParameters, []);
|
||||
$predisClient = new RedisClient(self::$redisParameters, []);
|
||||
$predisClient->ping();
|
||||
//It's connected
|
||||
}
|
||||
catch (\Predis\Connection\ConnectionException $ce) {
|
||||
catch (RedisConnectionException $rce) {
|
||||
self::$redisEnabled = false;
|
||||
}
|
||||
}
|
||||
@ -29,7 +37,7 @@ class CacheTest extends \PHPUnit_Framework_TestCase {
|
||||
*/
|
||||
function testCacheWorks() {
|
||||
|
||||
$mock = \Mockery::mock('AddrCache\Cache');
|
||||
$mock = \Mockery::mock('Addr\Cache');
|
||||
|
||||
$cacheValues = [];
|
||||
|
||||
@ -70,7 +78,7 @@ class CacheTest extends \PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
$prefix = time().uniqid('CacheTest');
|
||||
$apcCache = new \AddrCache\APCCache($prefix);
|
||||
$apcCache = new APCCache($prefix);
|
||||
$this->runCacheTest($apcCache);
|
||||
}
|
||||
|
||||
@ -88,26 +96,26 @@ class CacheTest extends \PHPUnit_Framework_TestCase {
|
||||
try {
|
||||
$predisClient = new \Predis\Client(self::$redisParameters, []);
|
||||
}
|
||||
catch (\Predis\Connection\ConnectionException $ce) {
|
||||
catch (RedisConnectionException $rce) {
|
||||
$this->markTestIncomplete("Could not connect to Redis server, cannot test redis cache.");
|
||||
return;
|
||||
}
|
||||
|
||||
$redisCache = new \AddrCache\RedisCache($predisClient, $prefix);
|
||||
$redisCache = new RedisCache($predisClient, $prefix);
|
||||
$this->runCacheTest($redisCache);
|
||||
}
|
||||
|
||||
|
||||
function testMemoryCache() {
|
||||
$memoryCache = new \AddrCache\MemoryCache;
|
||||
$memoryCache = new MemoryCache;
|
||||
$this->runCacheTest($memoryCache);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the actual test against an instance of a cache.
|
||||
* @param \AddrCache\Cache $cache
|
||||
* @param \Addr\Cache $cache
|
||||
*/
|
||||
function runCacheTest(\AddrCache\Cache $cache) {
|
||||
function runCacheTest(Cache $cache) {
|
||||
$key = 'TestKey';
|
||||
$value = '12345';
|
||||
$secondValue = '54321';
|
||||
@ -141,7 +149,7 @@ class CacheTest extends \PHPUnit_Framework_TestCase {
|
||||
$key = "TestKey";
|
||||
$value = '12345';
|
||||
|
||||
$memoryCache = new \AddrCache\MemoryCache;
|
||||
$memoryCache = new MemoryCache;
|
||||
|
||||
//A TTL of zero should be expired instantly
|
||||
$memoryCache->store($key, $value, 0);
|
||||
|
Loading…
x
Reference in New Issue
Block a user