Author: manik.surtani(a)jboss.com
Date: 2009-02-09 16:28:30 -0500 (Mon, 09 Feb 2009)
New Revision: 7671
Modified:
core/branches/flat/src/test/java/org/horizon/eviction/EvictionFunctionalTest.java
Log:
Improved test
Modified:
core/branches/flat/src/test/java/org/horizon/eviction/EvictionFunctionalTest.java
===================================================================
---
core/branches/flat/src/test/java/org/horizon/eviction/EvictionFunctionalTest.java 2009-02-09
18:43:20 UTC (rev 7670)
+++
core/branches/flat/src/test/java/org/horizon/eviction/EvictionFunctionalTest.java 2009-02-09
21:28:30 UTC (rev 7671)
@@ -2,27 +2,26 @@
import org.horizon.Cache;
import org.horizon.config.Configuration;
+import org.horizon.config.EvictionAlgorithmConfig;
import org.horizon.config.EvictionConfig;
+import org.horizon.container.DataContainer;
import org.horizon.eviction.algorithms.fifo.FIFOAlgorithmConfig;
+import org.horizon.eviction.events.EvictionEvent;
import org.horizon.manager.CacheManager;
import org.horizon.manager.DefaultCacheManager;
-import org.horizon.notifications.Listener;
-import org.horizon.notifications.cachelistener.annotation.CacheEntryEvicted;
-import org.horizon.notifications.cachelistener.event.CacheEntryEvictedEvent;
+import org.horizon.util.ReflectionUtil;
import org.horizon.util.TestingUtil;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
-@Test(groups = "functional", sequential = true, enabled = true)
+@Test(groups = "functional", sequential = true)
public class EvictionFunctionalTest {
Cache cache;
- EvictionListener el;
@BeforeMethod
public void setUp() {
@@ -36,8 +35,6 @@
ec.setWakeUpInterval(50); // 50 millis!
CacheManager cm = new DefaultCacheManager(cfg);
cache = cm.getCache();
- el = new EvictionListener();
- cache.addListener(el);
} catch (Exception e) {
e.printStackTrace();
}
@@ -49,33 +46,78 @@
}
public void testEviction() throws InterruptedException {
- el.latch = new CountDownLatch(1);
+ CountDownLatch evictionCompleteLatch = new CountDownLatch(1);
+ CountDownLatch cacheFillLatch = new CountDownLatch(1);
+ EvictionManager em = cache.getAdvancedCache().getEvictionManager();
+ EvictionAlgorithm actualAlgorithm = (EvictionAlgorithm)
TestingUtil.extractField(em, "evictionAlgorithm");
+ DelegatingAlgorithm a = new DelegatingAlgorithm(actualAlgorithm, cacheFillLatch,
evictionCompleteLatch);
+ ReflectionUtil.setValue(em, "evictionAlgorithm", a);
+
for (int i = 0; i < 20; i++) {
cache.put(i, "value");
}
+ cacheFillLatch.countDown();
- assert el.latch.await(60, TimeUnit.SECONDS);
- for (int i = 10; i < 20; i++) assert cache.containsKey(i);
+ assert evictionCompleteLatch.await(600, TimeUnit.SECONDS);
+ for (int i = 10; i < 20; i++)
+ assert cache.get(i).equals("value") : "Key " + i + "
should map to 'value', but was " + cache.get(i);
- for (int i = 0; i < 10; i++) assert !cache.containsKey(i);
+ for (int i = 0; i < 10; i++)
+ assert cache.get(i) == null :
+ "Key " + i + " should map to null, but was " +
cache.get(i) + " and cache count is " + cache.size();
}
- @Listener
- public static class EvictionListener {
- Set<Integer> expectedKeys = new HashSet<Integer>();
- CountDownLatch latch;
+ public class DelegatingAlgorithm implements EvictionAlgorithm {
+ EvictionAlgorithm delegate;
+ CountDownLatch cacheFillLatch;
+ CountDownLatch evictionOverLatch;
- public EvictionListener() {
- for (int i = 0; i < 10; i++) expectedKeys.add(i);
+ public DelegatingAlgorithm(EvictionAlgorithm delegate, CountDownLatch
cacheFillLatch, CountDownLatch evictionOverLatch) {
+ this.delegate = delegate;
+ this.cacheFillLatch = cacheFillLatch;
+ this.evictionOverLatch = evictionOverLatch;
}
- @CacheEntryEvicted
- public void handle(CacheEntryEvictedEvent event) {
- System.out.println("Saw event " + event);
- if (event.isPre()) {
- assert expectedKeys.remove(event.getKey());
- if (expectedKeys.isEmpty()) latch.countDown();
+ public void process(BlockingQueue<EvictionEvent> queue) throws
EvictionException {
+ try {
+ cacheFillLatch.await();
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
}
+ System.out.println("Queue size=" + queue.size());
+
+ delegate.process(queue);
+
+ // queue should now have processed
+ evictionOverLatch.countDown();
}
+
+ public void resetEvictionQueue() {
+ delegate.resetEvictionQueue();
+ }
+
+ public void setEvictionAction(EvictionAction evictionAction) {
+ delegate.setEvictionAction(evictionAction);
+ }
+
+ public void init(Cache<?, ?> cache, DataContainer<?, ?> dataContiner,
EvictionAlgorithmConfig evictionAlgorithmConfig) {
+ delegate.init(cache, dataContiner, evictionAlgorithmConfig);
+ }
+
+ public boolean canIgnoreEvent(EvictionEvent.Type eventType) {
+ return delegate.canIgnoreEvent(eventType);
+ }
+
+ public Class<? extends EvictionAlgorithmConfig> getConfigurationClass() {
+ return delegate.getConfigurationClass();
+ }
+
+ public void start() {
+ delegate.start();
+ }
+
+ public void stop() {
+ delegate.stop();
+ }
}
}
Show replies by date