From 2ecc54e061b4cc0b35716bb4fea35c45cb63b371 Mon Sep 17 00:00:00 2001 From: Paulo Magalhaes Date: Sun, 23 Jan 2022 22:29:32 +0000 Subject: [PATCH] Fix APC cache tests - Using negative TTLs to force immediate expiration of keys, while convenient in tests, doesn't work consistently with APC and is an undocumented feature. Using a low TTL and sleep() is what garantees that it works for APC. See https://github.com/krakjoe/apcu/issues/184 - The setting apc.use_request_time interferes with key expiration when running on CLI. Making sure it always has the sensible value for running the tests. See https://github.com/krakjoe/apcu/pull/392 --- test/unit/cache/sfAPCCacheTest.php | 4 ++++ test/unit/cache/sfCacheDriverTests.class.php | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/test/unit/cache/sfAPCCacheTest.php b/test/unit/cache/sfAPCCacheTest.php index dcfd902c3..203cb959b 100644 --- a/test/unit/cache/sfAPCCacheTest.php +++ b/test/unit/cache/sfAPCCacheTest.php @@ -37,4 +37,8 @@ $cache = new sfAPCCache(); $cache->initialize(); +// make sure expired keys are dropped +// see https://github.com/krakjoe/apcu/issues/391 +ini_set('apc.use_request_time', 0); + sfCacheDriverTests::launch($t, $cache); diff --git a/test/unit/cache/sfCacheDriverTests.class.php b/test/unit/cache/sfCacheDriverTests.class.php index 13e502f1c..60cb3e9ac 100644 --- a/test/unit/cache/sfCacheDriverTests.class.php +++ b/test/unit/cache/sfCacheDriverTests.class.php @@ -18,7 +18,8 @@ public static function launch($t, $cache) $t->is($cache->get('test'), $data, '->get() retrieves data form the cache'); $t->is($cache->has('test'), true, '->has() returns true if the cache exists'); - $t->ok($cache->set('test', $data, -10), '->set() takes a lifetime as its third argument'); + $t->ok($cache->set('test', $data, 1), '->set() takes a lifetime as its third argument'); + sleep(2); $t->is($cache->get('test', 'default'), 'default', '->get() returns the default value if cache has expired'); $t->is($cache->has('test'), false, '->has() returns true if the cache exists'); @@ -47,21 +48,24 @@ public static function launch($t, $cache) // ->clean() $t->diag('->clean()'); $data = 'some random data to store in the cache system...'; - $cache->set('foo', $data, -10); + $cache->set('foo', $data, 1); $cache->set('bar', $data, 86400); + sleep(2); $cache->clean(sfCache::OLD); $t->is($cache->has('foo'), false, '->clean() cleans old cache key if given the sfCache::OLD argument'); $t->is($cache->has('bar'), true, '->clean() cleans old cache key if given the sfCache::OLD argument'); - $cache->set('foo', $data, -10); + $cache->set('foo', $data, -1); + sleep(2); $cache->set('bar', $data, 86400); $cache->clean(sfCache::ALL); $t->is($cache->has('foo'), false, '->clean() cleans all cache key if given the sfCache::ALL argument'); $t->is($cache->has('bar'), false, '->clean() cleans all cache key if given the sfCache::ALL argument'); - $cache->set('foo', $data, -10); + $cache->set('foo', $data, 1); + sleep(2); $cache->set('bar', $data, 86400); $cache->clean(); @@ -126,7 +130,8 @@ public static function launch($t, $cache) $t->ok($delta >= $lifetime - 1 && $delta <= $lifetime, '->getTimeout() returns the timeout time for a given cache key'); } - $cache->set('bar', 'foo', -10); + $cache->set('bar', 'foo', 1); + sleep(2); $t->is($cache->getTimeout('bar'), 0, '->getTimeout() returns the timeout time for a given cache key'); foreach (array(86400, 10) as $lifetime) { @@ -148,7 +153,8 @@ public static function launch($t, $cache) $t->ok($lastModified >= time() - 1 && $lastModified <= time(), '->getLastModified() returns the last modified time for a given cache key'); } - $cache->set('bar', 'foo', -10); + $cache->set('bar', 'foo', 1); + sleep(2); $t->is($cache->getLastModified('bar'), 0, '->getLastModified() returns the last modified time for a given cache key'); foreach (array(86400, 10) as $lifetime) {