FixesUnitTestCase.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. if (!class_exists('Redis_Tests_AbstractUnitTestCase')) {
  3. require_once(__DIR__ . '/../AbstractUnitTestCase.php');
  4. }
  5. /**
  6. * Bugfixes made over time test class.
  7. */
  8. abstract class Redis_Tests_Cache_FixesUnitTestCase extends Redis_Tests_AbstractUnitTestCase
  9. {
  10. /**
  11. * @var Cache bin identifier
  12. */
  13. static private $id = 1;
  14. protected function createCacheInstance($name = null)
  15. {
  16. return new Redis_Cache($name);
  17. }
  18. /**
  19. * Get cache backend
  20. *
  21. * @return Redis_Cache
  22. */
  23. final protected function getBackend($name = null)
  24. {
  25. if (null === $name) {
  26. // This is needed to avoid conflict between tests, each test
  27. // seems to use the same Redis namespace and conflicts are
  28. // possible.
  29. $name = 'cache' . (self::$id++);
  30. }
  31. $backend = $this->createCacheInstance($name);
  32. $this->assert(true, "Redis client is " . ($backend->isSharded() ? '' : "NOT ") . " sharded");
  33. $this->assert(true, "Redis client is " . ($backend->allowTemporaryFlush() ? '' : "NOT ") . " allowed to flush temporary entries");
  34. $this->assert(true, "Redis client is " . ($backend->allowPipeline() ? '' : "NOT ") . " allowed to use pipeline");
  35. return $backend;
  36. }
  37. public function testTemporaryCacheExpire()
  38. {
  39. global $conf; // We are in unit tests so variable table does not exist.
  40. $backend = $this->getBackend();
  41. // Permanent entry.
  42. $backend->set('test1', 'foo', CACHE_PERMANENT);
  43. $data = $backend->get('test1');
  44. $this->assertNotEqual(false, $data);
  45. $this->assertIdentical('foo', $data->data);
  46. // Permanent entries should not be dropped on clear() call.
  47. $backend->clear();
  48. $data = $backend->get('test1');
  49. $this->assertNotEqual(false, $data);
  50. $this->assertIdentical('foo', $data->data);
  51. // Expiring entry with permanent default lifetime.
  52. $conf['cache_lifetime'] = 0;
  53. $backend->set('test2', 'bar', CACHE_TEMPORARY);
  54. sleep(2);
  55. $data = $backend->get('test2');
  56. $this->assertNotEqual(false, $data);
  57. $this->assertIdentical('bar', $data->data);
  58. sleep(2);
  59. $data = $backend->get('test2');
  60. $this->assertNotEqual(false, $data);
  61. $this->assertIdentical('bar', $data->data);
  62. // Expiring entry with negative lifetime.
  63. $backend->set('test3', 'baz', time() - 100);
  64. $data = $backend->get('test3');
  65. $this->assertEqual(false, $data);
  66. // Expiring entry with short lifetime.
  67. $backend->set('test4', 'foobar', time() + 2);
  68. $data = $backend->get('test4');
  69. $this->assertNotEqual(false, $data);
  70. $this->assertIdentical('foobar', $data->data);
  71. sleep(4);
  72. $data = $backend->get('test4');
  73. $this->assertEqual(false, $data);
  74. // Expiring entry with short default lifetime.
  75. $conf['cache_lifetime'] = 1;
  76. $backend->refreshMaxTtl();
  77. $backend->set('test5', 'foobaz', CACHE_TEMPORARY);
  78. $data = $backend->get('test5');
  79. $this->assertNotEqual(false, $data);
  80. $this->assertIdentical('foobaz', $data->data);
  81. sleep(3);
  82. $data = $backend->get('test5');
  83. $this->assertEqual(false, $data);
  84. }
  85. public function testDefaultPermTtl()
  86. {
  87. global $conf;
  88. unset($conf['redis_perm_ttl']);
  89. $backend = $this->getBackend();
  90. $this->assertIdentical(Redis_Cache::LIFETIME_PERM_DEFAULT, $backend->getPermTtl());
  91. }
  92. public function testUserSetDefaultPermTtl()
  93. {
  94. global $conf;
  95. // This also testes string parsing. Not fully, but at least one case.
  96. $conf['redis_perm_ttl'] = "3 months";
  97. $backend = $this->getBackend();
  98. $this->assertIdentical(7776000, $backend->getPermTtl());
  99. }
  100. public function testUserSetPermTtl()
  101. {
  102. global $conf;
  103. // This also testes string parsing. Not fully, but at least one case.
  104. $conf['redis_perm_ttl'] = "1 months";
  105. $backend = $this->getBackend();
  106. $this->assertIdentical(2592000, $backend->getPermTtl());
  107. }
  108. public function testGetMultiple()
  109. {
  110. $backend = $this->getBackend();
  111. $backend->set('multiple1', 1);
  112. $backend->set('multiple2', 2);
  113. $backend->set('multiple3', 3);
  114. $backend->set('multiple4', 4);
  115. $cidList = array('multiple1', 'multiple2', 'multiple3', 'multiple4', 'multiple5');
  116. $ret = $backend->getMultiple($cidList);
  117. $this->assertEqual(1, count($cidList));
  118. $this->assertFalse(isset($cidList[0]));
  119. $this->assertFalse(isset($cidList[1]));
  120. $this->assertFalse(isset($cidList[2]));
  121. $this->assertFalse(isset($cidList[3]));
  122. $this->assertTrue(isset($cidList[4]));
  123. $this->assertEqual(4, count($ret));
  124. $this->assertTrue(isset($ret['multiple1']));
  125. $this->assertTrue(isset($ret['multiple2']));
  126. $this->assertTrue(isset($ret['multiple3']));
  127. $this->assertTrue(isset($ret['multiple4']));
  128. $this->assertFalse(isset($ret['multiple5']));
  129. }
  130. public function testPermTtl()
  131. {
  132. global $conf;
  133. // This also testes string parsing. Not fully, but at least one case.
  134. $conf['redis_perm_ttl'] = "2 seconds";
  135. $backend = $this->getBackend();
  136. $this->assertIdentical(2, $backend->getPermTtl());
  137. $backend->set('test6', 'cats are mean');
  138. $this->assertIdentical('cats are mean', $backend->get('test6')->data);
  139. sleep(3);
  140. $item = $backend->get('test6');
  141. $this->assertTrue(empty($item));
  142. }
  143. public function testClearAsArray()
  144. {
  145. $backend = $this->getBackend();
  146. $backend->set('test7', 1);
  147. $backend->set('test8', 2);
  148. $backend->set('test9', 3);
  149. $backend->clear(array('test7', 'test9'));
  150. $item = $backend->get('test7');
  151. $this->assertTrue(empty($item));
  152. $item = $backend->get('test8');
  153. $this->assertEqual(2, $item->data);
  154. $item = $backend->get('test9');
  155. $this->assertTrue(empty($item));
  156. }
  157. public function testGetMultipleAlterCidsWhenCacheHitsOnly()
  158. {
  159. $backend = $this->getBackend();
  160. $backend->clear('*', true); // It seems that there are leftovers.
  161. $backend->set('mtest1', 'pouf');
  162. $cids_partial_hit = array('foo' => 'mtest1', 'bar' => 'mtest2');
  163. $entries = $backend->getMultiple($cids_partial_hit);
  164. $this->assertIdentical(1, count($entries));
  165. // Note that the key is important because the method should
  166. // keep the keys synchronized.
  167. $this->assertEqual(array('bar' => 'mtest2'), $cids_partial_hit);
  168. $backend->clear('mtest1');
  169. $cids_no_hit = array('cat' => 'mtest1', 'dog' => 'mtest2');
  170. $entries = $backend->getMultiple($cids_no_hit);
  171. $this->assertIdentical(0, count($entries));
  172. $this->assertEqual(array('cat' => 'mtest1', 'dog' => 'mtest2'), $cids_no_hit);
  173. }
  174. }