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
2019-01-30 20:26:17 +01:00
use Friendica\Test\MockedTest ;
2018-07-04 23:37:22 +02:00
2019-01-30 20:26:17 +01:00
abstract class LockTest extends MockedTest
2018-07-04 23:37:22 +02:00
{
2019-01-30 20:26:17 +01:00
/**
* @ var int Start time of the mock ( used for time operations )
*/
protected $startTime = 1417011228 ;
2018-07-04 23:37:22 +02:00
/**
2019-08-04 10:26:53 +02:00
* @ var \Friendica\Core\Lock\ILock
2018-07-04 23:37:22 +02:00
*/
protected $instance ;
abstract protected function getInstance ();
protected function setUp ()
{
2019-02-07 20:44:03 +01:00
parent :: setUp ();
2019-08-03 20:48:56 +02:00
2019-02-07 20:44:03 +01:00
$this -> instance = $this -> getInstance ();
2019-08-13 21:20:41 +02:00
$this -> instance -> releaseAll ( true );
2018-07-04 23:37:22 +02:00
}
2018-09-05 22:16:23 +02:00
protected function tearDown ()
{
2019-08-13 21:20:41 +02:00
$this -> instance -> releaseAll ( true );
2019-01-30 20:26:17 +01:00
parent :: tearDown ();
2018-09-05 22:16:23 +02:00
}
2018-07-18 21:04:18 +02:00
/**
* @ small
*/
2019-08-03 20:48:56 +02:00
public function testLock ()
{
2018-09-05 22:16:23 +02:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
2020-01-07 00:20:31 +01:00
$this -> assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
2018-07-04 23:37:22 +02:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
}
2018-07-18 21:04:18 +02:00
/**
* @ small
*/
2019-08-03 20:48:56 +02:00
public function testDoubleLock ()
{
2018-09-05 22:16:23 +02:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
2020-01-07 00:20:31 +01:00
$this -> assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
2018-07-04 23:37:22 +02:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
// We already locked it
2020-01-07 00:20:31 +01:00
$this -> assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
2018-07-04 23:37:22 +02:00
}
2018-07-18 21:04:18 +02:00
/**
* @ small
*/
2019-08-03 20:48:56 +02:00
public function testReleaseLock ()
{
2018-09-05 22:16:23 +02:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
2020-01-07 00:20:31 +01:00
$this -> assertTrue ( $this -> instance -> acquire ( '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' ));
}
2018-07-18 21:04:18 +02:00
/**
* @ small
*/
2019-08-03 20:48:56 +02:00
public function testReleaseAll ()
{
2020-01-07 00:20:31 +01:00
$this -> assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
$this -> assertTrue ( $this -> instance -> acquire ( 'bar' , 1 ));
$this -> assertTrue ( $this -> instance -> acquire ( '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' ));
2019-03-04 21:28:36 +01:00
$this -> assertTrue ( $this -> instance -> releaseAll ());
2018-07-04 23:37:22 +02:00
$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
}
2018-07-18 21:04:18 +02:00
/**
* @ small
*/
2019-08-03 20:48:56 +02:00
public function testReleaseAfterUnlock ()
{
2018-09-05 22:16:23 +02:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'nice' ));
2020-01-07 00:20:31 +01:00
$this -> assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
$this -> assertTrue ( $this -> instance -> acquire ( 'bar' , 1 ));
$this -> assertTrue ( $this -> instance -> acquire ( 'nice' , 1 ));
2018-07-04 23:37:22 +02:00
2019-03-04 21:28:36 +01:00
$this -> assertTrue ( $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' ));
2019-03-04 21:28:36 +01:00
$this -> assertTrue ( $this -> instance -> releaseAll ());
2018-07-04 23:37:22 +02:00
$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
2019-03-04 21:28:36 +01:00
/**
* @ small
*/
public function testReleaseWitTTL ()
{
$this -> assertFalse ( $this -> instance -> isLocked ( 'test' ));
2020-01-07 00:20:31 +01:00
$this -> assertTrue ( $this -> instance -> acquire ( 'test' , 1 , 10 ));
2019-03-04 21:28:36 +01:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'test' ));
$this -> assertTrue ( $this -> instance -> releaseLock ( 'test' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'test' ));
}
2019-08-13 21:20:41 +02:00
/**
* @ small
*/
public function testGetLocks ()
{
2020-01-07 00:20:31 +01:00
$this -> assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
$this -> assertTrue ( $this -> instance -> acquire ( 'bar' , 1 ));
$this -> assertTrue ( $this -> instance -> acquire ( 'nice' , 1 ));
2019-08-13 21:20:41 +02:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'bar' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'nice' ));
$locks = $this -> instance -> getLocks ();
$this -> assertContains ( 'foo' , $locks );
$this -> assertContains ( 'bar' , $locks );
$this -> assertContains ( 'nice' , $locks );
}
/**
* @ small
*/
public function testGetLocksWithPrefix ()
{
2020-01-07 00:20:31 +01:00
$this -> assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
$this -> assertTrue ( $this -> instance -> acquire ( 'test1' , 1 ));
$this -> assertTrue ( $this -> instance -> acquire ( 'test2' , 1 ));
2019-08-13 21:20:41 +02:00
$this -> assertTrue ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'test1' ));
$this -> assertTrue ( $this -> instance -> isLocked ( 'test2' ));
$locks = $this -> instance -> getLocks ( 'test' );
$this -> assertContains ( 'test1' , $locks );
$this -> assertContains ( 'test2' , $locks );
$this -> assertNotContains ( 'foo' , $locks );
}
2018-07-18 21:04:18 +02:00
/**
* @ medium
*/
2019-08-03 20:48:56 +02:00
function testLockTTL ()
{
2018-11-09 20:39:47 +01:00
$this -> markTestSkipped ( 'taking too much time without mocking' );
2018-09-05 22:16:23 +02:00
$this -> assertFalse ( $this -> instance -> isLocked ( 'foo' ));
$this -> assertFalse ( $this -> instance -> isLocked ( 'bar' ));
2018-07-07 19:46:16 +02: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
2020-01-07 00:20:31 +01:00
$this -> assertTrue ( $this -> instance -> acquire ( 'foo' , 2 , 1 ));
$this -> assertTrue ( $this -> instance -> acquire ( 'bar' , 2 , 3 ));
2018-07-07 19:46:16 +02: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' ));
}
2019-08-17 19:33:36 +02:00
/**
* Test if releasing a non - existing lock doesn ' t throw errors
*/
public function testReleaseLockWithoutLock ()
{
$this -> assertFalse ( $this -> instance -> isLocked ( 'wrongLock' ));
$this -> assertFalse ( $this -> instance -> releaseLock ( 'wrongLock' ));
}
2018-07-07 16:15:03 +02:00
}