FlushUnitTestCase.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. abstract class Redis_Tests_Cache_FlushUnitTestCase extends Redis_Tests_AbstractUnitTestCase
  3. {
  4. /**
  5. * @var Cache bin identifier
  6. */
  7. static private $id = 1;
  8. protected function createCacheInstance($name = null)
  9. {
  10. return new Redis_Cache($name);
  11. }
  12. /**
  13. * Get cache backend
  14. *
  15. * @return Redis_Cache
  16. */
  17. final protected function getBackend($name = null)
  18. {
  19. if (null === $name) {
  20. // This is needed to avoid conflict between tests, each test
  21. // seems to use the same Redis namespace and conflicts are
  22. // possible.
  23. $name = 'cache' . (self::$id++);
  24. }
  25. $backend = $this->createCacheInstance($name);
  26. $this->assert(true, "Redis client is " . ($backend->isSharded() ? '' : "NOT ") . " sharded");
  27. $this->assert(true, "Redis client is " . ($backend->allowTemporaryFlush() ? '' : "NOT ") . " allowed to flush temporary entries");
  28. $this->assert(true, "Redis client is " . ($backend->allowPipeline() ? '' : "NOT ") . " allowed to use pipeline");
  29. return $backend;
  30. }
  31. /**
  32. * Tests that with a default cache lifetime temporary non expired
  33. * items are kept even when in temporary flush mode.
  34. */
  35. public function testFlushIsTemporaryWithLifetime()
  36. {
  37. $GLOBALS['conf']['cache_lifetime'] = 112;
  38. $backend = $this->getBackend();
  39. // Even though we set a flush mode into this bin, Drupal default
  40. // behavior when a cache_lifetime is set is to override the backend
  41. // one in order to keep the core behavior and avoid potential
  42. // nasty bugs.
  43. $this->assertFalse($backend->allowTemporaryFlush());
  44. $backend->set('test7', 42, CACHE_PERMANENT);
  45. $backend->set('test8', 'foo', CACHE_TEMPORARY);
  46. $backend->set('test9', 'bar', time() + 1000);
  47. $backend->clear();
  48. $cache = $backend->get('test7');
  49. $this->assertNotEqual(false, $cache);
  50. $this->assertEqual($cache->data, 42);
  51. $cache = $backend->get('test8');
  52. $this->assertNotEqual(false, $cache);
  53. $this->assertEqual($cache->data, 'foo');
  54. $cache = $backend->get('test9');
  55. $this->assertNotEqual(false, $cache);
  56. $this->assertEqual($cache->data, 'bar');
  57. }
  58. /**
  59. * Tests that with no default cache lifetime all temporary items are
  60. * droppped when in temporary flush mode.
  61. */
  62. public function testFlushIsTemporaryWithoutLifetime()
  63. {
  64. $backend = $this->getBackend();
  65. $this->assertTrue($backend->allowTemporaryFlush());
  66. $backend->set('test10', 42, CACHE_PERMANENT);
  67. // Ugly concatenation with the mode, but it will be visible in tests
  68. // reports if the entry shows up, thus allowing us to know which real
  69. // test case is run at this time
  70. $backend->set('test11', 'foo' . $backend->isSharded(), CACHE_TEMPORARY);
  71. $backend->set('test12', 'bar' . $backend->isSharded(), time() + 10);
  72. $backend->clear();
  73. $cache = $backend->get('test10');
  74. $this->assertNotEqual(false, $cache);
  75. $this->assertEqual($cache->data, 42);
  76. $this->assertFalse($backend->get('test11'));
  77. $cache = $backend->get('test12');
  78. $this->assertNotEqual(false, $cache);
  79. }
  80. public function testNormalFlushing()
  81. {
  82. $backend = $this->getBackend();
  83. $backendUntouched = $this->getBackend();
  84. // Set a few entries.
  85. $backend->set('test13', 'foo');
  86. $backend->set('test14', 'bar', CACHE_TEMPORARY);
  87. $backend->set('test15', 'baz', time() + 3);
  88. $backendUntouched->set('test16', 'dog');
  89. $backendUntouched->set('test17', 'cat', CACHE_TEMPORARY);
  90. $backendUntouched->set('test18', 'xor', time() + 5);
  91. // This should not do anything (bugguy command)
  92. $backend->clear('', true);
  93. $backend->clear('', false);
  94. $this->assertNotIdentical(false, $backend->get('test13'));
  95. $this->assertNotIdentical(false, $backend->get('test14'));
  96. $this->assertNotIdentical(false, $backend->get('test15'));
  97. $this->assertNotIdentical(false, $backendUntouched->get('test16'));
  98. $this->assertNotIdentical(false, $backendUntouched->get('test17'));
  99. $this->assertNotIdentical(false, $backendUntouched->get('test18'));
  100. // This should clear every one, permanent and volatile
  101. $backend->clear('*', true);
  102. $this->assertFalse($backend->get('test13'));
  103. $this->assertFalse($backend->get('test14'));
  104. $this->assertFalse($backend->get('test15'));
  105. $this->assertNotIdentical(false, $backendUntouched->get('test16'));
  106. $this->assertNotIdentical(false, $backendUntouched->get('test17'));
  107. $this->assertNotIdentical(false, $backendUntouched->get('test18'));
  108. }
  109. public function testPrefixDeletionWithSeparatorChar()
  110. {
  111. $backend = $this->getBackend();
  112. $backend->set('testprefix10', 'foo');
  113. $backend->set('testprefix11', 'foo');
  114. $backend->set('testprefix:12', 'bar');
  115. $backend->set('testprefix:13', 'baz');
  116. $backend->set('testnoprefix14', 'giraffe');
  117. $backend->set('testnoprefix:15', 'elephant');
  118. $backend->clear('testprefix:', true);
  119. $this->assertFalse($backend->get('testprefix:12'));
  120. $this->assertFalse($backend->get('testprefix:13'));
  121. // @todo Temporary fix
  122. // At the moment shard enabled backends will erase all data instead
  123. // of just removing by prefix, so those tests won't pass
  124. if (!$backend->isSharded()) {
  125. $this->assertNotIdentical(false, $backend->get('testprefix10'));
  126. $this->assertNotIdentical(false, $backend->get('testprefix11'));
  127. $this->assertNotIdentical(false, $backend->get('testnoprefix14'));
  128. $this->assertNotIdentical(false, $backend->get('testnoprefix:15'));
  129. }
  130. $backend->clear('testprefix', true);
  131. $this->assertFalse($backend->get('testprefix10'));
  132. $this->assertFalse($backend->get('testprefix11'));
  133. // @todo Temporary fix
  134. // At the moment shard enabled backends will erase all data instead
  135. // of just removing by prefix, so those tests won't pass
  136. if (!$backend->isSharded()) {
  137. $this->assertNotIdentical(false, $backend->get('testnoprefix14'));
  138. $this->assertNotIdentical(false, $backend->get('testnoprefix:15'));
  139. }
  140. }
  141. public function testOrder()
  142. {
  143. $backend = $this->getBackend();
  144. for ($i = 0; $i < 10; ++$i) {
  145. $id = 'speedtest' . $i;
  146. $backend->set($id, 'somevalue');
  147. $this->assertNotIdentical(false, $backend->get($id));
  148. $backend->clear('*', true);
  149. // Value created the same second before is dropped
  150. $this->assertFalse($backend->get($id));
  151. $backend->set($id, 'somevalue');
  152. // Value created the same second after is kept
  153. $this->assertNotIdentical(false, $backend->get($id));
  154. }
  155. }
  156. }