2018-07-04 17:37:22 -04:00
|
|
|
<?php
|
|
|
|
|
2018-07-05 14:57:31 -04:00
|
|
|
namespace Friendica\Test\src\Core\Lock;
|
2018-07-04 17:37:22 -04:00
|
|
|
|
2019-08-03 14:48:56 -04:00
|
|
|
use Dice\Dice;
|
|
|
|
use Friendica\App;
|
2019-12-19 14:11:07 -05:00
|
|
|
use Friendica\Core\Config\IConfiguration;
|
2019-12-15 19:35:26 -05:00
|
|
|
use Friendica\Core\Config\JitConfiguration;
|
2019-08-04 04:26:53 -04:00
|
|
|
use Friendica\Core\Lock\SemaphoreLock;
|
2019-12-15 17:28:01 -05:00
|
|
|
use Friendica\DI;
|
2019-12-19 14:11:07 -05:00
|
|
|
use Mockery\MockInterface;
|
2018-07-04 17:37:22 -04:00
|
|
|
|
2019-08-04 04:26:53 -04:00
|
|
|
class SemaphoreLockTest extends LockTest
|
2018-07-04 17:37:22 -04:00
|
|
|
{
|
2019-01-30 14:26:17 -05:00
|
|
|
public function setUp()
|
|
|
|
{
|
2019-12-19 14:11:07 -05:00
|
|
|
/** @var MockInterface|Dice $dice */
|
2019-08-03 14:48:56 -04:00
|
|
|
$dice = \Mockery::mock(Dice::class)->makePartial();
|
2019-02-07 14:44:03 -05:00
|
|
|
|
2019-08-03 14:48:56 -04:00
|
|
|
$app = \Mockery::mock(App::class);
|
|
|
|
$app->shouldReceive('getHostname')->andReturn('friendica.local');
|
2019-12-19 14:11:07 -05:00
|
|
|
$dice->shouldReceive('create')->with(App::class, [])->andReturn($app);
|
2019-08-03 14:48:56 -04:00
|
|
|
|
2019-12-15 19:35:26 -05:00
|
|
|
$configMock = \Mockery::mock(JitConfiguration::class);
|
2019-08-03 14:48:56 -04:00
|
|
|
$configMock
|
2019-02-07 14:44:03 -05:00
|
|
|
->shouldReceive('get')
|
2019-08-03 14:48:56 -04:00
|
|
|
->with('system', 'temppath', NULL, false)
|
2019-08-17 13:38:51 -04:00
|
|
|
->andReturn('/tmp/');
|
2019-12-19 14:11:07 -05:00
|
|
|
$dice->shouldReceive('create')->with(IConfiguration::class, [])->andReturn($configMock);
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
// @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
|
2019-12-15 17:28:01 -05:00
|
|
|
DI::init($dice);
|
2019-08-13 15:20:41 -04:00
|
|
|
|
|
|
|
parent::setUp();
|
2019-01-30 14:26:17 -05:00
|
|
|
}
|
|
|
|
|
2018-07-04 17:37:22 -04:00
|
|
|
protected function getInstance()
|
|
|
|
{
|
2019-08-04 04:26:53 -04:00
|
|
|
return new SemaphoreLock();
|
2018-07-04 17:37:22 -04:00
|
|
|
}
|
2018-07-07 13:46:16 -04:00
|
|
|
|
|
|
|
function testLockTTL()
|
|
|
|
{
|
|
|
|
// Semaphore doesn't work with TTL
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-17 13:33:36 -04:00
|
|
|
|
|
|
|
/**
|
2019-08-17 13:38:51 -04:00
|
|
|
* Test if semaphore locking works even when trying to release locks, where the file exists
|
|
|
|
* but it shouldn't harm locking
|
2019-08-17 13:33:36 -04:00
|
|
|
*/
|
|
|
|
public function testMissingFileNotOverriding()
|
|
|
|
{
|
|
|
|
$file = get_temppath() . '/test.sem';
|
2019-08-17 13:38:51 -04:00
|
|
|
touch($file);
|
2019-08-17 13:33:36 -04:00
|
|
|
|
|
|
|
$this->assertTrue(file_exists($file));
|
|
|
|
$this->assertFalse($this->instance->releaseLock('test', false));
|
|
|
|
$this->assertTrue(file_exists($file));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test overriding semaphore release with already set semaphore
|
|
|
|
* This test proves that semaphore locks cannot get released by other instances except themselves
|
|
|
|
*
|
|
|
|
* Check for Bug https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
|
|
|
|
* @see https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
|
|
|
|
*/
|
|
|
|
public function testMissingFileOverriding()
|
|
|
|
{
|
|
|
|
$file = get_temppath() . '/test.sem';
|
2019-08-17 13:38:51 -04:00
|
|
|
touch($file);
|
2019-08-17 13:33:36 -04:00
|
|
|
|
|
|
|
$this->assertTrue(file_exists($file));
|
|
|
|
$this->assertFalse($this->instance->releaseLock('test', true));
|
|
|
|
$this->assertTrue(file_exists($file));
|
|
|
|
}
|
2019-08-17 13:38:51 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test acquire lock even the semaphore file exists, but isn't used
|
|
|
|
*/
|
|
|
|
public function testOverrideSemFile()
|
|
|
|
{
|
|
|
|
$file = get_temppath() . '/test.sem';
|
|
|
|
touch($file);
|
|
|
|
|
|
|
|
$this->assertTrue(file_exists($file));
|
2020-01-06 18:20:31 -05:00
|
|
|
$this->assertTrue($this->instance->acquire('test'));
|
2019-08-17 13:38:51 -04:00
|
|
|
$this->assertTrue($this->instance->isLocked('test'));
|
|
|
|
$this->assertTrue($this->instance->releaseLock('test'));
|
|
|
|
}
|
2018-07-07 10:15:03 -04:00
|
|
|
}
|