2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* MemcacheEngineTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs.cache
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5434
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-03-06 04:56:31 +00:00
|
|
|
|
|
|
|
App::uses('Cache', 'Cache');
|
|
|
|
App::uses('MemcacheEngine', 'Cache/Engine');
|
2010-09-26 16:04:06 +00:00
|
|
|
|
|
|
|
class TestMemcacheEngine extends MemcacheEngine {
|
|
|
|
/**
|
|
|
|
* public accessor to _parseServerString
|
|
|
|
*
|
|
|
|
* @param string $server
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function parseServerString($server) {
|
|
|
|
return $this->_parseServerString($server);
|
|
|
|
}
|
2011-01-14 01:27:10 +00:00
|
|
|
|
2011-01-19 01:20:49 +00:00
|
|
|
function setMemcache($memcache) {
|
|
|
|
$this->_Memcache = $memcache;
|
2011-01-14 01:27:10 +00:00
|
|
|
}
|
2010-09-26 16:04:06 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* MemcacheEngineTest class
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs.cache
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-11-19 21:19:23 +00:00
|
|
|
class MemcacheEngineTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* setUp method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function setUp() {
|
2010-09-20 01:41:25 +00:00
|
|
|
$this->skipIf(!class_exists('Memcache'), '%s Apc is not installed or configured properly');
|
2009-03-21 23:55:39 +00:00
|
|
|
$this->_cacheDisable = Configure::read('Cache.disable');
|
|
|
|
Configure::write('Cache.disable', false);
|
2010-04-17 22:47:45 +00:00
|
|
|
Cache::config('memcache', array(
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'prefix' => 'cake_',
|
|
|
|
'duration' => 3600
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-19 21:19:23 +00:00
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function tearDown() {
|
2009-03-21 23:55:39 +00:00
|
|
|
Configure::write('Cache.disable', $this->_cacheDisable);
|
2009-11-19 00:47:40 +00:00
|
|
|
Cache::drop('memcache');
|
2008-11-19 21:19:23 +00:00
|
|
|
Cache::config('default');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testSettings method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testSettings() {
|
2010-09-18 16:32:43 +00:00
|
|
|
$settings = Cache::settings('memcache');
|
2009-11-14 23:50:23 +00:00
|
|
|
unset($settings['serialize'], $settings['path']);
|
|
|
|
$expecting = array(
|
|
|
|
'prefix' => 'cake_',
|
|
|
|
'duration'=> 3600,
|
|
|
|
'probability' => 100,
|
|
|
|
'servers' => array('127.0.0.1'),
|
|
|
|
'compress' => false,
|
|
|
|
'engine' => 'Memcache'
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertEqual($settings, $expecting);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-19 21:19:23 +00:00
|
|
|
/**
|
|
|
|
* testSettings method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testMultipleServers() {
|
|
|
|
$servers = array('127.0.0.1:11211', '127.0.0.1:11222');
|
|
|
|
$available = true;
|
2010-11-13 04:05:44 +00:00
|
|
|
$Memcache = new Memcache();
|
2009-11-19 00:47:40 +00:00
|
|
|
|
2008-11-19 21:19:23 +00:00
|
|
|
foreach($servers as $server) {
|
|
|
|
list($host, $port) = explode(':', $server);
|
2010-01-21 18:57:42 +00:00
|
|
|
if (!@$Memcache->connect($host, $port)) {
|
2008-11-19 21:19:23 +00:00
|
|
|
$available = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-21 23:55:39 +00:00
|
|
|
if ($this->skipIf(!$available, '%s Need memcache servers at ' . implode(', ', $servers) . ' to run this test')) {
|
2008-11-19 21:19:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-11-13 04:05:44 +00:00
|
|
|
$Memcache = new MemcacheEngine();
|
2009-11-19 00:47:40 +00:00
|
|
|
$Memcache->init(array('engine' => 'Memcache', 'servers' => $servers));
|
2008-11-19 21:19:23 +00:00
|
|
|
|
2009-11-19 00:47:40 +00:00
|
|
|
$servers = array_keys($Memcache->__Memcache->getExtendedStats());
|
|
|
|
$settings = $Memcache->settings();
|
2008-11-19 21:19:23 +00:00
|
|
|
$this->assertEqual($servers, $settings['servers']);
|
2009-11-19 00:47:40 +00:00
|
|
|
Cache::drop('dual_server');
|
2008-11-19 21:19:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testConnect method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testConnect() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$Memcache = new MemcacheEngine();
|
2009-11-19 00:47:40 +00:00
|
|
|
$Memcache->init(Cache::settings('memcache'));
|
|
|
|
$result = $Memcache->connect('127.0.0.1');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-26 05:11:19 +00:00
|
|
|
/**
|
|
|
|
* test connecting to an ipv6 server.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testConnectIpv6() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$Memcache = new MemcacheEngine();
|
2010-09-26 05:11:19 +00:00
|
|
|
$result = $Memcache->init(array(
|
|
|
|
'prefix' => 'cake_',
|
|
|
|
'duration' => 200,
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'servers' => array(
|
|
|
|
'[::1]:11211'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2010-09-26 16:04:06 +00:00
|
|
|
/**
|
|
|
|
* test non latin domains.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testParseServerStringNonLatin() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$Memcache = new TestMemcacheEngine();
|
2010-09-26 16:04:06 +00:00
|
|
|
$result = $Memcache->parseServerString('schülervz.net:13211');
|
|
|
|
$this->assertEqual($result, array('schülervz.net', '13211'));
|
|
|
|
|
|
|
|
$result = $Memcache->parseServerString('sülül:1111');
|
|
|
|
$this->assertEqual($result, array('sülül', '1111'));
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testReadAndWriteCache method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testReadAndWriteCache() {
|
2010-09-18 16:32:43 +00:00
|
|
|
Cache::set(array('duration' => 1), null, 'memcache');
|
2009-02-06 22:56:06 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$expecting = '';
|
|
|
|
$this->assertEqual($result, $expecting);
|
|
|
|
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('test', $data, 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$expecting = $data;
|
|
|
|
$this->assertEqual($result, $expecting);
|
2009-02-06 22:56:06 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
Cache::delete('test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testExpiry method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testExpiry() {
|
2010-09-18 16:32:43 +00:00
|
|
|
Cache::set(array('duration' => 1), 'memcache');
|
2009-02-06 22:56:06 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('other_test', $data, 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
sleep(2);
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('other_test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
Cache::set(array('duration' => "+1 second"), 'memcache');
|
2008-07-26 20:00:20 +00:00
|
|
|
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('other_test', $data, 'memcache');
|
2008-07-26 20:00:20 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2009-02-06 22:56:06 +00:00
|
|
|
sleep(2);
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('other_test', 'memcache');
|
2009-02-06 22:56:06 +00:00
|
|
|
$this->assertFalse($result);
|
2008-07-26 20:00:20 +00:00
|
|
|
|
2009-11-19 00:47:40 +00:00
|
|
|
Cache::config('memcache', array('duration' => '+1 second'));
|
2008-07-26 20:00:20 +00:00
|
|
|
sleep(2);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('other_test', 'memcache');
|
2008-07-26 20:00:20 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2010-10-24 04:05:27 +00:00
|
|
|
Cache::config('memcache', array('duration' => '+29 days'));
|
2009-02-11 23:00:19 +00:00
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('long_expiry_test', $data, 'memcache');
|
2009-02-11 23:00:19 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
sleep(2);
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('long_expiry_test', 'memcache');
|
2009-02-11 23:00:19 +00:00
|
|
|
$expecting = $data;
|
|
|
|
$this->assertEqual($result, $expecting);
|
|
|
|
|
2009-11-19 00:47:40 +00:00
|
|
|
Cache::config('memcache', array('duration' => 3600));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testDeleteCache method
|
2008-07-26 20:00:20 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testDeleteCache() {
|
|
|
|
$data = 'this is a test of the emergency broadcasting system';
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('delete_test', $data, 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::delete('delete_test', 'memcache');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2010-01-21 16:41:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* testDecrement method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-01-21 18:57:42 +00:00
|
|
|
function testDecrement() {
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('test_decrement', 5, 'memcache');
|
2010-01-21 16:41:44 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::decrement('test_decrement', 1, 'memcache');
|
2010-01-21 16:41:44 +00:00
|
|
|
$this->assertEqual(4, $result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test_decrement', 'memcache');
|
2010-01-21 16:41:44 +00:00
|
|
|
$this->assertEqual(4, $result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::decrement('test_decrement', 2, 'memcache');
|
2010-01-21 16:41:44 +00:00
|
|
|
$this->assertEqual(2, $result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test_decrement', 'memcache');
|
2010-01-21 16:41:44 +00:00
|
|
|
$this->assertEqual(2, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testIncrement method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-01-21 18:57:42 +00:00
|
|
|
function testIncrement() {
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::write('test_increment', 5, 'memcache');
|
2010-01-21 16:41:44 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::increment('test_increment', 1, 'memcache');
|
2010-01-21 18:57:42 +00:00
|
|
|
$this->assertEqual(6, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test_increment', 'memcache');
|
2010-01-21 18:57:42 +00:00
|
|
|
$this->assertEqual(6, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::increment('test_increment', 2, 'memcache');
|
2010-01-21 18:57:42 +00:00
|
|
|
$this->assertEqual(8, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
|
2010-09-18 16:32:43 +00:00
|
|
|
$result = Cache::read('test_increment', 'memcache');
|
2010-01-21 18:57:42 +00:00
|
|
|
$this->assertEqual(8, $result);
|
2010-01-21 16:41:44 +00:00
|
|
|
}
|
2010-04-17 22:47:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that configurations don't conflict, when a file engine is declared after a memcache one.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testConfigurationConflict() {
|
|
|
|
Cache::config('long_memcache', array(
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'duration'=> '+2 seconds',
|
|
|
|
'servers' => array('127.0.0.1:11211'),
|
|
|
|
));
|
|
|
|
Cache::config('short_memcache', array(
|
|
|
|
'engine' => 'Memcache',
|
|
|
|
'duration'=> '+1 seconds',
|
|
|
|
'servers' => array('127.0.0.1:11211'),
|
|
|
|
));
|
|
|
|
Cache::config('some_file', array('engine' => 'File'));
|
|
|
|
|
|
|
|
$this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcache'));
|
|
|
|
$this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcache'));
|
|
|
|
|
|
|
|
$this->assertEqual(Cache::read('duration_test', 'long_memcache'), 'yay', 'Value was not read %s');
|
|
|
|
$this->assertEqual(Cache::read('short_duration_test', 'short_memcache'), 'boo', 'Value was not read %s');
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
$this->assertEqual(Cache::read('duration_test', 'long_memcache'), 'yay', 'Value was not read %s');
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
$this->assertFalse(Cache::read('short_duration_test', 'short_memcache'), 'Cache was not invalidated %s');
|
|
|
|
$this->assertFalse(Cache::read('duration_test', 'long_memcache'), 'Value did not expire %s');
|
|
|
|
|
|
|
|
Cache::delete('duration_test', 'long_memcache');
|
|
|
|
Cache::delete('short_duration_test', 'short_memcache');
|
|
|
|
}
|
|
|
|
|
2010-09-18 17:15:37 +00:00
|
|
|
/**
|
|
|
|
* test clearing memcache.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testClear() {
|
|
|
|
Cache::write('some_value', 'value', 'memcache');
|
|
|
|
|
|
|
|
$result = Cache::clear(false, 'memcache');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertFalse(Cache::read('some_value', 'memcache'));
|
|
|
|
}
|
2010-09-19 16:20:07 +00:00
|
|
|
/**
|
|
|
|
* test that a 0 duration can succesfully write.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testZeroDuration() {
|
|
|
|
Cache::config('memcache', array('duration' => 0));
|
|
|
|
$result = Cache::write('test_key', 'written!', 'memcache');
|
|
|
|
|
|
|
|
$this->assertTrue($result, 'Could not write with duration 0');
|
|
|
|
$result = Cache::read('test_key', 'memcache');
|
|
|
|
$this->assertEqual($result, 'written!');
|
2011-01-14 01:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that durations greater than 30 days never expire
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testLongDurationEqualToZero() {
|
|
|
|
$memcache =& new TestMemcacheEngine();
|
|
|
|
$memcache->settings['compress'] = false;
|
|
|
|
|
2011-01-19 01:20:49 +00:00
|
|
|
$mock = $this->getMock('Memcache');
|
2011-01-14 01:27:10 +00:00
|
|
|
$memcache->setMemcache($mock);
|
2011-01-19 01:20:49 +00:00
|
|
|
$mock->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with('key', 'value', false, 0);
|
2011-01-14 01:27:10 +00:00
|
|
|
|
|
|
|
$value = 'value';
|
|
|
|
$memcache->write('key', $value, 50 * DAY);
|
2010-09-19 16:20:07 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|