camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1098601 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/ main/java/org/apache/camel/management/mbean/ main/java/org/apache/camel/util/ test/java/org/apache/camel/management/ test/java/org/apache/camel/util/
Date Mon, 02 May 2011 14:37:34 GMT
Author: davsclaus
Date: Mon May  2 14:37:32 2011
New Revision: 1098601

URL: http://svn.apache.org/viewvc?rev=1098601&view=rev
Log:
CAMEL-3926: Added cache stats to LRUCache and exposed those for JMX as well.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedConsumerCacheHitsTest.java
      - copied, changed from r1098576, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedConsumerCacheTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedProducerCacheHitsTest.java
      - copied, changed from r1098576, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedProducerCacheTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedConsumerCache.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedProducerCache.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUCache.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUCacheTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUSoftCacheTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java?rev=1098601&r1=1098600&r2=1098601&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java Mon May
 2 14:37:32 2011
@@ -157,12 +157,59 @@ public class ConsumerCache extends Servi
     }
 
     /**
+     * Gets the cache hits statistic
+     * <p/>
+     * Will return <tt>-1</tt> if it cannot determine this if a custom cache
was used.
+     *
+     * @return the hits
+     */
+    public long getHits() {
+        long hits = -1;
+        if (consumers instanceof LRUCache) {
+            LRUCache cache = (LRUCache) consumers;
+            hits = cache.getHits();
+        }
+        return hits;
+    }
+
+    /**
+     * Gets the cache misses statistic
+     * <p/>
+     * Will return <tt>-1</tt> if it cannot determine this if a custom cache
was used.
+     *
+     * @return the misses
+     */
+    public long getMisses() {
+        long misses = -1;
+        if (consumers instanceof LRUCache) {
+            LRUCache cache = (LRUCache) consumers;
+            misses = cache.getMisses();
+        }
+        return misses;
+    }
+
+    /**
+     * Resets the cache statistics
+     */
+    public void resetCacheStatistics() {
+        if (consumers instanceof LRUCache) {
+            LRUCache cache = (LRUCache) consumers;
+            cache.resetStatistics();
+        }
+    }
+
+    /**
      * Purges this cache
      */
     public synchronized void purge() {
         consumers.clear();
     }
 
+    @Override
+    public String toString() {
+        return "ConsumerCache for source: " + source + ", capacity: " + getCapacity();
+    }
+
     protected void doStart() throws Exception {
         ServiceHelper.startServices(consumers);
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java?rev=1098601&r1=1098600&r2=1098601&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java Mon May
 2 14:37:32 2011
@@ -426,6 +426,48 @@ public class ProducerCache extends Servi
     }
 
     /**
+     * Gets the cache hits statistic
+     * <p/>
+     * Will return <tt>-1</tt> if it cannot determine this if a custom cache
was used.
+     *
+     * @return the hits
+     */
+    public long getHits() {
+        long hits = -1;
+        if (producers instanceof LRUCache) {
+            LRUCache cache = (LRUCache) producers;
+            hits = cache.getHits();
+        }
+        return hits;
+    }
+
+    /**
+     * Gets the cache misses statistic
+     * <p/>
+     * Will return <tt>-1</tt> if it cannot determine this if a custom cache
was used.
+     *
+     * @return the misses
+     */
+    public long getMisses() {
+        long misses = -1;
+        if (producers instanceof LRUCache) {
+            LRUCache cache = (LRUCache) producers;
+            misses = cache.getMisses();
+        }
+        return misses;
+    }
+
+    /**
+     * Resets the cache statistics
+     */
+    public void resetCacheStatistics() {
+        if (producers instanceof LRUCache) {
+            LRUCache cache = (LRUCache) producers;
+            cache.resetStatistics();
+        }
+    }
+
+    /**
      * Purges this cache
      */
     public synchronized void purge() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedConsumerCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedConsumerCache.java?rev=1098601&r1=1098600&r2=1098601&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedConsumerCache.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedConsumerCache.java
Mon May  2 14:37:32 2011
@@ -56,6 +56,21 @@ public class ManagedConsumerCache extend
         return consumerCache.getCapacity();
     }
 
+    @ManagedAttribute(description = "Cache hits")
+    public Long getHits() {
+        return consumerCache.getHits();
+    }
+
+    @ManagedAttribute(description = "Cache misses")
+    public Long getMisses() {
+        return consumerCache.getMisses();
+    }
+
+    @ManagedOperation(description = "Reset cache statistics")
+    public void resetStatistics() {
+        consumerCache.resetCacheStatistics();
+    }
+
     @ManagedOperation(description = "Purges the cache")
     public void purge() {
         consumerCache.purge();

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedProducerCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedProducerCache.java?rev=1098601&r1=1098600&r2=1098601&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedProducerCache.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedProducerCache.java
Mon May  2 14:37:32 2011
@@ -56,6 +56,21 @@ public class ManagedProducerCache extend
         return producerCache.getCapacity();
     }
 
+    @ManagedAttribute(description = "Cache hits")
+    public Long getHits() {
+        return producerCache.getHits();
+    }
+
+    @ManagedAttribute(description = "Cache misses")
+    public Long getMisses() {
+        return producerCache.getMisses();
+    }
+
+    @ManagedOperation(description = "Reset cache statistics")
+    public void resetStatistics() {
+        producerCache.resetCacheStatistics();
+    }
+
     @ManagedOperation(description = "Purges the cache")
     public void purge() {
         producerCache.purge();

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUCache.java?rev=1098601&r1=1098600&r2=1098601&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUCache.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUCache.java Mon May  2 14:37:32
2011
@@ -18,6 +18,7 @@ package org.apache.camel.util;
 
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.camel.Service;
 
@@ -27,8 +28,10 @@ import org.apache.camel.Service;
  * @version 
  */
 public class LRUCache<K, V> extends LinkedHashMap<K, V> implements Service {
-    private static final long serialVersionUID = -342098639681884413L;
+    private static final long serialVersionUID = -342098639681884414L;
     private int maxCacheSize = 10000;
+    private final AtomicLong hits = new AtomicLong();
+    private final AtomicLong misses = new AtomicLong();
 
     public LRUCache(int maximumCacheSize) {
         this(maximumCacheSize, maximumCacheSize, 0.75f, true);
@@ -51,6 +54,31 @@ public class LRUCache<K, V> extends Link
         this.maxCacheSize = maximumCacheSize;
     }
 
+    @Override
+    public V get(Object o) {
+        V answer = super.get(o);
+        if (answer != null) {
+            hits.incrementAndGet();
+        } else {
+            misses.incrementAndGet();
+        }
+        return answer;
+    }
+
+    /**
+     * Gets the number of cache hits
+     */
+    public long getHits() {
+        return hits.get();
+    }
+
+    /**
+     * Gets the number of cache misses.
+     */
+    public long getMisses() {
+        return misses.get();
+    }
+
     /**
      * Returns the maxCacheSize.
      */
@@ -58,6 +86,14 @@ public class LRUCache<K, V> extends Link
         return maxCacheSize;
     }
 
+    /**
+     * Rest the cache statistics such as hits and misses.
+     */
+    public void resetStatistics() {
+        hits.set(0);
+        misses.set(0);
+    }
+
     protected boolean removeEldestEntry(Map.Entry<K, V> entry) {
         return size() > maxCacheSize;
     }
@@ -71,6 +107,8 @@ public class LRUCache<K, V> extends Link
         if (!isEmpty()) {
             ServiceHelper.stopServices(values());
             clear();
+            hits.set(0);
+            misses.set(0);
         }
     }
 

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedConsumerCacheHitsTest.java
(from r1098576, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedConsumerCacheTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedConsumerCacheHitsTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedConsumerCacheHitsTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedConsumerCacheTest.java&r1=1098576&r2=1098601&rev=1098601&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedConsumerCacheTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedConsumerCacheHitsTest.java
Mon May  2 14:37:32 2011
@@ -27,9 +27,9 @@ import org.apache.camel.builder.RouteBui
 import org.apache.camel.impl.ConsumerCache;
 
 /**
- * @version 
+ * @version
  */
-public class ManagedConsumerCacheTest extends ManagementTestSupport {
+public class ManagedConsumerCacheHitsTest extends ManagementTestSupport {
 
     public void testManageConsumerCache() throws Exception {
         // always register services in JMX so we can enlist our consumer template/cache
@@ -38,9 +38,9 @@ public class ManagedConsumerCacheTest ex
         ConsumerCache cache = new ConsumerCache(this, context);
         context.addService(cache);
 
-        template.sendBody("direct:start", "Hello World");
+        template.sendBody("seda:a", "Hello World");
 
-        Exchange out = cache.getConsumer(context.getEndpoint("seda:queue")).receive(3000);
+        Exchange out = cache.getConsumer(context.getEndpoint("seda:a")).receive(3000);
         assertNotNull("Should got an exchange", out);
         assertEquals("Hello World", out.getIn().getBody());
 
@@ -65,15 +65,54 @@ public class ManagedConsumerCacheTest ex
         Integer current = (Integer) mbeanServer.getAttribute(on, "Size");
         assertEquals(1, current.intValue());
 
-        String source = (String) mbeanServer.getAttribute(on, "Source");
-        assertNotNull(source);
-        assertTrue(source.contains("testManageConsumerCache"));
-
-        // purge
-        mbeanServer.invoke(on, "purge", null, null);
-
-        current = (Integer) mbeanServer.getAttribute(on, "Size");
-        assertEquals(0, current.intValue());
+        // since we only send 1 message we should have 0 hits and 1 miss
+        Long hits = (Long) mbeanServer.getAttribute(on, "Hits");
+        assertEquals(0, hits.longValue());
+        Long misses = (Long) mbeanServer.getAttribute(on, "Misses");
+        assertEquals(1, misses.longValue());
+
+
+        // send more
+        // ---------
+
+        template.sendBody("seda:b", "Hello World");
+        template.sendBody("seda:c", "Hello World");
+        out = cache.getConsumer(context.getEndpoint("seda:b")).receive(3000);
+        assertNotNull(out);
+        out = cache.getConsumer(context.getEndpoint("seda:c")).receive(3000);
+        assertNotNull(out);
+
+        // we have only consumed from 3 different endpoints so all is misses
+        hits = (Long) mbeanServer.getAttribute(on, "Hits");
+        assertEquals(0, hits.longValue());
+        misses = (Long) mbeanServer.getAttribute(on, "Misses");
+        assertEquals(3, misses.longValue());
+
+
+        // send to same endpoints
+        // ----------------------
+
+        template.sendBody("seda:a", "Bye World");
+        template.sendBody("seda:b", "Bye World");
+        out = cache.getConsumer(context.getEndpoint("seda:a")).receive(3000);
+        assertNotNull(out);
+        out = cache.getConsumer(context.getEndpoint("seda:b")).receive(3000);
+        assertNotNull(out);
+
+        // we should have hits now
+        hits = (Long) mbeanServer.getAttribute(on, "Hits");
+        assertEquals(2, hits.longValue());
+        misses = (Long) mbeanServer.getAttribute(on, "Misses");
+        assertEquals(3, misses.longValue());
+
+
+        // reset statistics
+        // ----------------
+        mbeanServer.invoke(on, "resetStatistics", null, null);
+        hits = (Long) mbeanServer.getAttribute(on, "Hits");
+        assertEquals(0, hits.longValue());
+        misses = (Long) mbeanServer.getAttribute(on, "Misses");
+        assertEquals(0, misses.longValue());
     }
 
     @Override
@@ -81,7 +120,8 @@ public class ManagedConsumerCacheTest ex
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").to("seda:queue");
+                // note: this route is not used in the test
+                from("direct:start").to("mock:result");
             }
         };
     }

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedProducerCacheHitsTest.java
(from r1098576, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedProducerCacheTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedProducerCacheHitsTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedProducerCacheHitsTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedProducerCacheTest.java&r1=1098576&r2=1098601&rev=1098601&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedProducerCacheTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedProducerCacheHitsTest.java
Mon May  2 14:37:32 2011
@@ -27,13 +27,15 @@ import org.apache.camel.builder.RouteBui
 /**
  * @version 
  */
-public class ManagedProducerCacheTest extends ManagementTestSupport {
+public class ManagedProducerCacheHitsTest extends ManagementTestSupport {
 
     public void testManageProducerCache() throws Exception {
-        getMockEndpoint("mock:result").expectedMessageCount(1);
-
-        template.sendBody("direct:start", "Hello World");
-
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        getMockEndpoint("mock:b").expectedMessageCount(1);
+        getMockEndpoint("mock:c").expectedMessageCount(1);
+        template.sendBodyAndHeader("direct:start", "Hello World", "foo", "mock:a");
+        template.sendBodyAndHeader("direct:start", "Hello World", "foo", "mock:b");
+        template.sendBodyAndHeader("direct:start", "Hello World", "foo", "mock:c");
         assertMockEndpointsSatisfied();
 
         // get the stats for the route
@@ -55,16 +57,54 @@ public class ManagedProducerCacheTest ex
         assertEquals(1000, max.intValue());
 
         Integer current = (Integer) mbeanServer.getAttribute(on, "Size");
-        assertEquals(1, current.intValue());
+        assertEquals(3, current.intValue());
+
+        // since we only send 1 message to each of the 3, we will have 0 hits and 3 misses
+        Long hits = (Long) mbeanServer.getAttribute(on, "Hits");
+        assertEquals(0, hits.longValue());
+        Long misses = (Long) mbeanServer.getAttribute(on, "Misses");
+        assertEquals(3, misses.longValue());
+
+
+        // now send a message to a and b so we have 2 hits
+        // -----------------------------------------------
+        resetMocks();
+
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        getMockEndpoint("mock:b").expectedMessageCount(1);
+        template.sendBodyAndHeader("direct:start", "Bye World", "foo", "mock:a");
+        template.sendBodyAndHeader("direct:start", "Bye World", "foo", "mock:b");
+        assertMockEndpointsSatisfied();
+
+        // since we only send 1 message to each of the 3, we will have 0 hits and 3 misses
+        hits = (Long) mbeanServer.getAttribute(on, "Hits");
+        assertEquals(2, hits.longValue());
+        misses = (Long) mbeanServer.getAttribute(on, "Misses");
+        assertEquals(3, misses.longValue());
+
+
+        // and send to mock:d to have another miss
+        // ---------------------------------------
+        resetMocks();
+
+        getMockEndpoint("mock:d").expectedMessageCount(1);
+        template.sendBodyAndHeader("direct:start", "Hi World", "foo", "mock:d");
+        assertMockEndpointsSatisfied();
 
-        String source = (String) mbeanServer.getAttribute(on, "Source");
-        assertEquals("sendTo(Endpoint[mock://result])", source);
+        // sending to d should be a miss as this is the first time
+        hits = (Long) mbeanServer.getAttribute(on, "Hits");
+        assertEquals(2, hits.longValue());
+        misses = (Long) mbeanServer.getAttribute(on, "Misses");
+        assertEquals(4, misses.longValue());
 
-        // purge
-        mbeanServer.invoke(on, "purge", null, null);
 
-        current = (Integer) mbeanServer.getAttribute(on, "Size");
-        assertEquals(0, current.intValue());
+        // reset statistics
+        // ----------------
+        mbeanServer.invoke(on, "resetStatistics", null, null);
+        hits = (Long) mbeanServer.getAttribute(on, "Hits");
+        assertEquals(0, hits.longValue());
+        misses = (Long) mbeanServer.getAttribute(on, "Misses");
+        assertEquals(0, misses.longValue());
     }
 
     @Override
@@ -72,7 +112,7 @@ public class ManagedProducerCacheTest ex
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").to("mock:result");
+                from("direct:start").recipientList().header("foo");
             }
         };
     }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUCacheTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUCacheTest.java?rev=1098601&r1=1098600&r2=1098601&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUCacheTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUCacheTest.java Mon May 
2 14:37:32 2011
@@ -44,6 +44,80 @@ public class LRUCacheTest extends TestCa
         assertSame(service2, cache.get("B"));
     }
 
+    public void testLRUCacheHitsAndMisses() {
+        MyService service1 = new MyService();
+        MyService service2 = new MyService();
+
+        cache.put("A", service1);
+        cache.put("B", service2);
+
+        assertEquals(0, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get("A");
+        assertEquals(1, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get("A");
+        assertEquals(2, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get("B");
+        assertEquals(3, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get("C");
+        assertEquals(3, cache.getHits());
+        assertEquals(1, cache.getMisses());
+
+        cache.get("D");
+        assertEquals(3, cache.getHits());
+        assertEquals(2, cache.getMisses());
+
+        cache.resetStatistics();
+        assertEquals(0, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get("B");
+        assertEquals(1, cache.getHits());
+        assertEquals(0, cache.getMisses());
+    }
+
+    public void testLRUCacheHitsAndMissesStop() throws Exception {
+        MyService service1 = new MyService();
+        MyService service2 = new MyService();
+
+        cache.put("A", service1);
+        cache.put("B", service2);
+
+        assertEquals(0, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get("A");
+        assertEquals(1, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get("A");
+        assertEquals(2, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get("B");
+        assertEquals(3, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.stop();
+        assertEquals(0, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.start();
+        assertEquals(0, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get("B");
+        assertEquals(0, cache.getHits());
+        assertEquals(1, cache.getMisses());
+    }
+
     public void testLRUCacheStop() throws Exception {
         MyService service1 = new MyService();
         MyService service2 = new MyService();

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUSoftCacheTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUSoftCacheTest.java?rev=1098601&r1=1098600&r2=1098601&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUSoftCacheTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUSoftCacheTest.java Mon May
 2 14:37:32 2011
@@ -47,6 +47,31 @@ public class LRUSoftCacheTest extends Te
         cache.stop();
     }
 
+    public void testLRUSoftCacheHitsAndMisses() throws Exception {
+        LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
+        cache.start();
+
+        cache.put(1, "foo");
+        cache.put(2, "bar");
+
+        assertEquals(0, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get(1);
+        assertEquals(1, cache.getHits());
+        assertEquals(0, cache.getMisses());
+
+        cache.get(3);
+        assertEquals(1, cache.getHits());
+        assertEquals(1, cache.getMisses());
+
+        cache.get(2);
+        assertEquals(2, cache.getHits());
+        assertEquals(1, cache.getMisses());
+
+        cache.stop();
+    }
+
     public void testLRUSoftCachePutOverride() throws Exception {
         LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
         cache.start();
@@ -204,10 +229,10 @@ public class LRUSoftCacheTest extends Te
         log.info("Cache size {}", size);
         assertTrue("Cache size should not be max, was: " + size, size < cache.getMaxCacheSize());
 
-        // should be the last keys
         List<Integer> list = new ArrayList<Integer>(cache.keySet());
         log.info("Keys: " + list);
 
+        // we cannot store all 1000 in the cache as the JVM have re-claimed some values from
the soft cache
         assertTrue("Cache size should not be max, was: " + list.size(), list.size() <
cache.getMaxCacheSize());
 
         // first key should not be 0



Mime
View raw message