Index: source/test/intltest/unifiedcachetest.cpp |
diff --git a/source/test/intltest/unifiedcachetest.cpp b/source/test/intltest/unifiedcachetest.cpp |
index 042f1a09ffc581518076d1fdd4ae097e2dc2f6d9..d48b3e80c096668193ac3040caf08cef89e0da68 100644 |
--- a/source/test/intltest/unifiedcachetest.cpp |
+++ b/source/test/intltest/unifiedcachetest.cpp |
@@ -1,6 +1,6 @@ |
/* |
******************************************************************************* |
-* Copyright (C) 2014, International Business Machines Corporation and * |
+* Copyright (C) 2015, International Business Machines Corporation and * |
* others. All Rights Reserved. * |
******************************************************************************* |
* |
@@ -11,6 +11,7 @@ |
#include "cstring.h" |
#include "intltest.h" |
#include "unifiedcache.h" |
+#include "unicode/datefmt.h" |
class UCTItem : public SharedObject { |
public: |
@@ -30,14 +31,19 @@ U_NAMESPACE_BEGIN |
template<> U_EXPORT |
const UCTItem *LocaleCacheKey<UCTItem>::createObject( |
- const void * /*unused*/, UErrorCode &status) const { |
+ const void *context, UErrorCode &status) const { |
+ const UnifiedCache *cacheContext = (const UnifiedCache *) context; |
if (uprv_strcmp(fLoc.getName(), "zh") == 0) { |
status = U_MISSING_RESOURCE_ERROR; |
return NULL; |
} |
if (uprv_strcmp(fLoc.getLanguage(), fLoc.getName()) != 0) { |
const UCTItem *item = NULL; |
- UnifiedCache::getByLocale(fLoc.getLanguage(), item, status); |
+ if (cacheContext == NULL) { |
+ UnifiedCache::getByLocale(fLoc.getLanguage(), item, status); |
+ } else { |
+ cacheContext->get(LocaleCacheKey<UCTItem>(fLoc.getLanguage()), item, status); |
+ } |
if (U_FAILURE(status)) { |
return NULL; |
} |
@@ -63,19 +69,229 @@ public: |
} |
void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0); |
private: |
+ void TestEvictionPolicy(); |
+ void TestBounded(); |
void TestBasic(); |
void TestError(); |
void TestHashEquals(); |
+ void TestEvictionUnderStress(); |
}; |
void UnifiedCacheTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) { |
TESTCASE_AUTO_BEGIN; |
+ TESTCASE_AUTO(TestEvictionPolicy); |
+ TESTCASE_AUTO(TestBounded); |
TESTCASE_AUTO(TestBasic); |
TESTCASE_AUTO(TestError); |
TESTCASE_AUTO(TestHashEquals); |
+ TESTCASE_AUTO(TestEvictionUnderStress); |
TESTCASE_AUTO_END; |
} |
+void UnifiedCacheTest::TestEvictionUnderStress() { |
+#if !UCONFIG_NO_FORMATTING |
+ int32_t localeCount; |
+ const Locale *locales = DateFormat::getAvailableLocales(localeCount); |
+ UErrorCode status = U_ZERO_ERROR; |
+ const UnifiedCache *cache = UnifiedCache::getInstance(status); |
+ int64_t evictedCountBefore = cache->autoEvictedCount(); |
+ for (int32_t i = 0; i < localeCount; ++i) { |
+ LocalPointer<DateFormat> ptr(DateFormat::createInstanceForSkeleton("yMd", locales[i], status)); |
+ } |
+ int64_t evictedCountAfter = cache->autoEvictedCount(); |
+ if (evictedCountBefore == evictedCountAfter) { |
+ dataerrln("%s:%d Items should have been evicted from cache", |
+ __FILE__, __LINE__); |
+ } |
+#endif /* #if !UCONFIG_NO_FORMATTING */ |
+} |
+ |
+void UnifiedCacheTest::TestEvictionPolicy() { |
+ UErrorCode status = U_ZERO_ERROR; |
+ |
+ // We have to call this first or else calling the UnifiedCache |
+ // ctor will fail. This is by design to deter clients from using the |
+ // cache API incorrectly by creating their own cache instances. |
+ UnifiedCache::getInstance(status); |
+ |
+ // We create our own local UnifiedCache instance to ensure we have |
+ // complete control over it. Real clients should never ever create |
+ // their own cache! |
+ UnifiedCache cache(status); |
+ assertSuccess("", status); |
+ |
+ // Don't allow unused entries to exeed more than 100% of in use entries. |
+ cache.setEvictionPolicy(0, 100, status); |
+ |
+ static const char *locales[] = { |
+ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", |
+ "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"}; |
+ |
+ const UCTItem *usedReferences[] = {NULL, NULL, NULL, NULL, NULL}; |
+ const UCTItem *unusedReference = NULL; |
+ |
+ // Add 5 in-use entries |
+ for (int32_t i = 0; i < UPRV_LENGTHOF(usedReferences); i++) { |
+ cache.get( |
+ LocaleCacheKey<UCTItem>(locales[i]), |
+ &cache, |
+ usedReferences[i], |
+ status); |
+ } |
+ |
+ // Add 10 not in use entries. |
+ for (int32_t i = 0; i < 10; ++i) { |
+ cache.get( |
+ LocaleCacheKey<UCTItem>( |
+ locales[i + UPRV_LENGTHOF(usedReferences)]), |
+ &cache, |
+ unusedReference, |
+ status); |
+ } |
+ unusedReference->removeRef(); |
+ |
+ // unused count not to exeed in use count |
+ assertEquals("", UPRV_LENGTHOF(usedReferences), cache.unusedCount()); |
+ assertEquals("", 2*UPRV_LENGTHOF(usedReferences), cache.keyCount()); |
+ |
+ // Free up those used entries. |
+ for (int32_t i = 0; i < UPRV_LENGTHOF(usedReferences); i++) { |
+ usedReferences[i]->removeRef(); |
+ } |
+ |
+ // This should free up all cache items |
+ assertEquals("", 0, cache.keyCount()); |
+ |
+ assertSuccess("", status); |
+} |
+ |
+ |
+ |
+void UnifiedCacheTest::TestBounded() { |
+ UErrorCode status = U_ZERO_ERROR; |
+ |
+ // We have to call this first or else calling the UnifiedCache |
+ // ctor will fail. This is by design to deter clients from using the |
+ // cache API incorrectly by creating their own cache instances. |
+ UnifiedCache::getInstance(status); |
+ |
+ // We create our own local UnifiedCache instance to ensure we have |
+ // complete control over it. Real clients should never ever create |
+ // their own cache! |
+ UnifiedCache cache(status); |
+ assertSuccess("", status); |
+ |
+ // Maximum unused count is 3. |
+ cache.setEvictionPolicy(3, 0, status); |
+ |
+ // Our cache will hold up to 3 unused key-value pairs |
+ // We test the following invariants: |
+ // 1. unusedCount <= 3 |
+ // 2. cache->get(X) always returns the same reference as long as caller |
+ // already holds references to that same object. |
+ |
+ // We first add 5 key-value pairs with two distinct values, "en" and "fr" |
+ // keeping all those references. |
+ |
+ const UCTItem *en = NULL; |
+ const UCTItem *enGb = NULL; |
+ const UCTItem *enUs = NULL; |
+ const UCTItem *fr = NULL; |
+ const UCTItem *frFr = NULL; |
+ cache.get(LocaleCacheKey<UCTItem>("en_US"), &cache, enUs, status); |
+ cache.get(LocaleCacheKey<UCTItem>("en"), &cache, en, status); |
+ assertEquals("", 1, cache.unusedCount()); |
+ cache.get(LocaleCacheKey<UCTItem>("en_GB"), &cache, enGb, status); |
+ cache.get(LocaleCacheKey<UCTItem>("fr_FR"), &cache, frFr, status); |
+ cache.get(LocaleCacheKey<UCTItem>("fr"), &cache, fr, status); |
+ |
+ // Client holds two unique references, "en" and "fr" the other three |
+ // entries are eligible for eviction. |
+ assertEquals("", 3, cache.unusedCount()); |
+ assertEquals("", 5, cache.keyCount()); |
+ |
+ // Exercise cache more but don't hold the references except for |
+ // the last one. At the end of this, we will hold references to one |
+ // additional distinct value, so we will have references to 3 distinct |
+ // values. |
+ const UCTItem *throwAway = NULL; |
+ cache.get(LocaleCacheKey<UCTItem>("zn_AA"), &cache, throwAway, status); |
+ cache.get(LocaleCacheKey<UCTItem>("sr_AA"), &cache, throwAway, status); |
+ cache.get(LocaleCacheKey<UCTItem>("de_AU"), &cache, throwAway, status); |
+ |
+ const UCTItem *deAu(throwAway); |
+ deAu->addRef(); |
+ |
+ // Client holds three unique references, "en", "fr", "de" although we |
+ // could have a total of 8 entries in the cache maxUnusedCount == 3 |
+ // so we have only 6 entries. |
+ assertEquals("", 3, cache.unusedCount()); |
+ assertEquals("", 6, cache.keyCount()); |
+ |
+ // For all the references we have, cache must continue to return |
+ // those same references (#2) |
+ |
+ cache.get(LocaleCacheKey<UCTItem>("en"), &cache, throwAway, status); |
+ if (throwAway != en) { |
+ errln("Expected en to resolve to the same object."); |
+ } |
+ cache.get(LocaleCacheKey<UCTItem>("en_US"), &cache, throwAway, status); |
+ if (throwAway != enUs) { |
+ errln("Expected enUs to resolve to the same object."); |
+ } |
+ cache.get(LocaleCacheKey<UCTItem>("en_GB"), &cache, throwAway, status); |
+ if (throwAway != enGb) { |
+ errln("Expected enGb to resolve to the same object."); |
+ } |
+ cache.get(LocaleCacheKey<UCTItem>("fr_FR"), &cache, throwAway, status); |
+ if (throwAway != frFr) { |
+ errln("Expected frFr to resolve to the same object."); |
+ } |
+ cache.get(LocaleCacheKey<UCTItem>("fr_FR"), &cache, throwAway, status); |
+ cache.get(LocaleCacheKey<UCTItem>("fr"), &cache, throwAway, status); |
+ if (throwAway != fr) { |
+ errln("Expected fr to resolve to the same object."); |
+ } |
+ cache.get(LocaleCacheKey<UCTItem>("de_AU"), &cache, throwAway, status); |
+ if (throwAway != deAu) { |
+ errln("Expected deAu to resolve to the same object."); |
+ } |
+ |
+ assertEquals("", 3, cache.unusedCount()); |
+ assertEquals("", 6, cache.keyCount()); |
+ |
+ // Now we hold a references to two more distinct values. Cache size |
+ // should grow to 8. |
+ const UCTItem *es = NULL; |
+ const UCTItem *ru = NULL; |
+ cache.get(LocaleCacheKey<UCTItem>("es"), &cache, es, status); |
+ cache.get(LocaleCacheKey<UCTItem>("ru"), &cache, ru, status); |
+ assertEquals("", 3, cache.unusedCount()); |
+ assertEquals("", 8, cache.keyCount()); |
+ |
+ // Now release all the references we hold except for |
+ // es, ru, and en |
+ SharedObject::clearPtr(enGb); |
+ SharedObject::clearPtr(enUs); |
+ SharedObject::clearPtr(fr); |
+ SharedObject::clearPtr(frFr); |
+ SharedObject::clearPtr(deAu); |
+ SharedObject::clearPtr(es); |
+ SharedObject::clearPtr(ru); |
+ SharedObject::clearPtr(en); |
+ SharedObject::clearPtr(throwAway); |
+ |
+ // Size of cache should magically drop to 3. |
+ assertEquals("", 3, cache.unusedCount()); |
+ assertEquals("", 3, cache.keyCount()); |
+ |
+ // Be sure nothing happens setting the eviction policy in the middle of |
+ // a run. |
+ cache.setEvictionPolicy(3, 0, status); |
+ assertSuccess("", status); |
+ |
+} |
+ |
void UnifiedCacheTest::TestBasic() { |
UErrorCode status = U_ZERO_ERROR; |
const UnifiedCache *cache = UnifiedCache::getInstance(status); |
@@ -110,20 +326,25 @@ void UnifiedCacheTest::TestBasic() { |
assertEquals("", baseCount + 5, cache->keyCount()); |
SharedObject::clearPtr(enGb); |
cache->flush(); |
- assertEquals("", baseCount + 5, cache->keyCount()); |
+ |
+ // Only 2 unique values in the cache. flushing trims cache down |
+ // to this minimum size. |
+ assertEquals("", baseCount + 2, cache->keyCount()); |
SharedObject::clearPtr(enUs); |
SharedObject::clearPtr(en); |
cache->flush(); |
// With en_GB and en_US and en cleared there are no more hard references to |
// the "en" object, so it gets flushed and the keys that refer to it |
- // get removed from the cache. |
- assertEquals("", baseCount + 2, cache->keyCount()); |
+ // get removed from the cache. Now we have just one unique value, fr, in |
+ // the cache |
+ assertEquals("", baseCount + 1, cache->keyCount()); |
SharedObject::clearPtr(fr); |
cache->flush(); |
- assertEquals("", baseCount + 2, cache->keyCount()); |
+ assertEquals("", baseCount + 1, cache->keyCount()); |
SharedObject::clearPtr(frFr); |
cache->flush(); |
assertEquals("", baseCount + 0, cache->keyCount()); |
+ assertSuccess("", status); |
} |
void UnifiedCacheTest::TestError() { |