Refactor Cache/Lock to DICE

- Refactor Cache classes
- Refactor Lock classes
- Improved test speed (removed some seperate class annotations)
This commit is contained in:
Philipp Holzer 2019-08-03 20:48:56 +02:00
parent b95d4f41b9
commit d56bd28a07
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
40 changed files with 766 additions and 621 deletions

View file

@ -2,7 +2,6 @@
namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Cache\APCuCache;
use Friendica\Core\Lock\CacheLockDriver;
@ -19,6 +18,6 @@ class APCuCacheLockDriverTest extends LockTest
protected function getInstance()
{
return new CacheLockDriver(new APCuCache());
return new CacheLockDriver(new APCuCache('localhost'));
}
}

View file

@ -2,7 +2,6 @@
namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Cache\ArrayCache;
use Friendica\Core\Lock\CacheLockDriver;
@ -10,7 +9,7 @@ class ArrayCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
return new CacheLockDriver(new ArrayCache());
return new CacheLockDriver(new ArrayCache('localhost'));
}
public function testLockTTL()

View file

@ -2,117 +2,42 @@
namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Cache;
use Friendica\Core\Lock\DatabaseLockDriver;
use Friendica\Test\Util\DbaLockMockTrait;
use Friendica\Factory\ConfigFactory;
use Friendica\Test\DatabaseTestTrait;
use Friendica\Test\Util\Database\StaticDatabase;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\ConfigFileLoader;
use Friendica\Util\Profiler;
use Psr\Log\NullLogger;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class DatabaseLockDriverTest extends LockTest
{
use DbaLockMockTrait;
use VFSTrait;
use DatabaseTestTrait;
protected $pid = 123;
protected function setUp()
{
$this->mockConnected();
$this->mockConnect();
$this->mockReleaseAll($this->pid, 2);
$this->setUpVfsDir();
parent::setUp();
}
protected function getInstance()
{
return new DatabaseLockDriver($this->pid);
}
$logger = new NullLogger();
$profiler = \Mockery::mock(Profiler::class);
$profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
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);
// load real config to avoid mocking every config-entry which is related to the Database class
$configFactory = new ConfigFactory();
$loader = new ConfigFileLoader($this->root->url());
$configCache = $configFactory->createCache($loader);
parent::testLock();
}
$dba = new StaticDatabase($configCache, $profiler, $logger);
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();
}
public function testReleaseWitTTL()
{
$this->mockIsLocked('test', false, $this->startTime, 1);
$this->mockAcquireLock('test', 10, false, $this->pid, false, $this->startTime, 1);
$this->mockIsLocked('test', true, $this->startTime, 1);
$this->mockReleaseLock('test', $this->pid, 1);
$this->mockIsLocked('test', false, $this->startTime, 1);
parent::testReleaseWitTTL();
return new DatabaseLockDriver($dba, $this->pid);
}
}

View file

@ -3,16 +3,9 @@
namespace Friendica\Test\src\Core\Lock;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\AppMockTrait;
use Friendica\Test\Util\VFSTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
abstract class LockTest extends MockedTest
{
use VFSTrait;
use AppMockTrait;
/**
* @var int Start time of the mock (used for time operations)
*/
@ -27,19 +20,8 @@ abstract class LockTest extends MockedTest
protected function setUp()
{
// Reusable App object
$this->setUpVfsDir();
$this->mockApp($this->root);
$this->app
->shouldReceive('getHostname')
->andReturn('friendica.local');
$logger = new NullLogger();
$this->dice->shouldReceive('create')
->with(LoggerInterface::class)
->andReturn($logger);
parent::setUp();
$this->instance = $this->getInstance();
$this->instance->releaseAll();
}
@ -53,7 +35,8 @@ abstract class LockTest extends MockedTest
/**
* @small
*/
public function testLock() {
public function testLock()
{
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->isLocked('foo'));
@ -63,7 +46,8 @@ abstract class LockTest extends MockedTest
/**
* @small
*/
public function testDoubleLock() {
public function testDoubleLock()
{
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->isLocked('foo'));
@ -74,7 +58,8 @@ abstract class LockTest extends MockedTest
/**
* @small
*/
public function testReleaseLock() {
public function testReleaseLock()
{
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->isLocked('foo'));
@ -85,7 +70,8 @@ abstract class LockTest extends MockedTest
/**
* @small
*/
public function testReleaseAll() {
public function testReleaseAll()
{
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('bar', 1));
$this->assertTrue($this->instance->acquireLock('nice', 1));
@ -104,7 +90,8 @@ abstract class LockTest extends MockedTest
/**
* @small
*/
public function testReleaseAfterUnlock() {
public function testReleaseAfterUnlock()
{
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertFalse($this->instance->isLocked('bar'));
$this->assertFalse($this->instance->isLocked('nice'));
@ -139,7 +126,8 @@ abstract class LockTest extends MockedTest
/**
* @medium
*/
function testLockTTL() {
function testLockTTL()
{
$this->markTestSkipped('taking too much time without mocking');
$this->assertFalse($this->instance->isLocked('foo'));

View file

@ -3,7 +3,8 @@
namespace Friendica\Test\src\Core\Lock;
use Friendica\Factory\CacheDriverFactory;
use Friendica\Core\Cache\MemcacheCacheDriver;
use Friendica\Core\Config\Configuration;
use Friendica\Core\Lock\CacheLockDriver;
/**
@ -13,16 +14,17 @@ class MemcacheCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->configMock
$configMock = \Mockery::mock(Configuration::class);
$configMock
->shouldReceive('get')
->with('system', 'memcache_host')
->andReturn('localhost');
$this->configMock
$configMock
->shouldReceive('get')
->with('system', 'memcache_port')
->andReturn(11211);
return new CacheLockDriver(CacheDriverFactory::create('memcache'));
return new CacheLockDriver(new MemcacheCacheDriver('localhost', $configMock));
}
}

View file

@ -3,8 +3,10 @@
namespace Friendica\Test\src\Core\Lock;
use Friendica\Factory\CacheDriverFactory;
use Friendica\Core\Cache\MemcachedCacheDriver;
use Friendica\Core\Config\Configuration;
use Friendica\Core\Lock\CacheLockDriver;
use Psr\Log\NullLogger;
/**
* @requires extension memcached
@ -13,11 +15,15 @@ class MemcachedCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->configMock
$configMock = \Mockery::mock(Configuration::class);
$configMock
->shouldReceive('get')
->with('system', 'memcached_hosts')
->andReturn([0 => 'localhost, 11211']);
return new CacheLockDriver(CacheDriverFactory::create('memcached'));
$logger = new NullLogger();
return new CacheLockDriver(new MemcachedCacheDriver('localhost', $configMock, $logger));
}
}

View file

@ -3,8 +3,9 @@
namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Cache\RedisCacheDriver;
use Friendica\Core\Config\Configuration;
use Friendica\Core\Lock\CacheLockDriver;
use Friendica\Factory\CacheDriverFactory;
/**
* @requires extension redis
@ -13,26 +14,26 @@ class RedisCacheLockDriverTest extends LockTest
{
protected function getInstance()
{
$this->configMock
$configMock = \Mockery::mock(Configuration::class);
$configMock
->shouldReceive('get')
->with('system', 'redis_host')
->andReturn('localhost');
$this->configMock
$configMock
->shouldReceive('get')
->with('system', 'redis_port')
->andReturn(null);
$this->configMock
$configMock
->shouldReceive('get')
->with('system', 'redis_db')
->with('system', 'redis_db', 0)
->andReturn(3);
$this->configMock
$configMock
->shouldReceive('get')
->with('system', 'redis_password')
->andReturn(null);
return new CacheLockDriver(CacheDriverFactory::create('redis'));
return new CacheLockDriver(new RedisCacheDriver('localhost', $configMock));
}
}

View file

@ -2,6 +2,10 @@
namespace Friendica\Test\src\Core\Lock;
use Dice\Dice;
use Friendica\App;
use Friendica\BaseObject;
use Friendica\Core\Config\Configuration;
use Friendica\Core\Lock\SemaphoreLockDriver;
class SemaphoreLockDriverTest extends LockTest
@ -10,12 +14,21 @@ class SemaphoreLockDriverTest extends LockTest
{
parent::setUp();
$this->app->shouldReceive('getHostname')->andReturn('friendica.local');
$dice = \Mockery::mock(Dice::class)->makePartial();
$this->configMock
$app = \Mockery::mock(App::class);
$app->shouldReceive('getHostname')->andReturn('friendica.local');
$dice->shouldReceive('create')->with(App::class)->andReturn($app);
$configMock = \Mockery::mock(Configuration::class);
$configMock
->shouldReceive('get')
->with('system', 'temppath')
->with('system', 'temppath', NULL, false)
->andReturn('/tmp/');
$dice->shouldReceive('create')->with(Configuration::class)->andReturn($configMock);
// @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
BaseObject::setDependencyInjection($dice);
}
protected function getInstance()