diff options
Diffstat (limited to 'tests/lib/Memcache')
-rw-r--r-- | tests/lib/Memcache/APCuTest.php | 21 | ||||
-rw-r--r-- | tests/lib/Memcache/ArrayCacheTest.php | 14 | ||||
-rw-r--r-- | tests/lib/Memcache/Cache.php | 66 | ||||
-rw-r--r-- | tests/lib/Memcache/CasTraitTest.php | 31 | ||||
-rw-r--r-- | tests/lib/Memcache/FactoryTest.php | 71 | ||||
-rw-r--r-- | tests/lib/Memcache/MemcachedTest.php | 21 | ||||
-rw-r--r-- | tests/lib/Memcache/RedisTest.php | 50 |
7 files changed, 167 insertions, 107 deletions
diff --git a/tests/lib/Memcache/APCuTest.php b/tests/lib/Memcache/APCuTest.php index 50e3984ca34..199bdf298f6 100644 --- a/tests/lib/Memcache/APCuTest.php +++ b/tests/lib/Memcache/APCuTest.php @@ -1,32 +1,37 @@ <?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. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Memcache; +use OC\Memcache\APCu; + +/** + * @group Memcache + * @group APCu + */ class APCuTest extends Cache { protected function setUp(): void { parent::setUp(); - if (!\OC\Memcache\APCu::isAvailable()) { + if (!APCu::isAvailable()) { $this->markTestSkipped('The APCu extension is not available.'); return; } - $this->instance = new \OC\Memcache\APCu($this->getUniqueID()); + $this->instance = new APCu($this->getUniqueID()); } - public function testCasIntChanged() { + public function testCasIntChanged(): void { $this->instance->set('foo', 1); $this->assertTrue($this->instance->cas('foo', 1, 2)); $this->assertEquals(2, $this->instance->get('foo')); } - public function testCasIntNotChanged() { + public function testCasIntNotChanged(): void { $this->instance->set('foo', 1); $this->assertFalse($this->instance->cas('foo', 2, 3)); $this->assertEquals(1, $this->instance->get('foo')); diff --git a/tests/lib/Memcache/ArrayCacheTest.php b/tests/lib/Memcache/ArrayCacheTest.php index 4e3623d344d..e71c821729c 100644 --- a/tests/lib/Memcache/ArrayCacheTest.php +++ b/tests/lib/Memcache/ArrayCacheTest.php @@ -1,17 +1,21 @@ <?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. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Memcache; +use OC\Memcache\ArrayCache; + +/** + * @group Memcache + */ class ArrayCacheTest extends Cache { protected function setUp(): void { parent::setUp(); - $this->instance = new \OC\Memcache\ArrayCache(''); + $this->instance = new ArrayCache(''); } } diff --git a/tests/lib/Memcache/Cache.php b/tests/lib/Memcache/Cache.php index ab316738c9c..b48f5557faa 100644 --- a/tests/lib/Memcache/Cache.php +++ b/tests/lib/Memcache/Cache.php @@ -1,79 +1,80 @@ <?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. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Memcache; +use OCP\IMemcache; + abstract class Cache extends \Test\Cache\TestCache { /** - * @var \OCP\IMemcache cache; + * @var IMemcache cache; */ protected $instance; - public function testExistsAfterSet() { + public function testExistsAfterSet(): void { $this->assertFalse($this->instance->hasKey('foo')); $this->instance->set('foo', 'bar'); $this->assertTrue($this->instance->hasKey('foo')); } - public function testGetAfterSet() { + public function testGetAfterSet(): void { $this->assertNull($this->instance->get('foo')); $this->instance->set('foo', 'bar'); $this->assertEquals('bar', $this->instance->get('foo')); } - public function testGetArrayAfterSet() { + public function testGetArrayAfterSet(): void { $this->assertNull($this->instance->get('foo')); $this->instance->set('foo', ['bar']); $this->assertEquals(['bar'], $this->instance->get('foo')); } - public function testDoesNotExistAfterRemove() { + public function testDoesNotExistAfterRemove(): void { $this->instance->set('foo', 'bar'); $this->instance->remove('foo'); $this->assertFalse($this->instance->hasKey('foo')); } - public function testRemoveNonExisting() { + public function testRemoveNonExisting(): void { $this->instance->remove('foo'); $this->assertFalse($this->instance->hasKey('foo')); } - public function testArrayAccessSet() { + public function testArrayAccessSet(): void { $this->instance['foo'] = 'bar'; $this->assertEquals('bar', $this->instance->get('foo')); } - public function testArrayAccessGet() { + public function testArrayAccessGet(): void { $this->instance->set('foo', 'bar'); $this->assertEquals('bar', $this->instance['foo']); } - public function testArrayAccessExists() { + public function testArrayAccessExists(): void { $this->assertFalse(isset($this->instance['foo'])); $this->instance->set('foo', 'bar'); $this->assertTrue(isset($this->instance['foo'])); } - public function testArrayAccessUnset() { + public function testArrayAccessUnset(): void { $this->instance->set('foo', 'bar'); unset($this->instance['foo']); $this->assertFalse($this->instance->hasKey('foo')); } - public function testAdd() { + public function testAdd(): void { $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() { + public function testInc(): void { $this->assertEquals(1, $this->instance->inc('foo')); $this->assertEquals(1, $this->instance->get('foo')); $this->assertEquals(2, $this->instance->inc('foo')); @@ -86,7 +87,7 @@ abstract class Cache extends \Test\Cache\TestCache { $this->assertEquals('bar', $this->instance->get('foo')); } - public function testDec() { + public function testDec(): void { $this->assertFalse($this->instance->dec('foo')); $this->instance->set('foo', 20); $this->assertEquals(19, $this->instance->dec('foo')); @@ -98,30 +99,53 @@ abstract class Cache extends \Test\Cache\TestCache { $this->assertEquals('bar', $this->instance->get('foo')); } - public function testCasNotChanged() { + public function testCasNotChanged(): void { $this->instance->set('foo', 'bar'); $this->assertTrue($this->instance->cas('foo', 'bar', 'asd')); $this->assertEquals('asd', $this->instance->get('foo')); } - public function testCasChanged() { + public function testCasChanged(): void { $this->instance->set('foo', 'bar1'); $this->assertFalse($this->instance->cas('foo', 'bar', 'asd')); $this->assertEquals('bar1', $this->instance->get('foo')); } - public function testCadNotChanged() { + public function testCasNotSet(): void { + $this->assertFalse($this->instance->cas('foo', 'bar', 'asd')); + } + + public function testCadNotChanged(): void { $this->instance->set('foo', 'bar'); $this->assertTrue($this->instance->cad('foo', 'bar')); $this->assertFalse($this->instance->hasKey('foo')); } - public function testCadChanged() { + public function testCadChanged(): void { $this->instance->set('foo', 'bar1'); $this->assertFalse($this->instance->cad('foo', 'bar')); $this->assertTrue($this->instance->hasKey('foo')); } + public function testCadNotSet(): void { + $this->assertFalse($this->instance->cad('foo', 'bar')); + } + + public function testNcadNotChanged(): void { + $this->instance->set('foo', 'bar'); + $this->assertFalse($this->instance->ncad('foo', 'bar')); + $this->assertTrue($this->instance->hasKey('foo')); + } + + public function testNcadChanged(): void { + $this->instance->set('foo', 'bar1'); + $this->assertTrue($this->instance->ncad('foo', 'bar')); + $this->assertFalse($this->instance->hasKey('foo')); + } + + public function testNcadNotSet(): void { + $this->assertFalse($this->instance->ncad('foo', 'bar')); + } protected function tearDown(): void { if ($this->instance) { diff --git a/tests/lib/Memcache/CasTraitTest.php b/tests/lib/Memcache/CasTraitTest.php index 61972153886..9de04fa2726 100644 --- a/tests/lib/Memcache/CasTraitTest.php +++ b/tests/lib/Memcache/CasTraitTest.php @@ -1,34 +1,25 @@ <?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/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Memcache; +use OC\Memcache\ArrayCache; use Test\TestCase; +/** + * @group Memcache + */ class CasTraitTest extends TestCase { /** * @return \OC\Memcache\CasTrait */ private function getCache() { - $sourceCache = new \OC\Memcache\ArrayCache(); + $sourceCache = new ArrayCache(); $mock = $this->getMockForTrait('\OC\Memcache\CasTrait'); $mock->expects($this->any()) @@ -57,14 +48,14 @@ class CasTraitTest extends TestCase { return $mock; } - public function testCasNotChanged() { + public function testCasNotChanged(): void { $cache = $this->getCache(); $cache->set('foo', 'bar'); $this->assertTrue($cache->cas('foo', 'bar', 'asd')); $this->assertEquals('asd', $cache->get('foo')); } - public function testCasChanged() { + public function testCasChanged(): void { $cache = $this->getCache(); $cache->set('foo', 'bar1'); $this->assertFalse($cache->cas('foo', 'bar', 'asd')); diff --git a/tests/lib/Memcache/FactoryTest.php b/tests/lib/Memcache/FactoryTest.php index f16f70eddc2..e16e079349f 100644 --- a/tests/lib/Memcache/FactoryTest.php +++ b/tests/lib/Memcache/FactoryTest.php @@ -1,29 +1,18 @@ <?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/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Memcache; +use OC\Memcache\Factory; use OC\Memcache\NullCache; -use Psr\Log\LoggerInterface; +use OCP\HintException; use OCP\Profiler\IProfiler; +use Psr\Log\LoggerInterface; class Test_Factory_Available_Cache1 extends NullCache { public function __construct($prefix = '') { @@ -61,43 +50,46 @@ class Test_Factory_Unavailable_Cache2 extends NullCache { } } +/** + * @group Memcache + */ class FactoryTest extends \Test\TestCase { public const AVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Available_Cache1'; public const AVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Available_Cache2'; public const UNAVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache1'; public const UNAVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache2'; - public function cacheAvailabilityProvider() { + public static function cacheAvailabilityProvider(): array { return [ [ // local and distributed available self::AVAILABLE1, self::AVAILABLE2, null, - self::AVAILABLE1, self::AVAILABLE2, \OC\Memcache\Factory::NULL_CACHE + self::AVAILABLE1, self::AVAILABLE2, 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 + Factory::NULL_CACHE, Factory::NULL_CACHE, Factory::NULL_CACHE ], [ // local available, distributed null (most common scenario) self::AVAILABLE1, null, null, - self::AVAILABLE1, self::AVAILABLE1, \OC\Memcache\Factory::NULL_CACHE + self::AVAILABLE1, self::AVAILABLE1, Factory::NULL_CACHE ], [ // locking cache available null, null, self::AVAILABLE1, - \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, self::AVAILABLE1 + Factory::NULL_CACHE, 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 + Factory::NULL_CACHE, Factory::NULL_CACHE, Factory::NULL_CACHE ] ]; } - public function cacheUnavailableProvider() { + public static function cacheUnavailableProvider(): array { return [ [ // local available, distributed unavailable @@ -114,27 +106,34 @@ class FactoryTest extends \Test\TestCase { ]; } - /** - * @dataProvider cacheAvailabilityProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('cacheAvailabilityProvider')] public function testCacheAvailability($localCache, $distributedCache, $lockingCache, - $expectedLocalCache, $expectedDistributedCache, $expectedLockingCache) { + $expectedLocalCache, $expectedDistributedCache, $expectedLockingCache): void { $logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); $profiler = $this->getMockBuilder(IProfiler::class)->getMock(); - $factory = new \OC\Memcache\Factory('abc', $logger, $profiler, $localCache, $distributedCache, $lockingCache); + $factory = new Factory(fn () => 'abc', $logger, $profiler, $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 - */ - public function testCacheNotAvailableException($localCache, $distributedCache) { - $this->expectException(\OCP\HintException::class); + #[\PHPUnit\Framework\Attributes\DataProvider('cacheUnavailableProvider')] + public function testCacheNotAvailableException($localCache, $distributedCache): void { + $this->expectException(HintException::class); $logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); $profiler = $this->getMockBuilder(IProfiler::class)->getMock(); - new \OC\Memcache\Factory('abc', $logger, $profiler, $localCache, $distributedCache); + new Factory(fn () => 'abc', $logger, $profiler, $localCache, $distributedCache); + } + + public function testCreateInMemory(): void { + $logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); + $profiler = $this->getMockBuilder(IProfiler::class)->getMock(); + $factory = new Factory(fn () => 'abc', $logger, $profiler, null, null, null); + + $cache = $factory->createInMemory(); + $cache->set('test', 48); + + self::assertSame(48, $cache->get('test')); } } diff --git a/tests/lib/Memcache/MemcachedTest.php b/tests/lib/Memcache/MemcachedTest.php index caebf50cd6b..61e2f42e3d6 100644 --- a/tests/lib/Memcache/MemcachedTest.php +++ b/tests/lib/Memcache/MemcachedTest.php @@ -1,22 +1,27 @@ <?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. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Memcache; +use OC\Memcache\Memcached; + +/** + * @group Memcache + * @group Memcached + */ class MemcachedTest extends Cache { public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - if (!\OC\Memcache\Memcached::isAvailable()) { + if (!Memcached::isAvailable()) { self::markTestSkipped('The memcached extension is not available.'); } - $instance = new \OC\Memcache\Memcached(self::getUniqueID()); + $instance = new Memcached(self::getUniqueID()); if ($instance->set(self::getUniqueID(), self::getUniqueID()) === false) { self::markTestSkipped('memcached server seems to be down.'); } @@ -24,10 +29,10 @@ class MemcachedTest extends Cache { protected function setUp(): void { parent::setUp(); - $this->instance = new \OC\Memcache\Memcached($this->getUniqueID()); + $this->instance = new Memcached($this->getUniqueID()); } - public function testClear() { + public function testClear(): void { // Memcached is sometimes broken with clear(), so we don't test it thoroughly $value = 'ipsum lorum'; $this->instance->set('1_value1', $value); diff --git a/tests/lib/Memcache/RedisTest.php b/tests/lib/Memcache/RedisTest.php index e7bb9c29d36..c1dcc954925 100644 --- a/tests/lib/Memcache/RedisTest.php +++ b/tests/lib/Memcache/RedisTest.php @@ -1,36 +1,48 @@ <?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. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Memcache; +use OC\Memcache\Redis; +use OCP\IConfig; +use OCP\Server; + +/** + * @group Memcache + * @group Redis + */ class RedisTest extends Cache { + /** + * @var Redis cache; + */ + protected $instance; + public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - if (!\OC\Memcache\Redis::isAvailable()) { + if (!Redis::isAvailable()) { self::markTestSkipped('The redis extension is not available.'); } - if (\OC::$server->getConfig()->getSystemValue('redis', []) === []) { + if (Server::get(IConfig::class)->getSystemValue('redis', []) === []) { self::markTestSkipped('Redis not configured in config.php'); } $errorOccurred = false; set_error_handler( - function ($errno, $errstr) { + function ($errno, $errstr): void { throw new \RuntimeException($errstr, 123456789); }, E_WARNING ); $instance = null; try { - $instance = new \OC\Memcache\Redis(self::getUniqueID()); + $instance = new Redis(self::getUniqueID()); } catch (\RuntimeException $e) { $errorOccurred = $e->getCode() === 123456789 ? $e->getMessage() : false; } @@ -50,6 +62,26 @@ class RedisTest extends Cache { protected function setUp(): void { parent::setUp(); - $this->instance = new \OC\Memcache\Redis($this->getUniqueID()); + $this->instance = new Redis($this->getUniqueID()); + } + + public function testScriptHashes(): void { + foreach (Redis::LUA_SCRIPTS as $script) { + $this->assertEquals(sha1($script[0]), $script[1]); + } + } + + public function testCasTtlNotChanged(): void { + $this->instance->set('foo', 'bar', 50); + $this->assertTrue($this->instance->compareSetTTL('foo', 'bar', 100)); + // allow for 1s of inaccuracy due to time moving forward + $this->assertLessThan(1, 100 - $this->instance->getTTL('foo')); + } + + public function testCasTtlChanged(): void { + $this->instance->set('foo', 'bar1', 50); + $this->assertFalse($this->instance->compareSetTTL('foo', 'bar', 100)); + // allow for 1s of inaccuracy due to time moving forward + $this->assertLessThan(1, 50 - $this->instance->getTTL('foo')); } } |