Skip to content

Commit

Permalink
Fix APC cache tests
Browse files Browse the repository at this point in the history
- 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 krakjoe/apcu#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 krakjoe/apcu#392
  • Loading branch information
mentalstring committed Dec 22, 2023
1 parent 2a68033 commit 2ecc54e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions test/unit/cache/sfAPCCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
18 changes: 12 additions & 6 deletions test/unit/cache/sfCacheDriverTests.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 2ecc54e

Please sign in to comment.