2015-01-14 13:25:00 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-01-14 13:25:00 -05:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-01-14 13:25:00 -05:00
|
|
|
*/
|
2019-11-22 14:52:10 -05:00
|
|
|
|
2015-01-14 13:25:00 -05:00
|
|
|
namespace Test\Memcache;
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
use OC\Memcache\Factory;
|
2018-01-16 14:44:51 -05:00
|
|
|
use OC\Memcache\NullCache;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\HintException;
|
2022-01-19 17:30:34 -05:00
|
|
|
use OCP\Profiler\IProfiler;
|
2025-08-12 16:20:15 -04:00
|
|
|
use OCP\ServerVersion;
|
2023-11-23 04:22:34 -05:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-10-24 09:26:53 -04:00
|
|
|
|
2018-01-16 14:44:51 -05:00
|
|
|
class Test_Factory_Available_Cache1 extends NullCache {
|
2015-01-14 13:25:00 -05:00
|
|
|
public function __construct($prefix = '') {
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 17:30:34 -05:00
|
|
|
public static function isAvailable(): bool {
|
2015-01-14 13:25:00 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-16 14:44:51 -05:00
|
|
|
class Test_Factory_Available_Cache2 extends NullCache {
|
2015-01-14 13:25:00 -05:00
|
|
|
public function __construct($prefix = '') {
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 17:30:34 -05:00
|
|
|
public static function isAvailable(): bool {
|
2015-01-14 13:25:00 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-16 14:44:51 -05:00
|
|
|
class Test_Factory_Unavailable_Cache1 extends NullCache {
|
2015-01-14 13:25:00 -05:00
|
|
|
public function __construct($prefix = '') {
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 17:30:34 -05:00
|
|
|
public static function isAvailable(): bool {
|
2015-01-14 13:25:00 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-16 14:44:51 -05:00
|
|
|
class Test_Factory_Unavailable_Cache2 extends NullCache {
|
2015-01-14 13:25:00 -05:00
|
|
|
public function __construct($prefix = '') {
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 17:30:34 -05:00
|
|
|
public static function isAvailable(): bool {
|
2015-01-14 13:25:00 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 19:52:40 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\Group('Memcache')]
|
2016-05-19 02:53:06 -04:00
|
|
|
class FactoryTest extends \Test\TestCase {
|
2020-04-10 10:54:27 -04:00
|
|
|
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';
|
2015-01-14 13:25:00 -05:00
|
|
|
|
2025-05-13 04:10:13 -04:00
|
|
|
public static function cacheAvailabilityProvider(): array {
|
2015-01-14 13:25:00 -05:00
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
// local and distributed available
|
2015-06-09 09:22:33 -04:00
|
|
|
self::AVAILABLE1, self::AVAILABLE2, null,
|
2025-06-12 12:31:58 -04:00
|
|
|
self::AVAILABLE1, self::AVAILABLE2, Factory::NULL_CACHE
|
2015-01-14 13:25:00 -05:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-09 09:22:33 -04:00
|
|
|
// local and distributed null
|
|
|
|
|
null, null, null,
|
2025-06-12 12:31:58 -04:00
|
|
|
Factory::NULL_CACHE, Factory::NULL_CACHE, Factory::NULL_CACHE
|
2015-01-14 13:25:00 -05:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-09 09:22:33 -04:00
|
|
|
// local available, distributed null (most common scenario)
|
|
|
|
|
self::AVAILABLE1, null, null,
|
2025-06-12 12:31:58 -04:00
|
|
|
self::AVAILABLE1, self::AVAILABLE1, Factory::NULL_CACHE
|
2015-01-14 13:25:00 -05:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-09 09:22:33 -04:00
|
|
|
// locking cache available
|
|
|
|
|
null, null, self::AVAILABLE1,
|
2025-06-12 12:31:58 -04:00
|
|
|
Factory::NULL_CACHE, Factory::NULL_CACHE, self::AVAILABLE1
|
2015-01-14 13:25:00 -05:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-09 09:22:33 -04:00
|
|
|
// locking cache unavailable: no exception here in the factory
|
|
|
|
|
null, null, self::UNAVAILABLE1,
|
2025-06-12 12:31:58 -04:00
|
|
|
Factory::NULL_CACHE, Factory::NULL_CACHE, Factory::NULL_CACHE
|
2015-06-09 09:22:33 -04:00
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-13 04:10:13 -04:00
|
|
|
public static function cacheUnavailableProvider(): array {
|
2015-06-09 09:22:33 -04:00
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
// local available, distributed unavailable
|
|
|
|
|
self::AVAILABLE1, self::UNAVAILABLE1
|
2015-01-14 13:25:00 -05:00
|
|
|
],
|
|
|
|
|
[
|
2015-06-09 09:22:33 -04:00
|
|
|
// local unavailable, distributed available
|
|
|
|
|
self::UNAVAILABLE1, self::AVAILABLE1
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
// local and distributed unavailable
|
|
|
|
|
self::UNAVAILABLE1, self::UNAVAILABLE2
|
|
|
|
|
],
|
2015-01-14 13:25:00 -05:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('cacheAvailabilityProvider')]
|
2015-06-09 09:22:33 -04:00
|
|
|
public function testCacheAvailability($localCache, $distributedCache, $lockingCache,
|
2024-09-15 16:32:31 -04:00
|
|
|
$expectedLocalCache, $expectedDistributedCache, $expectedLockingCache): void {
|
2022-03-21 06:17:14 -04:00
|
|
|
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
|
2022-01-19 17:30:34 -05:00
|
|
|
$profiler = $this->getMockBuilder(IProfiler::class)->getMock();
|
2025-08-12 16:20:15 -04:00
|
|
|
$serverVersion = $this->createMock(ServerVersion::class);
|
|
|
|
|
$factory = new Factory($logger, $profiler, $serverVersion, $localCache, $distributedCache, $lockingCache);
|
2015-01-14 13:25:00 -05:00
|
|
|
$this->assertTrue(is_a($factory->createLocal(), $expectedLocalCache));
|
|
|
|
|
$this->assertTrue(is_a($factory->createDistributed(), $expectedDistributedCache));
|
2015-06-09 09:22:33 -04:00
|
|
|
$this->assertTrue(is_a($factory->createLocking(), $expectedLockingCache));
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('cacheUnavailableProvider')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testCacheNotAvailableException($localCache, $distributedCache): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(HintException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2022-03-21 06:17:14 -04:00
|
|
|
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
|
2022-01-19 17:30:34 -05:00
|
|
|
$profiler = $this->getMockBuilder(IProfiler::class)->getMock();
|
2025-08-12 16:20:15 -04:00
|
|
|
$serverVersion = $this->createMock(ServerVersion::class);
|
|
|
|
|
new Factory($logger, $profiler, $serverVersion, $localCache, $distributedCache);
|
2015-01-14 13:25:00 -05:00
|
|
|
}
|
2023-10-11 07:29:21 -04:00
|
|
|
|
|
|
|
|
public function testCreateInMemory(): void {
|
|
|
|
|
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
|
|
|
|
|
$profiler = $this->getMockBuilder(IProfiler::class)->getMock();
|
2025-08-12 16:20:15 -04:00
|
|
|
$serverVersion = $this->createMock(ServerVersion::class);
|
|
|
|
|
$factory = new Factory($logger, $profiler, $serverVersion, null, null, null);
|
2023-10-11 07:29:21 -04:00
|
|
|
|
|
|
|
|
$cache = $factory->createInMemory();
|
|
|
|
|
$cache->set('test', 48);
|
|
|
|
|
|
|
|
|
|
self::assertSame(48, $cache->get('test'));
|
|
|
|
|
}
|
2015-01-14 13:25:00 -05:00
|
|
|
}
|