]> git.mxchange.org Git - friendica.git/commitdiff
Fixings
authorPhilipp Holzer <admin@philipp.info>
Thu, 5 Jul 2018 18:57:31 +0000 (20:57 +0200)
committerPhilipp Holzer <admin@philipp.info>
Thu, 5 Jul 2018 18:57:31 +0000 (20:57 +0200)
- fixed test for semaphore
- fixed some issues
- changed namespace in Tests back to "src/"
- changed namings

15 files changed:
src/Core/Cache/MemcachedCacheDriver.php
src/Core/Cache/RedisCacheDriver.php
src/Core/Lock.php
src/Core/Lock/AbstractLockDriver.php
src/Core/Lock/CacheLockDriver.php
src/Core/Lock/DatabaseLockDriver.php
src/Core/Lock/ILockDriver.php
src/Core/Lock/SemaphoreLockDriver.php
src/Core/Worker.php
src/Model/Item.php
src/Protocol/OStatus.php
tests/src/Core/Lock/CacheLockDriverTest.php
tests/src/Core/Lock/DatabaseLockDriverTest.php
tests/src/Core/Lock/LockTest.php
tests/src/Core/Lock/SemaphoreLockDriverTest.php

index 9c860711f8cc8cab2d3d5ca1d073e17a1d4e9dd6..dda36ba19cd584572acd35ef9480a4bad4172457 100644 (file)
@@ -16,7 +16,7 @@ class MemcachedCacheDriver extends BaseObject implements IMemoryCacheDriver
        use TraitCompareDelete;
 
        /**
-        * @var Memcached
+        * @var \Memcached
         */
        private $memcached;
 
index 25c18aa6b636e401176349d29f820927edc4ba66..735418b2d220d8414d5fa48a341c2f047fd3bf8e 100644 (file)
@@ -14,7 +14,7 @@ use Friendica\Core\Cache;
 class RedisCacheDriver extends BaseObject implements IMemoryCacheDriver
 {
        /**
-        * @var Redis
+        * @var \Redis
         */
        private $redis;
 
index 47a1f9b4fe3090f9d7ea3aa95455478e1691bcad..9892f1f4e4d7e74e2dcab9212da8a09f4943401f 100644 (file)
@@ -1,7 +1,5 @@
 <?php
-/**
- * @file src/Util/Lock.php
- */
+
 namespace Friendica\Core;
 
 /**
@@ -115,20 +113,20 @@ class Lock
         *
         * @return boolean Was the lock successful?
         */
-       public static function acquireLock($key, $timeout = 120)
+       public static function acquire($key, $timeout = 120)
        {
-               return self::getDriver()->acquire($key, $timeout);
+               return self::getDriver()->acquireLock($key, $timeout);
        }
 
        /**
         * @brief Releases a lock if it was set by us
         *
         * @param string $key Name of the lock
-        * @return mixed
+        * @return void
         */
-       public static function releaseLock($key)
+       public static function release($key)
        {
-               return self::getDriver()->release($key);
+               self::getDriver()->releaseLock($key);
        }
 
        /**
index 53597d45fc9c316e243031f18011301ec9405140..033d6f356e74ce263f9909877e276db80ddb597a 100644 (file)
@@ -52,7 +52,7 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver
         */
        public function releaseAll() {
                foreach ($this->acquiredLocks as $acquiredLock => $hasLock) {
-                       $this->release($acquiredLock);
+                       $this->releaseLock($acquiredLock);
                }
        }
 }
index 57627acecf22a4206866b1868b3d9194857c0fa4..2dd6f3fad9defd290ca4c1fc7c90215d7607ee04 100644 (file)
@@ -24,7 +24,7 @@ class CacheLockDriver extends AbstractLockDriver
        /**
         * (@inheritdoc)
         */
-       public function acquire($key, $timeout = 120)
+       public function acquireLock($key, $timeout = 120)
        {
                $got_lock = false;
                $start = time();
@@ -60,7 +60,7 @@ class CacheLockDriver extends AbstractLockDriver
        /**
         * (@inheritdoc)
         */
-       public function release($key)
+       public function releaseLock($key)
        {
                $cachekey = self::getCacheKey($key);
 
index 8f8e174214184423a35ff4fbdf41c0c20939186e..6f4b942a4d2a72a4caa55075298f18265646bd16 100644 (file)
@@ -14,7 +14,7 @@ class DatabaseLockDriver extends AbstractLockDriver
        /**
         * (@inheritdoc)
         */
-       public function acquire($key, $timeout = 120)
+       public function acquireLock($key, $timeout = 120)
        {
                $got_lock = false;
                $start = time();
@@ -55,7 +55,7 @@ class DatabaseLockDriver extends AbstractLockDriver
        /**
         * (@inheritdoc)
         */
-       public function release($key)
+       public function releaseLock($key)
        {
                dba::delete('locks', ['name' => $key, 'pid' => getmypid()]);
 
index af8a1d56aef93dd45061cea5d6ceda7ff5fe20c3..cd54cc03c3fb514a7a3462aa14a4c71eedb3953d 100644 (file)
@@ -26,7 +26,7 @@ interface ILockDriver
         *
         * @return boolean Was the lock successful?
         */
-       public function acquire($key, $timeout = 120);
+       public function acquireLock($key, $timeout = 120);
 
        /**
         * Releases a lock if it was set by us
@@ -35,7 +35,7 @@ interface ILockDriver
         *
         * @return void
         */
-       public function release($key);
+       public function releaseLock($key);
 
        /**
         * Releases all lock that were set by us
index 250a75fbfe92030d513ff33b8e55b3a7c608df25..d032d46cc6d38ca389686ff199abf798541c47bc 100644 (file)
@@ -20,7 +20,7 @@ class SemaphoreLockDriver extends AbstractLockDriver
        {
                $temp = get_temppath();
 
-               $file = $temp.'/'.$key.'.sem';
+               $file = $temp . '/' . $key . '.sem';
 
                if (!file_exists($file)) {
                        file_put_contents($file, $key);
@@ -33,7 +33,7 @@ class SemaphoreLockDriver extends AbstractLockDriver
         *
         * (@inheritdoc)
         */
-       public function acquire($key, $timeout = 120)
+       public function acquireLock($key, $timeout = 120)
        {
                self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
                if (self::$semaphore[$key]) {
@@ -49,7 +49,7 @@ class SemaphoreLockDriver extends AbstractLockDriver
        /**
         * (@inheritdoc)
         */
-       public function release($key)
+       public function releaseLock($key)
        {
                if (empty(self::$semaphore[$key])) {
                        return false;
@@ -66,6 +66,6 @@ class SemaphoreLockDriver extends AbstractLockDriver
         */
        public function isLocked($key)
        {
-               return @sem_get(self::$semaphore[$key]) !== false;
+               return isset(self::$semaphore[$key]);
        }
 }
index cbf2ae8bd91d83f00778d96f9d3a7f5d34221b7c..c4c91bbf8b48f420fcef8c8e9451ce4ccbe5891a 100644 (file)
@@ -4,15 +4,11 @@
  */
 namespace Friendica\Core;
 
-use Friendica\Core\Addon;
-use Friendica\Core\Config;
-use Friendica\Core\System;
-use Friendica\Core\Lock;
+use dba;
 use Friendica\Database\DBM;
 use Friendica\Model\Process;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
-use dba;
 
 require_once 'include/dba.php';
 
@@ -108,16 +104,16 @@ class Worker
                                }
 
                                // If possible we will fetch new jobs for this worker
-                               if (!$refetched && Lock::acquireLock('worker_process', 0)) {
+                               if (!$refetched && Lock::acquire('worker_process', 0)) {
                                        $stamp = (float)microtime(true);
                                        $refetched = self::findWorkerProcesses($passing_slow);
                                        self::$db_duration += (microtime(true) - $stamp);
-                                       Lock::releaseLock('worker_process');
+                                       Lock::release('worker_process');
                                }
                        }
 
                        // To avoid the quitting of multiple workers only one worker at a time will execute the check
-                       if (Lock::acquireLock('worker', 0)) {
+                       if (Lock::acquire('worker', 0)) {
                                $stamp = (float)microtime(true);
                                // Count active workers and compare them with a maximum value that depends on the load
                                if (self::tooMuchWorkers()) {
@@ -130,7 +126,7 @@ class Worker
                                        logger('Memory limit reached, quitting.', LOGGER_DEBUG);
                                        return;
                                }
-                               Lock::releaseLock('worker');
+                               Lock::release('worker');
                                self::$db_duration += (microtime(true) - $stamp);
                        }
 
@@ -883,7 +879,7 @@ class Worker
                dba::close($r);
 
                $stamp = (float)microtime(true);
-               if (!Lock::acquireLock('worker_process')) {
+               if (!Lock::acquire('worker_process')) {
                        return false;
                }
                self::$lock_duration = (microtime(true) - $stamp);
@@ -892,7 +888,7 @@ class Worker
                $found = self::findWorkerProcesses($passing_slow);
                self::$db_duration += (microtime(true) - $stamp);
 
-               Lock::releaseLock('worker_process');
+               Lock::release('worker_process');
 
                if ($found) {
                        $r = dba::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
@@ -1097,13 +1093,13 @@ class Worker
                }
 
                // If there is a lock then we don't have to check for too much worker
-               if (!Lock::acquireLock('worker', 0)) {
+               if (!Lock::acquire('worker', 0)) {
                        return true;
                }
 
                // If there are already enough workers running, don't fork another one
                $quit = self::tooMuchWorkers();
-               Lock::releaseLock('worker');
+               Lock::release('worker');
 
                if ($quit) {
                        return true;
index ac53bb0220804fc756fa9a2ad5e4a006995886f9..d31d7c1323577b1e74d18d0aca7659ee3602eadd 100644 (file)
@@ -6,26 +6,22 @@
 
 namespace Friendica\Model;
 
+use dba;
 use Friendica\BaseObject;
 use Friendica\Content\Text;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
+use Friendica\Core\Lock;
 use Friendica\Core\PConfig;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBM;
-use Friendica\Model\Contact;
-use Friendica\Model\Conversation;
-use Friendica\Model\Group;
-use Friendica\Model\Term;
 use Friendica\Object\Image;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\XML;
-use Friendica\Util\Lock;
-use dba;
 use Text_LanguageDetect;
 
 require_once 'boot.php';
@@ -1595,7 +1591,7 @@ class Item extends BaseObject
                }
 
                // To avoid timing problems, we are using locks.
-               $locked = Lock::set('item_insert_content');
+               $locked = Lock::acquire('item_insert_content');
                if (!$locked) {
                        logger("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway.");
                }
@@ -1615,7 +1611,7 @@ class Item extends BaseObject
                        logger('Could not insert content for URI ' . $item['uri'] . ' - trying asynchronously');
                }
                if ($locked) {
-                       Lock::remove('item_insert_content');
+                       Lock::release('item_insert_content');
                }
        }
 
index d94f44814f899801bd59e423e315f0955fd8d694..dadc19dcca27691af4f36fbe941eeefe16f080a5 100644 (file)
@@ -4,6 +4,9 @@
  */
 namespace Friendica\Protocol;
 
+use dba;
+use DOMDocument;
+use DOMXPath;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Cache;
@@ -22,9 +25,6 @@ use Friendica\Object\Image;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 use Friendica\Util\XML;
-use dba;
-use DOMDocument;
-use DOMXPath;
 
 require_once 'include/dba.php';
 require_once 'include/items.php';
@@ -513,9 +513,9 @@ class OStatus
                                                        logger("Item with uri ".$item["uri"]." is from a blocked contact.", LOGGER_DEBUG);
                                                } else {
                                                        // We are having duplicated entries. Hopefully this solves it.
-                                                       if (Lock::acquireLock('ostatus_process_item_insert')) {
+                                                       if (Lock::acquire('ostatus_process_item_insert')) {
                                                                $ret = Item::insert($item);
-                                                               Lock::releaseLock('ostatus_process_item_insert');
+                                                               Lock::release('ostatus_process_item_insert');
                                                                logger("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);
                                                        } else {
                                                                $ret = Item::insert($item);
index b39000e119e90ad0f1fdc225b14c6c7ffee419a0..a089059725391c89b348bd928b8b25757575c7de 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Friendica\Test\Core\Lock;
+namespace Friendica\Test\src\Core\Lock;
 
 
 use Friendica\Core\Cache\ArrayCache;
index c6ded6e3d7a667f0cff10b6f9f1e1364ec4862bb..a80ff4c37cef4dbff7c6c50f5a112b26497dca89 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Friendica\Test\Core\Lock;
+namespace Friendica\Test\src\Core\Lock;
 
 use dba;
 use Friendica\Core\Lock\DatabaseLockDriver;
index d6a450c86bc3c5676541d30521d800d036e4bf10..c8c0c32ae2db563f923c81592e6a64a877c4d861 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Friendica\Test\Core\Lock;
+namespace Friendica\Test\src\Core\Lock;
 
 use Friendica\App;
 use Friendica\Core\Config;
@@ -34,43 +34,43 @@ abstract class LockTest extends TestCase
        }
 
        public function testLock() {
-               $this->instance->acquire('foo', 1);
+               $this->instance->acquireLock('foo', 1);
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->assertFalse($this->instance->isLocked('bar'));
        }
 
        public function testDoubleLock() {
-               $this->instance->acquire('foo', 1);
+               $this->instance->acquireLock('foo', 1);
                $this->assertTrue($this->instance->isLocked('foo'));
                // We already locked it
-               $this->assertTrue($this->instance->acquire('foo', 1));
+               $this->assertTrue($this->instance->acquireLock('foo', 1));
        }
 
        public function testReleaseLock() {
-               $this->instance->acquire('foo', 1);
+               $this->instance->acquireLock('foo', 1);
                $this->assertTrue($this->instance->isLocked('foo'));
-               $this->instance->release('foo');
+               $this->instance->releaseLock('foo');
                $this->assertFalse($this->instance->isLocked('foo'));
        }
 
        public function testReleaseAll() {
-               $this->instance->acquire('foo', 1);
-               $this->instance->acquire('bar', 1);
-               $this->instance->acquire('#/$%§', 1);
+               $this->instance->acquireLock('foo', 1);
+               $this->instance->acquireLock('bar', 1);
+               $this->instance->acquireLock('nice', 1);
 
                $this->instance->releaseAll();
 
                $this->assertFalse($this->instance->isLocked('foo'));
                $this->assertFalse($this->instance->isLocked('bar'));
-               $this->assertFalse($this->instance->isLocked('#/$%§'));
+               $this->assertFalse($this->instance->isLocked('nice'));
        }
 
        public function testReleaseAfterUnlock() {
-               $this->instance->acquire('foo', 1);
-               $this->instance->acquire('bar', 1);
-               $this->instance->acquire('#/$%§', 1);
+               $this->instance->acquireLock('foo', 1);
+               $this->instance->acquireLock('bar', 1);
+               $this->instance->acquireLock('nice', 1);
 
-               $this->instance->release('foo');
+               $this->instance->releaseLock('foo');
 
                $this->instance->releaseAll();
 
index 0fcf789e611a6a5a2fc455f302edebaf3f4c8744..56c96458f2a2e9110df3ddc2812c2d3c6ca9801d 100644 (file)
@@ -1,14 +1,26 @@
 <?php
 
-namespace Friendica\Test\Core\Lock;
+namespace Friendica\Test\src\Core\Lock;
 
 
 use Friendica\Core\Lock\SemaphoreLockDriver;
 
 class SemaphoreLockDriverTest extends LockTest
 {
+       /**
+        * @var \Friendica\Core\Lock\SemaphoreLockDriver
+        */
+       private $semaphoreLockDriver;
+
        protected function getInstance()
        {
-               return new SemaphoreLockDriver();
+               $this->semaphoreLockDriver = new SemaphoreLockDriver();
+               return $this->semaphoreLockDriver;
+       }
+
+       public function tearDown()
+       {
+               $this->semaphoreLockDriver->releaseAll();
+               parent::tearDown();
        }
 }
\ No newline at end of file