]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Lock/SemaphoreLockTest.php
Remove deprecated App::module - replace with DI::module()->getName()
[friendica.git] / tests / src / Core / Lock / SemaphoreLockTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Lock;
4
5 use Dice\Dice;
6 use Friendica\App;
7 use Friendica\Core\Config\Configuration;
8 use Friendica\Core\Config\JitConfiguration;
9 use Friendica\Core\Lock\SemaphoreLock;
10 use Friendica\DI;
11
12 class SemaphoreLockTest extends LockTest
13 {
14         public function setUp()
15         {
16                 $dice = \Mockery::mock(Dice::class)->makePartial();
17
18                 $app = \Mockery::mock(App::class);
19                 $app->shouldReceive('getHostname')->andReturn('friendica.local');
20                 $dice->shouldReceive('create')->with(App::class)->andReturn($app);
21
22                 $configMock = \Mockery::mock(JitConfiguration::class);
23                 $configMock
24                         ->shouldReceive('get')
25                         ->with('system', 'temppath', NULL, false)
26                         ->andReturn('/tmp/');
27                 $dice->shouldReceive('create')->with(Configuration::class)->andReturn($configMock);
28
29                 // @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
30                 DI::init($dice);
31
32                 parent::setUp();
33         }
34
35         protected function getInstance()
36         {
37                 return new SemaphoreLock();
38         }
39
40         function testLockTTL()
41         {
42                 // Semaphore doesn't work with TTL
43                 return true;
44         }
45
46         /**
47          * Test if semaphore locking works even when trying to release locks, where the file exists
48          * but it shouldn't harm locking
49          */
50         public function testMissingFileNotOverriding()
51         {
52                 $file = get_temppath() . '/test.sem';
53                 touch($file);
54
55                 $this->assertTrue(file_exists($file));
56                 $this->assertFalse($this->instance->releaseLock('test', false));
57                 $this->assertTrue(file_exists($file));
58         }
59
60         /**
61          * Test overriding semaphore release with already set semaphore
62          * This test proves that semaphore locks cannot get released by other instances except themselves
63          *
64          * Check for Bug https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
65          * @see https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
66          */
67         public function testMissingFileOverriding()
68         {
69                 $file = get_temppath() . '/test.sem';
70                 touch($file);
71
72                 $this->assertTrue(file_exists($file));
73                 $this->assertFalse($this->instance->releaseLock('test', true));
74                 $this->assertTrue(file_exists($file));
75         }
76
77         /**
78          * Test acquire lock even the semaphore file exists, but isn't used
79          */
80         public function testOverrideSemFile()
81         {
82                 $file = get_temppath() . '/test.sem';
83                 touch($file);
84
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'));
89         }
90 }