Make adaptiveTTL() less strict about $mtime type
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 7 Sep 2016 16:18:37 +0000 (09:18 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 7 Sep 2016 16:18:37 +0000 (09:18 -0700)
Callers using wfTimestamp( TS_UNIX, ... ) where getting $minTTL
due to the output being a string number.

Change-Id: I6b67a941940f40ef9a543f11d0dbccacafaaa53b

includes/libs/objectcache/WANObjectCache.php
tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php

index 8de68de..06b87e0 100644 (file)
@@ -1086,8 +1086,8 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface {
         * @since 1.28
         */
        public function adaptiveTTL( $mtime, $maxTTL, $minTTL = 30, $factor = .2 ) {
-               if ( is_float( $mtime ) ) {
-                       $mtime = (int)$mtime; // ignore fractional seconds
+               if ( is_float( $mtime ) || ctype_digit( $mtime ) ) {
+                       $mtime = (int)$mtime; // handle fractional seconds and string integers
                }
 
                if ( !is_int( $mtime ) || $mtime <= 0 ) {
index aeb4666..abef758 100644 (file)
@@ -734,6 +734,11 @@ class WANObjectCacheTest extends MediaWikiTestCase {
 
                $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
                $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
+
+               $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
+
+               $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
+               $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
        }
 
        public static function provideAdaptiveTTL() {