2018-07-04 21:37:22 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2021-03-29 06:40:20 +00:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 14:45:36 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-07-04 21:37:22 +00:00
|
|
|
|
2018-07-05 18:57:31 +00:00
|
|
|
namespace Friendica\Test\src\Core\Lock;
|
2018-07-04 21:37:22 +00:00
|
|
|
|
2019-08-03 18:48:56 +00:00
|
|
|
use Dice\Dice;
|
|
|
|
use Friendica\App;
|
2020-01-19 20:29:36 +00:00
|
|
|
use Friendica\Core\Config\IConfig;
|
|
|
|
use Friendica\Core\Config\JitConfig;
|
2019-08-04 08:26:53 +00:00
|
|
|
use Friendica\Core\Lock\SemaphoreLock;
|
2019-12-15 22:28:01 +00:00
|
|
|
use Friendica\DI;
|
2020-10-18 18:31:57 +00:00
|
|
|
use Mockery;
|
2019-12-19 19:11:07 +00:00
|
|
|
use Mockery\MockInterface;
|
2018-07-04 21:37:22 +00:00
|
|
|
|
2019-08-04 08:26:53 +00:00
|
|
|
class SemaphoreLockTest extends LockTest
|
2018-07-04 21:37:22 +00:00
|
|
|
{
|
2021-04-01 21:04:30 +00:00
|
|
|
protected function setUp(): void
|
2019-01-30 19:26:17 +00:00
|
|
|
{
|
2019-12-19 19:11:07 +00:00
|
|
|
/** @var MockInterface|Dice $dice */
|
2020-10-18 18:31:57 +00:00
|
|
|
$dice = Mockery::mock(Dice::class)->makePartial();
|
2019-02-07 19:44:03 +00:00
|
|
|
|
2020-10-18 18:31:57 +00:00
|
|
|
$app = Mockery::mock(App::class);
|
2019-08-03 18:48:56 +00:00
|
|
|
$app->shouldReceive('getHostname')->andReturn('friendica.local');
|
2020-01-18 12:48:29 +00:00
|
|
|
$dice->shouldReceive('create')->with(App::class)->andReturn($app);
|
2019-08-03 18:48:56 +00:00
|
|
|
|
2020-10-18 18:31:57 +00:00
|
|
|
$configMock = Mockery::mock(JitConfig::class);
|
2019-08-03 18:48:56 +00:00
|
|
|
$configMock
|
2019-02-07 19:44:03 +00:00
|
|
|
->shouldReceive('get')
|
2020-01-19 21:38:33 +00:00
|
|
|
->with('system', 'temppath')
|
2019-08-17 17:38:51 +00:00
|
|
|
->andReturn('/tmp/');
|
2020-01-19 20:29:36 +00:00
|
|
|
$dice->shouldReceive('create')->with(IConfig::class)->andReturn($configMock);
|
2019-08-03 18:48:56 +00:00
|
|
|
|
|
|
|
// @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
|
2019-12-15 22:28:01 +00:00
|
|
|
DI::init($dice);
|
2019-08-13 19:20:41 +00:00
|
|
|
|
|
|
|
parent::setUp();
|
2019-01-30 19:26:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 21:37:22 +00:00
|
|
|
protected function getInstance()
|
|
|
|
{
|
2019-08-04 08:26:53 +00:00
|
|
|
return new SemaphoreLock();
|
2018-07-04 21:37:22 +00:00
|
|
|
}
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2020-10-18 18:31:57 +00:00
|
|
|
public function testLockTTL()
|
2018-07-07 17:46:16 +00:00
|
|
|
{
|
2021-04-01 19:19:45 +00:00
|
|
|
self::markTestSkipped("Semaphore doesn't work with TTL");
|
2018-07-07 17:46:16 +00:00
|
|
|
}
|
2019-08-17 17:33:36 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-17 17:38:51 +00: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 17:33:36 +00:00
|
|
|
*/
|
|
|
|
public function testMissingFileNotOverriding()
|
|
|
|
{
|
|
|
|
$file = get_temppath() . '/test.sem';
|
2019-08-17 17:38:51 +00:00
|
|
|
touch($file);
|
2019-08-17 17:33:36 +00:00
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertTrue(file_exists($file));
|
|
|
|
self::assertFalse($this->instance->release('test', false));
|
|
|
|
self::assertTrue(file_exists($file));
|
2019-08-17 17:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2020-10-18 18:31:57 +00:00
|
|
|
*
|
2019-08-17 17:33:36 +00:00
|
|
|
* @see https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
|
|
|
|
*/
|
|
|
|
public function testMissingFileOverriding()
|
|
|
|
{
|
|
|
|
$file = get_temppath() . '/test.sem';
|
2019-08-17 17:38:51 +00:00
|
|
|
touch($file);
|
2019-08-17 17:33:36 +00:00
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertTrue(file_exists($file));
|
|
|
|
self::assertFalse($this->instance->release('test', true));
|
|
|
|
self::assertTrue(file_exists($file));
|
2019-08-17 17:33:36 +00:00
|
|
|
}
|
2019-08-17 17:38:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test acquire lock even the semaphore file exists, but isn't used
|
|
|
|
*/
|
|
|
|
public function testOverrideSemFile()
|
|
|
|
{
|
|
|
|
$file = get_temppath() . '/test.sem';
|
|
|
|
touch($file);
|
|
|
|
|
2020-10-17 12:19:57 +00:00
|
|
|
self::assertTrue(file_exists($file));
|
|
|
|
self::assertTrue($this->instance->acquire('test'));
|
|
|
|
self::assertTrue($this->instance->isLocked('test'));
|
|
|
|
self::assertTrue($this->instance->release('test'));
|
2019-08-17 17:38:51 +00:00
|
|
|
}
|
2018-07-07 14:15:03 +00:00
|
|
|
}
|