essobedo commented on code in PR #14424:
URL: https://github.com/apache/camel/pull/14424#discussion_r1634558793


##########
core/camel-core/src/test/java/org/apache/camel/support/cache/SimpleLRUCacheTest.java:
##########
@@ -287,4 +280,102 @@ void replaceWithOldValue() {
         assertEquals(3, map.size());
         assertEquals(0, consumed.size());
     }
+
+    @Test
+    void ignoreDuplicates() {
+        assertEquals(0, map.size());
+        for (int i = 0; i < 100; i++) {
+            map.put("1", Integer.toString(i));
+            assertEquals(1, map.size(), String.format("The expected size is 1 
but it fails after %d puts", i + 1));
+        }
+        assertEquals("99", map.get("1"));
+        assertNull(map.put("2", "Two"));
+        assertEquals(2, map.size());
+        assertEquals("99", map.get("1"));
+        assertNull(map.put("3", "Three"));
+        assertEquals(3, map.size());
+        assertEquals(0, consumed.size());
+        assertEquals("99", map.get("1"));
+        assertNull(map.put("4", "Four"));
+        assertEquals(3, map.size());
+        assertEquals(1, consumed.size());
+        assertFalse(map.containsKey("1"));
+        assertTrue(consumed.contains("99"));
+    }
+
+    @Test
+    void ensureEvictionOrdering() {
+        assertEquals(0, map.size());
+        assertNull(map.put("1", "One"));
+        assertNotNull(map.put("1", "One"));
+        assertNotNull(map.put("1", "One"));
+        assertNotNull(map.put("1", "One"));
+        assertNotNull(map.put("1", "One"));
+        assertNotNull(map.put("1", "One"));
+        assertNull(map.put("2", "Two"));
+        assertNotNull(map.put("1", "One"));
+        assertNull(map.put("3", "Three"));
+        assertEquals(3, map.size());
+        assertNull(map.put("4", "Four"));
+        assertEquals(3, map.size());
+        assertEquals(1, consumed.size());
+        assertFalse(map.containsKey("2"));
+        assertTrue(consumed.contains("Two"));
+    }
+
+    @ParameterizedTest
+    @ValueSource(ints = { 1, 2, 5, 10, 20, 50, 100, 1_000 })
+    void concurrentPut(int maximumCacheSize) throws Exception {
+        int threads = Runtime.getRuntime().availableProcessors();
+        int totalKeysPerThread = 1_000;
+        AtomicInteger counter = new AtomicInteger();
+        SimpleLRUCache<String, String> cache = new SimpleLRUCache<>(16, 
maximumCacheSize, v -> counter.incrementAndGet());
+        CountDownLatch latch = new CountDownLatch(threads);
+        for (int i = 0; i < threads; i++) {
+            int threadId = i;
+            new Thread(() -> {
+                try {
+                    for (int j = 0; j < totalKeysPerThread; j++) {
+                        cache.put(threadId + "-" + j, Integer.toString(j));
+                    }
+                } finally {
+                    latch.countDown();
+                }
+            }).start();
+        }
+        latch.await();

Review Comment:
   No, it cannot wait forever because `latch.countDown()` is called in a 
finally block so even in case of an error, it will be called



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to