MemcacheEngineTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Cache.Engine
  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. public function parseServerString($server) {
  29. return $this->_parseServerString($server);
  30. }
  31. public function setMemcache($memcache) {
  32. $this->_Memcache = $memcache;
  33. }
  34. }
  35. /**
  36. * MemcacheEngineTest class
  37. *
  38. * @package Cake.Test.Case.Cache.Engine
  39. */
  40. class MemcacheEngineTest extends CakeTestCase {
  41. /**
  42. * setUp method
  43. *
  44. * @return void
  45. */
  46. public function setUp() {
  47. $this->skipIf(!class_exists('Memcache'), 'Memcache is not installed or configured properly.');
  48. $this->_cacheDisable = Configure::read('Cache.disable');
  49. Configure::write('Cache.disable', false);
  50. Cache::config('memcache', array(
  51. 'engine' => 'Memcache',
  52. 'prefix' => 'cake_',
  53. 'duration' => 3600
  54. ));
  55. }
  56. /**
  57. * tearDown method
  58. *
  59. * @return void
  60. */
  61. public function tearDown() {
  62. Configure::write('Cache.disable', $this->_cacheDisable);
  63. Cache::drop('memcache');
  64. Cache::config('default');
  65. }
  66. /**
  67. * testSettings method
  68. *
  69. * @return void
  70. */
  71. public function testSettings() {
  72. $settings = Cache::settings('memcache');
  73. unset($settings['serialize'], $settings['path']);
  74. $expecting = array(
  75. 'prefix' => 'cake_',
  76. 'duration'=> 3600,
  77. 'probability' => 100,
  78. 'servers' => array('127.0.0.1'),
  79. 'persistent' => true,
  80. 'compress' => false,
  81. 'engine' => 'Memcache',
  82. 'persistent' => true,
  83. );
  84. $this->assertEquals($settings, $expecting);
  85. }
  86. /**
  87. * testSettings method
  88. *
  89. * @return void
  90. */
  91. public function testMultipleServers() {
  92. $servers = array('127.0.0.1:11211', '127.0.0.1:11222');
  93. $available = true;
  94. $Memcache = new Memcache();
  95. foreach ($servers as $server) {
  96. list($host, $port) = explode(':', $server);
  97. if (!@$Memcache->connect($host, $port)) {
  98. $available = false;
  99. }
  100. }
  101. $this->skipIf(!$available, 'Need memcache servers at ' . implode(', ', $servers) . ' to run this test.');
  102. $Memcache = new MemcacheEngine();
  103. $Memcache->init(array('engine' => 'Memcache', 'servers' => $servers));
  104. $servers = array_keys($Memcache->__Memcache->getExtendedStats());
  105. $settings = $Memcache->settings();
  106. $this->assertEquals($servers, $settings['servers']);
  107. Cache::drop('dual_server');
  108. }
  109. /**
  110. * testConnect method
  111. *
  112. * @return void
  113. */
  114. public function testConnect() {
  115. $Memcache = new MemcacheEngine();
  116. $Memcache->init(Cache::settings('memcache'));
  117. $result = $Memcache->connect('127.0.0.1');
  118. $this->assertTrue($result);
  119. }
  120. /**
  121. * test connecting to an ipv6 server.
  122. *
  123. * @return void
  124. */
  125. public function testConnectIpv6() {
  126. $Memcache = new MemcacheEngine();
  127. $result = $Memcache->init(array(
  128. 'prefix' => 'cake_',
  129. 'duration' => 200,
  130. 'engine' => 'Memcache',
  131. 'servers' => array(
  132. '[::1]:11211'
  133. )
  134. ));
  135. $this->assertTrue($result);
  136. }
  137. /**
  138. * test non latin domains.
  139. *
  140. * @return void
  141. */
  142. public function testParseServerStringNonLatin() {
  143. $Memcache = new TestMemcacheEngine();
  144. $result = $Memcache->parseServerString('schülervz.net:13211');
  145. $this->assertEquals($result, array('schülervz.net', '13211'));
  146. $result = $Memcache->parseServerString('sülül:1111');
  147. $this->assertEquals($result, array('sülül', '1111'));
  148. }
  149. /**
  150. * test unix sockets.
  151. *
  152. * @return void
  153. */
  154. function testParseServerStringUnix() {
  155. $Memcache =& new TestMemcacheEngine();
  156. $result = $Memcache->parseServerString('unix:///path/to/memcached.sock');
  157. $this->assertEquals($result, array('unix:///path/to/memcached.sock', 0));
  158. }
  159. /**
  160. * testReadAndWriteCache method
  161. *
  162. * @return void
  163. */
  164. public function testReadAndWriteCache() {
  165. Cache::set(array('duration' => 1), null, 'memcache');
  166. $result = Cache::read('test', 'memcache');
  167. $expecting = '';
  168. $this->assertEquals($result, $expecting);
  169. $data = 'this is a test of the emergency broadcasting system';
  170. $result = Cache::write('test', $data, 'memcache');
  171. $this->assertTrue($result);
  172. $result = Cache::read('test', 'memcache');
  173. $expecting = $data;
  174. $this->assertEquals($result, $expecting);
  175. Cache::delete('test', 'memcache');
  176. }
  177. /**
  178. * testExpiry method
  179. *
  180. * @return void
  181. */
  182. public function testExpiry() {
  183. Cache::set(array('duration' => 1), 'memcache');
  184. $result = Cache::read('test', 'memcache');
  185. $this->assertFalse($result);
  186. $data = 'this is a test of the emergency broadcasting system';
  187. $result = Cache::write('other_test', $data, 'memcache');
  188. $this->assertTrue($result);
  189. sleep(2);
  190. $result = Cache::read('other_test', 'memcache');
  191. $this->assertFalse($result);
  192. Cache::set(array('duration' => "+1 second"), 'memcache');
  193. $data = 'this is a test of the emergency broadcasting system';
  194. $result = Cache::write('other_test', $data, 'memcache');
  195. $this->assertTrue($result);
  196. sleep(2);
  197. $result = Cache::read('other_test', 'memcache');
  198. $this->assertFalse($result);
  199. Cache::config('memcache', array('duration' => '+1 second'));
  200. sleep(2);
  201. $result = Cache::read('other_test', 'memcache');
  202. $this->assertFalse($result);
  203. Cache::config('memcache', array('duration' => '+29 days'));
  204. $data = 'this is a test of the emergency broadcasting system';
  205. $result = Cache::write('long_expiry_test', $data, 'memcache');
  206. $this->assertTrue($result);
  207. sleep(2);
  208. $result = Cache::read('long_expiry_test', 'memcache');
  209. $expecting = $data;
  210. $this->assertEquals($result, $expecting);
  211. Cache::config('memcache', array('duration' => 3600));
  212. }
  213. /**
  214. * testDeleteCache method
  215. *
  216. * @return void
  217. */
  218. public function testDeleteCache() {
  219. $data = 'this is a test of the emergency broadcasting system';
  220. $result = Cache::write('delete_test', $data, 'memcache');
  221. $this->assertTrue($result);
  222. $result = Cache::delete('delete_test', 'memcache');
  223. $this->assertTrue($result);
  224. }
  225. /**
  226. * testDecrement method
  227. *
  228. * @return void
  229. */
  230. public function testDecrement() {
  231. $result = Cache::write('test_decrement', 5, 'memcache');
  232. $this->assertTrue($result);
  233. $result = Cache::decrement('test_decrement', 1, 'memcache');
  234. $this->assertEquals(4, $result);
  235. $result = Cache::read('test_decrement', 'memcache');
  236. $this->assertEquals(4, $result);
  237. $result = Cache::decrement('test_decrement', 2, 'memcache');
  238. $this->assertEquals(2, $result);
  239. $result = Cache::read('test_decrement', 'memcache');
  240. $this->assertEquals(2, $result);
  241. }
  242. /**
  243. * testIncrement method
  244. *
  245. * @return void
  246. */
  247. public function testIncrement() {
  248. $result = Cache::write('test_increment', 5, 'memcache');
  249. $this->assertTrue($result);
  250. $result = Cache::increment('test_increment', 1, 'memcache');
  251. $this->assertEquals(6, $result);
  252. $result = Cache::read('test_increment', 'memcache');
  253. $this->assertEquals(6, $result);
  254. $result = Cache::increment('test_increment', 2, 'memcache');
  255. $this->assertEquals(8, $result);
  256. $result = Cache::read('test_increment', 'memcache');
  257. $this->assertEquals(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. public 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->assertEquals(Cache::read('duration_test', 'long_memcache'), 'yay', 'Value was not read %s');
  279. $this->assertEquals(Cache::read('short_duration_test', 'short_memcache'), 'boo', 'Value was not read %s');
  280. sleep(1);
  281. $this->assertEquals(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. public function testClear() {
  294. Cache::config('memcache2', array(
  295. 'engine' => 'Memcache',
  296. 'prefix' => 'cake2_',
  297. 'duration' => 3600
  298. ));
  299. Cache::write('some_value', 'cache1', 'memcache');
  300. $result = Cache::clear(true, 'memcache');
  301. $this->assertTrue($result);
  302. $this->assertEquals('cache1', Cache::read('some_value', 'memcache'));
  303. Cache::write('some_value', 'cache2', 'memcache2');
  304. $result = Cache::clear(false, 'memcache');
  305. $this->assertTrue($result);
  306. $this->assertFalse(Cache::read('some_value', 'memcache'));
  307. $this->assertEquals('cache2', Cache::read('some_value', 'memcache2'));
  308. Cache::clear(false, 'memcache2');
  309. }
  310. /**
  311. * test that a 0 duration can succesfully write.
  312. *
  313. * @return void
  314. */
  315. public function testZeroDuration() {
  316. Cache::config('memcache', array('duration' => 0));
  317. $result = Cache::write('test_key', 'written!', 'memcache');
  318. $this->assertTrue($result, 'Could not write with duration 0');
  319. $result = Cache::read('test_key', 'memcache');
  320. $this->assertEquals($result, 'written!');
  321. }
  322. /**
  323. * test that durations greater than 30 days never expire
  324. *
  325. * @return void
  326. */
  327. public function testLongDurationEqualToZero() {
  328. $memcache =& new TestMemcacheEngine();
  329. $memcache->settings['compress'] = false;
  330. $mock = $this->getMock('Memcache');
  331. $memcache->setMemcache($mock);
  332. $mock->expects($this->once())
  333. ->method('set')
  334. ->with('key', 'value', false, 0);
  335. $value = 'value';
  336. $memcache->write('key', $value, 50 * DAY);
  337. }
  338. }