]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Lock/SemaphoreLockTest.php
Disable testImagickNotFound due low impact and test is not working (needs care in...
[friendica.git] / tests / src / Core / Lock / SemaphoreLockTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Core\Lock;
23
24 use Dice\Dice;
25 use Friendica\App;
26 use Friendica\Core\Config\IConfig;
27 use Friendica\Core\Config\JitConfig;
28 use Friendica\Core\Lock\SemaphoreLock;
29 use Friendica\DI;
30 use Mockery\MockInterface;
31
32 class SemaphoreLockTest extends LockTest
33 {
34         public function setUp()
35         {
36                 /** @var MockInterface|Dice $dice */
37                 $dice = \Mockery::mock(Dice::class)->makePartial();
38
39                 $app = \Mockery::mock(App::class);
40                 $app->shouldReceive('getHostname')->andReturn('friendica.local');
41                 $dice->shouldReceive('create')->with(App::class)->andReturn($app);
42
43                 $configMock = \Mockery::mock(JitConfig::class);
44                 $configMock
45                         ->shouldReceive('get')
46                         ->with('system', 'temppath')
47                         ->andReturn('/tmp/');
48                 $dice->shouldReceive('create')->with(IConfig::class)->andReturn($configMock);
49
50                 // @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
51                 DI::init($dice);
52
53                 parent::setUp();
54         }
55
56         protected function getInstance()
57         {
58                 return new SemaphoreLock();
59         }
60
61         function testLockTTL()
62         {
63                 // Semaphore doesn't work with TTL
64                 return true;
65         }
66
67         /**
68          * Test if semaphore locking works even when trying to release locks, where the file exists
69          * but it shouldn't harm locking
70          */
71         public function testMissingFileNotOverriding()
72         {
73                 $file = get_temppath() . '/test.sem';
74                 touch($file);
75
76                 $this->assertTrue(file_exists($file));
77                 $this->assertFalse($this->instance->release('test', false));
78                 $this->assertTrue(file_exists($file));
79         }
80
81         /**
82          * Test overriding semaphore release with already set semaphore
83          * This test proves that semaphore locks cannot get released by other instances except themselves
84          *
85          * Check for Bug https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
86          * @see https://github.com/friendica/friendica/issues/7298#issuecomment-521996540
87          */
88         public function testMissingFileOverriding()
89         {
90                 $file = get_temppath() . '/test.sem';
91                 touch($file);
92
93                 $this->assertTrue(file_exists($file));
94                 $this->assertFalse($this->instance->release('test', true));
95                 $this->assertTrue(file_exists($file));
96         }
97
98         /**
99          * Test acquire lock even the semaphore file exists, but isn't used
100          */
101         public function testOverrideSemFile()
102         {
103                 $file = get_temppath() . '/test.sem';
104                 touch($file);
105
106                 $this->assertTrue(file_exists($file));
107                 $this->assertTrue($this->instance->acquire('test'));
108                 $this->assertTrue($this->instance->isLocked('test'));
109                 $this->assertTrue($this->instance->release('test'));
110         }
111 }