3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Core\Lock;
26 use Friendica\Core\Config\IConfig;
27 use Friendica\Core\Config\JitConfig;
28 use Friendica\Core\Lock\SemaphoreLock;
31 use Mockery\MockInterface;
33 class SemaphoreLockTest extends LockTest
35 protected function setUp(): void
37 /** @var MockInterface|Dice $dice */
38 $dice = Mockery::mock(Dice::class)->makePartial();
40 $app = Mockery::mock(App::class);
41 $app->shouldReceive('getHostname')->andReturn('friendica.local');
42 $dice->shouldReceive('create')->with(App::class)->andReturn($app);
44 $configMock = Mockery::mock(JitConfig::class);
46 ->shouldReceive('get')
47 ->with('system', 'temppath')
49 $dice->shouldReceive('create')->with(IConfig::class)->andReturn($configMock);
51 // @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
57 protected function getInstance()
59 return new SemaphoreLock();
63 * @doesNotPerformAssertions
65 public function testLockTTL()
67 self::markTestSkipped("Semaphore doesn't work with TTL");
71 * Test if semaphore locking works even when trying to release locks, where the file exists
72 * but it shouldn't harm locking
74 public function testMissingFileNotOverriding()
76 $file = get_temppath() . '/test.sem';
79 self::assertTrue(file_exists($file));
80 self::assertFalse($this->instance->release('test', false));
81 self::assertTrue(file_exists($file));
85 * Test overriding semaphore release with already set semaphore
86 * This test proves that semaphore locks cannot get released by other instances except themselves
88 * Check for Bug https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
90 * @see https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
92 public function testMissingFileOverriding()
94 $file = get_temppath() . '/test.sem';
97 self::assertTrue(file_exists($file));
98 self::assertFalse($this->instance->release('test', true));
99 self::assertTrue(file_exists($file));
103 * Test acquire lock even the semaphore file exists, but isn't used
105 public function testOverrideSemFile()
107 $file = get_temppath() . '/test.sem';
110 self::assertTrue(file_exists($file));
111 self::assertTrue($this->instance->acquire('test'));
112 self::assertTrue($this->instance->isLocked('test'));
113 self::assertTrue($this->instance->release('test'));