3 namespace Friendica\Test\src\Core\Lock;
7 use Friendica\Core\Config\Configuration;
8 use Friendica\Core\Config\JitConfiguration;
9 use Friendica\Core\Lock\SemaphoreLock;
12 class SemaphoreLockTest extends LockTest
14 public function setUp()
16 $dice = \Mockery::mock(Dice::class)->makePartial();
18 $app = \Mockery::mock(App::class);
19 $app->shouldReceive('getHostname')->andReturn('friendica.local');
20 $dice->shouldReceive('create')->with(App::class)->andReturn($app);
22 $configMock = \Mockery::mock(JitConfiguration::class);
24 ->shouldReceive('get')
25 ->with('system', 'temppath', NULL, false)
27 $dice->shouldReceive('create')->with(Configuration::class)->andReturn($configMock);
29 // @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
35 protected function getInstance()
37 return new SemaphoreLock();
40 function testLockTTL()
42 // Semaphore doesn't work with TTL
47 * Test if semaphore locking works even when trying to release locks, where the file exists
48 * but it shouldn't harm locking
50 public function testMissingFileNotOverriding()
52 $file = get_temppath() . '/test.sem';
55 $this->assertTrue(file_exists($file));
56 $this->assertFalse($this->instance->releaseLock('test', false));
57 $this->assertTrue(file_exists($file));
61 * Test overriding semaphore release with already set semaphore
62 * This test proves that semaphore locks cannot get released by other instances except themselves
64 * Check for Bug https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
65 * @see https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
67 public function testMissingFileOverriding()
69 $file = get_temppath() . '/test.sem';
72 $this->assertTrue(file_exists($file));
73 $this->assertFalse($this->instance->releaseLock('test', true));
74 $this->assertTrue(file_exists($file));
78 * Test acquire lock even the semaphore file exists, but isn't used
80 public function testOverrideSemFile()
82 $file = get_temppath() . '/test.sem';
85 $this->assertTrue(file_exists($file));
86 $this->assertTrue($this->instance->acquireLock('test'));
87 $this->assertTrue($this->instance->isLocked('test'));
88 $this->assertTrue($this->instance->releaseLock('test'));