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