|
34 | 34 | import java.util.concurrent.ExecutorService;
|
35 | 35 | import java.util.concurrent.Executors;
|
36 | 36 | import java.util.concurrent.TimeUnit;
|
| 37 | +import java.util.concurrent.atomic.AtomicInteger; |
37 | 38 |
|
38 | 39 | public class MetaCacheTest {
|
39 | 40 |
|
@@ -216,4 +217,160 @@ public void testMetaObjCacheLoader() throws InterruptedException {
|
216 | 217 | latch.await();
|
217 | 218 |
|
218 | 219 | }
|
| 220 | + |
| 221 | + @Test |
| 222 | + public void testGetMetaObjCacheLoading() throws InterruptedException { |
| 223 | + // Create a CountDownLatch to track cache loading invocations |
| 224 | + CountDownLatch loadLatch = new CountDownLatch(2); |
| 225 | + |
| 226 | + // Create a custom cache loader that counts invocations |
| 227 | + CacheLoader<String, Optional<String>> metaObjCacheLoader = key -> { |
| 228 | + loadLatch.countDown(); |
| 229 | + return Optional.of("loaded_" + key); |
| 230 | + }; |
| 231 | + |
| 232 | + // Create a new MetaCache instance with our custom loader |
| 233 | + MetaCache<String> testCache = new MetaCache<>( |
| 234 | + "testCache", |
| 235 | + Executors.newCachedThreadPool(), |
| 236 | + OptionalLong.of(1), |
| 237 | + OptionalLong.of(1), |
| 238 | + 100, |
| 239 | + key -> Lists.newArrayList(), |
| 240 | + metaObjCacheLoader, |
| 241 | + (key, value, cause) -> { |
| 242 | + } |
| 243 | + ); |
| 244 | + |
| 245 | + // Case 1: Test when key does not exist in cache (val == null) |
| 246 | + Optional<String> result1 = testCache.getMetaObj("non_existent_key", 1L); |
| 247 | + Assert.assertTrue(result1.isPresent()); |
| 248 | + Assert.assertEquals("loaded_non_existent_key", result1.get()); |
| 249 | + |
| 250 | + // Case 2: Test when key exists but value is empty Optional |
| 251 | + // First, manually put an empty Optional into cache |
| 252 | + testCache.getMetaObjCache().put("empty_key", Optional.empty()); |
| 253 | + Optional<String> result2 = testCache.getMetaObj("empty_key", 2L); |
| 254 | + Assert.assertTrue(result2.isPresent()); |
| 255 | + Assert.assertEquals("loaded_empty_key", result2.get()); |
| 256 | + |
| 257 | + // Verify that cache loader was invoked exactly twice |
| 258 | + Assert.assertTrue(loadLatch.await(1, TimeUnit.SECONDS)); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + public void testGetMetaObjConcurrent() throws InterruptedException { |
| 263 | + // Create a CountDownLatch to track cache loading invocations |
| 264 | + CountDownLatch loadLatch = new CountDownLatch(1); |
| 265 | + AtomicInteger loadCount = new AtomicInteger(0); |
| 266 | + |
| 267 | + // Create a custom cache loader that counts invocations and simulates slow loading |
| 268 | + CacheLoader<String, Optional<String>> metaObjCacheLoader = key -> { |
| 269 | + loadCount.incrementAndGet(); |
| 270 | + Thread.sleep(100); // Simulate slow loading |
| 271 | + loadLatch.countDown(); |
| 272 | + return Optional.of("loaded_" + key); |
| 273 | + }; |
| 274 | + |
| 275 | + // Create a new MetaCache instance with our custom loader |
| 276 | + MetaCache<String> testCache = new MetaCache<>( |
| 277 | + "testCache", |
| 278 | + Executors.newCachedThreadPool(), |
| 279 | + OptionalLong.of(1), |
| 280 | + OptionalLong.of(1), |
| 281 | + 100, |
| 282 | + key -> Lists.newArrayList(), |
| 283 | + metaObjCacheLoader, |
| 284 | + (key, value, cause) -> { |
| 285 | + } |
| 286 | + ); |
| 287 | + |
| 288 | + // Test concurrent access to non-existent key |
| 289 | + ExecutorService executor = Executors.newFixedThreadPool(10); |
| 290 | + final CountDownLatch startLatch = new CountDownLatch(1); |
| 291 | + final CountDownLatch finishLatch = new CountDownLatch(10); |
| 292 | + |
| 293 | + for (int i = 0; i < 10; i++) { |
| 294 | + executor.submit(() -> { |
| 295 | + try { |
| 296 | + startLatch.await(); |
| 297 | + Optional<String> result = testCache.getMetaObj("concurrent_key", 1L); |
| 298 | + Assert.assertTrue(result.isPresent()); |
| 299 | + Assert.assertEquals("loaded_concurrent_key", result.get()); |
| 300 | + } catch (Exception e) { |
| 301 | + Assert.fail("Exception occurred: " + e.getMessage()); |
| 302 | + } finally { |
| 303 | + finishLatch.countDown(); |
| 304 | + } |
| 305 | + }); |
| 306 | + } |
| 307 | + |
| 308 | + // Start all threads |
| 309 | + startLatch.countDown(); |
| 310 | + // Wait for all threads to complete |
| 311 | + finishLatch.await(5, TimeUnit.SECONDS); |
| 312 | + // Wait for cache loading to complete |
| 313 | + loadLatch.await(5, TimeUnit.SECONDS); |
| 314 | + |
| 315 | + // Verify that cache loader was invoked exactly once |
| 316 | + Assert.assertEquals(1, loadCount.get()); |
| 317 | + |
| 318 | + // Test concurrent access to existing but empty key |
| 319 | + loadCount.set(0); |
| 320 | + CountDownLatch loadLatch2 = new CountDownLatch(1); |
| 321 | + CacheLoader<String, Optional<String>> metaObjCacheLoader2 = key -> { |
| 322 | + loadCount.incrementAndGet(); |
| 323 | + Thread.sleep(100); // Simulate slow loading |
| 324 | + loadLatch2.countDown(); |
| 325 | + return Optional.of("loaded_" + key); |
| 326 | + }; |
| 327 | + |
| 328 | + // Create another MetaCache instance |
| 329 | + MetaCache<String> testCache2 = new MetaCache<>( |
| 330 | + "testCache2", |
| 331 | + Executors.newCachedThreadPool(), |
| 332 | + OptionalLong.of(1), |
| 333 | + OptionalLong.of(1), |
| 334 | + 100, |
| 335 | + key -> Lists.newArrayList(), |
| 336 | + metaObjCacheLoader2, |
| 337 | + (key, value, cause) -> { |
| 338 | + } |
| 339 | + ); |
| 340 | + |
| 341 | + // Manually put an empty Optional into cache |
| 342 | + testCache2.getMetaObjCache().put("empty_concurrent_key", Optional.empty()); |
| 343 | + |
| 344 | + // Reset latches for second test |
| 345 | + final CountDownLatch startLatch2 = new CountDownLatch(1); |
| 346 | + final CountDownLatch finishLatch2 = new CountDownLatch(10); |
| 347 | + |
| 348 | + for (int i = 0; i < 10; i++) { |
| 349 | + executor.submit(() -> { |
| 350 | + try { |
| 351 | + startLatch2.await(); |
| 352 | + Optional<String> result = testCache2.getMetaObj("empty_concurrent_key", 2L); |
| 353 | + Assert.assertTrue(result.isPresent()); |
| 354 | + Assert.assertEquals("loaded_empty_concurrent_key", result.get()); |
| 355 | + } catch (Exception e) { |
| 356 | + Assert.fail("Exception occurred: " + e.getMessage()); |
| 357 | + } finally { |
| 358 | + finishLatch2.countDown(); |
| 359 | + } |
| 360 | + }); |
| 361 | + } |
| 362 | + |
| 363 | + // Start all threads |
| 364 | + startLatch2.countDown(); |
| 365 | + // Wait for all threads to complete |
| 366 | + finishLatch2.await(5, TimeUnit.SECONDS); |
| 367 | + // Wait for cache loading to complete |
| 368 | + loadLatch2.await(5, TimeUnit.SECONDS); |
| 369 | + |
| 370 | + // Verify that cache loader was invoked exactly once |
| 371 | + Assert.assertEquals(1, loadCount.get()); |
| 372 | + |
| 373 | + executor.shutdown(); |
| 374 | + executor.awaitTermination(1, TimeUnit.SECONDS); |
| 375 | + } |
219 | 376 | }
|
0 commit comments