OLD | NEW |
1 /* | 1 /* |
2 ******************************************************************************* | 2 ******************************************************************************* |
3 * Copyright (C) 2014, International Business Machines Corporation and * | 3 * Copyright (C) 2015, International Business Machines Corporation and * |
4 * others. All Rights Reserved. * | 4 * others. All Rights Reserved. * |
5 ******************************************************************************* | 5 ******************************************************************************* |
6 * | 6 * |
7 * File UNIFIEDCACHETEST.CPP | 7 * File UNIFIEDCACHETEST.CPP |
8 * | 8 * |
9 ******************************************************************************** | 9 ******************************************************************************** |
10 */ | 10 */ |
11 #include "cstring.h" | 11 #include "cstring.h" |
12 #include "intltest.h" | 12 #include "intltest.h" |
13 #include "unifiedcache.h" | 13 #include "unifiedcache.h" |
| 14 #include "unicode/datefmt.h" |
14 | 15 |
15 class UCTItem : public SharedObject { | 16 class UCTItem : public SharedObject { |
16 public: | 17 public: |
17 char *value; | 18 char *value; |
18 UCTItem(const char *x) : value(NULL) { | 19 UCTItem(const char *x) : value(NULL) { |
19 value = uprv_strdup(x); | 20 value = uprv_strdup(x); |
20 } | 21 } |
21 virtual ~UCTItem() { | 22 virtual ~UCTItem() { |
22 uprv_free(value); | 23 uprv_free(value); |
23 } | 24 } |
24 }; | 25 }; |
25 | 26 |
26 class UCTItem2 : public SharedObject { | 27 class UCTItem2 : public SharedObject { |
27 }; | 28 }; |
28 | 29 |
29 U_NAMESPACE_BEGIN | 30 U_NAMESPACE_BEGIN |
30 | 31 |
31 template<> U_EXPORT | 32 template<> U_EXPORT |
32 const UCTItem *LocaleCacheKey<UCTItem>::createObject( | 33 const UCTItem *LocaleCacheKey<UCTItem>::createObject( |
33 const void * /*unused*/, UErrorCode &status) const { | 34 const void *context, UErrorCode &status) const { |
| 35 const UnifiedCache *cacheContext = (const UnifiedCache *) context; |
34 if (uprv_strcmp(fLoc.getName(), "zh") == 0) { | 36 if (uprv_strcmp(fLoc.getName(), "zh") == 0) { |
35 status = U_MISSING_RESOURCE_ERROR; | 37 status = U_MISSING_RESOURCE_ERROR; |
36 return NULL; | 38 return NULL; |
37 } | 39 } |
38 if (uprv_strcmp(fLoc.getLanguage(), fLoc.getName()) != 0) { | 40 if (uprv_strcmp(fLoc.getLanguage(), fLoc.getName()) != 0) { |
39 const UCTItem *item = NULL; | 41 const UCTItem *item = NULL; |
40 UnifiedCache::getByLocale(fLoc.getLanguage(), item, status); | 42 if (cacheContext == NULL) { |
| 43 UnifiedCache::getByLocale(fLoc.getLanguage(), item, status); |
| 44 } else { |
| 45 cacheContext->get(LocaleCacheKey<UCTItem>(fLoc.getLanguage()), item,
status); |
| 46 } |
41 if (U_FAILURE(status)) { | 47 if (U_FAILURE(status)) { |
42 return NULL; | 48 return NULL; |
43 } | 49 } |
44 return item; | 50 return item; |
45 } | 51 } |
46 UCTItem *result = new UCTItem(fLoc.getName()); | 52 UCTItem *result = new UCTItem(fLoc.getName()); |
47 result->addRef(); | 53 result->addRef(); |
48 return result; | 54 return result; |
49 } | 55 } |
50 | 56 |
51 template<> U_EXPORT | 57 template<> U_EXPORT |
52 const UCTItem2 *LocaleCacheKey<UCTItem2>::createObject( | 58 const UCTItem2 *LocaleCacheKey<UCTItem2>::createObject( |
53 const void * /*unused*/, UErrorCode & /*status*/) const { | 59 const void * /*unused*/, UErrorCode & /*status*/) const { |
54 return NULL; | 60 return NULL; |
55 } | 61 } |
56 | 62 |
57 U_NAMESPACE_END | 63 U_NAMESPACE_END |
58 | 64 |
59 | 65 |
60 class UnifiedCacheTest : public IntlTest { | 66 class UnifiedCacheTest : public IntlTest { |
61 public: | 67 public: |
62 UnifiedCacheTest() { | 68 UnifiedCacheTest() { |
63 } | 69 } |
64 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=
0); | 70 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=
0); |
65 private: | 71 private: |
| 72 void TestEvictionPolicy(); |
| 73 void TestBounded(); |
66 void TestBasic(); | 74 void TestBasic(); |
67 void TestError(); | 75 void TestError(); |
68 void TestHashEquals(); | 76 void TestHashEquals(); |
| 77 void TestEvictionUnderStress(); |
69 }; | 78 }; |
70 | 79 |
71 void UnifiedCacheTest::runIndexedTest(int32_t index, UBool exec, const char* &na
me, char* /*par*/) { | 80 void UnifiedCacheTest::runIndexedTest(int32_t index, UBool exec, const char* &na
me, char* /*par*/) { |
72 TESTCASE_AUTO_BEGIN; | 81 TESTCASE_AUTO_BEGIN; |
| 82 TESTCASE_AUTO(TestEvictionPolicy); |
| 83 TESTCASE_AUTO(TestBounded); |
73 TESTCASE_AUTO(TestBasic); | 84 TESTCASE_AUTO(TestBasic); |
74 TESTCASE_AUTO(TestError); | 85 TESTCASE_AUTO(TestError); |
75 TESTCASE_AUTO(TestHashEquals); | 86 TESTCASE_AUTO(TestHashEquals); |
| 87 TESTCASE_AUTO(TestEvictionUnderStress); |
76 TESTCASE_AUTO_END; | 88 TESTCASE_AUTO_END; |
77 } | 89 } |
78 | 90 |
| 91 void UnifiedCacheTest::TestEvictionUnderStress() { |
| 92 #if !UCONFIG_NO_FORMATTING |
| 93 int32_t localeCount; |
| 94 const Locale *locales = DateFormat::getAvailableLocales(localeCount); |
| 95 UErrorCode status = U_ZERO_ERROR; |
| 96 const UnifiedCache *cache = UnifiedCache::getInstance(status); |
| 97 int64_t evictedCountBefore = cache->autoEvictedCount(); |
| 98 for (int32_t i = 0; i < localeCount; ++i) { |
| 99 LocalPointer<DateFormat> ptr(DateFormat::createInstanceForSkeleton("yMd"
, locales[i], status)); |
| 100 } |
| 101 int64_t evictedCountAfter = cache->autoEvictedCount(); |
| 102 if (evictedCountBefore == evictedCountAfter) { |
| 103 dataerrln("%s:%d Items should have been evicted from cache", |
| 104 __FILE__, __LINE__); |
| 105 } |
| 106 #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 107 } |
| 108 |
| 109 void UnifiedCacheTest::TestEvictionPolicy() { |
| 110 UErrorCode status = U_ZERO_ERROR; |
| 111 |
| 112 // We have to call this first or else calling the UnifiedCache |
| 113 // ctor will fail. This is by design to deter clients from using the |
| 114 // cache API incorrectly by creating their own cache instances. |
| 115 UnifiedCache::getInstance(status); |
| 116 |
| 117 // We create our own local UnifiedCache instance to ensure we have |
| 118 // complete control over it. Real clients should never ever create |
| 119 // their own cache! |
| 120 UnifiedCache cache(status); |
| 121 assertSuccess("", status); |
| 122 |
| 123 // Don't allow unused entries to exeed more than 100% of in use entries. |
| 124 cache.setEvictionPolicy(0, 100, status); |
| 125 |
| 126 static const char *locales[] = { |
| 127 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", |
| 128 "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"}; |
| 129 |
| 130 const UCTItem *usedReferences[] = {NULL, NULL, NULL, NULL, NULL}; |
| 131 const UCTItem *unusedReference = NULL; |
| 132 |
| 133 // Add 5 in-use entries |
| 134 for (int32_t i = 0; i < UPRV_LENGTHOF(usedReferences); i++) { |
| 135 cache.get( |
| 136 LocaleCacheKey<UCTItem>(locales[i]), |
| 137 &cache, |
| 138 usedReferences[i], |
| 139 status); |
| 140 } |
| 141 |
| 142 // Add 10 not in use entries. |
| 143 for (int32_t i = 0; i < 10; ++i) { |
| 144 cache.get( |
| 145 LocaleCacheKey<UCTItem>( |
| 146 locales[i + UPRV_LENGTHOF(usedReferences)]), |
| 147 &cache, |
| 148 unusedReference, |
| 149 status); |
| 150 } |
| 151 unusedReference->removeRef(); |
| 152 |
| 153 // unused count not to exeed in use count |
| 154 assertEquals("", UPRV_LENGTHOF(usedReferences), cache.unusedCount()); |
| 155 assertEquals("", 2*UPRV_LENGTHOF(usedReferences), cache.keyCount()); |
| 156 |
| 157 // Free up those used entries. |
| 158 for (int32_t i = 0; i < UPRV_LENGTHOF(usedReferences); i++) { |
| 159 usedReferences[i]->removeRef(); |
| 160 } |
| 161 |
| 162 // This should free up all cache items |
| 163 assertEquals("", 0, cache.keyCount()); |
| 164 |
| 165 assertSuccess("", status); |
| 166 } |
| 167 |
| 168 |
| 169 |
| 170 void UnifiedCacheTest::TestBounded() { |
| 171 UErrorCode status = U_ZERO_ERROR; |
| 172 |
| 173 // We have to call this first or else calling the UnifiedCache |
| 174 // ctor will fail. This is by design to deter clients from using the |
| 175 // cache API incorrectly by creating their own cache instances. |
| 176 UnifiedCache::getInstance(status); |
| 177 |
| 178 // We create our own local UnifiedCache instance to ensure we have |
| 179 // complete control over it. Real clients should never ever create |
| 180 // their own cache! |
| 181 UnifiedCache cache(status); |
| 182 assertSuccess("", status); |
| 183 |
| 184 // Maximum unused count is 3. |
| 185 cache.setEvictionPolicy(3, 0, status); |
| 186 |
| 187 // Our cache will hold up to 3 unused key-value pairs |
| 188 // We test the following invariants: |
| 189 // 1. unusedCount <= 3 |
| 190 // 2. cache->get(X) always returns the same reference as long as caller |
| 191 // already holds references to that same object. |
| 192 |
| 193 // We first add 5 key-value pairs with two distinct values, "en" and "fr" |
| 194 // keeping all those references. |
| 195 |
| 196 const UCTItem *en = NULL; |
| 197 const UCTItem *enGb = NULL; |
| 198 const UCTItem *enUs = NULL; |
| 199 const UCTItem *fr = NULL; |
| 200 const UCTItem *frFr = NULL; |
| 201 cache.get(LocaleCacheKey<UCTItem>("en_US"), &cache, enUs, status); |
| 202 cache.get(LocaleCacheKey<UCTItem>("en"), &cache, en, status); |
| 203 assertEquals("", 1, cache.unusedCount()); |
| 204 cache.get(LocaleCacheKey<UCTItem>("en_GB"), &cache, enGb, status); |
| 205 cache.get(LocaleCacheKey<UCTItem>("fr_FR"), &cache, frFr, status); |
| 206 cache.get(LocaleCacheKey<UCTItem>("fr"), &cache, fr, status); |
| 207 |
| 208 // Client holds two unique references, "en" and "fr" the other three |
| 209 // entries are eligible for eviction. |
| 210 assertEquals("", 3, cache.unusedCount()); |
| 211 assertEquals("", 5, cache.keyCount()); |
| 212 |
| 213 // Exercise cache more but don't hold the references except for |
| 214 // the last one. At the end of this, we will hold references to one |
| 215 // additional distinct value, so we will have references to 3 distinct |
| 216 // values. |
| 217 const UCTItem *throwAway = NULL; |
| 218 cache.get(LocaleCacheKey<UCTItem>("zn_AA"), &cache, throwAway, status); |
| 219 cache.get(LocaleCacheKey<UCTItem>("sr_AA"), &cache, throwAway, status); |
| 220 cache.get(LocaleCacheKey<UCTItem>("de_AU"), &cache, throwAway, status); |
| 221 |
| 222 const UCTItem *deAu(throwAway); |
| 223 deAu->addRef(); |
| 224 |
| 225 // Client holds three unique references, "en", "fr", "de" although we |
| 226 // could have a total of 8 entries in the cache maxUnusedCount == 3 |
| 227 // so we have only 6 entries. |
| 228 assertEquals("", 3, cache.unusedCount()); |
| 229 assertEquals("", 6, cache.keyCount()); |
| 230 |
| 231 // For all the references we have, cache must continue to return |
| 232 // those same references (#2) |
| 233 |
| 234 cache.get(LocaleCacheKey<UCTItem>("en"), &cache, throwAway, status); |
| 235 if (throwAway != en) { |
| 236 errln("Expected en to resolve to the same object."); |
| 237 } |
| 238 cache.get(LocaleCacheKey<UCTItem>("en_US"), &cache, throwAway, status); |
| 239 if (throwAway != enUs) { |
| 240 errln("Expected enUs to resolve to the same object."); |
| 241 } |
| 242 cache.get(LocaleCacheKey<UCTItem>("en_GB"), &cache, throwAway, status); |
| 243 if (throwAway != enGb) { |
| 244 errln("Expected enGb to resolve to the same object."); |
| 245 } |
| 246 cache.get(LocaleCacheKey<UCTItem>("fr_FR"), &cache, throwAway, status); |
| 247 if (throwAway != frFr) { |
| 248 errln("Expected frFr to resolve to the same object."); |
| 249 } |
| 250 cache.get(LocaleCacheKey<UCTItem>("fr_FR"), &cache, throwAway, status); |
| 251 cache.get(LocaleCacheKey<UCTItem>("fr"), &cache, throwAway, status); |
| 252 if (throwAway != fr) { |
| 253 errln("Expected fr to resolve to the same object."); |
| 254 } |
| 255 cache.get(LocaleCacheKey<UCTItem>("de_AU"), &cache, throwAway, status); |
| 256 if (throwAway != deAu) { |
| 257 errln("Expected deAu to resolve to the same object."); |
| 258 } |
| 259 |
| 260 assertEquals("", 3, cache.unusedCount()); |
| 261 assertEquals("", 6, cache.keyCount()); |
| 262 |
| 263 // Now we hold a references to two more distinct values. Cache size |
| 264 // should grow to 8. |
| 265 const UCTItem *es = NULL; |
| 266 const UCTItem *ru = NULL; |
| 267 cache.get(LocaleCacheKey<UCTItem>("es"), &cache, es, status); |
| 268 cache.get(LocaleCacheKey<UCTItem>("ru"), &cache, ru, status); |
| 269 assertEquals("", 3, cache.unusedCount()); |
| 270 assertEquals("", 8, cache.keyCount()); |
| 271 |
| 272 // Now release all the references we hold except for |
| 273 // es, ru, and en |
| 274 SharedObject::clearPtr(enGb); |
| 275 SharedObject::clearPtr(enUs); |
| 276 SharedObject::clearPtr(fr); |
| 277 SharedObject::clearPtr(frFr); |
| 278 SharedObject::clearPtr(deAu); |
| 279 SharedObject::clearPtr(es); |
| 280 SharedObject::clearPtr(ru); |
| 281 SharedObject::clearPtr(en); |
| 282 SharedObject::clearPtr(throwAway); |
| 283 |
| 284 // Size of cache should magically drop to 3. |
| 285 assertEquals("", 3, cache.unusedCount()); |
| 286 assertEquals("", 3, cache.keyCount()); |
| 287 |
| 288 // Be sure nothing happens setting the eviction policy in the middle of |
| 289 // a run. |
| 290 cache.setEvictionPolicy(3, 0, status); |
| 291 assertSuccess("", status); |
| 292 |
| 293 } |
| 294 |
79 void UnifiedCacheTest::TestBasic() { | 295 void UnifiedCacheTest::TestBasic() { |
80 UErrorCode status = U_ZERO_ERROR; | 296 UErrorCode status = U_ZERO_ERROR; |
81 const UnifiedCache *cache = UnifiedCache::getInstance(status); | 297 const UnifiedCache *cache = UnifiedCache::getInstance(status); |
82 assertSuccess("", status); | 298 assertSuccess("", status); |
83 cache->flush(); | 299 cache->flush(); |
84 int32_t baseCount = cache->keyCount(); | 300 int32_t baseCount = cache->keyCount(); |
85 const UCTItem *en = NULL; | 301 const UCTItem *en = NULL; |
86 const UCTItem *enGb = NULL; | 302 const UCTItem *enGb = NULL; |
87 const UCTItem *enGb2 = NULL; | 303 const UCTItem *enGb2 = NULL; |
88 const UCTItem *enUs = NULL; | 304 const UCTItem *enUs = NULL; |
(...skipping 14 matching lines...) Expand all Loading... |
103 } | 319 } |
104 if (enGb == fr) { | 320 if (enGb == fr) { |
105 errln("Expected en_GB and fr to return different objects."); | 321 errln("Expected en_GB and fr to return different objects."); |
106 } | 322 } |
107 assertSuccess("", status); | 323 assertSuccess("", status); |
108 // en_US, en_GB, en share one object; fr_FR and fr don't share. | 324 // en_US, en_GB, en share one object; fr_FR and fr don't share. |
109 // 5 keys in all. | 325 // 5 keys in all. |
110 assertEquals("", baseCount + 5, cache->keyCount()); | 326 assertEquals("", baseCount + 5, cache->keyCount()); |
111 SharedObject::clearPtr(enGb); | 327 SharedObject::clearPtr(enGb); |
112 cache->flush(); | 328 cache->flush(); |
113 assertEquals("", baseCount + 5, cache->keyCount()); | 329 |
| 330 // Only 2 unique values in the cache. flushing trims cache down |
| 331 // to this minimum size. |
| 332 assertEquals("", baseCount + 2, cache->keyCount()); |
114 SharedObject::clearPtr(enUs); | 333 SharedObject::clearPtr(enUs); |
115 SharedObject::clearPtr(en); | 334 SharedObject::clearPtr(en); |
116 cache->flush(); | 335 cache->flush(); |
117 // With en_GB and en_US and en cleared there are no more hard references to | 336 // With en_GB and en_US and en cleared there are no more hard references to |
118 // the "en" object, so it gets flushed and the keys that refer to it | 337 // the "en" object, so it gets flushed and the keys that refer to it |
119 // get removed from the cache. | 338 // get removed from the cache. Now we have just one unique value, fr, in |
120 assertEquals("", baseCount + 2, cache->keyCount()); | 339 // the cache |
| 340 assertEquals("", baseCount + 1, cache->keyCount()); |
121 SharedObject::clearPtr(fr); | 341 SharedObject::clearPtr(fr); |
122 cache->flush(); | 342 cache->flush(); |
123 assertEquals("", baseCount + 2, cache->keyCount()); | 343 assertEquals("", baseCount + 1, cache->keyCount()); |
124 SharedObject::clearPtr(frFr); | 344 SharedObject::clearPtr(frFr); |
125 cache->flush(); | 345 cache->flush(); |
126 assertEquals("", baseCount + 0, cache->keyCount()); | 346 assertEquals("", baseCount + 0, cache->keyCount()); |
| 347 assertSuccess("", status); |
127 } | 348 } |
128 | 349 |
129 void UnifiedCacheTest::TestError() { | 350 void UnifiedCacheTest::TestError() { |
130 UErrorCode status = U_ZERO_ERROR; | 351 UErrorCode status = U_ZERO_ERROR; |
131 const UnifiedCache *cache = UnifiedCache::getInstance(status); | 352 const UnifiedCache *cache = UnifiedCache::getInstance(status); |
132 assertSuccess("", status); | 353 assertSuccess("", status); |
133 cache->flush(); | 354 cache->flush(); |
134 int32_t baseCount = cache->keyCount(); | 355 int32_t baseCount = cache->keyCount(); |
135 const UCTItem *zh = NULL; | 356 const UCTItem *zh = NULL; |
136 const UCTItem *zhTw = NULL; | 357 const UCTItem *zhTw = NULL; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 assertTrue("", diffKey1.hashCode() != diffKey2.hashCode()); | 390 assertTrue("", diffKey1.hashCode() != diffKey2.hashCode()); |
170 assertTrue("", key1 == key2); | 391 assertTrue("", key1 == key2); |
171 assertTrue("", key1 != diffKey1); | 392 assertTrue("", key1 != diffKey1); |
172 assertTrue("", key1 != diffKey2); | 393 assertTrue("", key1 != diffKey2); |
173 assertTrue("", diffKey1 != diffKey2); | 394 assertTrue("", diffKey1 != diffKey2); |
174 } | 395 } |
175 | 396 |
176 extern IntlTest *createUnifiedCacheTest() { | 397 extern IntlTest *createUnifiedCacheTest() { |
177 return new UnifiedCacheTest(); | 398 return new UnifiedCacheTest(); |
178 } | 399 } |
OLD | NEW |