RedisTest.php 2.2 KB

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