objectcache: add setMockTime() method to BagOStuff/WANObjectCache
[lhc/web/wiklou.git] / includes / libs / objectcache / BagOStuff.php
index f834ccf..0ea57a1 100644 (file)
@@ -70,6 +70,9 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
        /** @var callable[] */
        protected $busyCallbacks = [];
 
+       /** @var float|null */
+       private $wallClockOverride;
+
        /** @var int[] Map of (ATTR_* class constant => QOS_* class constant) */
        protected $attrMap = [];
 
@@ -473,11 +476,11 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
                        return null;
                }
 
-               $lSince = microtime( true ); // lock timestamp
+               $lSince = $this->getCurrentTime(); // lock timestamp
 
                return new ScopedCallback( function () use ( $key, $lSince, $expiry ) {
-                       $latency = .050; // latency skew (err towards keeping lock present)
-                       $age = ( microtime( true ) - $lSince + $latency );
+                       $latency = 0.050; // latency skew (err towards keeping lock present)
+                       $age = ( $this->getCurrentTime() - $lSince + $latency );
                        if ( ( $age + $latency ) >= $expiry ) {
                                $this->logger->warning( "Lock for $key held too long ($age sec)." );
                                return; // expired; it's not "safe" to delete the key
@@ -691,7 +694,7 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
         */
        protected function convertExpiry( $exptime ) {
                if ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ) {
-                       return time() + $exptime;
+                       return (int)$this->getCurrentTime() + $exptime;
                } else {
                        return $exptime;
                }
@@ -706,7 +709,7 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
         */
        protected function convertToRelative( $exptime ) {
                if ( $exptime >= ( 10 * self::TTL_YEAR ) ) {
-                       $exptime -= time();
+                       $exptime -= (int)$this->getCurrentTime();
                        if ( $exptime <= 0 ) {
                                $exptime = 1;
                        }
@@ -732,7 +735,7 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
         * @since 1.27
         * @param string $keyspace
         * @param array $args
-        * @return string
+        * @return string Colon-delimited list of $keyspace followed by escaped components of $args
         */
        public function makeKeyInternal( $keyspace, $args ) {
                $key = $keyspace;
@@ -747,10 +750,11 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
         * Make a global cache key.
         *
         * @since 1.27
-        * @param string $keys,... Key component
-        * @return string
+        * @param string $class Key class
+        * @param string $component [optional] Key component (starting with a key collection name)
+        * @return string Colon-delimited list of $keyspace followed by escaped components of $args
         */
-       public function makeGlobalKey() {
+       public function makeGlobalKey( $class, $component = null ) {
                return $this->makeKeyInternal( 'global', func_get_args() );
        }
 
@@ -758,10 +762,11 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
         * Make a cache key, scoped to this instance's keyspace.
         *
         * @since 1.27
-        * @param string $keys,... Key component
-        * @return string
+        * @param string $class Key class
+        * @param string $component [optional] Key component (starting with a key collection name)
+        * @return string Colon-delimited list of $keyspace followed by escaped components of $args
         */
-       public function makeKey() {
+       public function makeKey( $class, $component = null ) {
                return $this->makeKeyInternal( $this->keyspace, func_get_args() );
        }
 
@@ -794,4 +799,20 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
 
                return $map;
        }
+
+       /**
+        * @return float UNIX timestamp
+        * @codeCoverageIgnore
+        */
+       protected function getCurrentTime() {
+               return $this->wallClockOverride ?: microtime( true );
+       }
+
+       /**
+        * @param float|null &$time Mock UNIX timestamp for testing
+        * @codeCoverageIgnore
+        */
+       public function setMockTime( &$time ) {
+               $this->wallClockOverride =& $time;
+       }
 }