]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Core/Lock/LockTest.php
Fixing redis cachekey
[friendica.git] / tests / src / Core / Lock / LockTest.php
index dafbd74a6ff19d1bc1371f6d9cf42ee76b19e19f..28f51733f428d9f7748dd0d52fa5a037a50bc047 100644 (file)
@@ -2,13 +2,20 @@
 
 namespace Friendica\Test\src\Core\Lock;
 
-use Friendica\App;
-use Friendica\Core\Config;
-use Friendica\Test\DatabaseTest;
-use PHPUnit\Framework\TestCase;
+use Friendica\Test\MockedTest;
+use Friendica\Test\Util\AppMockTrait;
+use Friendica\Test\Util\VFSTrait;
 
-abstract class LockTest extends DatabaseTest
+abstract class LockTest extends MockedTest
 {
+       use VFSTrait;
+       use AppMockTrait;
+
+       /**
+        * @var int Start time of the mock (used for time operations)
+        */
+       protected $startTime = 1417011228;
+
        /**
         * @var \Friendica\Core\Lock\ILockDriver
         */
@@ -18,80 +25,122 @@ abstract class LockTest extends DatabaseTest
 
        protected function setUp()
        {
-               global $a;
+               // Reusable App object
+               $this->setUpVfsDir();
+               $this->mockApp($this->root);
+               $this->app
+                       ->shouldReceive('getHostname')
+                       ->andReturn('friendica.local');
+
                parent::setUp();
                $this->instance = $this->getInstance();
+               $this->instance->releaseAll();
+       }
 
-               // Reusable App object
-               $this->app = new App(__DIR__.'/../');
-               $a = $this->app;
-
-               // Default config
-               Config::set('config', 'hostname', 'localhost');
-               Config::set('system', 'throttle_limit_day', 100);
-               Config::set('system', 'throttle_limit_week', 100);
-               Config::set('system', 'throttle_limit_month', 100);
-               Config::set('system', 'theme', 'system_theme');
+       protected function tearDown()
+       {
+               $this->instance->releaseAll();
+               parent::tearDown();
        }
 
+       /**
+        * @small
+        */
        public function testLock() {
-               $this->instance->acquireLock('foo', 1);
+               $this->assertFalse($this->instance->isLocked('foo'));
+               $this->assertTrue($this->instance->acquireLock('foo', 1));
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->assertFalse($this->instance->isLocked('bar'));
        }
 
+       /**
+        * @small
+        */
        public function testDoubleLock() {
-               $this->instance->acquireLock('foo', 1);
+               $this->assertFalse($this->instance->isLocked('foo'));
+               $this->assertTrue($this->instance->acquireLock('foo', 1));
                $this->assertTrue($this->instance->isLocked('foo'));
                // We already locked it
                $this->assertTrue($this->instance->acquireLock('foo', 1));
        }
 
+       /**
+        * @small
+        */
        public function testReleaseLock() {
-               $this->instance->acquireLock('foo', 1);
+               $this->assertFalse($this->instance->isLocked('foo'));
+               $this->assertTrue($this->instance->acquireLock('foo', 1));
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->instance->releaseLock('foo');
                $this->assertFalse($this->instance->isLocked('foo'));
        }
 
+       /**
+        * @small
+        */
        public function testReleaseAll() {
-               $this->instance->acquireLock('foo', 1);
-               $this->instance->acquireLock('bar', 1);
-               $this->instance->acquireLock('nice', 1);
+               $this->assertTrue($this->instance->acquireLock('foo', 1));
+               $this->assertTrue($this->instance->acquireLock('bar', 1));
+               $this->assertTrue($this->instance->acquireLock('nice', 1));
 
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->isLocked('bar'));
                $this->assertTrue($this->instance->isLocked('nice'));
 
-               $this->instance->releaseAll();
+               $this->assertTrue($this->instance->releaseAll());
 
                $this->assertFalse($this->instance->isLocked('foo'));
                $this->assertFalse($this->instance->isLocked('bar'));
                $this->assertFalse($this->instance->isLocked('nice'));
        }
 
+       /**
+        * @small
+        */
        public function testReleaseAfterUnlock() {
-               $this->instance->acquireLock('foo', 1);
-               $this->instance->acquireLock('bar', 1);
-               $this->instance->acquireLock('nice', 1);
+               $this->assertFalse($this->instance->isLocked('foo'));
+               $this->assertFalse($this->instance->isLocked('bar'));
+               $this->assertFalse($this->instance->isLocked('nice'));
+               $this->assertTrue($this->instance->acquireLock('foo', 1));
+               $this->assertTrue($this->instance->acquireLock('bar', 1));
+               $this->assertTrue($this->instance->acquireLock('nice', 1));
 
-               $this->instance->releaseLock('foo');
+               $this->assertTrue($this->instance->releaseLock('foo'));
 
                $this->assertFalse($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->isLocked('bar'));
                $this->assertTrue($this->instance->isLocked('nice'));
 
-               $this->instance->releaseAll();
+               $this->assertTrue($this->instance->releaseAll());
 
                $this->assertFalse($this->instance->isLocked('bar'));
                $this->assertFalse($this->instance->isLocked('nice'));
        }
 
+       /**
+        * @small
+        */
+       public function testReleaseWitTTL()
+       {
+               $this->assertFalse($this->instance->isLocked('test'));
+               $this->assertTrue($this->instance->acquireLock('test', 1, 10));
+               $this->assertTrue($this->instance->isLocked('test'));
+               $this->assertTrue($this->instance->releaseLock('test'));
+               $this->assertFalse($this->instance->isLocked('test'));
+       }
+
+       /**
+        * @medium
+        */
        function testLockTTL() {
+               $this->markTestSkipped('taking too much time without mocking');
+
+               $this->assertFalse($this->instance->isLocked('foo'));
+               $this->assertFalse($this->instance->isLocked('bar'));
 
                // TODO [nupplaphil] - Because of the Datetime-Utils for the database, we have to wait a FULL second between the checks to invalidate the db-locks/cache
-               $this->instance->acquireLock('foo', 1, 1);
-               $this->instance->acquireLock('bar', 1, 3);
+               $this->assertTrue($this->instance->acquireLock('foo', 2, 1));
+               $this->assertTrue($this->instance->acquireLock('bar', 2, 3));
 
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->isLocked('bar'));