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