1
0

RedisTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Memcache;
  8. use OC\Memcache\Redis;
  9. /**
  10. * @group Memcache
  11. * @group Redis
  12. */
  13. class RedisTest extends Cache {
  14. /**
  15. * @var Redis cache;
  16. */
  17. protected $instance;
  18. public static function setUpBeforeClass(): void {
  19. parent::setUpBeforeClass();
  20. if (!\OC\Memcache\Redis::isAvailable()) {
  21. self::markTestSkipped('The redis extension is not available.');
  22. }
  23. if (\OC::$server->getConfig()->getSystemValue('redis', []) === []) {
  24. self::markTestSkipped('Redis not configured in config.php');
  25. }
  26. $errorOccurred = false;
  27. set_error_handler(
  28. function ($errno, $errstr) {
  29. throw new \RuntimeException($errstr, 123456789);
  30. },
  31. E_WARNING
  32. );
  33. $instance = null;
  34. try {
  35. $instance = new \OC\Memcache\Redis(self::getUniqueID());
  36. } catch (\RuntimeException $e) {
  37. $errorOccurred = $e->getCode() === 123456789 ? $e->getMessage() : false;
  38. }
  39. restore_error_handler();
  40. if ($errorOccurred !== false) {
  41. self::markTestSkipped($errorOccurred);
  42. }
  43. if ($instance === null) {
  44. throw new \Exception('redis server is not reachable');
  45. }
  46. if ($instance->set(self::getUniqueID(), self::getUniqueID()) === false) {
  47. self::markTestSkipped('redis server seems to be down.');
  48. }
  49. }
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->instance = new \OC\Memcache\Redis($this->getUniqueID());
  53. }
  54. public function testScriptHashes(): void {
  55. foreach (\OC\Memcache\Redis::LUA_SCRIPTS as $script) {
  56. $this->assertEquals(sha1($script[0]), $script[1]);
  57. }
  58. }
  59. public function testCasTtlNotChanged(): void {
  60. $this->instance->set('foo', 'bar', 50);
  61. $this->assertTrue($this->instance->compareSetTTL('foo', 'bar', 100));
  62. // allow for 1s of inaccuracy due to time moving forward
  63. $this->assertLessThan(1, 100 - $this->instance->getTTL('foo'));
  64. }
  65. public function testCasTtlChanged(): void {
  66. $this->instance->set('foo', 'bar1', 50);
  67. $this->assertFalse($this->instance->compareSetTTL('foo', 'bar', 100));
  68. // allow for 1s of inaccuracy due to time moving forward
  69. $this->assertLessThan(1, 50 - $this->instance->getTTL('foo'));
  70. }
  71. }