]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Core/Lock/LockTest.php
Merge pull request #10703 from tobiasd/20210913-fr
[friendica.git] / tests / src / Core / Lock / LockTest.php
index 0c231713ae670c1032a1802a8e3f8f730374426f..8fc4926b06b08293bd26377c416fb7a149812cb9 100644 (file)
@@ -1,7 +1,27 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Test\src\Core\Lock;
 
+use Friendica\Core\Lock\ILock;
 use Friendica\Test\MockedTest;
 
 abstract class LockTest extends MockedTest
@@ -12,23 +32,23 @@ abstract class LockTest extends MockedTest
        protected $startTime = 1417011228;
 
        /**
-        * @var \Friendica\Core\Lock\ILock
+        * @var ILock
         */
        protected $instance;
 
        abstract protected function getInstance();
 
-       protected function setUp()
+       protected function setUp(): void
        {
                parent::setUp();
 
                $this->instance = $this->getInstance();
-               $this->instance->releaseAll();
+               $this->instance->releaseAll(true);
        }
 
-       protected function tearDown()
+       protected function tearDown(): void
        {
-               $this->instance->releaseAll();
+               $this->instance->releaseAll(true);
                parent::tearDown();
        }
 
@@ -37,10 +57,10 @@ abstract class LockTest extends MockedTest
         */
        public function testLock()
        {
-               $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'));
+               self::assertFalse($this->instance->isLocked('foo'));
+               self::assertTrue($this->instance->acquire('foo', 1));
+               self::assertTrue($this->instance->isLocked('foo'));
+               self::assertFalse($this->instance->isLocked('bar'));
        }
 
        /**
@@ -48,11 +68,11 @@ abstract class LockTest extends MockedTest
         */
        public function testDoubleLock()
        {
-               $this->assertFalse($this->instance->isLocked('foo'));
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
-               $this->assertTrue($this->instance->isLocked('foo'));
+               self::assertFalse($this->instance->isLocked('foo'));
+               self::assertTrue($this->instance->acquire('foo', 1));
+               self::assertTrue($this->instance->isLocked('foo'));
                // We already locked it
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
+               self::assertTrue($this->instance->acquire('foo', 1));
        }
 
        /**
@@ -60,11 +80,11 @@ abstract class LockTest extends MockedTest
         */
        public function testReleaseLock()
        {
-               $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'));
+               self::assertFalse($this->instance->isLocked('foo'));
+               self::assertTrue($this->instance->acquire('foo', 1));
+               self::assertTrue($this->instance->isLocked('foo'));
+               $this->instance->release('foo');
+               self::assertFalse($this->instance->isLocked('foo'));
        }
 
        /**
@@ -72,19 +92,19 @@ abstract class LockTest extends MockedTest
         */
        public function testReleaseAll()
        {
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
-               $this->assertTrue($this->instance->acquireLock('bar', 1));
-               $this->assertTrue($this->instance->acquireLock('nice', 1));
+               self::assertTrue($this->instance->acquire('foo', 1));
+               self::assertTrue($this->instance->acquire('bar', 1));
+               self::assertTrue($this->instance->acquire('nice', 1));
 
-               $this->assertTrue($this->instance->isLocked('foo'));
-               $this->assertTrue($this->instance->isLocked('bar'));
-               $this->assertTrue($this->instance->isLocked('nice'));
+               self::assertTrue($this->instance->isLocked('foo'));
+               self::assertTrue($this->instance->isLocked('bar'));
+               self::assertTrue($this->instance->isLocked('nice'));
 
-               $this->assertTrue($this->instance->releaseAll());
+               self::assertTrue($this->instance->releaseAll());
 
-               $this->assertFalse($this->instance->isLocked('foo'));
-               $this->assertFalse($this->instance->isLocked('bar'));
-               $this->assertFalse($this->instance->isLocked('nice'));
+               self::assertFalse($this->instance->isLocked('foo'));
+               self::assertFalse($this->instance->isLocked('bar'));
+               self::assertFalse($this->instance->isLocked('nice'));
        }
 
        /**
@@ -92,23 +112,23 @@ abstract class LockTest extends MockedTest
         */
        public function testReleaseAfterUnlock()
        {
-               $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));
+               self::assertFalse($this->instance->isLocked('foo'));
+               self::assertFalse($this->instance->isLocked('bar'));
+               self::assertFalse($this->instance->isLocked('nice'));
+               self::assertTrue($this->instance->acquire('foo', 1));
+               self::assertTrue($this->instance->acquire('bar', 1));
+               self::assertTrue($this->instance->acquire('nice', 1));
 
-               $this->assertTrue($this->instance->releaseLock('foo'));
+               self::assertTrue($this->instance->release('foo'));
 
-               $this->assertFalse($this->instance->isLocked('foo'));
-               $this->assertTrue($this->instance->isLocked('bar'));
-               $this->assertTrue($this->instance->isLocked('nice'));
+               self::assertFalse($this->instance->isLocked('foo'));
+               self::assertTrue($this->instance->isLocked('bar'));
+               self::assertTrue($this->instance->isLocked('nice'));
 
-               $this->assertTrue($this->instance->releaseAll());
+               self::assertTrue($this->instance->releaseAll());
 
-               $this->assertFalse($this->instance->isLocked('bar'));
-               $this->assertFalse($this->instance->isLocked('nice'));
+               self::assertFalse($this->instance->isLocked('bar'));
+               self::assertFalse($this->instance->isLocked('nice'));
        }
 
        /**
@@ -116,38 +136,87 @@ abstract class LockTest extends MockedTest
         */
        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'));
+               self::assertFalse($this->instance->isLocked('test'));
+               self::assertTrue($this->instance->acquire('test', 1, 10));
+               self::assertTrue($this->instance->isLocked('test'));
+               self::assertTrue($this->instance->release('test'));
+               self::assertFalse($this->instance->isLocked('test'));
+       }
+
+       /**
+        * @small
+        */
+       public function testGetLocks()
+       {
+               self::assertTrue($this->instance->acquire('foo', 1));
+               self::assertTrue($this->instance->acquire('bar', 1));
+               self::assertTrue($this->instance->acquire('nice', 1));
+
+               self::assertTrue($this->instance->isLocked('foo'));
+               self::assertTrue($this->instance->isLocked('bar'));
+               self::assertTrue($this->instance->isLocked('nice'));
+
+               $locks = $this->instance->getLocks();
+
+               self::assertContains('foo', $locks);
+               self::assertContains('bar', $locks);
+               self::assertContains('nice', $locks);
+       }
+
+       /**
+        * @small
+        */
+       public function testGetLocksWithPrefix()
+       {
+               self::assertTrue($this->instance->acquire('foo', 1));
+               self::assertTrue($this->instance->acquire('test1', 1));
+               self::assertTrue($this->instance->acquire('test2', 1));
+
+               self::assertTrue($this->instance->isLocked('foo'));
+               self::assertTrue($this->instance->isLocked('test1'));
+               self::assertTrue($this->instance->isLocked('test2'));
+
+               $locks = $this->instance->getLocks('test');
+
+               self::assertContains('test1', $locks);
+               self::assertContains('test2', $locks);
+               self::assertNotContains('foo', $locks);
        }
 
        /**
         * @medium
         */
-       function testLockTTL()
+       public function testLockTTL()
        {
-               $this->markTestSkipped('taking too much time without mocking');
+               static::markTestSkipped('taking too much time without mocking');
 
-               $this->assertFalse($this->instance->isLocked('foo'));
-               $this->assertFalse($this->instance->isLocked('bar'));
+               self::assertFalse($this->instance->isLocked('foo'));
+               self::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->assertTrue($this->instance->acquireLock('foo', 2, 1));
-               $this->assertTrue($this->instance->acquireLock('bar', 2, 3));
+               self::assertTrue($this->instance->acquire('foo', 2, 1));
+               self::assertTrue($this->instance->acquire('bar', 2, 3));
 
-               $this->assertTrue($this->instance->isLocked('foo'));
-               $this->assertTrue($this->instance->isLocked('bar'));
+               self::assertTrue($this->instance->isLocked('foo'));
+               self::assertTrue($this->instance->isLocked('bar'));
 
                sleep(2);
 
-               $this->assertFalse($this->instance->isLocked('foo'));
-               $this->assertTrue($this->instance->isLocked('bar'));
+               self::assertFalse($this->instance->isLocked('foo'));
+               self::assertTrue($this->instance->isLocked('bar'));
 
                sleep(2);
 
-               $this->assertFalse($this->instance->isLocked('foo'));
-               $this->assertFalse($this->instance->isLocked('bar'));
+               self::assertFalse($this->instance->isLocked('foo'));
+               self::assertFalse($this->instance->isLocked('bar'));
+       }
+
+       /**
+        * Test if releasing a non-existing lock doesn't throw errors
+        */
+       public function testReleaseLockWithoutLock()
+       {
+               self::assertFalse($this->instance->isLocked('wrongLock'));
+               self::assertFalse($this->instance->release('wrongLock'));
        }
 }