3 namespace Friendica\Test\src\Core\Lock;
7 use Friendica\BaseObject;
8 use Friendica\Core\Config\Configuration;
9 use Friendica\Core\Lock\SemaphoreLock;
11 class SemaphoreLockTest extends LockTest
13 public function setUp()
15 $dice = \Mockery::mock(Dice::class)->makePartial();
17 $app = \Mockery::mock(App::class);
18 $app->shouldReceive('getHostname')->andReturn('friendica.local');
19 $dice->shouldReceive('create')->with(App::class)->andReturn($app);
21 $configMock = \Mockery::mock(Configuration::class);
23 ->shouldReceive('get')
24 ->with('system', 'temppath', NULL, false)
26 $dice->shouldReceive('create')->with(Configuration::class)->andReturn($configMock);
28 // @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
29 BaseObject::setDependencyInjection($dice);
34 protected function getInstance()
36 return new SemaphoreLock();
39 function testLockTTL()
41 // Semaphore doesn't work with TTL
46 * Test if semaphore locking works even when trying to release locks, where the file exists
47 * but it shouldn't harm locking
49 public function testMissingFileNotOverriding()
51 $file = get_temppath() . '/test.sem';
54 $this->assertTrue(file_exists($file));
55 $this->assertFalse($this->instance->releaseLock('test', false));
56 $this->assertTrue(file_exists($file));
60 * Test overriding semaphore release with already set semaphore
61 * This test proves that semaphore locks cannot get released by other instances except themselves
63 * Check for Bug https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
64 * @see https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
66 public function testMissingFileOverriding()
68 $file = get_temppath() . '/test.sem';
71 $this->assertTrue(file_exists($file));
72 $this->assertFalse($this->instance->releaseLock('test', true));
73 $this->assertTrue(file_exists($file));
77 * Test acquire lock even the semaphore file exists, but isn't used
79 public function testOverrideSemFile()
81 $file = get_temppath() . '/test.sem';
84 $this->assertTrue(file_exists($file));
85 $this->assertTrue($this->instance->acquireLock('test'));
86 $this->assertTrue($this->instance->isLocked('test'));
87 $this->assertTrue($this->instance->releaseLock('test'));