2018-07-04 21:37:22 +00:00
< ? php
2024-08-24 12:31:41 +00:00
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2018-07-04 21:37:22 +00:00
2024-12-15 21:15:21 +00:00
namespace Friendica\Test ;
2018-07-04 21:37:22 +00:00
2021-10-26 19:44:29 +00:00
use Friendica\Core\Lock\Capability\ICanLock ;
2024-12-11 22:45:17 +00:00
use Friendica\Test\MockedTestCase ;
2018-07-04 21:37:22 +00:00
2024-12-15 21:15:21 +00:00
abstract class LockTestCase extends MockedTestCase
2018-07-04 21:37:22 +00:00
{
2019-01-30 19:26:17 +00:00
/**
* @ var int Start time of the mock ( used for time operations )
*/
protected $startTime = 1417011228 ;
2018-07-04 21:37:22 +00:00
/**
2021-10-26 19:44:29 +00:00
* @ var ICanLock
2018-07-04 21:37:22 +00:00
*/
protected $instance ;
abstract protected function getInstance ();
2021-04-01 21:04:30 +00:00
protected function setUp () : void
2018-07-04 21:37:22 +00:00
{
2019-02-07 19:44:03 +00:00
parent :: setUp ();
2019-08-03 18:48:56 +00:00
2019-02-07 19:44:03 +00:00
$this -> instance = $this -> getInstance ();
2019-08-13 19:20:41 +00:00
$this -> instance -> releaseAll ( true );
2018-07-04 21:37:22 +00:00
}
2021-04-01 21:04:30 +00:00
protected function tearDown () : void
2018-09-05 20:16:23 +00:00
{
2019-08-13 19:20:41 +00:00
$this -> instance -> releaseAll ( true );
2019-01-30 19:26:17 +00:00
parent :: tearDown ();
2018-09-05 20:16:23 +00:00
}
2018-07-18 19:04:18 +00:00
/**
* @ small
*/
2019-08-03 18:48:56 +00:00
public function testLock ()
{
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'foo' ));
self :: assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
self :: assertTrue ( $this -> instance -> isLocked ( 'foo' ));
self :: assertFalse ( $this -> instance -> isLocked ( 'bar' ));
2018-07-04 21:37:22 +00:00
}
2018-07-18 19:04:18 +00:00
/**
* @ small
*/
2019-08-03 18:48:56 +00:00
public function testDoubleLock ()
{
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'foo' ));
self :: assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
self :: assertTrue ( $this -> instance -> isLocked ( 'foo' ));
2018-07-04 21:37:22 +00:00
// We already locked it
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
2018-07-04 21:37:22 +00:00
}
2018-07-18 19:04:18 +00:00
/**
* @ small
*/
2019-08-03 18:48:56 +00:00
public function testReleaseLock ()
{
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'foo' ));
self :: assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
self :: assertTrue ( $this -> instance -> isLocked ( 'foo' ));
2020-01-06 23:24:10 +00:00
$this -> instance -> release ( 'foo' );
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'foo' ));
2018-07-04 21:37:22 +00:00
}
2018-07-18 19:04:18 +00:00
/**
* @ small
*/
2019-08-03 18:48:56 +00:00
public function testReleaseAll ()
{
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
self :: assertTrue ( $this -> instance -> acquire ( 'bar' , 1 ));
self :: assertTrue ( $this -> instance -> acquire ( 'nice' , 1 ));
2018-07-04 21:37:22 +00:00
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> isLocked ( 'foo' ));
self :: assertTrue ( $this -> instance -> isLocked ( 'bar' ));
self :: assertTrue ( $this -> instance -> isLocked ( 'nice' ));
2018-07-07 14:15:03 +00:00
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> releaseAll ());
2018-07-04 21:37:22 +00:00
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'foo' ));
self :: assertFalse ( $this -> instance -> isLocked ( 'bar' ));
self :: assertFalse ( $this -> instance -> isLocked ( 'nice' ));
2018-07-04 21:37:22 +00:00
}
2018-07-18 19:04:18 +00:00
/**
* @ small
*/
2019-08-03 18:48:56 +00:00
public function testReleaseAfterUnlock ()
{
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'foo' ));
self :: assertFalse ( $this -> instance -> isLocked ( 'bar' ));
self :: assertFalse ( $this -> instance -> isLocked ( 'nice' ));
self :: assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
self :: assertTrue ( $this -> instance -> acquire ( 'bar' , 1 ));
self :: assertTrue ( $this -> instance -> acquire ( 'nice' , 1 ));
2018-07-04 21:37:22 +00:00
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> release ( 'foo' ));
2018-07-04 21:37:22 +00:00
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'foo' ));
self :: assertTrue ( $this -> instance -> isLocked ( 'bar' ));
self :: assertTrue ( $this -> instance -> isLocked ( 'nice' ));
2018-07-07 14:15:03 +00:00
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> releaseAll ());
2018-07-04 21:37:22 +00:00
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'bar' ));
self :: assertFalse ( $this -> instance -> isLocked ( 'nice' ));
2018-07-04 21:37:22 +00:00
}
2018-07-07 17:46:16 +00:00
2019-03-04 20:28:36 +00:00
/**
* @ small
*/
public function testReleaseWitTTL ()
{
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'test' ));
self :: assertTrue ( $this -> instance -> acquire ( 'test' , 1 , 10 ));
self :: assertTrue ( $this -> instance -> isLocked ( 'test' ));
self :: assertTrue ( $this -> instance -> release ( 'test' ));
self :: assertFalse ( $this -> instance -> isLocked ( 'test' ));
2019-03-04 20:28:36 +00:00
}
2019-08-13 19:20:41 +00:00
/**
* @ small
*/
public function testGetLocks ()
{
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
self :: assertTrue ( $this -> instance -> acquire ( 'bar' , 1 ));
self :: assertTrue ( $this -> instance -> acquire ( 'nice' , 1 ));
2019-08-13 19:20:41 +00:00
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> isLocked ( 'foo' ));
self :: assertTrue ( $this -> instance -> isLocked ( 'bar' ));
self :: assertTrue ( $this -> instance -> isLocked ( 'nice' ));
2019-08-13 19:20:41 +00:00
$locks = $this -> instance -> getLocks ();
2020-10-17 12:19:57 +00:00
self :: assertContains ( 'foo' , $locks );
self :: assertContains ( 'bar' , $locks );
self :: assertContains ( 'nice' , $locks );
2019-08-13 19:20:41 +00:00
}
/**
* @ small
*/
public function testGetLocksWithPrefix ()
{
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> acquire ( 'foo' , 1 ));
self :: assertTrue ( $this -> instance -> acquire ( 'test1' , 1 ));
self :: assertTrue ( $this -> instance -> acquire ( 'test2' , 1 ));
2019-08-13 19:20:41 +00:00
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> isLocked ( 'foo' ));
self :: assertTrue ( $this -> instance -> isLocked ( 'test1' ));
self :: assertTrue ( $this -> instance -> isLocked ( 'test2' ));
2019-08-13 19:20:41 +00:00
$locks = $this -> instance -> getLocks ( 'test' );
2020-10-17 12:19:57 +00:00
self :: assertContains ( 'test1' , $locks );
self :: assertContains ( 'test2' , $locks );
self :: assertNotContains ( 'foo' , $locks );
2019-08-13 19:20:41 +00:00
}
2018-07-18 19:04:18 +00:00
/**
* @ medium
*/
2020-10-18 18:31:57 +00:00
public function testLockTTL ()
2019-08-03 18:48:56 +00:00
{
2020-10-18 18:31:57 +00:00
static :: markTestSkipped ( 'taking too much time without mocking' );
2018-11-09 19:39:47 +00:00
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'foo' ));
self :: 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
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> acquire ( 'foo' , 2 , 1 ));
self :: assertTrue ( $this -> instance -> acquire ( 'bar' , 2 , 3 ));
2018-07-07 17:46:16 +00:00
2020-10-17 12:19:57 +00:00
self :: assertTrue ( $this -> instance -> isLocked ( 'foo' ));
self :: assertTrue ( $this -> instance -> isLocked ( 'bar' ));
2018-07-07 17:46:16 +00:00
sleep ( 2 );
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'foo' ));
self :: assertTrue ( $this -> instance -> isLocked ( 'bar' ));
2018-07-07 17:46:16 +00:00
sleep ( 2 );
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'foo' ));
self :: assertFalse ( $this -> instance -> isLocked ( 'bar' ));
2018-07-07 17:46:16 +00:00
}
2019-08-17 17:33:36 +00:00
/**
* Test if releasing a non - existing lock doesn ' t throw errors
*/
public function testReleaseLockWithoutLock ()
{
2020-10-17 12:19:57 +00:00
self :: assertFalse ( $this -> instance -> isLocked ( 'wrongLock' ));
self :: assertFalse ( $this -> instance -> release ( 'wrongLock' ));
2019-08-17 17:33:36 +00:00
}
2018-07-07 14:15:03 +00:00
}