summaryrefslogtreecommitdiffstats
path: root/tests/lib/Memcache
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@gmx.de>2016-05-20 15:38:20 +0200
committerThomas Müller <DeepDiver1975@users.noreply.github.com>2016-05-20 15:38:20 +0200
commit94ad54ec9b96d41a614fbbad4a97b34c41a6901f (patch)
treef3eb7cdda2704aaf0cd59d58efe66bcbd34cb67d /tests/lib/Memcache
parent2ef751b1ec28f7b5c7113af60ec8c9fa0ae1cf87 (diff)
downloadnextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.tar.gz
nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.zip
Move tests/ to PSR-4 (#24731)
* Move a-b to PSR-4 * Move c-d to PSR-4 * Move e+g to PSR-4 * Move h-l to PSR-4 * Move m-r to PSR-4 * Move s-u to PSR-4 * Move files/ to PSR-4 * Move remaining tests to PSR-4 * Remove Test\ from old autoloader
Diffstat (limited to 'tests/lib/Memcache')
-rw-r--r--tests/lib/Memcache/APCTest.php26
-rw-r--r--tests/lib/Memcache/APCuTest.php22
-rw-r--r--tests/lib/Memcache/ArrayCacheTest.php17
-rw-r--r--tests/lib/Memcache/Cache.php133
-rw-r--r--tests/lib/Memcache/CasTraitTest.php73
-rw-r--r--tests/lib/Memcache/FactoryTest.php132
-rw-r--r--tests/lib/Memcache/MemcachedTest.php52
-rw-r--r--tests/lib/Memcache/RedisTest.php39
-rw-r--r--tests/lib/Memcache/XCacheTest.php22
9 files changed, 516 insertions, 0 deletions
diff --git a/tests/lib/Memcache/APCTest.php b/tests/lib/Memcache/APCTest.php
new file mode 100644
index 00000000000..4bd7e62b94a
--- /dev/null
+++ b/tests/lib/Memcache/APCTest.php
@@ -0,0 +1,26 @@
+<?php
+
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Memcache;
+
+class APCTest extends Cache {
+ protected function setUp() {
+ parent::setUp();
+
+ if(!\OC\Memcache\APC::isAvailable()) {
+ $this->markTestSkipped('The apc extension is not available.');
+ return;
+ }
+ if(\OC\Memcache\APCu::isAvailable()) {
+ $this->markTestSkipped('The apc extension is emulated by ACPu.');
+ return;
+ }
+ $this->instance=new \OC\Memcache\APC($this->getUniqueID());
+ }
+}
diff --git a/tests/lib/Memcache/APCuTest.php b/tests/lib/Memcache/APCuTest.php
new file mode 100644
index 00000000000..73fd5a12880
--- /dev/null
+++ b/tests/lib/Memcache/APCuTest.php
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Memcache;
+
+class APCuTest extends Cache {
+ protected function setUp() {
+ parent::setUp();
+
+ if(!\OC\Memcache\APCu::isAvailable()) {
+ $this->markTestSkipped('The APCu extension is not available.');
+ return;
+ }
+ $this->instance=new \OC\Memcache\APCu($this->getUniqueID());
+ }
+}
diff --git a/tests/lib/Memcache/ArrayCacheTest.php b/tests/lib/Memcache/ArrayCacheTest.php
new file mode 100644
index 00000000000..3ae8c116a01
--- /dev/null
+++ b/tests/lib/Memcache/ArrayCacheTest.php
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * Copyright (c) 2015 Joas Schilling <nickvergessen@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Memcache;
+
+class ArrayCacheTest extends Cache {
+ protected function setUp() {
+ parent::setUp();
+ $this->instance = new \OC\Memcache\ArrayCache('');
+ }
+}
diff --git a/tests/lib/Memcache/Cache.php b/tests/lib/Memcache/Cache.php
new file mode 100644
index 00000000000..8d6a231dd8d
--- /dev/null
+++ b/tests/lib/Memcache/Cache.php
@@ -0,0 +1,133 @@
+<?php
+
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Memcache;
+
+abstract class Cache extends \Test\Cache\TestCache {
+ /**
+ * @var \OCP\IMemcache cache;
+ */
+ protected $instance;
+
+ public function testExistsAfterSet() {
+ $this->assertFalse($this->instance->hasKey('foo'));
+ $this->instance->set('foo', 'bar');
+ $this->assertTrue($this->instance->hasKey('foo'));
+ }
+
+ public function testGetAfterSet() {
+ $this->assertNull($this->instance->get('foo'));
+ $this->instance->set('foo', 'bar');
+ $this->assertEquals('bar', $this->instance->get('foo'));
+ }
+
+ public function testGetArrayAfterSet() {
+ $this->assertNull($this->instance->get('foo'));
+ $this->instance->set('foo', ['bar']);
+ $this->assertEquals(['bar'], $this->instance->get('foo'));
+ }
+
+ public function testDoesNotExistAfterRemove() {
+ $this->instance->set('foo', 'bar');
+ $this->instance->remove('foo');
+ $this->assertFalse($this->instance->hasKey('foo'));
+ }
+
+ public function testRemoveNonExisting() {
+ $this->instance->remove('foo');
+ $this->assertFalse($this->instance->hasKey('foo'));
+ }
+
+ public function testArrayAccessSet() {
+ $this->instance['foo'] = 'bar';
+ $this->assertEquals('bar', $this->instance->get('foo'));
+ }
+
+ public function testArrayAccessGet() {
+ $this->instance->set('foo', 'bar');
+ $this->assertEquals('bar', $this->instance['foo']);
+ }
+
+ public function testArrayAccessExists() {
+ $this->assertFalse(isset($this->instance['foo']));
+ $this->instance->set('foo', 'bar');
+ $this->assertTrue(isset($this->instance['foo']));
+ }
+
+ public function testArrayAccessUnset() {
+ $this->instance->set('foo', 'bar');
+ unset($this->instance['foo']);
+ $this->assertFalse($this->instance->hasKey('foo'));
+ }
+
+ public function testAdd() {
+ $this->assertTrue($this->instance->add('foo', 'bar'));
+ $this->assertEquals('bar', $this->instance->get('foo'));
+ $this->assertFalse($this->instance->add('foo', 'asd'));
+ $this->assertEquals('bar', $this->instance->get('foo'));
+ }
+
+ public function testInc() {
+ $this->assertEquals(1, $this->instance->inc('foo'));
+ $this->assertEquals(1, $this->instance->get('foo'));
+ $this->assertEquals(2, $this->instance->inc('foo'));
+ $this->assertEquals(2, $this->instance->get('foo'));
+ $this->assertEquals(12, $this->instance->inc('foo', 10));
+ $this->assertEquals(12, $this->instance->get('foo'));
+
+ $this->instance->set('foo', 'bar');
+ $this->assertFalse($this->instance->inc('foo'));
+ $this->assertEquals('bar', $this->instance->get('foo'));
+ }
+
+ public function testDec() {
+ $this->assertFalse($this->instance->dec('foo'));
+ $this->instance->set('foo', 20);
+ $this->assertEquals(19, $this->instance->dec('foo'));
+ $this->assertEquals(19, $this->instance->get('foo'));
+ $this->assertEquals(9, $this->instance->dec('foo', 10));
+
+ $this->instance->set('foo', 'bar');
+ $this->assertFalse($this->instance->dec('foo'));
+ $this->assertEquals('bar', $this->instance->get('foo'));
+ }
+
+ public function testCasNotChanged() {
+ $this->instance->set('foo', 'bar');
+ $this->assertTrue($this->instance->cas('foo', 'bar', 'asd'));
+ $this->assertEquals('asd', $this->instance->get('foo'));
+ }
+
+ public function testCasChanged() {
+ $this->instance->set('foo', 'bar1');
+ $this->assertFalse($this->instance->cas('foo', 'bar', 'asd'));
+ $this->assertEquals('bar1', $this->instance->get('foo'));
+ }
+
+ public function testCadNotChanged() {
+ $this->instance->set('foo', 'bar');
+ $this->assertTrue($this->instance->cad('foo', 'bar'));
+ $this->assertFalse($this->instance->hasKey('foo'));
+ }
+
+ public function testCadChanged() {
+ $this->instance->set('foo', 'bar1');
+ $this->assertFalse($this->instance->cad('foo', 'bar'));
+ $this->assertTrue($this->instance->hasKey('foo'));
+ }
+
+
+ protected function tearDown() {
+ if ($this->instance) {
+ $this->instance->clear();
+ }
+
+ parent::tearDown();
+ }
+}
diff --git a/tests/lib/Memcache/CasTraitTest.php b/tests/lib/Memcache/CasTraitTest.php
new file mode 100644
index 00000000000..2f0ef939aa7
--- /dev/null
+++ b/tests/lib/Memcache/CasTraitTest.php
@@ -0,0 +1,73 @@
+<?php
+/**
+ * @author Robin Appelman <icewind@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Memcache;
+
+use Test\TestCase;
+
+class CasTraitTest extends TestCase {
+ /**
+ * @return \OC\Memcache\CasTrait
+ */
+ private function getCache() {
+ $sourceCache = new \OC\Memcache\ArrayCache();
+ $mock = $this->getMockForTrait('\OC\Memcache\CasTrait');
+
+ $mock->expects($this->any())
+ ->method('set')
+ ->will($this->returnCallback(function ($key, $value, $ttl) use ($sourceCache) {
+ return $sourceCache->set($key, $value, $ttl);
+ }));
+
+ $mock->expects($this->any())
+ ->method('get')
+ ->will($this->returnCallback(function ($key) use ($sourceCache) {
+ return $sourceCache->get($key);
+ }));
+
+ $mock->expects($this->any())
+ ->method('add')
+ ->will($this->returnCallback(function ($key, $value, $ttl) use ($sourceCache) {
+ return $sourceCache->add($key, $value, $ttl);
+ }));
+
+ $mock->expects($this->any())
+ ->method('remove')
+ ->will($this->returnCallback(function ($key) use ($sourceCache) {
+ return $sourceCache->remove($key);
+ }));
+ return $mock;
+ }
+
+ public function testCasNotChanged() {
+ $cache = $this->getCache();
+ $cache->set('foo', 'bar');
+ $this->assertTrue($cache->cas('foo', 'bar', 'asd'));
+ $this->assertEquals('asd', $cache->get('foo'));
+ }
+
+ public function testCasChanged() {
+ $cache = $this->getCache();
+ $cache->set('foo', 'bar1');
+ $this->assertFalse($cache->cas('foo', 'bar', 'asd'));
+ $this->assertEquals('bar1', $cache->get('foo'));
+ }
+}
diff --git a/tests/lib/Memcache/FactoryTest.php b/tests/lib/Memcache/FactoryTest.php
new file mode 100644
index 00000000000..8607ea7de9b
--- /dev/null
+++ b/tests/lib/Memcache/FactoryTest.php
@@ -0,0 +1,132 @@
+<?php
+/**
+ * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+namespace Test\Memcache;
+
+class Test_Factory_Available_Cache1 {
+ public function __construct($prefix = '') {
+ }
+
+ public static function isAvailable() {
+ return true;
+ }
+}
+
+class Test_Factory_Available_Cache2 {
+ public function __construct($prefix = '') {
+ }
+
+ public static function isAvailable() {
+ return true;
+ }
+}
+
+class Test_Factory_Unavailable_Cache1 {
+ public function __construct($prefix = '') {
+ }
+
+ public static function isAvailable() {
+ return false;
+ }
+}
+
+class Test_Factory_Unavailable_Cache2 {
+ public function __construct($prefix = '') {
+ }
+
+ public static function isAvailable() {
+ return false;
+ }
+}
+
+class FactoryTest extends \Test\TestCase {
+ const AVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Available_Cache1';
+ const AVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Available_Cache2';
+ const UNAVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache1';
+ const UNAVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache2';
+
+ public function cacheAvailabilityProvider() {
+ return [
+ [
+ // local and distributed available
+ self::AVAILABLE1, self::AVAILABLE2, null,
+ self::AVAILABLE1, self::AVAILABLE2, \OC\Memcache\Factory::NULL_CACHE
+ ],
+ [
+ // local and distributed null
+ null, null, null,
+ \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE
+ ],
+ [
+ // local available, distributed null (most common scenario)
+ self::AVAILABLE1, null, null,
+ self::AVAILABLE1, self::AVAILABLE1, \OC\Memcache\Factory::NULL_CACHE
+ ],
+ [
+ // locking cache available
+ null, null, self::AVAILABLE1,
+ \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, self::AVAILABLE1
+ ],
+ [
+ // locking cache unavailable: no exception here in the factory
+ null, null, self::UNAVAILABLE1,
+ \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE
+ ]
+ ];
+ }
+
+ public function cacheUnavailableProvider() {
+ return [
+ [
+ // local available, distributed unavailable
+ self::AVAILABLE1, self::UNAVAILABLE1
+ ],
+ [
+ // local unavailable, distributed available
+ self::UNAVAILABLE1, self::AVAILABLE1
+ ],
+ [
+ // local and distributed unavailable
+ self::UNAVAILABLE1, self::UNAVAILABLE2
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider cacheAvailabilityProvider
+ */
+ public function testCacheAvailability($localCache, $distributedCache, $lockingCache,
+ $expectedLocalCache, $expectedDistributedCache, $expectedLockingCache) {
+ $logger = $this->getMockBuilder('\OCP\ILogger')->getMock();
+ $factory = new \OC\Memcache\Factory('abc', $logger, $localCache, $distributedCache, $lockingCache);
+ $this->assertTrue(is_a($factory->createLocal(), $expectedLocalCache));
+ $this->assertTrue(is_a($factory->createDistributed(), $expectedDistributedCache));
+ $this->assertTrue(is_a($factory->createLocking(), $expectedLockingCache));
+ }
+
+ /**
+ * @dataProvider cacheUnavailableProvider
+ * @expectedException \OC\HintException
+ */
+ public function testCacheNotAvailableException($localCache, $distributedCache) {
+ $logger = $this->getMockBuilder('\OCP\ILogger')->getMock();
+ new \OC\Memcache\Factory('abc', $logger, $localCache, $distributedCache);
+ }
+}
diff --git a/tests/lib/Memcache/MemcachedTest.php b/tests/lib/Memcache/MemcachedTest.php
new file mode 100644
index 00000000000..865d8594bbb
--- /dev/null
+++ b/tests/lib/Memcache/MemcachedTest.php
@@ -0,0 +1,52 @@
+<?php
+
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Memcache;
+
+class MemcachedTest extends Cache {
+ static public function setUpBeforeClass() {
+ parent::setUpBeforeClass();
+
+ if (!\OC\Memcache\Memcached::isAvailable()) {
+ self::markTestSkipped('The memcached extension is not available.');
+ }
+ $instance = new \OC\Memcache\Memcached(self::getUniqueID());
+ if ($instance->set(self::getUniqueID(), self::getUniqueID()) === false) {
+ self::markTestSkipped('memcached server seems to be down.');
+ }
+ }
+
+ protected function setUp() {
+ parent::setUp();
+ $this->instance = new \OC\Memcache\Memcached($this->getUniqueID());
+ }
+
+ public function testClear() {
+ // Memcached is sometimes broken with clear(), so we don't test it thoroughly
+ $value='ipsum lorum';
+ $this->instance->set('1_value1', $value);
+ $this->instance->set('1_value2', $value);
+ $this->instance->set('2_value1', $value);
+ $this->instance->set('3_value1', $value);
+
+ $this->assertTrue($this->instance->clear('1_'));
+
+ $this->assertFalse($this->instance->hasKey('1_value1'));
+ $this->assertFalse($this->instance->hasKey('1_value2'));
+ //$this->assertTrue($this->instance->hasKey('2_value1'));
+ //$this->assertTrue($this->instance->hasKey('3_value1'));
+
+ $this->assertTrue($this->instance->clear());
+
+ $this->assertFalse($this->instance->hasKey('1_value1'));
+ $this->assertFalse($this->instance->hasKey('1_value2'));
+ $this->assertFalse($this->instance->hasKey('2_value1'));
+ $this->assertFalse($this->instance->hasKey('3_value1'));
+ }
+}
diff --git a/tests/lib/Memcache/RedisTest.php b/tests/lib/Memcache/RedisTest.php
new file mode 100644
index 00000000000..094954d4a1a
--- /dev/null
+++ b/tests/lib/Memcache/RedisTest.php
@@ -0,0 +1,39 @@
+<?php
+
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Memcache;
+
+class RedisTest extends Cache {
+ static public function setUpBeforeClass() {
+ parent::setUpBeforeClass();
+
+ if (!\OC\Memcache\Redis::isAvailable()) {
+ self::markTestSkipped('The redis extension is not available.');
+ }
+
+ set_error_handler(
+ function($errno, $errstr) {
+ restore_error_handler();
+ self::markTestSkipped($errstr);
+ },
+ E_WARNING
+ );
+ $instance = new \OC\Memcache\Redis(self::getUniqueID());
+ restore_error_handler();
+
+ if ($instance->set(self::getUniqueID(), self::getUniqueID()) === false) {
+ self::markTestSkipped('redis server seems to be down.');
+ }
+ }
+
+ protected function setUp() {
+ parent::setUp();
+ $this->instance = new \OC\Memcache\Redis($this->getUniqueID());
+ }
+}
diff --git a/tests/lib/Memcache/XCacheTest.php b/tests/lib/Memcache/XCacheTest.php
new file mode 100644
index 00000000000..af720115d01
--- /dev/null
+++ b/tests/lib/Memcache/XCacheTest.php
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Memcache;
+
+class XCacheTest extends Cache {
+ protected function setUp() {
+ parent::setUp();
+
+ if (!\OC\Memcache\XCache::isAvailable()) {
+ $this->markTestSkipped('The xcache extension is not available.');
+ return;
+ }
+ $this->instance = new \OC\Memcache\XCache($this->getUniqueID());
+ }
+}