2018-07-04 23:37:22 +02:00
< ? php
2018-07-05 20:57:31 +02:00
namespace Friendica\Test\src\Core\Lock ;
2018-07-04 23:37:22 +02:00
use Friendica\App ;
use Friendica\Core\Config ;
2018-07-07 16:15:03 +02:00
use Friendica\Test\DatabaseTest ;
2018-07-04 23:37:22 +02:00
use PHPUnit\Framework\TestCase ;
2018-07-07 16:15:03 +02:00
abstract class LockTest extends DatabaseTest
2018-07-04 23:37:22 +02:00
{
/**
* @ 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 () {
2018-07-05 20:57:31 +02:00
$this -> instance -> acquireLock ( 'foo' , 1 );
2018-07-04 23:37:22 +02:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
}
public function testDoubleLock () {
2018-07-05 20:57:31 +02:00
$this -> instance -> acquireLock ( 'foo' , 1 );
2018-07-04 23:37:22 +02:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
// We already locked it
2018-07-05 20:57:31 +02:00
$this -> assertTrue ( $this -> instance -> acquireLock ( 'foo' , 1 ));
2018-07-04 23:37:22 +02:00
}
public function testReleaseLock () {
2018-07-05 20:57:31 +02:00
$this -> instance -> acquireLock ( 'foo' , 1 );
2018-07-04 23:37:22 +02:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
2018-07-05 20:57:31 +02:00
$this -> instance -> releaseLock ( 'foo' );
2018-07-04 23:37:22 +02:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
}
public function testReleaseAll () {
2018-07-05 20:57:31 +02:00
$this -> instance -> acquireLock ( 'foo' , 1 );
$this -> instance -> acquireLock ( 'bar' , 1 );
$this -> instance -> acquireLock ( 'nice' , 1 );
2018-07-04 23:37:22 +02:00
2018-07-07 16:15:03 +02:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'bar' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'nice' ));
2018-07-04 23:37:22 +02:00
$this -> instance -> releaseAll ();
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
2018-07-05 20:57:31 +02:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'nice' ));
2018-07-04 23:37:22 +02:00
}
public function testReleaseAfterUnlock () {
2018-07-05 20:57:31 +02:00
$this -> instance -> acquireLock ( 'foo' , 1 );
$this -> instance -> acquireLock ( 'bar' , 1 );
$this -> instance -> acquireLock ( 'nice' , 1 );
2018-07-04 23:37:22 +02:00
2018-07-05 20:57:31 +02:00
$this -> instance -> releaseLock ( 'foo' );
2018-07-04 23:37:22 +02:00
2018-07-07 16:15:03 +02:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'bar' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'nice' ));
2018-07-04 23:37:22 +02:00
$this -> instance -> releaseAll ();
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
2018-07-07 16:15:03 +02:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'nice' ));
2018-07-04 23:37:22 +02:00
}
2018-07-07 19:46:16 +02:00
function testLockTTL () {
// TODO [nupplaphil] - Because of the Datetime-Utils for the database, we have to wait a FULL second between the checks to invalidate the db-locks/cache
$this -> instance -> acquireLock ( 'foo' , 1 , 1 );
$this -> instance -> acquireLock ( 'bar' , 1 , 3 );
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'bar' ));
sleep ( 2 );
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'bar' ));
sleep ( 2 );
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
}
2018-07-07 16:15:03 +02:00
}