]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #5331 from nupplaphil/lock_tests_fixings
authorHypolite Petovan <mrpetovan@eml.cc>
Sat, 7 Jul 2018 19:03:01 +0000 (15:03 -0400)
committerGitHub <noreply@github.com>
Sat, 7 Jul 2018 19:03:01 +0000 (15:03 -0400)
Lock fixings & unittests

29 files changed:
.travis.yml
database.sql
include/dba.php
src/Core/Cache/AbstractCacheDriver.php
src/Core/Cache/ArrayCache.php
src/Core/Cache/DatabaseCacheDriver.php
src/Core/Cache/ICacheDriver.php
src/Core/Cache/MemcacheCacheDriver.php
src/Core/Cache/MemcachedCacheDriver.php
src/Core/Cache/RedisCacheDriver.php
src/Core/Lock/CacheLockDriver.php
src/Core/Lock/DatabaseLockDriver.php
src/Core/Lock/ILockDriver.php
src/Core/Lock/SemaphoreLockDriver.php
tests/src/Core/Cache/ArrayCacheDriverTest.php [new file with mode: 0644]
tests/src/Core/Cache/CacheTest.php [new file with mode: 0644]
tests/src/Core/Cache/DatabaseCacheDriverTest.php [new file with mode: 0644]
tests/src/Core/Cache/MemcacheCacheDriverTest.php [new file with mode: 0644]
tests/src/Core/Cache/MemcachedCacheDriverTest.php [new file with mode: 0644]
tests/src/Core/Cache/MemoryCacheTest.php [new file with mode: 0644]
tests/src/Core/Cache/RedisCacheDriverTest.php [new file with mode: 0644]
tests/src/Core/Lock/ArrayCacheLockDriverTest.php [new file with mode: 0644]
tests/src/Core/Lock/CacheLockDriverTest.php [deleted file]
tests/src/Core/Lock/DatabaseLockDriverTest.php
tests/src/Core/Lock/LockTest.php
tests/src/Core/Lock/MemcacheCacheLockDriverTest.php [new file with mode: 0644]
tests/src/Core/Lock/MemcachedCacheLockDriverTest.php [new file with mode: 0644]
tests/src/Core/Lock/RedisCacheLockDriverTest.php [new file with mode: 0644]
tests/src/Core/Lock/SemaphoreLockDriverTest.php

index 78c29817db6e03101cb5da91376cbf3eb8bef9f9..6e7ac1c2e0b0f1c2dbcd02e828173c04400b5962 100644 (file)
@@ -9,6 +9,8 @@ php:
 
 services:
  - mysql
+ - redis-server
+ - memcached
 env:
  - MYSQL_HOST=localhost MYSQL_PORT=3306 MYSQL_USERNAME=travis MYSQL_PASSWORD= MYSQL_DATABASE=test
 
@@ -17,3 +19,5 @@ install:
 before_script:
  - mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
  - mysql -utravis test < database.sql
+ - echo "extension=redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
+ - echo "extension=memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
index b3d8f5dd0502ffcee47d5534734851dcb65691e3..e51f2a1e9b457f1601bc628bfac33cf4643bcd4e 100644 (file)
@@ -576,8 +576,10 @@ CREATE TABLE IF NOT EXISTS `locks` (
        `id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID',
        `name` varchar(128) NOT NULL DEFAULT '' COMMENT '',
        `locked` boolean NOT NULL DEFAULT '0' COMMENT '',
+       `expires` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'datetime of lock expiration',
        `pid` int unsigned NOT NULL DEFAULT 0 COMMENT 'Process ID',
-        PRIMARY KEY(`id`)
+        PRIMARY KEY(`id`),
+        INDEX `name_expires` (`name`,`expires`)
 ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='';
 
 --
index 9d828f8b440e40ac8147662eefc32acd328bd4ff..061f5399c76d11b38bdb72c1a9a7ec60b2b11e40 100644 (file)
@@ -1109,7 +1109,7 @@ class dba {
 
                                        // Split the SQL queries in chunks of 100 values
                                        // We do the $i stuff here to make the code better readable
-                                       $i = $counter[$key_table][$key_condition];
+                                       $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
                                        if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
                                                ++$i;
                                        }
index 417accdce75ccd7f8d1ae065df12857f9016090c..15b822dc3b519f8e0fac9412429991a1f06384f7 100644 (file)
@@ -18,6 +18,7 @@ abstract class AbstractCacheDriver extends BaseObject
         * @return string               The cache key used for the cache
         */
        protected function getCacheKey($key) {
+               // We fetch with the hostname as key to avoid problems with other applications
                return self::getApp()->get_hostname() . ":" . $key;
        }
 }
index ec9f3a2577fda4e0691c67b1452691ad75d25a8b..b1982871487c241830dea1d70e5eb6a0750cd2ac 100644 (file)
@@ -51,7 +51,7 @@ class ArrayCache extends AbstractCacheDriver implements IMemoryCacheDriver
        /**
         * (@inheritdoc)
         */
-       public function clear()
+       public function clear($outdated = true)
        {
                $this->cachedData = [];
                return true;
index 5c71fb19623cab9f3ce3ecf2b0e9a4e8bf99f71e..059fef3290626779252fded72a321e1e7df81fbb 100644 (file)
@@ -37,7 +37,7 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
        {
                $fields = [
                        'v'       => serialize($value),
-                       'expires' => DateTimeFormat::utc('now + ' . $ttl . ' seconds'),
+                       'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
                        'updated' => DateTimeFormat::utcNow()
                ];
 
@@ -49,8 +49,12 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
                return dba::delete('cache', ['k' => $key]);
        }
 
-       public function clear()
+       public function clear($outdated = true)
        {
-               return dba::delete('cache', ['`expires` < NOW()']);
+               if ($outdated) {
+                       return dba::delete('cache', ['`expires` < NOW()']);
+               } else {
+                       return dba::delete('cache', ['`k` IS NOT NULL ']);
+               }
        }
 }
index ced7b4e2168b935ab72467631df50581fd3f0e1a..e83b921c6ab1c7baac2fafbd86ddb05ab3249065 100644 (file)
@@ -42,8 +42,9 @@ interface ICacheDriver
 
        /**
         * Remove outdated data from the cache
+        * @param  boolean $outdated just remove outdated values
         *
         * @return bool
         */
-       public function clear();
+       public function clear($outdated = true);
 }
index af7e5ab0e226df58e0289ec862a27cac04b43a9d..bff543b542478ed4db0f80f9e7488f49cae758ed 100644 (file)
@@ -96,9 +96,13 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
        /**
         * (@inheritdoc)
         */
-       public function clear()
+       public function clear($outdated = true)
        {
-               return $this->memcache->flush();
+               if ($outdated) {
+                       return true;
+               } else {
+                       return $this->memcache->flush();
+               }
        }
 
        /**
@@ -107,6 +111,6 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
        public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
        {
                $cachekey = $this->getCacheKey($key);
-               return $this->memcache->add($cachekey, $value, $ttl);
+               return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
        }
 }
index 819cf71c5c6fba73676137f6d04a359b922d6e09..d4aab15c92430045bb5d058ca2a6889afdcf28af 100644 (file)
@@ -58,7 +58,7 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
                        return $this->memcached->set(
                                $cachekey,
                                $value,
-                               time() + $ttl
+                               $ttl
                        );
                } else {
                        return $this->memcached->set(
@@ -75,9 +75,13 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
                return $this->memcached->delete($cachekey);
        }
 
-       public function clear()
+       public function clear($outdated = true)
        {
-               return true;
+               if ($outdated) {
+                       return true;
+               } else {
+                       return $this->memcached->flush();
+               }
        }
 
        /**
index 67df8e8fee21904dee1583074c411a91fb8a0376..223c2b8a943493b146ef2983af7bace20dd0dbcf 100644 (file)
@@ -35,15 +35,12 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
                $return = null;
                $cachekey = $this->getCacheKey($key);
 
-               // We fetch with the hostname as key to avoid problems with other applications
                $cached = $this->redis->get($cachekey);
-
-               // @see http://php.net/manual/en/redis.get.php#84275
-               if (is_bool($cached) || is_double($cached) || is_long($cached)) {
-                       return $return;
+               if ($cached === false && !$this->redis->exists($cachekey)) {
+                       return null;
                }
 
-               $value = @unserialize($cached);
+               $value = json_decode($cached);
 
                // Only return a value if the serialized value is valid.
                // We also check if the db entry is a serialized
@@ -59,44 +56,46 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
        {
                $cachekey = $this->getCacheKey($key);
 
-               // We store with the hostname as key to avoid problems with other applications
+               $cached = json_encode($value);
+
                if ($ttl > 0) {
                        return $this->redis->setex(
                                $cachekey,
-                               time() + $ttl,
-                               serialize($value)
+                               $ttl,
+                               $cached
                        );
                } else {
                        return $this->redis->set(
                                $cachekey,
-                               serialize($value)
+                               $cached
                        );
                }
        }
 
        public function delete($key)
        {
-               return $this->redis->delete($key);
+               $cachekey = $this->getCacheKey($key);
+               return ($this->redis->delete($cachekey) > 0);
        }
 
-       public function clear()
+       public function clear($outdated = true)
        {
-               return true;
+               if ($outdated) {
+                       return true;
+               } else {
+                       return $this->redis->flushAll();
+               }
        }
 
-
        /**
         * (@inheritdoc)
         */
        public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
        {
                $cachekey = $this->getCacheKey($key);
+               $cached = json_encode($value);
 
-               if (!is_int($value)) {
-                       $value = serialize($value);
-               }
-
-               return $this->redis->setnx($cachekey, $value);
+               return $this->redis->setnx($cachekey, $cached);
        }
 
        /**
@@ -106,16 +105,14 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
        {
                $cachekey = $this->getCacheKey($key);
 
-               if (!is_int($newValue)) {
-                       $newValue = serialize($newValue);
-               }
+               $newCached = json_encode($newValue);
 
                $this->redis->watch($cachekey);
                // If the old value isn't what we expected, somebody else changed the key meanwhile
-               if ($this->get($cachekey) === $oldValue) {
+               if ($this->get($key) === $oldValue) {
                        if ($ttl > 0) {
                                $result = $this->redis->multi()
-                                       ->setex($cachekey, $ttl, $newValue)
+                                       ->setex($cachekey, $ttl, $newCached)
                                        ->exec();
                        } else {
                                $result = $this->redis->multi()
index ccbbb8a350da26b8b0589ea5510930d21c988602..18d441ffea09d5a0910ec6ac3ada461db885ab7c 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Friendica\Core\Lock;
 
+use Friendica\Core\Cache;
 use Friendica\Core\Cache\IMemoryCacheDriver;
 
 class CacheLockDriver extends AbstractLockDriver
@@ -24,7 +25,7 @@ class CacheLockDriver extends AbstractLockDriver
        /**
         * (@inheritdoc)
         */
-       public function acquireLock($key, $timeout = 120)
+       public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
        {
                $got_lock = false;
                $start = time();
@@ -43,7 +44,7 @@ class CacheLockDriver extends AbstractLockDriver
                                // At first initialize it with "0"
                                $this->cache->add($cachekey, 0);
                                // Now the value has to be "0" because otherwise the key was used by another process meanwhile
-                               if ($this->cache->compareSet($cachekey, 0, getmypid(), 300)) {
+                               if ($this->cache->compareSet($cachekey, 0, getmypid(), $ttl)) {
                                        $got_lock = true;
                                        $this->markAcquire($key);
                                }
index 6f4b942a4d2a72a4caa55075298f18265646bd16..fc057c6b55a8c2c91a171e0a9a544f92d0ee4565 100644 (file)
@@ -3,6 +3,7 @@
 namespace Friendica\Core\Lock;
 
 use dba;
+use Friendica\Core\Cache;
 use Friendica\Database\DBM;
 use Friendica\Util\DateTimeFormat;
 
@@ -14,7 +15,7 @@ class DatabaseLockDriver extends AbstractLockDriver
        /**
         * (@inheritdoc)
         */
-       public function acquireLock($key, $timeout = 120)
+       public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
        {
                $got_lock = false;
                $start = time();
@@ -28,16 +29,14 @@ class DatabaseLockDriver extends AbstractLockDriver
                                        // We want to lock something that was already locked by us? So we got the lock.
                                        if ($lock['pid'] == getmypid()) {
                                                $got_lock = true;
-                                               $this->markAcquire($key);
                                        }
                                }
                                if (!$lock['locked']) {
-                                       dba::update('locks', ['locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + 300seconds')], ['name' => $key]);
+                                       dba::update('locks', ['locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
                                        $got_lock = true;
-                                       $this->markAcquire($key);
                                }
                        } else {
-                               dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + 300seconds')]);
+                               dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
                                $got_lock = true;
                                $this->markAcquire($key);
                        }
index cd54cc03c3fb514a7a3462aa14a4c71eedb3953d..a255f683454753e414e2c20677f106661dccf486 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 namespace Friendica\Core\Lock;
+use Friendica\Core\Cache;
 
 /**
  * Lock Driver Interface
@@ -23,10 +24,11 @@ interface ILockDriver
         *
         * @param string  $key      The Name of the lock
         * @param integer $timeout  Seconds until we give up
+        * @param integer $ttl      Seconds The lock lifespan, must be one of the Cache constants
         *
         * @return boolean Was the lock successful?
         */
-       public function acquireLock($key, $timeout = 120);
+       public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES);
 
        /**
         * Releases a lock if it was set by us
index d032d46cc6d38ca389686ff199abf798541c47bc..cf1ce5a8d8bc770cdc06120868f351e4aef65402 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace Friendica\Core\Lock;
 
+use Friendica\Core\Cache;
+
 class SemaphoreLockDriver extends AbstractLockDriver
 {
        private static $semaphore = [];
@@ -30,10 +32,9 @@ class SemaphoreLockDriver extends AbstractLockDriver
        }
 
        /**
-        *
         * (@inheritdoc)
         */
-       public function acquireLock($key, $timeout = 120)
+       public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
        {
                self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
                if (self::$semaphore[$key]) {
diff --git a/tests/src/Core/Cache/ArrayCacheDriverTest.php b/tests/src/Core/Cache/ArrayCacheDriverTest.php
new file mode 100644 (file)
index 0000000..0cad6e9
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+namespace Friendica\Test\src\Core\Cache;
+
+
+use Friendica\Core\Cache\ArrayCache;
+
+class ArrayCacheDriverTest extends MemoryCacheTest
+{
+       /**
+        * @var \Friendica\Core\Cache\IMemoryCacheDriver
+        */
+       private $cache;
+
+       protected function getInstance()
+       {
+               $this->cache = new ArrayCache();
+               return $this->cache;
+       }
+
+       public function tearDown()
+       {
+               $this->cache->clear(false);
+               parent::tearDown();
+       }
+
+       public function testTTL()
+       {
+               // Array Cache doesn't support TTL
+               return true;
+       }
+}
diff --git a/tests/src/Core/Cache/CacheTest.php b/tests/src/Core/Cache/CacheTest.php
new file mode 100644 (file)
index 0000000..4f3e4d3
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+
+namespace Friendica\Test\src\Core\Cache;
+
+use Friendica\App;
+use Friendica\Core\Config;
+use Friendica\Test\DatabaseTest;
+
+abstract class CacheTest extends DatabaseTest
+{
+       /**
+        * @var \Friendica\Core\Cache\ICacheDriver
+        */
+       protected $instance;
+
+       abstract protected function getInstance();
+
+       protected function setUp()
+       {
+               global $a;
+               parent::setUp();
+               $this->instance = $this->getInstance();
+
+               // 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');
+       }
+
+       function testSimple() {
+               $this->assertNull($this->instance->get('value1'));
+
+               $value = 'foobar';
+               $this->instance->set('value1', $value);
+               $received = $this->instance->get('value1');
+               $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
+
+               $value = 'ipsum lorum';
+               $this->instance->set('value1', $value);
+               $received = $this->instance->get('value1');
+               $this->assertEquals($value, $received, 'Value not overwritten by second set');
+
+               $value2 = 'foobar';
+               $this->instance->set('value2', $value2);
+               $received2 = $this->instance->get('value2');
+               $this->assertEquals($value, $received, 'Value changed while setting other variable');
+               $this->assertEquals($value2, $received2, 'Second value not equal to original');
+
+               $this->assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
+
+               $this->assertTrue($this->instance->delete('value1'));
+               $this->assertNull($this->instance->get('value1'));
+       }
+
+       function testClear() {
+               $value = 'ipsum lorum';
+               $this->instance->set('1_value1', $value . '1');
+               $this->instance->set('1_value2', $value . '2');
+               $this->instance->set('2_value1', $value . '3');
+               $this->instance->set('3_value1', $value . '4');
+
+               $this->assertEquals([
+                       '1_value1' => 'ipsum lorum1',
+                       '1_value2' => 'ipsum lorum2',
+                       '2_value1' => 'ipsum lorum3',
+                       '3_value1' => 'ipsum lorum4',
+               ], [
+                       '1_value1' => $this->instance->get('1_value1'),
+                       '1_value2' => $this->instance->get('1_value2'),
+                       '2_value1' => $this->instance->get('2_value1'),
+                       '3_value1' => $this->instance->get('3_value1'),
+               ]);
+
+               $this->assertTrue($this->instance->clear(false));
+
+               $this->assertEquals([
+                       '1_value1' => null,
+                       '1_value2' => null,
+                       '2_value1' => null,
+                       '3_value1' => null,
+               ], [
+                       '1_value1' => $this->instance->get('1_value1'),
+                       '1_value2' => $this->instance->get('1_value2'),
+                       '2_value1' => $this->instance->get('2_value1'),
+                       '3_value1' => $this->instance->get('3_value1'),
+               ]);
+       }
+
+       function testTTL() {
+               $this->assertNull($this->instance->get('value1'));
+
+               $value = 'foobar';
+               $this->instance->set('value1', $value, 1);
+               $received = $this->instance->get('value1');
+               $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
+
+               sleep(2);
+
+               $this->assertNull($this->instance->get('value1'));
+       }
+}
diff --git a/tests/src/Core/Cache/DatabaseCacheDriverTest.php b/tests/src/Core/Cache/DatabaseCacheDriverTest.php
new file mode 100644 (file)
index 0000000..5df00fc
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+namespace Friendica\Test\src\Core\Cache;
+
+use Friendica\Core\Cache\CacheDriverFactory;
+
+class DatabaseCacheDriverTest extends CacheTest
+{
+       /**
+        * @var \Friendica\Core\Cache\IMemoryCacheDriver
+        */
+       private $cache;
+
+       protected function getInstance()
+       {
+               $this->cache = CacheDriverFactory::create('database');
+               return $this->cache;
+       }
+
+       public function tearDown()
+       {
+               $this->cache->clear(false);
+               parent::tearDown();
+       }
+}
diff --git a/tests/src/Core/Cache/MemcacheCacheDriverTest.php b/tests/src/Core/Cache/MemcacheCacheDriverTest.php
new file mode 100644 (file)
index 0000000..d207823
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+
+namespace Friendica\Test\src\Core\Cache;
+
+
+use Friendica\Core\Cache\CacheDriverFactory;
+
+class MemcacheCacheDriverTest extends MemoryCacheTest
+{
+       /**
+        * @var \Friendica\Core\Cache\IMemoryCacheDriver
+        */
+       private $cache;
+
+       protected function getInstance()
+       {
+               if (class_exists('Memcache')) {
+                       try {
+                               $this->cache = CacheDriverFactory::create('memcache');
+                       } catch (\Exception $exception) {
+                               print "Memcache - TestCase failed: " . $exception->getMessage();
+                               throw new \Exception();
+                       }
+                       return $this->cache;
+               } else {
+                       $this->markTestSkipped('Memcache driver isn\'t available');
+                       return null;
+               }
+       }
+
+       public function tearDown()
+       {
+               if (class_exists('Memcache')) {
+                       $this->cache->clear(false);
+               }
+               parent::tearDown();
+       }
+}
diff --git a/tests/src/Core/Cache/MemcachedCacheDriverTest.php b/tests/src/Core/Cache/MemcachedCacheDriverTest.php
new file mode 100644 (file)
index 0000000..2484517
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+
+namespace Friendica\Test\src\Core\Cache;
+
+
+use Friendica\Core\Cache\CacheDriverFactory;
+
+class MemcachedCacheDriverTest extends MemoryCacheTest
+{
+       /**
+        * @var \Friendica\Core\Cache\IMemoryCacheDriver
+        */
+       private $cache;
+
+       protected function getInstance()
+       {
+               if (class_exists('Memcached')) {
+                       try {
+                               $this->cache = CacheDriverFactory::create('memcached');
+                       } catch (\Exception $exception) {
+                               print "Memcached - TestCase failed: " . $exception->getMessage();
+                               throw new \Exception();
+                       }
+                       return $this->cache;
+               } else {
+                       $this->markTestSkipped('Memcached driver isn\'t available');
+                       return null;
+               }
+       }
+
+       public function tearDown()
+       {
+               if (class_exists('Memcached')) {
+                       $this->cache->clear(false);
+               }
+               parent::tearDown();
+       }
+}
diff --git a/tests/src/Core/Cache/MemoryCacheTest.php b/tests/src/Core/Cache/MemoryCacheTest.php
new file mode 100644 (file)
index 0000000..670df2f
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+
+namespace Friendica\Test\src\Core\Cache;
+
+use Friendica\Core\Cache\IMemoryCacheDriver;
+
+abstract class MemoryCacheTest extends CacheTest
+{
+       /**
+        * @var \Friendica\Core\Cache\IMemoryCacheDriver
+        */
+       protected $instance;
+
+       function setUp()
+       {
+               parent::setUp();
+               if (!($this->instance instanceof IMemoryCacheDriver)) {
+                       throw new \Exception('MemoryCacheTest unsupported');
+               }
+       }
+
+       function testCompareSet() {
+               $this->assertNull($this->instance->get('value1'));
+
+               $value = 'foobar';
+               $this->instance->add('value1', $value);
+               $received = $this->instance->get('value1');
+               $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
+
+               $newValue = 'ipsum lorum';
+               $this->instance->compareSet('value1', $value, $newValue);
+               $received = $this->instance->get('value1');
+               $this->assertEquals($newValue, $received, 'Value not overwritten by compareSet');
+       }
+
+       function testNegativeCompareSet() {
+               $this->assertNull($this->instance->get('value1'));
+
+               $value = 'foobar';
+               $this->instance->add('value1', $value);
+               $received = $this->instance->get('value1');
+               $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
+
+               $newValue = 'ipsum lorum';
+               $this->instance->compareSet('value1', 'wrong', $newValue);
+               $received = $this->instance->get('value1');
+               $this->assertNotEquals($newValue, $received, 'Value was wrongly overwritten by compareSet');
+               $this->assertEquals($value, $received, 'Value was wrongly overwritten by any other value');
+       }
+
+       function testCompareDelete() {
+               $this->assertNull($this->instance->get('value1'));
+
+               $value = 'foobar';
+               $this->instance->add('value1', $value);
+               $received = $this->instance->get('value1');
+               $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
+               $this->instance->compareDelete('value1', $value);
+               $this->assertNull($this->instance->get('value1'), 'Value was not deleted by compareDelete');
+       }
+
+       function testNegativeCompareDelete() {
+               $this->assertNull($this->instance->get('value1'));
+
+               $value = 'foobar';
+               $this->instance->add('value1', $value);
+               $received = $this->instance->get('value1');
+               $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
+               $this->instance->compareDelete('value1', 'wrong');
+               $this->assertNotNull($this->instance->get('value1'), 'Value was wrongly compareDeleted');
+
+               $this->instance->compareDelete('value1', $value);
+               $this->assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
+       }
+
+       function testAdd() {
+               $this->assertNull($this->instance->get('value1'));
+
+               $value = 'foobar';
+               $this->instance->add('value1', $value);
+
+               $newValue = 'ipsum lorum';
+               $this->instance->add('value1', $newValue);
+               $received = $this->instance->get('value1');
+               $this->assertNotEquals($newValue, $received, 'Value was wrongly overwritten by add');
+               $this->assertEquals($value, $received, 'Value was wrongly overwritten by any other value');
+
+               $this->instance->delete('value1');
+               $this->instance->add('value1', $newValue);
+               $received = $this->instance->get('value1');
+               $this->assertEquals($newValue, $received, 'Value was not overwritten by add');
+               $this->assertNotEquals($value, $received, 'Value was not overwritten by any other value');
+       }
+}
\ No newline at end of file
diff --git a/tests/src/Core/Cache/RedisCacheDriverTest.php b/tests/src/Core/Cache/RedisCacheDriverTest.php
new file mode 100644 (file)
index 0000000..e13d95d
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+
+namespace Friendica\Test\src\Core\Cache;
+
+
+use Friendica\Core\Cache\CacheDriverFactory;
+
+class RedisCacheDriverTest extends MemoryCacheTest
+{
+       /**
+        * @var \Friendica\Core\Cache\IMemoryCacheDriver
+        */
+       private $cache;
+
+       protected function getInstance()
+       {
+               if (class_exists('Redis')) {
+                       try {
+                               $this->cache = CacheDriverFactory::create('redis');
+                       } catch (\Exception $exception) {
+                               print "Redis - TestCase failed: " . $exception->getMessage();
+                               throw new \Exception();
+                       }
+                       return $this->cache;
+               } else {
+                       $this->markTestSkipped('Redis driver isn\'t available');
+                       return null;
+               }
+       }
+
+       public function tearDown()
+       {
+               if (class_exists('Redis')) {
+                       $this->cache->clear(false);
+               }
+               parent::tearDown();
+       }
+}
diff --git a/tests/src/Core/Lock/ArrayCacheLockDriverTest.php b/tests/src/Core/Lock/ArrayCacheLockDriverTest.php
new file mode 100644 (file)
index 0000000..dc044f5
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace Friendica\Test\src\Core\Lock;
+
+
+use Friendica\Core\Cache\ArrayCache;
+use Friendica\Core\Lock\CacheLockDriver;
+
+class ArrayCacheLockDriverTest extends LockTest
+{
+       /**
+        * @var \Friendica\Core\Cache\IMemoryCacheDriver
+        */
+       private $cache;
+
+       protected function getInstance()
+       {
+               $this->cache = new ArrayCache();
+               return new CacheLockDriver($this->cache);
+       }
+
+       public function tearDown()
+       {
+               $this->cache->clear();
+               parent::tearDown();
+       }
+
+       public function testLockTTL()
+       {
+               // ArrayCache doesn't support TTL
+               return true;
+       }
+}
diff --git a/tests/src/Core/Lock/CacheLockDriverTest.php b/tests/src/Core/Lock/CacheLockDriverTest.php
deleted file mode 100644 (file)
index a089059..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-<?php
-
-namespace Friendica\Test\src\Core\Lock;
-
-
-use Friendica\Core\Cache\ArrayCache;
-use Friendica\Core\Lock\CacheLockDriver;
-
-class CacheLockDriverTest extends LockTest
-{
-       /**
-        * @var \Friendica\Core\Cache\IMemoryCacheDriver
-        */
-       private $cache;
-
-       protected function getInstance()
-       {
-               $this->cache = new ArrayCache();
-               return new CacheLockDriver($this->cache);
-       }
-
-       public function tearDown()
-       {
-               $this->cache->clear();
-               parent::tearDown();
-       }
-}
\ No newline at end of file
index a80ff4c37cef4dbff7c6c50f5a112b26497dca89..f55ab0f9e26f54145b9cc28449ac89ed7e087e55 100644 (file)
@@ -11,49 +11,6 @@ use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
 
 class DatabaseLockDriverTest extends LockTest
 {
-       use TestCaseTrait;
-
-       /**
-        * Get database connection.
-        *
-        * This function is executed before each test in order to get a database connection that can be used by tests.
-        * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
-        *
-        * If it could not connect to the database, the test is skipped.
-        *
-        * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
-        * @see https://phpunit.de/manual/5.7/en/database.html
-        */
-       protected function getConnection()
-       {
-               if (!dba::$connected) {
-                       dba::connect('localhost', getenv('USER'), getenv('PASS'), getenv('DB'));
-
-                       if (dba::$connected) {
-                               $app = get_app();
-                               // We need to do this in order to disable logging
-                               $app->module = 'install';
-
-                               // Create database structure
-                               DBStructure::update(false, true, true);
-                       } else {
-                               $this->markTestSkipped('Could not connect to the database.');
-                       }
-               }
-
-               return $this->createDefaultDBConnection(dba::get_db(), getenv('DB'));
-       }
-
-       /**
-        * Get dataset to populate the database with.
-        * @return YamlDataSet
-        * @see https://phpunit.de/manual/5.7/en/database.html
-        */
-       protected function getDataSet()
-       {
-               return new YamlDataSet(__DIR__ . '/../../../datasets/api.yml');
-       }
-
        protected function getInstance()
        {
                return new DatabaseLockDriver();
@@ -64,4 +21,4 @@ class DatabaseLockDriverTest extends LockTest
                dba::delete('locks', [ 'id > 0']);
                parent::tearDown();
        }
-}
\ No newline at end of file
+}
index c8c0c32ae2db563f923c81592e6a64a877c4d861..dafbd74a6ff19d1bc1371f6d9cf42ee76b19e19f 100644 (file)
@@ -4,9 +4,10 @@ namespace Friendica\Test\src\Core\Lock;
 
 use Friendica\App;
 use Friendica\Core\Config;
+use Friendica\Test\DatabaseTest;
 use PHPUnit\Framework\TestCase;
 
-abstract class LockTest extends TestCase
+abstract class LockTest extends DatabaseTest
 {
        /**
         * @var \Friendica\Core\Lock\ILockDriver
@@ -58,6 +59,10 @@ abstract class LockTest extends TestCase
                $this->instance->acquireLock('bar', 1);
                $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->assertFalse($this->instance->isLocked('foo'));
@@ -72,9 +77,33 @@ abstract class LockTest extends TestCase
 
                $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->assertFalse($this->instance->isLocked('bar'));
-               $this->assertFalse($this->instance->isLocked('#/$%ยง'));
+               $this->assertFalse($this->instance->isLocked('nice'));
+       }
+
+       function testLockTTL() {
+
+               // 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->isLocked('foo'));
+               $this->assertTrue($this->instance->isLocked('bar'));
+
+               sleep(2);
+
+               $this->assertFalse($this->instance->isLocked('foo'));
+               $this->assertTrue($this->instance->isLocked('bar'));
+
+               sleep(2);
+
+               $this->assertFalse($this->instance->isLocked('foo'));
+               $this->assertFalse($this->instance->isLocked('bar'));
        }
-}
\ No newline at end of file
+}
diff --git a/tests/src/Core/Lock/MemcacheCacheLockDriverTest.php b/tests/src/Core/Lock/MemcacheCacheLockDriverTest.php
new file mode 100644 (file)
index 0000000..67ccdb5
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+
+namespace Friendica\Test\src\Core\Lock;
+
+
+use Friendica\Core\Cache\CacheDriverFactory;
+use Friendica\Core\Lock\CacheLockDriver;
+
+class MemcacheCacheLockDriverTest extends LockTest
+{
+       /**
+        * @var \Friendica\Core\Cache\IMemoryCacheDriver
+        */
+       private $cache;
+
+       protected function getInstance()
+       {
+               if (class_exists('Memcache')) {
+                       try {
+                               $this->cache = CacheDriverFactory::create('memcache');
+                       } catch (\Exception $exception) {
+                               print "Memcache - TestCase failed: " . $exception->getMessage();
+                               throw new \Exception();
+                       }
+                       return new CacheLockDriver($this->cache);
+               } else {
+                       $this->markTestSkipped('Memcache driver isn\'t available');
+                       return null;
+               }
+       }
+
+       public function tearDown()
+       {
+               if (class_exists('Memcache')) {
+                       $this->cache->clear();
+               }
+               parent::tearDown();
+       }
+}
diff --git a/tests/src/Core/Lock/MemcachedCacheLockDriverTest.php b/tests/src/Core/Lock/MemcachedCacheLockDriverTest.php
new file mode 100644 (file)
index 0000000..e08358c
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+
+namespace Friendica\Test\src\Core\Lock;
+
+
+use Friendica\Core\Cache\CacheDriverFactory;
+use Friendica\Core\Lock\CacheLockDriver;
+
+class MemcachedCacheLockDriverTest extends LockTest
+{
+       /**
+        * @var \Friendica\Core\Cache\IMemoryCacheDriver
+        */
+       private $cache;
+
+       protected function getInstance()
+       {
+               if (class_exists('Memcached')) {
+                       try {
+                               $this->cache = CacheDriverFactory::create('memcached');
+                       } catch (\Exception $exception) {
+                               print "Memcached - TestCase failed: " . $exception->getMessage();
+                               throw new \Exception();
+                       }
+                       return new CacheLockDriver($this->cache);
+               } else {
+                       $this->markTestSkipped('Memcached driver isn\'t available');
+                       return null;
+               }
+       }
+
+       public function tearDown()
+       {
+               if (class_exists('Memcached')) {
+                       $this->cache->clear();
+               }
+               parent::tearDown();
+       }
+}
diff --git a/tests/src/Core/Lock/RedisCacheLockDriverTest.php b/tests/src/Core/Lock/RedisCacheLockDriverTest.php
new file mode 100644 (file)
index 0000000..82d9b50
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+
+namespace Friendica\Test\src\Core\Lock;
+
+
+use Friendica\Core\Cache\CacheDriverFactory;
+use Friendica\Core\Lock\CacheLockDriver;
+
+class RedisCacheLockDriverTest extends LockTest
+{
+       /**
+        * @var \Friendica\Core\Cache\IMemoryCacheDriver
+        */
+       private $cache;
+
+       protected function getInstance()
+       {
+               if (class_exists('Redis')) {
+                       try {
+                               $this->cache = CacheDriverFactory::create('redis');
+                       } catch (\Exception $exception) {
+                               print "Redis - TestCase failed: " . $exception->getMessage();
+                               throw new \Exception();
+                       }
+                       return new CacheLockDriver($this->cache);
+               } else {
+                       $this->markTestSkipped('Redis driver isn\'t available');
+                       return null;
+               }
+       }
+
+       public function tearDown()
+       {
+               if (class_exists('Redis')) {
+                       $this->cache->clear();
+               }
+               parent::tearDown();
+       }
+}
index 56c96458f2a2e9110df3ddc2812c2d3c6ca9801d..cd4b9157334a1da5119e2adc9d889229ddd7e64d 100644 (file)
@@ -23,4 +23,10 @@ class SemaphoreLockDriverTest extends LockTest
                $this->semaphoreLockDriver->releaseAll();
                parent::tearDown();
        }
-}
\ No newline at end of file
+
+       function testLockTTL()
+       {
+               // Semaphore doesn't work with TTL
+               return true;
+       }
+}