[infinispan-commits] Infinispan SVN: r804 - in trunk/core/src: test/java/org/infinispan/eviction and 2 other directories.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Thu Sep 10 07:32:06 EDT 2009
Author: galder.zamarreno at jboss.com
Date: 2009-09-10 07:32:06 -0400 (Thu, 10 Sep 2009)
New Revision: 804
Added:
trunk/core/src/test/java/org/infinispan/eviction/MarshalledValuesEvictionTest.java
trunk/core/src/test/java/org/infinispan/eviction/MarshalledValuesManualEvictionTest.java
Modified:
trunk/core/src/main/java/org/infinispan/interceptors/InterceptorChain.java
trunk/core/src/test/java/org/infinispan/marshall/MarshalledValueTest.java
trunk/core/src/test/java/org/infinispan/test/TestingUtil.java
Log:
[ISPN-158] (Verify eviction and marshalled values interaction) Added tests that verify whether wrapping/unwrapping occurs under certain eviction circumstances.
Modified: trunk/core/src/main/java/org/infinispan/interceptors/InterceptorChain.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/interceptors/InterceptorChain.java 2009-09-10 01:15:57 UTC (rev 803)
+++ trunk/core/src/main/java/org/infinispan/interceptors/InterceptorChain.java 2009-09-10 11:32:06 UTC (rev 804)
@@ -40,6 +40,7 @@
* Knows how to build and manage an chain of interceptors. Also in charge with invoking methods on the chain.
*
* @author Mircea.Markus at jboss.com
+ * @author Galder Zamarreño
* @since 4.0 todo - if you add the same interceptor instance twice, things get really dirty. -- this should be treated
* as an missuse and an exception should be thrown
*/
@@ -207,6 +208,34 @@
}
return false;
}
+
+ /**
+ * Replaces an existing interceptor of the given type in the interceptor chain with a new interceptor instance passed as parameter.
+ *
+ * @param replacingInterceptor the interceptor to add to the interceptor chain
+ * @param toBeReplacedInterceptorType the type of interceptor that should be swapped with the new one
+ * @return true if the interceptor was replaced
+ */
+ public boolean replaceInterceptor(CommandInterceptor replacingInterceptor, Class<? extends CommandInterceptor> toBeReplacedInterceptorType) {
+ if (firstInChain.getClass().equals(toBeReplacedInterceptorType)) {
+ replacingInterceptor.setNext(firstInChain.getNext());
+ firstInChain = replacingInterceptor;
+ return true;
+ }
+ CommandInterceptor it = firstInChain;
+ CommandInterceptor previous = firstInChain;
+ while (it.getNext() != null) {
+ CommandInterceptor current = it.getNext();
+ if (current.getClass().equals(toBeReplacedInterceptorType)) {
+ replacingInterceptor.setNext(current.getNext());
+ previous.setNext(replacingInterceptor);
+ return true;
+ }
+ previous = current;
+ it = current;
+ }
+ return false;
+ }
/**
* Appends at the end.
Added: trunk/core/src/test/java/org/infinispan/eviction/MarshalledValuesEvictionTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/eviction/MarshalledValuesEvictionTest.java (rev 0)
+++ trunk/core/src/test/java/org/infinispan/eviction/MarshalledValuesEvictionTest.java 2009-09-10 11:32:06 UTC (rev 804)
@@ -0,0 +1,123 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and
+ * individual contributors as indicated by the @author tags. See the
+ * copyright.txt file in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.eviction;
+
+import java.io.NotSerializableException;
+
+import org.infinispan.commands.write.EvictCommand;
+import org.infinispan.config.Configuration;
+import org.infinispan.context.InvocationContext;
+import org.infinispan.interceptors.MarshalledValueInterceptor;
+import org.infinispan.manager.CacheManager;
+import org.infinispan.marshall.MarshalledValue;
+import org.infinispan.marshall.MarshalledValueTest;
+import org.infinispan.marshall.Marshaller;
+import org.infinispan.test.SingleCacheManagerTest;
+import org.infinispan.test.TestingUtil;
+import org.infinispan.test.fwk.TestCacheManagerFactory;
+import org.testng.annotations.Test;
+
+ at Test(groups = "functional", testName = "eviction.MarshalledValuesEvictionFunctionalTest")
+public class MarshalledValuesEvictionTest extends SingleCacheManagerTest {
+
+ @Override
+ protected CacheManager createCacheManager() throws Exception {
+ Configuration cfg = new Configuration();
+ cfg.setEvictionStrategy(EvictionStrategy.FIFO);
+ cfg.setEvictionWakeUpInterval(100);
+ cfg.setEvictionMaxEntries(1); // 1 max entries
+ cfg.setUseLockStriping(false); // to minimise chances of deadlock in the unit test
+ cfg.setUseLazyDeserialization(true);
+ CacheManager cm = TestCacheManagerFactory.createCacheManager(cfg);
+ cache = cm.getCache();
+ Marshaller marshaller = TestingUtil.extractComponent(cache, Marshaller.class);
+ MockMarshalledValueInterceptor interceptor = new MockMarshalledValueInterceptor(marshaller);
+ assert TestingUtil.replaceInterceptor(cache, interceptor, MarshalledValueInterceptor.class);
+ return cm;
+ }
+
+ public void testEvictCustomKeyValue() {
+ MarshalledValueTest.Pojo p1 = new MarshalledValueTest.Pojo();
+ p1.i = 64;
+ MarshalledValueTest.Pojo p2 = new MarshalledValueTest.Pojo();
+ p2.i = 24;
+ MarshalledValueTest.Pojo p3 = new MarshalledValueTest.Pojo();
+ p3.i = 97;
+ MarshalledValueTest.Pojo p4 = new MarshalledValueTest.Pojo();
+ p4.i = 35;
+
+ cache.put(p1, p2);
+ cache.put(p3, p4);
+
+ // wait for the cache size to drop to 1, up to a specified amount of time.
+ long giveupTime = System.currentTimeMillis() + (1000 * 60 * 1); // 1 mins?
+ while (cache.getAdvancedCache().getDataContainer().size() > 1 && System.currentTimeMillis() < giveupTime) {
+ TestingUtil.sleepThread(100);
+ }
+
+ MockMarshalledValueInterceptor interceptor = (MockMarshalledValueInterceptor) TestingUtil.findInterceptor(cache, MarshalledValueInterceptor.class);
+ assert !interceptor.marshalledValueCreated;
+ }
+
+ public void testEvictPrimitiveKeyCustomValue() {
+ MarshalledValueTest.Pojo p1 = new MarshalledValueTest.Pojo();
+ p1.i = 51;
+ MarshalledValueTest.Pojo p2 = new MarshalledValueTest.Pojo();
+ p2.i = 78;
+
+ cache.put("key-isoprene", p1);
+ cache.put("key-hexastyle", p2);
+
+ // wait for the cache size to drop to 1, up to a specified amount of time.
+ long giveupTime = System.currentTimeMillis() + (1000 * 60 * 1); // 1 mins?
+ while (cache.getAdvancedCache().getDataContainer().size() > 1 && System.currentTimeMillis() < giveupTime) {
+ TestingUtil.sleepThread(100);
+ }
+
+ MockMarshalledValueInterceptor interceptor = (MockMarshalledValueInterceptor) TestingUtil.findInterceptor(cache, MarshalledValueInterceptor.class);
+ assert !interceptor.marshalledValueCreated;
+ }
+
+ static class MockMarshalledValueInterceptor extends MarshalledValueInterceptor {
+ boolean marshalledValueCreated;
+
+ MockMarshalledValueInterceptor(Marshaller marshaller) {
+ injectMarshaller(marshaller);
+ }
+
+ @Override
+ protected MarshalledValue createMarshalledValue(Object toWrap, InvocationContext ctx)
+ throws NotSerializableException {
+ marshalledValueCreated = true;
+ return super.createMarshalledValue(toWrap, ctx);
+ }
+
+ @Override
+ public Object visitEvictCommand(InvocationContext ctx, EvictCommand command) throws Throwable {
+ // Reset value so that changes due to invocation can be asserted
+ if (marshalledValueCreated) marshalledValueCreated = false;
+ return super.visitEvictCommand(ctx, command);
+ }
+ }
+
+}
Added: trunk/core/src/test/java/org/infinispan/eviction/MarshalledValuesManualEvictionTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/eviction/MarshalledValuesManualEvictionTest.java (rev 0)
+++ trunk/core/src/test/java/org/infinispan/eviction/MarshalledValuesManualEvictionTest.java 2009-09-10 11:32:06 UTC (rev 804)
@@ -0,0 +1,84 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and
+ * individual contributors as indicated by the @author tags. See the
+ * copyright.txt file in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.eviction;
+
+import org.infinispan.config.Configuration;
+import org.infinispan.eviction.MarshalledValuesEvictionTest.MockMarshalledValueInterceptor;
+import org.infinispan.interceptors.MarshalledValueInterceptor;
+import org.infinispan.manager.CacheManager;
+import org.infinispan.marshall.MarshalledValueTest;
+import org.infinispan.marshall.Marshaller;
+import org.infinispan.test.SingleCacheManagerTest;
+import org.infinispan.test.TestingUtil;
+import org.infinispan.test.fwk.TestCacheManagerFactory;
+import org.testng.annotations.Test;
+
+ at Test(groups = "functional", testName = "eviction.MarshalledValuesEvictionFunctionalTest")
+public class MarshalledValuesManualEvictionTest extends SingleCacheManagerTest {
+
+ @Override
+ protected CacheManager createCacheManager() throws Exception {
+ Configuration cfg = new Configuration();
+ cfg.setUseLockStriping(false); // to minimise chances of deadlock in the unit test
+ cfg.setUseLazyDeserialization(true);
+ CacheManager cm = TestCacheManagerFactory.createCacheManager(cfg);
+ cache = cm.getCache();
+ Marshaller marshaller = TestingUtil.extractComponent(cache, Marshaller.class);
+ MockMarshalledValueInterceptor interceptor = new MockMarshalledValueInterceptor(marshaller);
+ assert TestingUtil.replaceInterceptor(cache, interceptor, MarshalledValueInterceptor.class);
+ return cm;
+ }
+
+ public void testManualEvictCustomKeyValue() {
+ MarshalledValueTest.Pojo p1 = new MarshalledValueTest.Pojo();
+ p1.i = 64;
+ MarshalledValueTest.Pojo p2 = new MarshalledValueTest.Pojo();
+ p2.i = 24;
+ MarshalledValueTest.Pojo p3 = new MarshalledValueTest.Pojo();
+ p3.i = 97;
+ MarshalledValueTest.Pojo p4 = new MarshalledValueTest.Pojo();
+ p4.i = 35;
+
+ cache.put(p1, p2);
+ cache.put(p3, p4);
+ cache.evict(p1);
+
+ MockMarshalledValueInterceptor interceptor = (MockMarshalledValueInterceptor) TestingUtil.findInterceptor(cache, MarshalledValueInterceptor.class);
+ assert interceptor.marshalledValueCreated;
+ }
+
+ public void testEvictPrimitiveKeyCustomValue() {
+ MarshalledValueTest.Pojo p1 = new MarshalledValueTest.Pojo();
+ p1.i = 51;
+ MarshalledValueTest.Pojo p2 = new MarshalledValueTest.Pojo();
+ p2.i = 78;
+
+ cache.put("key-isoprene", p1);
+ cache.put("key-hexastyle", p2);
+ cache.evict("key-isoprene");
+
+ MockMarshalledValueInterceptor interceptor = (MockMarshalledValueInterceptor) TestingUtil.findInterceptor(cache, MarshalledValueInterceptor.class);
+ assert !interceptor.marshalledValueCreated;
+ }
+
+}
Modified: trunk/core/src/test/java/org/infinispan/marshall/MarshalledValueTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/marshall/MarshalledValueTest.java 2009-09-10 01:15:57 UTC (rev 803)
+++ trunk/core/src/test/java/org/infinispan/marshall/MarshalledValueTest.java 2009-09-10 11:32:06 UTC (rev 804)
@@ -471,7 +471,7 @@
}
public static class Pojo implements Externalizable {
- int i;
+ public int i;
boolean b;
static int serializationCount, deserializationCount;
final Log log = LogFactory.getLog(Pojo.class);
Modified: trunk/core/src/test/java/org/infinispan/test/TestingUtil.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/test/TestingUtil.java 2009-09-10 01:15:57 UTC (rev 803)
+++ trunk/core/src/test/java/org/infinispan/test/TestingUtil.java 2009-09-10 11:32:06 UTC (rev 804)
@@ -484,7 +484,7 @@
* @param cache cache that needs to be altered
* @param interceptor the first interceptor in the new chain.
*/
- public static void replaceInterceptorChain(Cache<?, ?> cache, CommandInterceptor interceptor) {
+ public static void replaceInterceptorChain(Cache cache, CommandInterceptor interceptor) {
ComponentRegistry cr = extractComponentRegistry(cache);
// make sure all interceptors here are wired.
CommandInterceptor i = interceptor;
@@ -498,6 +498,25 @@
}
/**
+ * Replaces an existing interceptor of the given type in the interceptor chain with a new interceptor instance passed as parameter.
+ *
+ * @param replacingInterceptor the interceptor to add to the interceptor chain
+ * @param toBeReplacedInterceptorType the type of interceptor that should be swapped with the new one
+ * @return true if the interceptor was replaced
+ */
+ public static boolean replaceInterceptor(Cache cache, CommandInterceptor replacingInterceptor, Class<? extends CommandInterceptor> toBeReplacedInterceptorType) {
+ ComponentRegistry cr = extractComponentRegistry(cache);
+ // make sure all interceptors here are wired.
+ CommandInterceptor i = replacingInterceptor;
+ do {
+ cr.wireDependencies(i);
+ }
+ while ((i = i.getNext()) != null);
+ InterceptorChain inch = cr.getComponent(InterceptorChain.class);
+ return inch.replaceInterceptor(replacingInterceptor, toBeReplacedInterceptorType);
+ }
+
+ /**
* Retrieves the remote delegate for a given cache. It is on this remote delegate that the JGroups RPCDispatcher
* invokes remote methods.
*
More information about the infinispan-commits
mailing list