Refactoring DBA-mocking tests

- Reducing DB-dependencies
- Creating DB-cache mocks
- Creating DB-lock mocks
- Switching to mocked dependencies for Cache/Lock/App
This commit is contained in:
Philipp Holzer 2019-01-30 20:26:17 +01:00
parent f7e95f65b1
commit 433d6abe8c
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
21 changed files with 848 additions and 193 deletions

View file

@ -2,8 +2,9 @@
namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Cache;
use Friendica\Core\Lock\DatabaseLockDriver;
use Friendica\Database\DBA;
use Friendica\Test\Util\DbaLockMockTrait;
/**
* @runTestsInSeparateProcesses
@ -11,14 +12,96 @@ use Friendica\Database\DBA;
*/
class DatabaseLockDriverTest extends LockTest
{
protected function getInstance()
use DbaLockMockTrait;
protected $pid = 123;
protected function setUp()
{
return new DatabaseLockDriver();
$this->mockConnected();
$this->mockConnect();
$this->mockReleaseAll($this->pid, 2);
parent::setUp();
}
public function tearDown()
protected function getInstance()
{
DBA::delete('locks', [ 'id > 0']);
parent::tearDown();
return new DatabaseLockDriver($this->pid);
}
public function testLock()
{
$this->mockIsLocked('foo', false, $this->startTime, 1);
$this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
$this->mockIsLocked('foo', true, $this->startTime, 1);
$this->mockIsLocked('bar', false, $this->startTime, 1);
parent::testLock();
}
public function testDoubleLock()
{
$this->mockIsLocked('foo', false, $this->startTime, 1);
$this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
$this->mockIsLocked('foo', true, $this->startTime, 1);
$this->mockAcquireLock('foo', Cache::FIVE_MINUTES, true, $this->pid, true, $this->startTime, 1);
parent::testDoubleLock();
}
public function testReleaseLock()
{
$this->mockIsLocked('foo', false, $this->startTime, 1);
$this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
$this->mockIsLocked('foo', true, $this->startTime, 1);
$this->mockReleaseLock('foo', $this->pid, 1);
$this->mockIsLocked('foo', false, $this->startTime, 1);
parent::testReleaseLock();
}
public function testReleaseAll()
{
$this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
$this->mockAcquireLock('bar', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
$this->mockAcquireLock('nice', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
$this->mockIsLocked('foo', true, $this->startTime, 1);
$this->mockIsLocked('bar', true, $this->startTime, 1);
$this->mockIsLocked('nice', true, $this->startTime, 1);
$this->mockReleaseAll($this->pid, 1);
$this->mockIsLocked('foo', false, $this->startTime, 1);
$this->mockIsLocked('bar', false, $this->startTime, 1);
$this->mockIsLocked('nice', false, $this->startTime, 1);
parent::testReleaseAll();
}
public function testReleaseAfterUnlock()
{
$this->mockIsLocked('foo', false, $this->startTime, 1);
$this->mockIsLocked('bar', false, $this->startTime, 1);
$this->mockIsLocked('nice', false, $this->startTime, 1);
$this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
$this->mockAcquireLock('bar', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
$this->mockAcquireLock('nice', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
$this->mockReleaseLock('foo', $this->pid, 1);
$this->mockIsLocked('foo', false, $this->startTime, 1);
$this->mockIsLocked('bar', true, $this->startTime, 1);
$this->mockIsLocked('nice', true, $this->startTime, 1);
$this->mockReleaseAll($this->pid, 1);
$this->mockIsLocked('bar', false, $this->startTime, 1);
$this->mockIsLocked('nice', false, $this->startTime, 1);
parent::testReleaseAfterUnlock();
}
}

View file

@ -2,12 +2,20 @@
namespace Friendica\Test\src\Core\Lock;
use Friendica\BaseObject;
use Friendica\Core\Config;
use Friendica\Test\DatabaseTest;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\AppMockTrait;
use Friendica\Test\Util\VFSTrait;
abstract class LockTest extends DatabaseTest
abstract class LockTest extends MockedTest
{
use VFSTrait;
use AppMockTrait;
/**
* @var int Start time of the mock (used for time operations)
*/
protected $startTime = 1417011228;
/**
* @var \Friendica\Core\Lock\ILockDriver
*/
@ -22,20 +30,24 @@ abstract class LockTest extends DatabaseTest
$this->instance->releaseAll();
// Reusable App object
$this->app = BaseObject::getApp();
$this->setUpVfsDir();
$this->mockApp($this->root);
$this->app
->shouldReceive('getHostname')
->andReturn('friendica.local');
// 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');
$this->mockConfigGet('config', 'hostname', 'localhost');
$this->mockConfigGet('system', 'throttle_limit_day', 100);
$this->mockConfigGet('system', 'throttle_limit_week', 100);
$this->mockConfigGet('system', 'throttle_limit_month', 100);
$this->mockConfigGet('system', 'theme', 'system_theme');
}
protected function tearDown()
{
parent::tearDown();
$this->instance->releaseAll();
parent::tearDown();
}
/**

View file

@ -15,6 +15,9 @@ class MemcacheCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->mockConfigGet('system', 'memcache_host', 'localhost', 1);
$this->mockConfigGet('system', 'memcache_port', 11211, 1);
return new CacheLockDriver(CacheDriverFactory::create('memcache'));
}
}

View file

@ -15,6 +15,8 @@ class MemcachedCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->mockConfigGet('system', 'memcached_hosts', [0 => 'localhost, 11211']);
return new CacheLockDriver(CacheDriverFactory::create('memcached'));
}
}

View file

@ -15,7 +15,9 @@ class RedisCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
return new CacheLockDriver(CacheDriverFactory::create('redis'));
$this->mockConfigGet('system', 'redis_host', 'localhost', 1);
$this->mockConfigGet('system', 'redis_port', null, 1);
return new CacheLockDriver(CacheDriverFactory::create('redis'));
}
}

View file

@ -10,6 +10,14 @@ use Friendica\Core\Lock\SemaphoreLockDriver;
*/
class SemaphoreLockDriverTest extends LockTest
{
public function setUp()
{
parent::setUp();
$this->app->shouldReceive('getHostname')->andReturn('friendica.local');
$this->mockConfigGet('system', 'temppath', '/tmp/');
}
protected function getInstance()
{
return new SemaphoreLockDriver();