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,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);
}
}