Add process cache support to WANObjectCache
[lhc/web/wiklou.git] / tests / phpunit / includes / objectcache / WANObjectCacheTest.php
index 9b4eca7..8981f2f 100644 (file)
@@ -13,11 +13,14 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                        $this->cache = ObjectCache::getWANInstance( $name );
                } else {
                        $this->cache = new WANObjectCache( array(
-                               'cache'   => new HashBagOStuff(),
-                               'pool'    => 'testcache-hash',
+                               'cache' => new HashBagOStuff(),
+                               'pool' => 'testcache-hash',
                                'relayer' => new EventRelayerNull( array() )
                        ) );
                }
+
+               $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
+               $this->internalCache = $wanCache->cache;
        }
 
        /**
@@ -74,6 +77,14 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                }
        }
 
+       public function testStaleSet() {
+               $key = wfRandomString();
+               $value = wfRandomString();
+               $this->cache->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
+
+               $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
+       }
+
        /**
         * @covers WANObjectCache::getWithSetCallback()
         */
@@ -94,24 +105,27 @@ class WANObjectCacheTest extends MediaWikiTestCase {
 
                $wasSet = 0;
                $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
-               $this->assertEquals( $v, $value );
+               $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value regenerated" );
 
                $curTTL = null;
-               $v = $cache->get( $key, $curTTL );
+               $cache->get( $key, $curTTL );
                $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
                $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
 
                $wasSet = 0;
-               $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
-               $this->assertEquals( $v, $value );
+               $v = $cache->getWithSetCallback( $key, $func, 30, array(), array(
+                       'lowTTL' => 0,
+                       'lockTSE' => 5,
+               ) );
+               $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 0, $wasSet, "Value not regenerated" );
 
                $priorTime = microtime( true );
                usleep( 1 );
                $wasSet = 0;
                $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
-               $this->assertEquals( $v, $value );
+               $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
                $t1 = $cache->getCheckKeyTime( $cKey1 );
                $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
@@ -121,7 +135,7 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $priorTime = microtime( true );
                $wasSet = 0;
                $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
-               $this->assertEquals( $v, $value );
+               $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
                $t1 = $cache->getCheckKeyTime( $cKey1 );
                $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
@@ -130,8 +144,43 @@ class WANObjectCacheTest extends MediaWikiTestCase {
 
                $curTTL = null;
                $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
-               $this->assertEquals( $v, $value );
+               $this->assertEquals( $value, $v, "Value returned" );
                $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
+
+               $wasSet = 0;
+               $key = wfRandomString();
+               $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'pcTTL' => 5 ) );
+               $this->assertEquals( $value, $v, "Value returned" );
+               $cache->delete( $key );
+               $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'pcTTL' => 5 ) );
+               $this->assertEquals( $value, $v, "Value still returned after deleted" );
+               $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
+       }
+
+       /**
+        * @covers WANObjectCache::getWithSetCallback()
+        */
+       public function testLockTSE() {
+               $cache = $this->cache;
+               $key = wfRandomString();
+               $value = wfRandomString();
+
+               $calls = 0;
+               $func = function() use ( &$calls, $value ) {
+                       ++$calls;
+                       return $value;
+               };
+
+               $cache->delete( $key );
+               $ret = $cache->getWithSetCallback( $key, 30, $func, array(), array( 'lockTSE' => 5 ) );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 1, $calls, 'Value was populated' );
+
+               // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
+               $this->internalCache->lock( $key, 0 );
+               $ret = $cache->getWithSetCallback( $key, 30, $func, array(), array( 'lockTSE' => 5 ) );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 1, $calls, 'Callback was not used' );
        }
 
        /**
@@ -231,12 +280,12 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $key = wfRandomString();
 
                $priorTime = microtime( true );
-               usleep( 1 );
+               usleep( 100 );
                $t0 = $this->cache->getCheckKeyTime( $key );
                $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
 
                $priorTime = microtime( true );
-               usleep( 1 );
+               usleep( 100 );
                $this->cache->touchCheckKey( $key );
                $t1 = $this->cache->getCheckKeyTime( $key );
                $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
@@ -244,7 +293,7 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $t2 = $this->cache->getCheckKeyTime( $key );
                $this->assertEquals( $t1, $t2, 'Check key time did not change' );
 
-               usleep( 1 );
+               usleep( 100 );
                $this->cache->touchCheckKey( $key );
                $t3 = $this->cache->getCheckKeyTime( $key );
                $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
@@ -252,7 +301,7 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $t4 = $this->cache->getCheckKeyTime( $key );
                $this->assertEquals( $t3, $t4, 'Check key time did not change' );
 
-               usleep( 1 );
+               usleep( 100 );
                $this->cache->resetCheckKey( $key );
                $t5 = $this->cache->getCheckKeyTime( $key );
                $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );