Adding multihost - locking

Adding Unit-Tests for it
This commit is contained in:
Philipp Holzer 2018-07-04 23:37:22 +02:00
parent b07dfbb03f
commit aac94d1d74
No known key found for this signature in database
GPG key ID: 58160D7D6AF942B6
22 changed files with 741 additions and 154 deletions

View file

@ -0,0 +1,27 @@
<?php
namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Cache\ArrayCache;
use Friendica\Core\Lock\CacheLockDriver;
class CacheLockDriverTest extends LockTest
{
/**
* @var \Friendica\Core\Cache\IMemoryCacheDriver
*/
private $cache;
protected function getInstance()
{
$this->cache = new ArrayCache();
return new CacheLockDriver($this->cache);
}
public function tearDown()
{
$this->cache->clear();
parent::tearDown();
}
}

View file

@ -0,0 +1,67 @@
<?php
namespace Friendica\Test\src\Core\Lock;
use dba;
use Friendica\Core\Lock\DatabaseLockDriver;
use Friendica\Database\DBStructure;
use PHPUnit\DbUnit\DataSet\YamlDataSet;
use PHPUnit\DbUnit\TestCaseTrait;
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();
}
public function tearDown()
{
dba::delete('locks', [ 'id > 0']);
parent::tearDown();
}
}

View file

@ -0,0 +1,80 @@
<?php
namespace Friendica\Test\src\Core\Lock;
use Friendica\App;
use Friendica\Core\Config;
use PHPUnit\Framework\TestCase;
abstract class LockTest extends TestCase
{
/**
* @var \Friendica\Core\Lock\ILockDriver
*/
protected $instance;
abstract protected function getInstance();
protected function setUp()
{
global $a;
parent::setUp();
$this->instance = $this->getInstance();
// Reusable App object
$this->app = new App(__DIR__.'/../');
$a = $this->app;
// Default config
Config::set('config', 'hostname', 'localhost');
Config::set('system', 'throttle_limit_day', 100);
Config::set('system', 'throttle_limit_week', 100);
Config::set('system', 'throttle_limit_month', 100);
Config::set('system', 'theme', 'system_theme');
}
public function testLock() {
$this->instance->acquireLock('foo', 1);
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertFalse($this->instance->isLocked('bar'));
}
public function testDoubleLock() {
$this->instance->acquireLock('foo', 1);
$this->assertTrue($this->instance->isLocked('foo'));
// We already locked it
$this->assertTrue($this->instance->acquireLock('foo', 1));
}
public function testReleaseLock() {
$this->instance->acquireLock('foo', 1);
$this->assertTrue($this->instance->isLocked('foo'));
$this->instance->releaseLock('foo');
$this->assertFalse($this->instance->isLocked('foo'));
}
public function testReleaseAll() {
$this->instance->acquireLock('foo', 1);
$this->instance->acquireLock('bar', 1);
$this->instance->acquireLock('#/$%§', 1);
$this->instance->releaseAll();
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertFalse($this->instance->isLocked('bar'));
$this->assertFalse($this->instance->isLocked('#/$%§'));
}
public function testReleaseAfterUnlock() {
$this->instance->acquireLock('foo', 1);
$this->instance->acquireLock('bar', 1);
$this->instance->acquireLock('#/$%§', 1);
$this->instance->releaseLock('foo');
$this->instance->releaseAll();
$this->assertFalse($this->instance->isLocked('bar'));
$this->assertFalse($this->instance->isLocked('#/$%§'));
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Lock\SemaphoreLockDriver;
class SemaphoreLockDriverTest extends LockTest
{
protected function getInstance()
{
return new SemaphoreLockDriver();
}
}