2018-07-04 21:37:22 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2023-01-01 14:36:24 +00:00
|
|
|
* @copyright Copyright (C) 2010-2023, 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;
|
2021-10-26 19:44:29 +00:00
|
|
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
2023-02-04 23:15:01 +00:00
|
|
|
use Friendica\Core\Config\Model\ReadOnlyFileConfig;
|
2023-01-01 20:10:41 +00:00
|
|
|
use Friendica\Core\Config\ValueObject\Cache;
|
2021-11-04 20:29:59 +00:00
|
|
|
use Friendica\Core\System;
|
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
|
|
|
|
2023-01-01 20:10:41 +00:00
|
|
|
$configCache = new Cache(['system' => ['temppath' => '/tmp']]);
|
2023-02-04 23:15:01 +00:00
|
|
|
$configMock = new ReadOnlyFileConfig($configCache);
|
2021-10-26 19:44:29 +00:00
|
|
|
$dice->shouldReceive('create')->with(IManageConfigValues::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
|
2023-02-04 23:15:01 +00:00
|
|
|
DI::init($dice, true);
|
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()
|
|
|
|
{
|
2021-10-23 09:36:37 +00:00
|
|
|
return new \Friendica\Core\Lock\Type\SemaphoreLock();
|
2018-07-04 21:37:22 +00:00
|
|
|
}
|
2018-07-07 17:46:16 +00:00
|
|
|
|
2021-05-16 21:39:03 +00:00
|
|
|
/**
|
|
|
|
* @doesNotPerformAssertions
|
|
|
|
*/
|
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()
|
|
|
|
{
|
2021-11-04 20:29:59 +00:00
|
|
|
$file = System::getTempPath() . '/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()
|
|
|
|
{
|
2021-11-04 20:29:59 +00:00
|
|
|
$file = System::getTempPath() . '/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()
|
|
|
|
{
|
2021-11-04 20:29:59 +00:00
|
|
|
$file = System::getTempPath() . '/test.sem';
|
2019-08-17 17:38:51 +00:00
|
|
|
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
|
|
|
}
|