MemcacheTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * MemcacheEngineTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs.cache
  16. * @since CakePHP(tm) v 1.2.0.5434
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Cache', 'Cache');
  20. App::uses('MemcacheEngine', 'Cache/Engine');
  21. class TestMemcacheEngine extends MemcacheEngine {
  22. /**
  23. * public accessor to _parseServerString
  24. *
  25. * @param string $server
  26. * @return array
  27. */
  28. function parseServerString($server) {
  29. return $this->_parseServerString($server);
  30. }
  31. function setMemcache($memcache) {
  32. $this->_Memcache = $memcache;
  33. }
  34. }
  35. /**
  36. * MemcacheEngineTest class
  37. *
  38. * @package cake.tests.cases.libs.cache
  39. */
  40. class MemcacheEngineTest extends CakeTestCase {
  41. /**
  42. * setUp method
  43. *
  44. * @access public
  45. * @return void
  46. */
  47. function setUp() {
  48. $this->skipIf(!class_exists('Memcache'), '%s Apc is not installed or configured properly');
  49. $this->_cacheDisable = Configure::read('Cache.disable');
  50. Configure::write('Cache.disable', false);
  51. Cache::config('memcache', array(
  52. 'engine' => 'Memcache',
  53. 'prefix' => 'cake_',
  54. 'duration' => 3600
  55. ));
  56. }
  57. /**
  58. * tearDown method
  59. *
  60. * @access public
  61. * @return void
  62. */
  63. function tearDown() {
  64. Configure::write('Cache.disable', $this->_cacheDisable);
  65. Cache::drop('memcache');
  66. Cache::config('default');
  67. }
  68. /**
  69. * testSettings method
  70. *
  71. * @access public
  72. * @return void
  73. */
  74. function testSettings() {
  75. $settings = Cache::settings('memcache');
  76. unset($settings['serialize'], $settings['path']);
  77. $expecting = array(
  78. 'prefix' => 'cake_',
  79. 'duration'=> 3600,
  80. 'probability' => 100,
  81. 'servers' => array('127.0.0.1'),
  82. 'compress' => false,
  83. 'engine' => 'Memcache'
  84. );
  85. $this->assertEqual($settings, $expecting);
  86. }
  87. /**
  88. * testSettings method
  89. *
  90. * @access public
  91. * @return void
  92. */
  93. function testMultipleServers() {
  94. $servers = array('127.0.0.1:11211', '127.0.0.1:11222');
  95. $available = true;
  96. $Memcache = new Memcache();
  97. foreach($servers as $server) {
  98. list($host, $port) = explode(':', $server);
  99. if (!@$Memcache->connect($host, $port)) {
  100. $available = false;
  101. }
  102. }
  103. if ($this->skipIf(!$available, '%s Need memcache servers at ' . implode(', ', $servers) . ' to run this test')) {
  104. return;
  105. }
  106. $Memcache = new MemcacheEngine();
  107. $Memcache->init(array('engine' => 'Memcache', 'servers' => $servers));
  108. $servers = array_keys($Memcache->__Memcache->getExtendedStats());
  109. $settings = $Memcache->settings();
  110. $this->assertEqual($servers, $settings['servers']);
  111. Cache::drop('dual_server');
  112. }
  113. /**
  114. * testConnect method
  115. *
  116. * @access public
  117. * @return void
  118. */
  119. function testConnect() {
  120. $Memcache = new MemcacheEngine();
  121. $Memcache->init(Cache::settings('memcache'));
  122. $result = $Memcache->connect('127.0.0.1');
  123. $this->assertTrue($result);
  124. }
  125. /**
  126. * test connecting to an ipv6 server.
  127. *
  128. * @return void
  129. */
  130. function testConnectIpv6() {
  131. $Memcache = new MemcacheEngine();
  132. $result = $Memcache->init(array(
  133. 'prefix' => 'cake_',
  134. 'duration' => 200,
  135. 'engine' => 'Memcache',
  136. 'servers' => array(
  137. '[::1]:11211'
  138. )
  139. ));
  140. $this->assertTrue($result);
  141. }
  142. /**
  143. * test non latin domains.
  144. *
  145. * @return void
  146. */
  147. function testParseServerStringNonLatin() {
  148. $Memcache = new TestMemcacheEngine();
  149. $result = $Memcache->parseServerString('schülervz.net:13211');
  150. $this->assertEqual($result, array('schülervz.net', '13211'));
  151. $result = $Memcache->parseServerString('sülül:1111');
  152. $this->assertEqual($result, array('sülül', '1111'));
  153. }
  154. /**
  155. * testReadAndWriteCache method
  156. *
  157. * @access public
  158. * @return void
  159. */
  160. function testReadAndWriteCache() {
  161. Cache::set(array('duration' => 1), null, 'memcache');
  162. $result = Cache::read('test', 'memcache');
  163. $expecting = '';
  164. $this->assertEqual($result, $expecting);
  165. $data = 'this is a test of the emergency broadcasting system';
  166. $result = Cache::write('test', $data, 'memcache');
  167. $this->assertTrue($result);
  168. $result = Cache::read('test', 'memcache');
  169. $expecting = $data;
  170. $this->assertEqual($result, $expecting);
  171. Cache::delete('test', 'memcache');
  172. }
  173. /**
  174. * testExpiry method
  175. *
  176. * @access public
  177. * @return void
  178. */
  179. function testExpiry() {
  180. Cache::set(array('duration' => 1), 'memcache');
  181. $result = Cache::read('test', 'memcache');
  182. $this->assertFalse($result);
  183. $data = 'this is a test of the emergency broadcasting system';
  184. $result = Cache::write('other_test', $data, 'memcache');
  185. $this->assertTrue($result);
  186. sleep(2);
  187. $result = Cache::read('other_test', 'memcache');
  188. $this->assertFalse($result);
  189. Cache::set(array('duration' => "+1 second"), 'memcache');
  190. $data = 'this is a test of the emergency broadcasting system';
  191. $result = Cache::write('other_test', $data, 'memcache');
  192. $this->assertTrue($result);
  193. sleep(2);
  194. $result = Cache::read('other_test', 'memcache');
  195. $this->assertFalse($result);
  196. Cache::config('memcache', array('duration' => '+1 second'));
  197. sleep(2);
  198. $result = Cache::read('other_test', 'memcache');
  199. $this->assertFalse($result);
  200. Cache::config('memcache', array('duration' => '+29 days'));
  201. $data = 'this is a test of the emergency broadcasting system';
  202. $result = Cache::write('long_expiry_test', $data, 'memcache');
  203. $this->assertTrue($result);
  204. sleep(2);
  205. $result = Cache::read('long_expiry_test', 'memcache');
  206. $expecting = $data;
  207. $this->assertEqual($result, $expecting);
  208. Cache::config('memcache', array('duration' => 3600));
  209. }
  210. /**
  211. * testDeleteCache method
  212. *
  213. * @access public
  214. * @return void
  215. */
  216. function testDeleteCache() {
  217. $data = 'this is a test of the emergency broadcasting system';
  218. $result = Cache::write('delete_test', $data, 'memcache');
  219. $this->assertTrue($result);
  220. $result = Cache::delete('delete_test', 'memcache');
  221. $this->assertTrue($result);
  222. }
  223. /**
  224. * testDecrement method
  225. *
  226. * @access public
  227. * @return void
  228. */
  229. function testDecrement() {
  230. $result = Cache::write('test_decrement', 5, 'memcache');
  231. $this->assertTrue($result);
  232. $result = Cache::decrement('test_decrement', 1, 'memcache');
  233. $this->assertEqual(4, $result);
  234. $result = Cache::read('test_decrement', 'memcache');
  235. $this->assertEqual(4, $result);
  236. $result = Cache::decrement('test_decrement', 2, 'memcache');
  237. $this->assertEqual(2, $result);
  238. $result = Cache::read('test_decrement', 'memcache');
  239. $this->assertEqual(2, $result);
  240. }
  241. /**
  242. * testIncrement method
  243. *
  244. * @access public
  245. * @return void
  246. */
  247. function testIncrement() {
  248. $result = Cache::write('test_increment', 5, 'memcache');
  249. $this->assertTrue($result);
  250. $result = Cache::increment('test_increment', 1, 'memcache');
  251. $this->assertEqual(6, $result);
  252. $result = Cache::read('test_increment', 'memcache');
  253. $this->assertEqual(6, $result);
  254. $result = Cache::increment('test_increment', 2, 'memcache');
  255. $this->assertEqual(8, $result);
  256. $result = Cache::read('test_increment', 'memcache');
  257. $this->assertEqual(8, $result);
  258. }
  259. /**
  260. * test that configurations don't conflict, when a file engine is declared after a memcache one.
  261. *
  262. * @return void
  263. */
  264. function testConfigurationConflict() {
  265. Cache::config('long_memcache', array(
  266. 'engine' => 'Memcache',
  267. 'duration'=> '+2 seconds',
  268. 'servers' => array('127.0.0.1:11211'),
  269. ));
  270. Cache::config('short_memcache', array(
  271. 'engine' => 'Memcache',
  272. 'duration'=> '+1 seconds',
  273. 'servers' => array('127.0.0.1:11211'),
  274. ));
  275. Cache::config('some_file', array('engine' => 'File'));
  276. $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcache'));
  277. $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcache'));
  278. $this->assertEqual(Cache::read('duration_test', 'long_memcache'), 'yay', 'Value was not read %s');
  279. $this->assertEqual(Cache::read('short_duration_test', 'short_memcache'), 'boo', 'Value was not read %s');
  280. sleep(1);
  281. $this->assertEqual(Cache::read('duration_test', 'long_memcache'), 'yay', 'Value was not read %s');
  282. sleep(2);
  283. $this->assertFalse(Cache::read('short_duration_test', 'short_memcache'), 'Cache was not invalidated %s');
  284. $this->assertFalse(Cache::read('duration_test', 'long_memcache'), 'Value did not expire %s');
  285. Cache::delete('duration_test', 'long_memcache');
  286. Cache::delete('short_duration_test', 'short_memcache');
  287. }
  288. /**
  289. * test clearing memcache.
  290. *
  291. * @return void
  292. */
  293. function testClear() {
  294. Cache::write('some_value', 'value', 'memcache');
  295. $result = Cache::clear(false, 'memcache');
  296. $this->assertTrue($result);
  297. $this->assertFalse(Cache::read('some_value', 'memcache'));
  298. }
  299. /**
  300. * test that a 0 duration can succesfully write.
  301. *
  302. * @return void
  303. */
  304. function testZeroDuration() {
  305. Cache::config('memcache', array('duration' => 0));
  306. $result = Cache::write('test_key', 'written!', 'memcache');
  307. $this->assertTrue($result, 'Could not write with duration 0');
  308. $result = Cache::read('test_key', 'memcache');
  309. $this->assertEqual($result, 'written!');
  310. }
  311. /**
  312. * test that durations greater than 30 days never expire
  313. *
  314. * @return void
  315. */
  316. function testLongDurationEqualToZero() {
  317. $memcache =& new TestMemcacheEngine();
  318. $memcache->settings['compress'] = false;
  319. $mock = $this->getMock('Memcache');
  320. $memcache->setMemcache($mock);
  321. $mock->expects($this->once())
  322. ->method('set')
  323. ->with('key', 'value', false, 0);
  324. $value = 'value';
  325. $memcache->write('key', $value, 50 * DAY);
  326. }
  327. }