From 20b015e100926f06e8a6a3802116c64f082e33e1 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 10 Apr 2013 20:35:22 -0400 Subject: [PATCH] CRM-12321 - SqlGroupTest - Test prefetching more directly. When multi-tier caching was introduced, testPrefetch regressed because it relied on side-effects from having totally independent caches -- but the multi-tier cache added extra, unforeseen sharing between SqlGroup instances. In reality, prefetching works. This revision tests prefetch more directly to workaround the changed assumptions. ---------------------------------------- * CRM-12321: Multi-tier caching causes multiple test regressions http://issues.civicrm.org/jira/browse/CRM-12321 --- CRM/Utils/Cache/SqlGroup.php | 4 +++ .../phpunit/CRM/Utils/Cache/SqlGroupTest.php | 26 +++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/CRM/Utils/Cache/SqlGroup.php b/CRM/Utils/Cache/SqlGroup.php index 5a73fbfda2..c8c083c543 100644 --- a/CRM/Utils/Cache/SqlGroup.php +++ b/CRM/Utils/Cache/SqlGroup.php @@ -96,6 +96,10 @@ class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface { return $this->frontCache[$key]; } + function getFromFrontCache($key, $default = NULL) { + return CRM_Utils_Array::value($key, $this->frontCache, $default); + } + function delete($key) { CRM_Core_BAO_Cache::deleteGroup($this->group, $key); unset($this->frontCache[$key]); diff --git a/tests/phpunit/CRM/Utils/Cache/SqlGroupTest.php b/tests/phpunit/CRM/Utils/Cache/SqlGroupTest.php index 6962ef4fe5..4f07998e33 100644 --- a/tests/phpunit/CRM/Utils/Cache/SqlGroupTest.php +++ b/tests/phpunit/CRM/Utils/Cache/SqlGroupTest.php @@ -55,29 +55,33 @@ class CRM_Utils_Cache_SqlGroupTest extends CiviUnitTestCase { } /** - * Add item to one cache instance then read (and prefetch) with another + * Add item to one cache instance then read (with or without prefetch) from another */ function testPrefetch() { + // 1. put data in cache $a = new CRM_Utils_Cache_SqlGroup(array( 'group' => 'testPrefetch', + 'prefetch' => FALSE, )); $fooValue = array('whiz' => 'bang', 'bar' => 4); $a->set('foo', $fooValue); $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 4)); + // 2. see what happens when prefetch is TRUE $b = new CRM_Utils_Cache_SqlGroup(array( 'group' => 'testPrefetch', 'prefetch' => TRUE, )); - // assuming the values have been prefetched in $b, we can do a stale - // read -- i.e. change the underlying data table and then read the - // prefetched value from $b - $fooValue2 = 'muahahaha'; - $a->set('foo', $fooValue2); - $this->assertEquals($b->get('foo'), array('whiz' => 'bang', 'bar' => 4)); - - // ok, enough with the stale reading - $b->prefetch(); - $this->assertEquals($b->get('foo'), 'muahahaha'); + $this->assertEquals($fooValue, $b->getFromFrontCache('foo')); // should work b/c value was prefetched + $this->assertEquals($fooValue, $b->get('foo')); // should work b/c value was prefetched + + // 3. see what happens when prefetch is FALSE + $c = new CRM_Utils_Cache_SqlGroup(array( + 'group' => 'testPrefetch', + 'prefetch' => FALSE, + )); + $this->assertEquals(NULL, $c->getFromFrontCache('foo')); // should be NULL b/c value was NOT prefetched + $this->assertEquals($fooValue, $c->get('foo')); // should work b/c value is fetched on demand + $this->assertEquals($fooValue, $c->getFromFrontCache('foo')); // should work b/c value was fetched on demand } } -- 2.25.1