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