Added Lock Unittests & Bugfixings

Added Redis Lock Unittests
Added Memcached Lock Unittests

Fixed a bug in dba
Fixed a bug in RedisLock
This commit is contained in:
Philipp Holzer 2018-07-07 16:15:03 +02:00
parent e719a25082
commit 1ffe0cfd81
No known key found for this signature in database
GPG key ID: 58160D7D6AF942B6
10 changed files with 114 additions and 70 deletions

View file

@ -6,7 +6,7 @@ namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Cache\ArrayCache;
use Friendica\Core\Lock\CacheLockDriver;
class CacheLockDriverTest extends LockTest
class ArrayCacheLockDriverTest extends LockTest
{
/**
* @var \Friendica\Core\Cache\IMemoryCacheDriver
@ -24,4 +24,4 @@ class CacheLockDriverTest extends LockTest
$this->cache->clear();
parent::tearDown();
}
}
}

View file

@ -11,49 +11,6 @@ use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
class DatabaseLockDriverTest extends LockTest
{
use TestCaseTrait;
/**
* Get database connection.
*
* This function is executed before each test in order to get a database connection that can be used by tests.
* If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
*
* If it could not connect to the database, the test is skipped.
*
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
* @see https://phpunit.de/manual/5.7/en/database.html
*/
protected function getConnection()
{
if (!dba::$connected) {
dba::connect('localhost', getenv('USER'), getenv('PASS'), getenv('DB'));
if (dba::$connected) {
$app = get_app();
// We need to do this in order to disable logging
$app->module = 'install';
// Create database structure
DBStructure::update(false, true, true);
} else {
$this->markTestSkipped('Could not connect to the database.');
}
}
return $this->createDefaultDBConnection(dba::get_db(), getenv('DB'));
}
/**
* Get dataset to populate the database with.
* @return YamlDataSet
* @see https://phpunit.de/manual/5.7/en/database.html
*/
protected function getDataSet()
{
return new YamlDataSet(__DIR__ . '/../../../datasets/api.yml');
}
protected function getInstance()
{
return new DatabaseLockDriver();
@ -64,4 +21,4 @@ class DatabaseLockDriverTest extends LockTest
dba::delete('locks', [ 'id > 0']);
parent::tearDown();
}
}
}

View file

@ -4,9 +4,10 @@ namespace Friendica\Test\src\Core\Lock;
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Test\DatabaseTest;
use PHPUnit\Framework\TestCase;
abstract class LockTest extends TestCase
abstract class LockTest extends DatabaseTest
{
/**
* @var \Friendica\Core\Lock\ILockDriver
@ -58,6 +59,10 @@ abstract class LockTest extends TestCase
$this->instance->acquireLock('bar', 1);
$this->instance->acquireLock('nice', 1);
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('bar'));
$this->assertTrue($this->instance->isLocked('nice'));
$this->instance->releaseAll();
$this->assertFalse($this->instance->isLocked('foo'));
@ -72,9 +77,13 @@ abstract class LockTest extends TestCase
$this->instance->releaseLock('foo');
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('bar'));
$this->assertTrue($this->instance->isLocked('nice'));
$this->instance->releaseAll();
$this->assertFalse($this->instance->isLocked('bar'));
$this->assertFalse($this->instance->isLocked('#/$%§'));
$this->assertFalse($this->instance->isLocked('nice'));
}
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Cache\CacheDriverFactory;
use Friendica\Core\Lock\CacheLockDriver;
class MemcachedCacheLockDriverTest extends LockTest
{
/**
* @var \Friendica\Core\Cache\IMemoryCacheDriver
*/
private $cache;
protected function getInstance()
{
if (class_exists('Memcached')) {
try {
$this->cache = CacheDriverFactory::create('memcached');
} catch (\Exception $exception) {
print "Redis - TestCase failed: " . $exception->getMessage();
throw new \Exception();
}
return new CacheLockDriver($this->cache);
} else {
$this->markTestSkipped('Redis driver isn\'t available');
return null;
}
}
public function tearDown()
{
if (class_exists('Redis')) {
$this->cache->clear();
}
parent::tearDown();
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Cache\CacheDriverFactory;
use Friendica\Core\Lock\CacheLockDriver;
class RedisCacheLockDriverTest extends LockTest
{
/**
* @var \Friendica\Core\Cache\IMemoryCacheDriver
*/
private $cache;
protected function getInstance()
{
if (class_exists('Redis')) {
try {
$this->cache = CacheDriverFactory::create('redis');
} catch (\Exception $exception) {
print "Redis - TestCase failed: " . $exception->getMessage();
throw new \Exception();
}
return new CacheLockDriver($this->cache);
} else {
$this->markTestSkipped('Redis driver isn\'t available');
return null;
}
}
public function tearDown()
{
if (class_exists('Redis')) {
$this->cache->clear();
}
parent::tearDown();
}
}

View file

@ -23,4 +23,4 @@ class SemaphoreLockDriverTest extends LockTest
$this->semaphoreLockDriver->releaseAll();
parent::tearDown();
}
}
}