mirror of
https://github.com/friendica/friendica
synced 2025-04-25 21:10:11 +00:00
Merge pull request #7465 from nupplaphil/task/dice_cache_lock
Refactor Cache/Lock to DICE
This commit is contained in:
commit
6b7dfd0c71
60 changed files with 1150 additions and 881 deletions
|
@ -7,6 +7,7 @@ use Friendica\Core\L10n\L10n;
|
|||
use Friendica\Test\MockedTest;
|
||||
use Friendica\Test\Util\AppMockTrait;
|
||||
use Friendica\Test\Util\VFSTrait;
|
||||
use Friendica\Util\BaseURL;
|
||||
|
||||
class BBCodeTest extends MockedTest
|
||||
{
|
||||
|
@ -44,6 +45,12 @@ class BBCodeTest extends MockedTest
|
|||
$this->dice->shouldReceive('create')
|
||||
->with(L10n::class)
|
||||
->andReturn($l10nMock);
|
||||
|
||||
$baseUrlMock = \Mockery::mock(BaseURL::class);
|
||||
$baseUrlMock->shouldReceive('get')->withAnyArgs()->andReturn('friendica.local');
|
||||
$this->dice->shouldReceive('create')
|
||||
->with(BaseURL::class)
|
||||
->andReturn($baseUrlMock);
|
||||
}
|
||||
|
||||
public function dataLinks()
|
||||
|
|
|
@ -4,7 +4,7 @@ namespace Friendica\Test\src\Core\Cache;
|
|||
|
||||
use Friendica\Core\Cache\APCuCache;
|
||||
|
||||
class APCuCacheDriverTest extends MemoryCacheTest
|
||||
class APCuCacheTest extends MemoryCacheTest
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
|
@ -17,7 +17,7 @@ class APCuCacheDriverTest extends MemoryCacheTest
|
|||
|
||||
protected function getInstance()
|
||||
{
|
||||
$this->cache = new APCuCache();
|
||||
$this->cache = new APCuCache('localhost');
|
||||
return $this->cache;
|
||||
}
|
||||
|
|
@ -4,11 +4,11 @@ namespace Friendica\Test\src\Core\Cache;
|
|||
|
||||
use Friendica\Core\Cache\ArrayCache;
|
||||
|
||||
class ArrayCacheDriverTest extends MemoryCacheTest
|
||||
class ArrayCacheTest extends MemoryCacheTest
|
||||
{
|
||||
protected function getInstance()
|
||||
{
|
||||
$this->cache = new ArrayCache();
|
||||
$this->cache = new ArrayCache('localhost');
|
||||
return $this->cache;
|
||||
}
|
||||
|
|
@ -2,34 +2,30 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache\MemcachedCacheDriver;
|
||||
use Friendica\Core\Cache\MemcachedCache;
|
||||
use Friendica\Test\MockedTest;
|
||||
use Friendica\Test\Util\AppMockTrait;
|
||||
use Friendica\Test\Util\VFSTrait;
|
||||
use Friendica\Util\PidFile;
|
||||
|
||||
abstract class CacheTest extends MockedTest
|
||||
{
|
||||
use VFSTrait;
|
||||
use AppMockTrait;
|
||||
|
||||
/**
|
||||
* @var int Start time of the mock (used for time operations)
|
||||
*/
|
||||
protected $startTime = 1417011228;
|
||||
|
||||
/**
|
||||
* @var \Friendica\Core\Cache\ICacheDriver
|
||||
* @var \Friendica\Core\Cache\ICache
|
||||
*/
|
||||
protected $instance;
|
||||
|
||||
/**
|
||||
* @var \Friendica\Core\Cache\IMemoryCacheDriver
|
||||
* @var \Friendica\Core\Cache\IMemoryCache
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* Dataset for test setting different types in the cache
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function dataTypesInCache()
|
||||
|
@ -48,6 +44,7 @@ abstract class CacheTest extends MockedTest
|
|||
|
||||
/**
|
||||
* Dataset for simple value sets/gets
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function dataSimple()
|
||||
|
@ -66,12 +63,6 @@ abstract class CacheTest extends MockedTest
|
|||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->setUpVfsDir();
|
||||
$this->mockApp($this->root);
|
||||
$this->app
|
||||
->shouldReceive('getHostname')
|
||||
->andReturn('friendica.local');
|
||||
|
||||
parent::setUp();
|
||||
|
||||
$this->instance = $this->getInstance();
|
||||
|
@ -82,10 +73,12 @@ abstract class CacheTest extends MockedTest
|
|||
/**
|
||||
* @small
|
||||
* @dataProvider dataSimple
|
||||
*
|
||||
* @param mixed $value1 a first
|
||||
* @param mixed $value2 a second
|
||||
*/
|
||||
function testSimple($value1, $value2) {
|
||||
function testSimple($value1, $value2)
|
||||
{
|
||||
$this->assertNull($this->instance->get('value1'));
|
||||
|
||||
$this->instance->set('value1', $value1);
|
||||
|
@ -110,12 +103,14 @@ abstract class CacheTest extends MockedTest
|
|||
/**
|
||||
* @small
|
||||
* @dataProvider dataSimple
|
||||
*
|
||||
* @param mixed $value1 a first
|
||||
* @param mixed $value2 a second
|
||||
* @param mixed $value3 a third
|
||||
* @param mixed $value4 a fourth
|
||||
*/
|
||||
function testClear($value1, $value2, $value3, $value4) {
|
||||
function testClear($value1, $value2, $value3, $value4)
|
||||
{
|
||||
$value = 'ipsum lorum';
|
||||
$this->instance->set('1_value1', $value1);
|
||||
$this->instance->set('1_value2', $value2);
|
||||
|
@ -166,7 +161,8 @@ abstract class CacheTest extends MockedTest
|
|||
/**
|
||||
* @medium
|
||||
*/
|
||||
function testTTL() {
|
||||
function testTTL()
|
||||
{
|
||||
$this->markTestSkipped('taking too much time without mocking');
|
||||
|
||||
$this->assertNull($this->instance->get('value1'));
|
||||
|
@ -183,10 +179,13 @@ abstract class CacheTest extends MockedTest
|
|||
|
||||
/**
|
||||
* @small
|
||||
*
|
||||
* @param $data mixed the data to store in the cache
|
||||
*
|
||||
* @dataProvider dataTypesInCache
|
||||
*/
|
||||
function testDifferentTypesInCache($data) {
|
||||
function testDifferentTypesInCache($data)
|
||||
{
|
||||
$this->instance->set('val', $data);
|
||||
$received = $this->instance->get('val');
|
||||
$this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
|
||||
|
@ -194,13 +193,16 @@ abstract class CacheTest extends MockedTest
|
|||
|
||||
/**
|
||||
* @small
|
||||
*
|
||||
* @param mixed $value1 a first
|
||||
* @param mixed $value2 a second
|
||||
* @param mixed $value3 a third
|
||||
*
|
||||
* @dataProvider dataSimple
|
||||
*/
|
||||
public function testGetAllKeys($value1, $value2, $value3) {
|
||||
if ($this->cache instanceof MemcachedCacheDriver) {
|
||||
public function testGetAllKeys($value1, $value2, $value3)
|
||||
{
|
||||
if ($this->cache instanceof MemcachedCache) {
|
||||
$this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore');
|
||||
}
|
||||
|
||||
|
|
|
@ -1,141 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Factory\CacheDriverFactory;
|
||||
use Friendica\Test\Util\DbaCacheMockTrait;
|
||||
|
||||
/**
|
||||
* @runTestsInSeparateProcesses
|
||||
* @preserveGlobalState disabled
|
||||
*/
|
||||
class DatabaseCacheDriverTest extends CacheTest
|
||||
{
|
||||
use DbaCacheMockTrait;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->mockUtcNow($this->startTime);
|
||||
|
||||
$this->mockConnected();
|
||||
$this->mockConnect();
|
||||
|
||||
// The first "clear" at setup
|
||||
$this->mockClear(false, true, 2);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
{
|
||||
$this->cache = CacheDriverFactory::create('database');
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
$this->cache->clear(false);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @dataProvider dataSimple
|
||||
*/
|
||||
public function testSimple($value1, $value2)
|
||||
{
|
||||
// assertNull
|
||||
$this->mockGet('value1', null, $this->startTime, 1);
|
||||
|
||||
// assertEquals
|
||||
$this->mockSet('value1', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
$this->mockGet('value1', $value1, $this->startTime, 1);
|
||||
|
||||
// assertEquals
|
||||
$this->mockSet('value1', $value2, Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
$this->mockGet('value1', $value2, $this->startTime, 1);
|
||||
|
||||
// assertEquals
|
||||
$this->mockSet('value2', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
$this->mockGet('value2', $value1, $this->startTime, 1);
|
||||
|
||||
// assertNull
|
||||
$this->mockGet('not_set', null, $this->startTime, 1);
|
||||
|
||||
// assertNull
|
||||
$this->mockDelete('value1', true, 1);
|
||||
$this->mockGet('value1', null, $this->startTime, 1);
|
||||
|
||||
parent::testSimple($value1, $value2);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @dataProvider dataSimple
|
||||
*/
|
||||
public function testClear($value1, $value2, $value3, $value4)
|
||||
{
|
||||
// assert Equals
|
||||
$this->mockSet('1_value1', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
$this->mockSet('1_value2', $value2, Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
$this->mockSet('2_value1', $value3, Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
$this->mockSet('3_value1', $value4, Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
|
||||
$this->mockGet('1_value1', $value1, $this->startTime, 2);
|
||||
$this->mockGet('1_value2', $value2, $this->startTime, 2);
|
||||
$this->mockGet('2_value1', $value3, $this->startTime, 2);
|
||||
$this->mockGet('3_value1', $value4, $this->startTime, 2);
|
||||
|
||||
// assertTrue
|
||||
$this->mockClear(true, true, 1);
|
||||
$this->mockClear(false, true, 1);
|
||||
|
||||
// assertEquals
|
||||
$this->mockGet('1_value1', null, $this->startTime, 1);
|
||||
$this->mockGet('1_value2', null, $this->startTime, 1);
|
||||
$this->mockGet('2_value3', null, $this->startTime, 1);
|
||||
$this->mockGet('3_value4', null, $this->startTime, 1);
|
||||
|
||||
parent::testClear($value1, $value2, $value3, $value4);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @dataProvider dataTypesInCache
|
||||
*/
|
||||
public function testDifferentTypesInCache($data)
|
||||
{
|
||||
$this->mockSet('val', $data, Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
$this->mockGet('val', $data, $this->startTime, 1);
|
||||
|
||||
parent::testDifferentTypesInCache($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @dataProvider dataSimple
|
||||
*/
|
||||
public function testGetAllKeys($value1, $value2, $value3)
|
||||
{
|
||||
$this->mockSet('value1', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
$this->mockSet('value2', $value2,Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
$this->mockSet('test_value3', $value3, Cache::FIVE_MINUTES, $this->startTime, true, 1);
|
||||
|
||||
$result = [
|
||||
['k' => 'value1'],
|
||||
['k' => 'value2'],
|
||||
['k' => 'test_value3'],
|
||||
];
|
||||
|
||||
$this->mockGetAllKeys(null, $result, $this->startTime, 1);
|
||||
|
||||
$result = [
|
||||
['k' => 'test_value3'],
|
||||
];
|
||||
|
||||
$this->mockGetAllKeys('test', $result, $this->startTime, 1);
|
||||
|
||||
parent::testGetAllKeys($value1, $value2, $value3);
|
||||
}
|
||||
}
|
48
tests/src/Core/Cache/DatabaseCacheTest.php
Normal file
48
tests/src/Core/Cache/DatabaseCacheTest.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
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;
|
||||
|
||||
class DatabaseCacheTest extends CacheTest
|
||||
{
|
||||
use DatabaseTestTrait;
|
||||
use VFSTrait;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->setUpVfsDir();
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
{
|
||||
$logger = new NullLogger();
|
||||
$profiler = \Mockery::mock(Profiler::class);
|
||||
$profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
|
||||
|
||||
// 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);
|
||||
|
||||
$dba = new StaticDatabase($configCache, $profiler, $logger);
|
||||
|
||||
$this->cache = new Cache\DatabaseCache('database', $dba);
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
$this->cache->clear(false);
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
|
@ -1,30 +1,30 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Friendica\Test\src\Core\Cache;
|
||||
|
||||
use Friendica\Factory\CacheDriverFactory;
|
||||
use Friendica\Core\Cache\MemcacheCache;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
|
||||
/**
|
||||
* @requires extension memcache
|
||||
*/
|
||||
class MemcacheCacheDriverTest extends MemoryCacheTest
|
||||
class MemcacheCacheTest extends MemoryCacheTest
|
||||
{
|
||||
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);
|
||||
|
||||
$this->cache = CacheDriverFactory::create('memcache');
|
||||
$this->cache = new MemcacheCache('localhost', $configMock);
|
||||
return $this->cache;
|
||||
|
||||
}
|
||||
|
||||
public function tearDown()
|
|
@ -3,21 +3,27 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Cache;
|
||||
|
||||
use Friendica\Factory\CacheDriverFactory;
|
||||
use Friendica\Core\Cache\MemcachedCache;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
* @requires extension memcached
|
||||
*/
|
||||
class MemcachedCacheDriverTest extends MemoryCacheTest
|
||||
class MemcachedCacheTest extends MemoryCacheTest
|
||||
{
|
||||
protected function getInstance()
|
||||
{
|
||||
$this->configMock
|
||||
$configMock = \Mockery::mock(Configuration::class);
|
||||
|
||||
$configMock
|
||||
->shouldReceive('get')
|
||||
->with('system', 'memcached_hosts')
|
||||
->andReturn([0 => 'localhost, 11211']);
|
||||
|
||||
$this->cache = CacheDriverFactory::create('memcached');
|
||||
$logger = new NullLogger();
|
||||
|
||||
$this->cache = new MemcachedCache('localhost', $configMock, $logger);
|
||||
return $this->cache;
|
||||
}
|
||||
|
|
@ -2,14 +2,12 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache\IMemoryCacheDriver;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
use Friendica\Core\Cache\IMemoryCache;
|
||||
|
||||
abstract class MemoryCacheTest extends CacheTest
|
||||
{
|
||||
/**
|
||||
* @var \Friendica\Core\Cache\IMemoryCacheDriver
|
||||
* @var \Friendica\Core\Cache\IMemoryCache
|
||||
*/
|
||||
protected $instance;
|
||||
|
||||
|
@ -17,12 +15,7 @@ abstract class MemoryCacheTest extends CacheTest
|
|||
{
|
||||
parent::setUp();
|
||||
|
||||
$logger = new NullLogger();
|
||||
$this->dice->shouldReceive('create')
|
||||
->with(LoggerInterface::class)
|
||||
->andReturn($logger);
|
||||
|
||||
if (!($this->instance instanceof IMemoryCacheDriver)) {
|
||||
if (!($this->instance instanceof IMemoryCache)) {
|
||||
throw new \Exception('MemoryCacheTest unsupported');
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +24,8 @@ abstract class MemoryCacheTest extends CacheTest
|
|||
* @small
|
||||
* @dataProvider dataSimple
|
||||
*/
|
||||
function testCompareSet($value1, $value2) {
|
||||
function testCompareSet($value1, $value2)
|
||||
{
|
||||
$this->assertNull($this->instance->get('value1'));
|
||||
|
||||
$this->instance->add('value1', $value1);
|
||||
|
@ -47,7 +41,8 @@ abstract class MemoryCacheTest extends CacheTest
|
|||
* @small
|
||||
* @dataProvider dataSimple
|
||||
*/
|
||||
function testNegativeCompareSet($value1, $value2) {
|
||||
function testNegativeCompareSet($value1, $value2)
|
||||
{
|
||||
$this->assertNull($this->instance->get('value1'));
|
||||
|
||||
$this->instance->add('value1', $value1);
|
||||
|
@ -64,7 +59,8 @@ abstract class MemoryCacheTest extends CacheTest
|
|||
* @small
|
||||
* @dataProvider dataSimple
|
||||
*/
|
||||
function testCompareDelete($data) {
|
||||
function testCompareDelete($data)
|
||||
{
|
||||
$this->assertNull($this->instance->get('value1'));
|
||||
|
||||
$this->instance->add('value1', $data);
|
||||
|
@ -78,7 +74,8 @@ abstract class MemoryCacheTest extends CacheTest
|
|||
* @small
|
||||
* @dataProvider dataSimple
|
||||
*/
|
||||
function testNegativeCompareDelete($data) {
|
||||
function testNegativeCompareDelete($data)
|
||||
{
|
||||
$this->assertNull($this->instance->get('value1'));
|
||||
|
||||
$this->instance->add('value1', $data);
|
||||
|
@ -95,7 +92,8 @@ abstract class MemoryCacheTest extends CacheTest
|
|||
* @small
|
||||
* @dataProvider dataSimple
|
||||
*/
|
||||
function testAdd($value1, $value2) {
|
||||
function testAdd($value1, $value2)
|
||||
{
|
||||
$this->assertNull($this->instance->get('value1'));
|
||||
|
||||
$this->instance->add('value1', $value1);
|
||||
|
@ -111,4 +109,4 @@ abstract class MemoryCacheTest extends CacheTest
|
|||
$this->assertEquals($value2, $received, 'Value was not overwritten by add');
|
||||
$this->assertNotEquals($value1, $received, 'Value was not overwritten by any other value');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,36 +3,37 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Cache;
|
||||
|
||||
use Friendica\Factory\CacheDriverFactory;
|
||||
use Friendica\Core\Cache\RedisCache;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
|
||||
/**
|
||||
* @requires extension redis
|
||||
*/
|
||||
class RedisCacheDriverTest extends MemoryCacheTest
|
||||
class RedisCacheTest extends MemoryCacheTest
|
||||
{
|
||||
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);
|
||||
|
||||
$this->cache = CacheDriverFactory::create('redis');
|
||||
$this->cache = new RedisCache('localhost', $configMock);
|
||||
return $this->cache;
|
||||
}
|
||||
|
|
@ -2,11 +2,10 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
|
||||
use Friendica\Core\Cache\APCuCache;
|
||||
use Friendica\Core\Lock\CacheLockDriver;
|
||||
use Friendica\Core\Lock\CacheLock;
|
||||
|
||||
class APCuCacheLockDriverTest extends LockTest
|
||||
class APCuCacheLockTest extends LockTest
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
|
@ -19,6 +18,6 @@ class APCuCacheLockDriverTest extends LockTest
|
|||
|
||||
protected function getInstance()
|
||||
{
|
||||
return new CacheLockDriver(new APCuCache());
|
||||
return new CacheLock(new APCuCache('localhost'));
|
||||
}
|
||||
}
|
|
@ -2,15 +2,14 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
|
||||
use Friendica\Core\Cache\ArrayCache;
|
||||
use Friendica\Core\Lock\CacheLockDriver;
|
||||
use Friendica\Core\Lock\CacheLock;
|
||||
|
||||
class ArrayCacheLockDriverTest extends LockTest
|
||||
class ArrayCacheLockTest extends LockTest
|
||||
{
|
||||
protected function getInstance()
|
||||
{
|
||||
return new CacheLockDriver(new ArrayCache());
|
||||
return new CacheLock(new ArrayCache('localhost'));
|
||||
}
|
||||
|
||||
public function testLockTTL()
|
|
@ -1,118 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Core\Lock\DatabaseLockDriver;
|
||||
use Friendica\Test\Util\DbaLockMockTrait;
|
||||
|
||||
/**
|
||||
* @runTestsInSeparateProcesses
|
||||
* @preserveGlobalState disabled
|
||||
*/
|
||||
class DatabaseLockDriverTest extends LockTest
|
||||
{
|
||||
use DbaLockMockTrait;
|
||||
|
||||
protected $pid = 123;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->mockConnected();
|
||||
$this->mockConnect();
|
||||
|
||||
$this->mockReleaseAll($this->pid, 2);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
43
tests/src/Core/Lock/DatabaseLockTest.php
Normal file
43
tests/src/Core/Lock/DatabaseLockTest.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\Core\Lock\DatabaseLock;
|
||||
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;
|
||||
|
||||
class DatabaseLockDriverTest extends LockTest
|
||||
{
|
||||
use VFSTrait;
|
||||
use DatabaseTestTrait;
|
||||
|
||||
protected $pid = 123;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->setUpVfsDir();
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
{
|
||||
$logger = new NullLogger();
|
||||
$profiler = \Mockery::mock(Profiler::class);
|
||||
$profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
|
||||
|
||||
// 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);
|
||||
|
||||
$dba = new StaticDatabase($configCache, $profiler, $logger);
|
||||
|
||||
return new DatabaseLock($dba, $this->pid);
|
||||
}
|
||||
}
|
|
@ -3,23 +3,16 @@
|
|||
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)
|
||||
*/
|
||||
protected $startTime = 1417011228;
|
||||
|
||||
/**
|
||||
* @var \Friendica\Core\Lock\ILockDriver
|
||||
* @var \Friendica\Core\Lock\ILock
|
||||
*/
|
||||
protected $instance;
|
||||
|
||||
|
@ -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'));
|
||||
|
|
|
@ -3,26 +3,28 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\Factory\CacheDriverFactory;
|
||||
use Friendica\Core\Lock\CacheLockDriver;
|
||||
use Friendica\Core\Cache\MemcacheCache;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Core\Lock\CacheLock;
|
||||
|
||||
/**
|
||||
* @requires extension Memcache
|
||||
*/
|
||||
class MemcacheCacheLockDriverTest extends LockTest
|
||||
class MemcacheCacheLockTest 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 CacheLock(new MemcacheCache('localhost', $configMock));
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\Factory\CacheDriverFactory;
|
||||
use Friendica\Core\Lock\CacheLockDriver;
|
||||
|
||||
/**
|
||||
* @requires extension memcached
|
||||
*/
|
||||
class MemcachedCacheLockDriverTest extends LockTest
|
||||
{
|
||||
protected function getInstance()
|
||||
{
|
||||
$this->configMock
|
||||
->shouldReceive('get')
|
||||
->with('system', 'memcached_hosts')
|
||||
->andReturn([0 => 'localhost, 11211']);
|
||||
|
||||
return new CacheLockDriver(CacheDriverFactory::create('memcached'));
|
||||
}
|
||||
}
|
29
tests/src/Core/Lock/MemcachedCacheLockTest.php
Normal file
29
tests/src/Core/Lock/MemcachedCacheLockTest.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache\MemcachedCache;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Core\Lock\CacheLock;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
* @requires extension memcached
|
||||
*/
|
||||
class MemcachedCacheLockTest extends LockTest
|
||||
{
|
||||
protected function getInstance()
|
||||
{
|
||||
$configMock = \Mockery::mock(Configuration::class);
|
||||
|
||||
$configMock
|
||||
->shouldReceive('get')
|
||||
->with('system', 'memcached_hosts')
|
||||
->andReturn([0 => 'localhost, 11211']);
|
||||
|
||||
$logger = new NullLogger();
|
||||
|
||||
return new CacheLock(new MemcachedCache('localhost', $configMock, $logger));
|
||||
}
|
||||
}
|
|
@ -3,36 +3,37 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\Core\Lock\CacheLockDriver;
|
||||
use Friendica\Factory\CacheDriverFactory;
|
||||
use Friendica\Core\Cache\RedisCache;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Core\Lock\CacheLock;
|
||||
|
||||
/**
|
||||
* @requires extension redis
|
||||
*/
|
||||
class RedisCacheLockDriverTest extends LockTest
|
||||
class RedisCacheLockTest 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 CacheLock(new RedisCache('localhost', $configMock));
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\Core\Lock\SemaphoreLockDriver;
|
||||
|
||||
class SemaphoreLockDriverTest extends LockTest
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->app->shouldReceive('getHostname')->andReturn('friendica.local');
|
||||
|
||||
$this->configMock
|
||||
->shouldReceive('get')
|
||||
->with('system', 'temppath')
|
||||
->andReturn('/tmp/');
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
{
|
||||
return new SemaphoreLockDriver();
|
||||
}
|
||||
|
||||
function testLockTTL()
|
||||
{
|
||||
// Semaphore doesn't work with TTL
|
||||
return true;
|
||||
}
|
||||
}
|
44
tests/src/Core/Lock/SemaphoreLockTest.php
Normal file
44
tests/src/Core/Lock/SemaphoreLockTest.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Dice\Dice;
|
||||
use Friendica\App;
|
||||
use Friendica\BaseObject;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Core\Lock\SemaphoreLock;
|
||||
|
||||
class SemaphoreLockTest extends LockTest
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$dice = \Mockery::mock(Dice::class)->makePartial();
|
||||
|
||||
$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', 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()
|
||||
{
|
||||
return new SemaphoreLock();
|
||||
}
|
||||
|
||||
function testLockTTL()
|
||||
{
|
||||
// Semaphore doesn't work with TTL
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -15,9 +15,9 @@ class DBATest extends DatabaseTest
|
|||
{
|
||||
parent::setUp();
|
||||
|
||||
$dice = new Dice();
|
||||
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
|
||||
$dice = $dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]);
|
||||
$dice = (new Dice())
|
||||
->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
|
||||
->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]);
|
||||
BaseObject::setDependencyInjection($dice);
|
||||
|
||||
// Default config
|
||||
|
|
|
@ -15,9 +15,9 @@ class DBStructureTest extends DatabaseTest
|
|||
{
|
||||
parent::setUp();
|
||||
|
||||
$dice = new Dice();
|
||||
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
|
||||
$dice = $dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]);
|
||||
$dice = (new Dice())
|
||||
->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
|
||||
->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]);
|
||||
BaseObject::setDependencyInjection($dice);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue