3 namespace Friendica\Test\src\Core\Lock;
7 use Friendica\Core\Config\IConfiguration;
8 use Friendica\Core\Config\JitConfiguration;
9 use Friendica\Core\Lock\SemaphoreLock;
11 use Mockery\MockInterface;
13 class SemaphoreLockTest extends LockTest
15 public function setUp()
17 /** @var MockInterface|Dice $dice */
18 $dice = \Mockery::mock(Dice::class)->makePartial();
20 $app = \Mockery::mock(App::class);
21 $app->shouldReceive('getHostname')->andReturn('friendica.local');
22 $dice->shouldReceive('create')->with(App::class, [])->andReturn($app);
24 $configMock = \Mockery::mock(JitConfiguration::class);
26 ->shouldReceive('get')
27 ->with('system', 'temppath', NULL, false)
29 $dice->shouldReceive('create')->with(IConfiguration::class, [])->andReturn($configMock);
31 // @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
37 protected function getInstance()
39 return new SemaphoreLock();
42 function testLockTTL()
44 // Semaphore doesn't work with TTL
49 * Test if semaphore locking works even when trying to release locks, where the file exists
50 * but it shouldn't harm locking
52 public function testMissingFileNotOverriding()
54 $file = get_temppath() . '/test.sem';
57 $this->assertTrue(file_exists($file));
58 $this->assertFalse($this->instance->release('test', false));
59 $this->assertTrue(file_exists($file));
63 * Test overriding semaphore release with already set semaphore
64 * This test proves that semaphore locks cannot get released by other instances except themselves
66 * Check for Bug https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
67 * @see https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
69 public function testMissingFileOverriding()
71 $file = get_temppath() . '/test.sem';
74 $this->assertTrue(file_exists($file));
75 $this->assertFalse($this->instance->release('test', true));
76 $this->assertTrue(file_exists($file));
80 * Test acquire lock even the semaphore file exists, but isn't used
82 public function testOverrideSemFile()
84 $file = get_temppath() . '/test.sem';
87 $this->assertTrue(file_exists($file));
88 $this->assertTrue($this->instance->acquire('test'));
89 $this->assertTrue($this->instance->isLocked('test'));
90 $this->assertTrue($this->instance->release('test'));