2018-07-04 21:37:22 +00:00
< ? php
2018-07-05 18:57:31 +00:00
namespace Friendica\Test\src\Core\Lock ;
2018-07-04 21:37:22 +00:00
2018-07-21 12:43:43 +00:00
use Friendica\BaseObject ;
2018-07-04 21:37:22 +00:00
use Friendica\Core\Config ;
2018-07-07 14:15:03 +00:00
use Friendica\Test\DatabaseTest ;
2018-07-04 21:37:22 +00:00
2018-07-07 14:15:03 +00:00
abstract class LockTest extends DatabaseTest
2018-07-04 21:37:22 +00:00
{
/**
* @ var \Friendica\Core\Lock\ILockDriver
*/
protected $instance ;
abstract protected function getInstance ();
protected function setUp ()
{
parent :: setUp ();
$this -> instance = $this -> getInstance ();
2018-09-05 20:16:23 +00:00
$this -> instance -> releaseAll ();
2018-07-04 21:37:22 +00:00
// Reusable App object
2018-07-21 12:43:43 +00:00
$this -> app = BaseObject :: getApp ();
2018-07-04 21:37:22 +00:00
// 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' );
}
2018-09-05 20:16:23 +00:00
protected function tearDown ()
{
parent :: tearDown ();
$this -> instance -> releaseAll ();
}
2018-07-18 19:04:18 +00:00
/**
* @ small
*/
2018-07-04 21:37:22 +00:00
public function testLock () {
2018-09-05 20:16:23 +00:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> acquireLock ( 'foo' , 1 ));
2018-07-04 21:37:22 +00:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
}
2018-07-18 19:04:18 +00:00
/**
* @ small
*/
2018-07-04 21:37:22 +00:00
public function testDoubleLock () {
2018-09-05 20:16:23 +00:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> acquireLock ( 'foo' , 1 ));
2018-07-04 21:37:22 +00:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
// We already locked it
2018-07-05 18:57:31 +00:00
$this -> assertTrue ( $this -> instance -> acquireLock ( 'foo' , 1 ));
2018-07-04 21:37:22 +00:00
}
2018-07-18 19:04:18 +00:00
/**
* @ small
*/
2018-07-04 21:37:22 +00:00
public function testReleaseLock () {
2018-09-05 20:16:23 +00:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> acquireLock ( 'foo' , 1 ));
2018-07-04 21:37:22 +00:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
2018-07-05 18:57:31 +00:00
$this -> instance -> releaseLock ( 'foo' );
2018-07-04 21:37:22 +00:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
}
2018-07-18 19:04:18 +00:00
/**
* @ small
*/
2018-07-04 21:37:22 +00:00
public function testReleaseAll () {
2018-09-05 20:16:23 +00:00
$this -> assertTrue ( $this -> instance -> acquireLock ( 'foo' , 1 ));
$this -> assertTrue ( $this -> instance -> acquireLock ( 'bar' , 1 ));
$this -> assertTrue ( $this -> instance -> acquireLock ( 'nice' , 1 ));
2018-07-04 21:37:22 +00:00
2018-07-07 14:15:03 +00:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'bar' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'nice' ));
2018-07-04 21:37:22 +00:00
$this -> instance -> releaseAll ();
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
2018-07-05 18:57:31 +00:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'nice' ));
2018-07-04 21:37:22 +00:00
}
2018-07-18 19:04:18 +00:00
/**
* @ small
*/
2018-07-04 21:37:22 +00:00
public function testReleaseAfterUnlock () {
2018-09-05 20:16:23 +00:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'nice' ));
$this -> assertTrue ( $this -> instance -> acquireLock ( 'foo' , 1 ));
$this -> assertTrue ( $this -> instance -> acquireLock ( 'bar' , 1 ));
$this -> assertTrue ( $this -> instance -> acquireLock ( 'nice' , 1 ));
2018-07-04 21:37:22 +00:00
2018-07-05 18:57:31 +00:00
$this -> instance -> releaseLock ( 'foo' );
2018-07-04 21:37:22 +00:00
2018-07-07 14:15:03 +00:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'bar' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'nice' ));
2018-07-04 21:37:22 +00:00
$this -> instance -> releaseAll ();
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
2018-07-07 14:15:03 +00:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'nice' ));
2018-07-04 21:37:22 +00:00
}
2018-07-07 17:46:16 +00:00
2018-07-18 19:04:18 +00:00
/**
* @ medium
*/
2018-07-07 17:46:16 +00:00
function testLockTTL () {
2018-11-09 19:39:47 +00:00
$this -> markTestSkipped ( 'taking too much time without mocking' );
2018-09-05 20:16:23 +00:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
2018-07-07 17:46:16 +00:00
// 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
2018-09-05 20:16:23 +00:00
$this -> assertTrue ( $this -> instance -> acquireLock ( 'foo' , 2 , 1 ));
$this -> assertTrue ( $this -> instance -> acquireLock ( 'bar' , 2 , 3 ));
2018-07-07 17:46:16 +00:00
$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 14:15:03 +00:00
}