You are viewing a plain text version of this content. The canonical link for it is here.
Posted to wadi-commits@incubator.apache.org by bd...@apache.org on 2005/12/14 23:36:16 UTC
svn commit: r356933 [19/35] - in /incubator/wadi/trunk: ./ etc/ modules/
modules/assembly/ modules/assembly/src/ modules/assembly/src/bin/
modules/assembly/src/conf/ modules/assembly/src/main/
modules/assembly/src/main/assembly/ modules/core/ modules/c...
Added: incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestAttributes.java
URL: http://svn.apache.org/viewcvs/incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestAttributes.java?rev=356933&view=auto
==============================================================================
--- incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestAttributes.java (added)
+++ incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestAttributes.java Wed Dec 14 15:32:56 2005
@@ -0,0 +1,398 @@
+/**
+ *
+ * Copyright 2003-2005 Core Developers Network Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.codehaus.wadi.test;
+
+import java.io.IOException;
+import java.io.Serializable;
+
+import javax.servlet.http.HttpSessionActivationListener;
+import javax.servlet.http.HttpSessionEvent;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.codehaus.wadi.Streamer;
+import org.codehaus.wadi.StreamerConfig;
+import org.codehaus.wadi.Value;
+import org.codehaus.wadi.ValueFactory;
+import org.codehaus.wadi.ValueHelper;
+import org.codehaus.wadi.ValuePool;
+import org.codehaus.wadi.impl.DistributableValue;
+import org.codehaus.wadi.impl.DistributableValueFactory;
+import org.codehaus.wadi.impl.SimpleStreamer;
+import org.codehaus.wadi.impl.SimpleValuePool;
+import org.codehaus.wadi.impl.Utils;
+
+import junit.framework.TestCase;
+
+/**
+ * Test Attribute and Attributes classes and subclasses...
+ *
+ * @author <a href="mailto:jules@coredevelopers.net">Jules Gosnell</a>
+ * @version $Revision: 1.3 $
+ */
+
+public class TestAttributes extends TestCase {
+ protected Log _log = LogFactory.getLog(getClass());
+
+ /*
+ * @see TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /*
+ * @see TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ /**
+ * Constructor for TestDistribution.
+ * @param name
+ */
+ public TestAttributes(String name) {
+ super(name);
+ }
+
+ static class NotSerializable {
+
+ final String _content;
+ public NotSerializable(String content) {_content=content;}
+
+ public boolean equals(Object that) {
+ return this==that || (that instanceof NotSerializable && safeEquals(this._content, ((NotSerializable)that)._content));
+ }
+ }
+
+ static class IsSerializable implements Serializable {
+ String _content;
+ public IsSerializable(){/* empty */} // for Serialising...
+ public IsSerializable(String content){_content=content;} // for Helper
+
+ private Object readResolve() {
+ return new NotSerializable(_content);
+ }
+}
+
+ static class NotSerializableHelper implements ValueHelper {
+ public Serializable replace(Object object) {return new IsSerializable(((NotSerializable)object)._content);}
+ }
+
+ public void testAttribute() throws Exception {
+ ValueFactory factory=new DistributableValueFactory();
+ ValuePool pool=new SimpleValuePool(factory);
+
+ Value a=pool.take(null);
+ // test get/set
+ assertTrue(a.getValue()==null);
+ String foo="foo";
+ a.setValue(foo);
+ assertTrue(a.getValue()==foo);
+ a.setValue(null);
+ assertTrue(a.getValue()==null);
+ pool.put(a);
+
+ // test serialisation with various values
+ {
+ DistributableValue attr1=(DistributableValue)pool.take(null);
+ DistributableValue attr2=(DistributableValue)pool.take(null);
+ testAttributeSerialisation(attr1, attr2, null);
+ pool.put(attr1);
+ pool.put(attr2);
+ }
+ {
+ DistributableValue attr1=(DistributableValue)pool.take(null);
+ DistributableValue attr2=(DistributableValue)pool.take(null);
+ testAttributeSerialisation(new DistributableValue(null), new DistributableValue(null), foo);
+ pool.put(attr1);
+ pool.put(attr2);
+ }
+
+// FIXME
+
+// // try using a Helper
+// DistributableValue.registerHelper(NotSerializable.class, new NotSerializableHelper());
+// {
+// DistributableValue attr1=(DistributableValue)pool.take(null);
+// DistributableValue attr2=(DistributableValue)pool.take(null);
+// testAttributeSerialisation(new DistributableValue(null), new DistributableValue(null), new NotSerializable(foo));
+// pool.put(attr1);
+// pool.put(attr2);
+// }
+//
+// // try without the Helper
+// assertTrue(DistributableValue.deregisterHelper(NotSerializable.class));
+// assertTrue(!DistributableValue.deregisterHelper(NotSerializable.class)); // can't be removed twice
+// {
+// DistributableValue attr1=(DistributableValue)pool.take(null);
+// DistributableValue attr2=(DistributableValue)pool.take(null);
+// try {
+// testAttributeSerialisation(attr1, attr2, new NotSerializable(foo));
+// assertTrue(false); // not expected
+// } catch (NotSerializableException ignore) {
+// // expected
+// }
+// pool.put(attr1);
+// pool.put(attr2);
+// }
+ }
+
+
+ static class ActivationListener implements HttpSessionActivationListener, Serializable {
+
+ protected double _content=Math.random();
+ public boolean equals(Object that) {
+ return this==that || (that instanceof ActivationListener && this._content==((ActivationListener)that)._content);
+ }
+
+ int _activations;
+ int _passivations;
+
+ public void sessionDidActivate(HttpSessionEvent se){_activations++;}
+ public void sessionWillPassivate(HttpSessionEvent se){_passivations++;}
+
+ }
+
+// public void testActivatableAttribute() throws Exception {
+// ActivationListener al=new ActivationListener();
+// ValueFactory factory=new DistributableValueFactory();
+// ValuePool pool=new SimpleValuePool(factory);
+// DistributableValue attr1=(DistributableValue)pool.take(null);
+// DistributableValue attr2=(DistributableValue)pool.take(null);
+// testAttributeSerialisation(attr1, attr2, al);
+// _log.info("passivations: "+al._passivations);
+// assertTrue(al._passivations==1);
+// _log.info("activations: "+al._activations);
+// assertTrue(al._activations==1);
+// pool.put(attr1);
+// pool.put(attr2);
+// }
+
+ public void testAttributeSerialisation(DistributableValue a, DistributableValue b, Object s) throws Exception {
+ Streamer streamer=new SimpleStreamer();
+ streamer.init(new StreamerConfig(){public ClassLoader getClassLoader() {return getClass().getClassLoader();}});
+ a.setValue(s);
+ byte[] bytes=Utils.getContent(a, streamer);
+ assertTrue(a.getValue()==s); // activation
+ Utils.setContent(b, bytes, streamer);
+ assertTrue(safeEquals(b.getValue(), s)); // activation
+ assertTrue(safeEquals(b.getValue(), s)); // do this twice to show that activation only occurs once (for this Attribute)
+ }
+
+ public static boolean safeEquals(Object a, Object b) {
+ if (a==null)
+ return b==null;
+ else
+ return a.equals(b);
+ }
+
+ public void testAtomicAttributes() throws Exception {
+ //AttributesConfig config=null;
+// AttributesFactory factory=new
+ }
+
+ public static int _serialisations=0;
+ public static int _deserialisations=0;
+
+ public static class Counter implements Serializable {
+
+ double _random=Math.random();
+
+ public boolean equals(Object that) {
+ return (that==this) || ((that instanceof Counter) && ((Counter)that)._random==this._random);
+ }
+
+ private void writeObject(java.io.ObjectOutputStream out) throws IOException {
+ _serialisations++;
+ out.defaultWriteObject();
+ }
+
+ private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
+ in.defaultReadObject();
+ _deserialisations++;
+ }
+ }
+
+// public void testAttributes() throws Exception {
+// Dirtier dirtier=new WriteDirtier();
+// StreamingStrategy streamer=new SimpleStreamingStrategy();
+// boolean evictObjectRepASAP=false;
+// boolean evictByteRepASAP=false;
+//
+// evictObjectRepASAP=false;
+// evictByteRepASAP=false;
+// testSerialisation(new WholeAttributes(dirtier, streamer, evictObjectRepASAP, evictByteRepASAP), evictObjectRepASAP, evictByteRepASAP, false);
+// evictObjectRepASAP=true;
+// evictByteRepASAP=false;
+// testSerialisation(new WholeAttributes(dirtier, streamer, evictObjectRepASAP, evictByteRepASAP), evictObjectRepASAP, evictByteRepASAP, false);
+// evictObjectRepASAP=false;
+// evictByteRepASAP=true;
+// testSerialisation(new WholeAttributes(dirtier, streamer, evictObjectRepASAP, evictByteRepASAP), evictObjectRepASAP, evictByteRepASAP, false);
+// evictObjectRepASAP=true;
+// evictByteRepASAP=true;
+// testSerialisation(new WholeAttributes(dirtier, streamer, evictObjectRepASAP, evictByteRepASAP), evictObjectRepASAP, evictByteRepASAP, false);
+//
+// // dirtier=new ReadWriteDirtier();
+// // evictObjectRepASAP=false;
+// // evictByteRepASAP=false;
+// // testSerialisation(new WholeAttributesWrapper(new SimpleAttributes(), dirtier, streamer, evictObjectRepASAP, evictByteRepASAP), evictObjectRepASAP, evictByteRepASAP, true);
+// // evictObjectRepASAP=true;
+// // evictByteRepASAP=false;
+// // testSerialisation(new WholeAttributesWrapper(new SimpleAttributes(), dirtier, streamer, evictObjectRepASAP, evictByteRepASAP), evictObjectRepASAP, evictByteRepASAP, true);
+// // evictObjectRepASAP=false;
+// // evictByteRepASAP=true;
+// // testSerialisation(new WholeAttributesWrapper(new SimpleAttributes(), dirtier, streamer, evictObjectRepASAP, evictByteRepASAP), evictObjectRepASAP, evictByteRepASAP, true);
+// // evictObjectRepASAP=true;
+// // evictByteRepASAP=true;
+// // testSerialisation(new WholeAttributesWrapper(new SimpleAttributes(), dirtier, streamer, evictObjectRepASAP, evictByteRepASAP), evictObjectRepASAP, evictByteRepASAP, true);
+// }
+
+// public void testWholeAttributes() throws Exception {
+// testAttributes(new SimpleAttributes());
+// testAttributes(new SimpleAttributes());
+// testAttributes(new SimpleAttributes());
+// testAttributes(new SimpleAttributes());
+// testAttributes(new SimpleAttributes());
+// testAttributes(new SimpleAttributes());
+// testAttributes(new SimpleAttributes());
+// testAttributes(new SimpleAttributes());
+//
+// testAttributes(new WholeAttributes(new WriteDirtier(), new SimpleStreamingStrategy(), false, false));
+// testAttributes(new WholeAttributes(new WriteDirtier(), new SimpleStreamingStrategy(), true, false));
+// testAttributes(new WholeAttributes(new WriteDirtier(), new SimpleStreamingStrategy(), false, true));
+// testAttributes(new WholeAttributes(new WriteDirtier(), new SimpleStreamingStrategy(), true, true));
+// testAttributes(new WholeAttributes(new ReadWriteDirtier(), new SimpleStreamingStrategy(), false, false));
+// testAttributes(new WholeAttributes(new ReadWriteDirtier(), new SimpleStreamingStrategy(), true, false));
+// testAttributes(new WholeAttributes(new ReadWriteDirtier(), new SimpleStreamingStrategy(), false, true));
+// testAttributes(new WholeAttributes(new ReadWriteDirtier(), new SimpleStreamingStrategy(), true, true));
+//
+// testAttributes(new PartAttributes(new WriteDirtier(), new SimpleStreamingStrategy(), false, false));
+// testAttributes(new PartAttributes(new WriteDirtier(), new SimpleStreamingStrategy(), true, false));
+// testAttributes(new PartAttributes(new WriteDirtier(), new SimpleStreamingStrategy(), false, true));
+// testAttributes(new PartAttributes(new WriteDirtier(), new SimpleStreamingStrategy(), true, true));
+// testAttributes(new PartAttributes(new ReadWriteDirtier(), new SimpleStreamingStrategy(), false, false));
+// testAttributes(new PartAttributes(new ReadWriteDirtier(), new SimpleStreamingStrategy(), true, false));
+// testAttributes(new PartAttributes(new ReadWriteDirtier(), new SimpleStreamingStrategy(), false, true));
+// testAttributes(new PartAttributes(new ReadWriteDirtier(), new SimpleStreamingStrategy(), true, true));
+//
+// }
+
+// public void testAttributes(Attributes a) throws Exception {
+// // can we serialise an empty instance OK ?
+// byte[] bytes=a.getBytes();
+// // and then put the result back in ?
+// a.setBytes(bytes);
+//
+// String key="foo";
+// Object val=key;
+// a.put(key, val);
+// assertTrue(a.get(key).equals(val));
+// bytes=a.getBytes();
+// a.remove(key);
+// assertTrue(a.get(key)==null);
+// a.setBytes(bytes);
+// assertTrue(a.get(key).equals(val));
+// }
+//
+// public void testSerialisation(Attributes wrapper, boolean evictObjectRepASAP, boolean evictByteRepASAP, boolean readIsDirty) throws Exception {
+// String key="foo";
+// Counter val=new Counter();
+// int serialisations=0;
+// _serialisations=serialisations;
+// int deserialisations=0;
+// _deserialisations=deserialisations;
+// // check initial state
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// // insert into container and reinspect - expect no change
+// wrapper.put(key, val);
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// // can we retrieve the same reference ?
+// assertTrue(val==wrapper.get(key));
+// // try serialising container - should serialise content..
+// byte[] bytes=wrapper.getBytes();
+// serialisations++;
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// // serialise container again - should used cached serialised content
+// bytes=wrapper.getBytes();
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// // can we still retrieve the original reference ?
+// if (evictObjectRepASAP) {
+// assertTrue(val.equals(wrapper.get(key)));
+// deserialisations++;
+// } else {
+// assertTrue(val==wrapper.get(key));
+// }
+// // did this last operation affect effect the content ?
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// // serialise the container again - should still not alter contents ...
+// bytes=wrapper.getBytes();
+// if (evictObjectRepASAP && evictByteRepASAP)
+// serialisations++;
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// // reinsert content, should invalidate serialised cache...
+// wrapper.put(key, val);
+// assertTrue(val==wrapper.get(key));
+// if (evictObjectRepASAP && evictByteRepASAP)
+// deserialisations++;
+// assertTrue(_serialisations==serialisations);
+// _log.info(""+_deserialisations+"=="+deserialisations);
+// assertTrue(_deserialisations==deserialisations);
+// bytes=wrapper.getBytes();
+// serialisations++;
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+//
+// // Looks good - now let's try deserialising...
+//
+// // populate the container - should not change old content state...
+// wrapper.setBytes(bytes);
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// // retrieve content - should cause deserialisation
+// val=(Counter)wrapper.get(key);
+// deserialisations++;
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// // retrieve content again - should be found in cache - no deserialisation...
+// val=(Counter)wrapper.get(key);
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// // reinitialise content - should invalidate object cache...
+// bytes=wrapper.getBytes();
+// if (evictByteRepASAP) {
+// serialisations++;
+// }
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// wrapper.setBytes(bytes);
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// // reretrieve content - should cause fresh deserialisation
+// val=(Counter)wrapper.get(key);
+// deserialisations++;
+// assertTrue(_serialisations==serialisations);
+// assertTrue(_deserialisations==deserialisations);
+// }
+}
Added: incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestContextualiser.java
URL: http://svn.apache.org/viewcvs/incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestContextualiser.java?rev=356933&view=auto
==============================================================================
--- incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestContextualiser.java (added)
+++ incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestContextualiser.java Wed Dec 14 15:32:56 2005
@@ -0,0 +1,640 @@
+/**
+ *
+ * Copyright 2003-2005 Core Developers Network Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.codehaus.wadi.test;
+
+import java.io.File;
+import java.io.Serializable;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.sql.DataSource;
+
+import junit.framework.TestCase;
+
+import org.activecluster.ClusterFactory;
+import org.activemq.ActiveMQConnectionFactory;
+import org.activemq.broker.impl.BrokerContainerFactoryImpl;
+import org.activemq.store.vm.VMPersistenceAdapter;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.axiondb.jdbc.AxionDataSource;
+import org.codehaus.wadi.AttributesFactory;
+import org.codehaus.wadi.Collapser;
+import org.codehaus.wadi.Context;
+import org.codehaus.wadi.ContextPool;
+import org.codehaus.wadi.Contextualiser;
+import org.codehaus.wadi.Emoter;
+import org.codehaus.wadi.Evictable;
+import org.codehaus.wadi.Evicter;
+import org.codehaus.wadi.Immoter;
+import org.codehaus.wadi.InvocationContext;
+import org.codehaus.wadi.InvocationException;
+import org.codehaus.wadi.InvocationProxy;
+import org.codehaus.wadi.Location;
+import org.codehaus.wadi.Motable;
+import org.codehaus.wadi.PoolableInvocationWrapperPool;
+import org.codehaus.wadi.ProxiedLocation;
+import org.codehaus.wadi.ProxyingException;
+import org.codehaus.wadi.Relocater;
+import org.codehaus.wadi.Router;
+import org.codehaus.wadi.Session;
+import org.codehaus.wadi.SessionIdFactory;
+import org.codehaus.wadi.SessionPool;
+import org.codehaus.wadi.SessionWrapperFactory;
+import org.codehaus.wadi.Streamer;
+import org.codehaus.wadi.ValuePool;
+import org.codehaus.wadi.gridstate.Dispatcher;
+import org.codehaus.wadi.gridstate.PartitionManager;
+import org.codehaus.wadi.gridstate.activecluster.ActiveClusterDispatcher;
+import org.codehaus.wadi.gridstate.activecluster.CustomCluster;
+import org.codehaus.wadi.gridstate.activecluster.CustomClusterFactory;
+import org.codehaus.wadi.gridstate.impl.DummyPartitionManager;
+import org.codehaus.wadi.http.HTTPProxiedLocation;
+import org.codehaus.wadi.impl.AbsoluteEvicter;
+import org.codehaus.wadi.impl.AbstractContextualiser;
+import org.codehaus.wadi.impl.ClusterContextualiser;
+import org.codehaus.wadi.impl.ClusteredManager;
+import org.codehaus.wadi.impl.DatabaseStore;
+import org.codehaus.wadi.impl.DistributableAttributesFactory;
+import org.codehaus.wadi.impl.DistributableSessionFactory;
+import org.codehaus.wadi.impl.DistributableValueFactory;
+import org.codehaus.wadi.impl.DummyContextualiser;
+import org.codehaus.wadi.impl.DummyDistributableContextualiserConfig;
+import org.codehaus.wadi.impl.DummyEvicter;
+import org.codehaus.wadi.impl.DummyHttpServletRequest;
+import org.codehaus.wadi.impl.DummyReplicaterFactory;
+import org.codehaus.wadi.impl.DummyRouter;
+import org.codehaus.wadi.impl.ExclusiveStoreContextualiser;
+import org.codehaus.wadi.impl.GZIPStreamer;
+import org.codehaus.wadi.impl.HashingCollapser;
+import org.codehaus.wadi.impl.MemoryContextualiser;
+import org.codehaus.wadi.impl.NeverEvicter;
+import org.codehaus.wadi.impl.SerialContextualiser;
+import org.codehaus.wadi.impl.SessionToContextPoolAdapter;
+import org.codehaus.wadi.impl.SharedStoreContextualiser;
+import org.codehaus.wadi.impl.SimpleEvictable;
+import org.codehaus.wadi.impl.SimpleSessionPool;
+import org.codehaus.wadi.impl.SimpleStreamer;
+import org.codehaus.wadi.impl.SimpleValuePool;
+import org.codehaus.wadi.impl.StandardAttributesFactory;
+import org.codehaus.wadi.impl.StandardHttpProxy;
+import org.codehaus.wadi.impl.StandardManager;
+import org.codehaus.wadi.impl.StandardSessionFactory;
+import org.codehaus.wadi.impl.StandardSessionWrapperFactory;
+import org.codehaus.wadi.impl.StandardValueFactory;
+import org.codehaus.wadi.impl.TomcatSessionIdFactory;
+import org.codehaus.wadi.impl.Utils;
+import org.codehaus.wadi.impl.WebHybridRelocater;
+import org.codehaus.wadi.impl.WebInvocationContext;
+
+import EDU.oswego.cs.dl.util.concurrent.NullSync;
+import EDU.oswego.cs.dl.util.concurrent.Sync;
+
+/**
+ * Test various Contualisers, evicters etc...
+ *
+ * @author <a href="mailto:jules@coredevelopers.net">Jules Gosnell</a>
+ * @version $Revision: 1.21 $
+ */
+public class TestContextualiser extends TestCase {
+ protected Log _log = LogFactory.getLog(getClass());
+ protected DataSource _ds=new AxionDataSource("jdbc:axiondb:testdb"); // db springs into existance in-vm beneath us
+ protected String _table="MyTable";
+ protected DatabaseStore _store=new DatabaseStore("jdbc:axiondb:testdb", _ds, _table, false, false, false);
+ protected final String _clusterUri=Utils.getClusterUri();
+ protected final String _clusterName="WADI.TEST";
+
+ protected final HttpServletRequest _request=new DummyHttpServletRequest();
+ protected final PoolableInvocationWrapperPool _requestPool=new MyDummyHttpServletRequestWrapperPool();
+
+ protected final InvocationProxy _httpProxy=new StandardHttpProxy("jsessionid");
+ protected ProxiedLocation _location;
+
+ /*
+ * @see TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ _log.info("starting ...");
+ _location=new HTTPProxiedLocation(
+ new InetSocketAddress(InetAddress.getLocalHost(), 8888));
+ _store.init();
+ _dir.mkdir();
+ }
+
+ /*
+ * @see TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ _dir.delete();
+ _store.destroy();
+ super.tearDown();
+ _log.info("...stopped");
+ }
+
+ /**
+ * Constructor for TestContextualiser.
+ * @param arg0
+ */
+ public TestContextualiser(String arg0) {
+ super(arg0);
+ }
+
+ // test how interning works - we will use locking of keys to ensure that concurrent loading of same session does not occur...
+
+ public void testIntern() {
+ String s1="foo"; // automatically interned
+
+ String s2=s1.intern();
+ assertTrue(s2==s1);
+
+ String s3=s1.intern();
+ assertTrue(s3==s2);
+
+ String s4="fo"+"o"; // automatically interned
+ assertTrue(s4==s3);
+
+ String s5=new StringBuffer("fo").append("o").toString(); // NOT interned
+ assertTrue(s5!=s4);
+ s5=s5.intern();
+ assertTrue(s5==s4); // now it is
+
+ String s6="-foo-".substring(1, 4); // NOT interned
+ assertTrue(s6!=s5);
+
+ // etc...
+ }
+
+ class MyFilterChain
+ implements FilterChain
+ {
+ public void
+ doFilter(ServletRequest request, ServletResponse response) {
+ _log.info("invoking FilterChain...");
+ }
+ }
+
+ protected final Evicter _dummyEvicter=new DummyEvicter();
+ protected final String _nodeName="node0";
+
+ public void testExclusivePromotion() throws Exception {
+ Map d2=new HashMap();
+ ExclusiveStoreContextualiser disc2=new ExclusiveStoreContextualiser(_dummyContextualiser, _collapser, true, _dummyEvicter, d2, _streamer, _dir);
+ Map d1=new HashMap();
+ ExclusiveStoreContextualiser disc1=new ExclusiveStoreContextualiser(disc2, _collapser, true, _dummyEvicter, d1, _streamer, _dir);
+ Map m=new HashMap();
+ Contextualiser serial=new SerialContextualiser(disc1, _collapser, m);
+ Contextualiser memory=new MemoryContextualiser(serial, _dummyEvicter, m, _streamer, _distributableContextPool, _requestPool);
+ PartitionManager partitionManager=new DummyPartitionManager(24);
+ Dispatcher dispatcher=new ActiveClusterDispatcher(_nodeName, _clusterName, _clusterUri, 5000L);
+ StandardManager manager=new ClusteredManager(_distributableSessionPool, _distributableAttributesFactory, _distributableValuePool, _sessionWrapperFactory, _sessionIdFactory, memory, m, _router, true, _streamer, _accessOnLoad, new DummyReplicaterFactory(), _location, _httpProxy, dispatcher, partitionManager, _collapser);
+ manager.init(new DummyManagerConfig());
+
+ {
+ // place a "baz" item onto second local disc
+ String id="baz";
+ Motable emotable=_distributableSessionPool.take();
+ emotable.init(3, 3, 6, id);
+ Immoter immoter=disc2.getDemoter(id, emotable);
+ Emoter emoter=new EtherEmoter();
+ Utils.mote(emoter, immoter, emotable, id);
+ assertTrue(d2.containsKey(id));
+ assertTrue(d2.size()==1);
+ }
+ {
+ // place a "bar" item onto first local disc
+ String id="bar";
+ Motable emotable=_distributableSessionPool.take();
+ emotable.init(2, 2, 4, id);
+ Immoter immoter=disc1.getDemoter(id, emotable);
+ Emoter emoter=new EtherEmoter();
+ Utils.mote(emoter, immoter, emotable, id);
+ assertTrue(d1.containsKey(id));
+ assertTrue(d1.size()==1);
+ }
+ {
+ // place a "foo" item into memory
+ String id="foo";
+ Motable emotable=_distributableSessionPool.take();
+ emotable.init(1, 1, 2, id);
+ m.put(id, emotable);
+ assertTrue(m.containsKey(id));
+ assertTrue(m.size()==1);
+ }
+
+ // ensure that all 3 sessions are promoted to memory...
+ FilterChain fc=new MyFilterChain();
+ memory.contextualise(new WebInvocationContext(_request,null,fc),"foo", null, null, false);
+ memory.contextualise(new WebInvocationContext(null,null,fc),"bar", null, null, false);
+ memory.contextualise(new WebInvocationContext(null,null,fc),"baz", null, null, false);
+ assertTrue(d2.size()==0);
+ assertTrue(d1.size()==0);
+ assertTrue(m.size()==3);
+
+ // check their content...
+ Session baz=(Session)m.get("baz");
+ assertTrue(baz!=null);
+ assertTrue("baz".equals(baz.getName()));
+ assertTrue(baz.getCreationTime()==3);
+ assertTrue(baz.getMaxInactiveInterval()==6);
+
+ Session bar=(Session)m.get("bar");
+ assertTrue(bar!=null);
+ assertTrue("bar".equals(bar.getName()));
+ assertTrue(bar.getCreationTime()==2);
+ assertTrue(bar.getMaxInactiveInterval()==4);
+
+ Session foo=(Session)m.get("foo");
+ assertTrue(foo!=null);
+ assertTrue("foo".equals(foo.getName()));
+ assertTrue(foo.getCreationTime()==1);
+ assertTrue(foo.getMaxInactiveInterval()==2);
+ }
+
+ public void testSharedPromotion() throws Exception {
+ SharedStoreContextualiser db=new SharedStoreContextualiser(_dummyContextualiser, _collapser, true, _store);
+ Map m=new HashMap();
+ Contextualiser serial=new SerialContextualiser(db, _collapser, m);
+ Contextualiser memory=new MemoryContextualiser(serial, _dummyEvicter, m, _streamer, _distributableContextPool, _requestPool);
+ PartitionManager partitionManager=new DummyPartitionManager(24);
+ Dispatcher dispatcher=new ActiveClusterDispatcher(_nodeName, _clusterName, _clusterUri, 5000L);
+ StandardManager manager=new ClusteredManager(_distributableSessionPool, _distributableAttributesFactory, _distributableValuePool, _sessionWrapperFactory, _sessionIdFactory, memory, m, _router, true, _streamer, _accessOnLoad, new DummyReplicaterFactory(), _location, _httpProxy, dispatcher, partitionManager, _collapser);
+ manager.init(new DummyManagerConfig());
+
+ {
+ // place a "foo" item into shared database
+ String id="foo";
+ Motable emotable=_distributableSessionPool.take();
+ emotable.init(2, 2, 4, id);
+ Immoter immoter=db.getDemoter(id, emotable);
+ Emoter emoter=new EtherEmoter();
+ Utils.mote(emoter, immoter, emotable, id);
+ }
+
+ FilterChain fc=new MyFilterChain();
+
+ memory.contextualise(new WebInvocationContext(null,null,fc),"foo", null, null, false);
+ assertTrue(m.size()==0); // this should not go to the db...
+
+ manager.start(); // this should promote all shared sessions into exclusively owned space (i.e. memory)
+
+ memory.contextualise(new WebInvocationContext(null,null,fc),"foo", null, null, false);
+ assertTrue(m.size()==1); // foo should be here now...
+
+ // check it's content
+ Session foo=(Session)m.get("foo");
+ assertTrue(foo!=null);
+ assertTrue("foo".equals(foo.getName()));
+ assertTrue(foo.getCreationTime()==2);
+ assertTrue(foo.getMaxInactiveInterval()==4);
+ }
+
+ class MyPromotingContextualiser extends AbstractContextualiser {
+ int _counter=0;
+ MyContext _context;
+
+ public MyPromotingContextualiser(String context) {
+ _context=new MyContext(context, context);
+ }
+
+ public boolean contextualise(InvocationContext invocationContext, String id, Immoter immoter, Sync motionLock, boolean exclusiveOnly) throws InvocationException {
+ _counter++;
+
+ Motable emotable=_context;
+ Emoter emoter=new EtherEmoter();
+ Motable immotable=Utils.mote(emoter, immoter, emotable, id);
+ if (immotable!=null) {
+ return immoter.contextualise(invocationContext, id, immotable, motionLock);
+ } else {
+ return false;
+ }
+ }
+
+ public Evicter getEvicter(){return null;}
+
+ public boolean isExclusive(){return false;}
+
+ public Immoter getDemoter(String name, Motable motable) {
+ return null;
+ }
+
+ public Immoter getSharedDemoter(){throw new UnsupportedOperationException();}
+
+ public void promoteToExclusive(Immoter immoter){/* empty */}
+ public void load(Emoter emoter, Immoter immoter) {/* empty */}
+
+ public void setLastAccessedTime(Evictable evictable, long oldTime, long newTime){/* empty */}
+ public void setMaxInactiveInterval(Evictable evictable, int oldInterval, int newInterval) {/* do nothing */}
+
+ public int getLocalSessionCount() {
+ return 0;
+ }
+ }
+
+ class MyActiveContextualiser extends AbstractContextualiser {
+ int _counter=0;
+ MyContext _context;
+
+ public MyActiveContextualiser(String context) {
+ _context=new MyContext(context, context);
+ }
+
+ public boolean contextualise(InvocationContext invocationContext, String id, Immoter immoter, Sync motionLock, boolean exclusiveOnly) throws InvocationException {
+ _counter++;
+ Context context=_context;
+ Sync shared=context.getSharedLock();
+ try {
+ shared.acquire();
+ motionLock.release();
+ if ( _log.isInfoEnabled() ) {
+ _log.info("running locally: " + id);
+ }
+ invocationContext.invoke();
+ shared.release();
+ return true;
+ } catch (InterruptedException e) {
+ throw new InvocationException("problem processing request for: "+id, e);
+ }
+ }
+
+ public Evicter getEvicter(){return null;}
+
+ public boolean isExclusive(){return false;}
+
+ public Immoter getDemoter(String name, Motable motable) {
+ return null;
+ }
+
+ public Immoter getSharedDemoter(){throw new UnsupportedOperationException();}
+
+ public void promoteToExclusive(Immoter immoter){/* empty */}
+ public void load(Emoter emoter, Immoter immoter) {/* empty */}
+
+ public void setLastAccessedTime(Evictable evictable, long oldTime, long newTime){/* empty */}
+ public void setMaxInactiveInterval(Evictable evictable, int oldInterval, int newInterval) {/* do nothing */}
+
+
+ public int getLocalSessionCount() {
+ return 0;
+ }
+ }
+
+ class MyRunnable implements Runnable {
+ Contextualiser _contextualiser;
+ FilterChain _chain;
+ String _id;
+
+ MyRunnable(Contextualiser contextualiser, FilterChain chain, String id) {
+ _contextualiser=contextualiser;
+ _chain=chain;
+ _id=id;
+ }
+
+ public void run() {
+ try {
+ _contextualiser.contextualise(new WebInvocationContext(null, null, _chain), _id, null, null, false);
+ } catch (Exception e) {
+ _log.error("unexpected problem", e);
+ assertTrue(false);
+ }
+ }
+ }
+
+ public void testPromotion(Contextualiser c, int n) throws Exception {
+ Contextualiser mc=new MemoryContextualiser(c, new DummyEvicter(), new HashMap(), new GZIPStreamer(), new MyContextPool(), _requestPool);
+ FilterChain fc=new MyFilterChain();
+
+ for (int i=0; i<n; i++)
+ mc.contextualise(new WebInvocationContext(null,null,fc),"baz", null, new NullSync(), false);
+ }
+
+ public void testPromotion() throws Exception {
+ int n=10;
+ MyPromotingContextualiser mpc=new MyPromotingContextualiser("baz");
+ testPromotion(mpc, n);
+ assertTrue(mpc._counter==1);
+
+ MyActiveContextualiser mac=new MyActiveContextualiser("baz");
+ testPromotion(mac, n);
+ assertTrue(mac._counter==n);
+ }
+
+ public void testCollapsing(Contextualiser c, int n) throws Exception {
+ Map map=new HashMap();
+ Contextualiser sc=new SerialContextualiser(c, new HashingCollapser(1, 1000), map);
+ Contextualiser mc=new MemoryContextualiser(sc, new DummyEvicter(), map, new GZIPStreamer(), new MyContextPool(), _requestPool);
+ FilterChain fc=new MyFilterChain();
+
+ Runnable r=new MyRunnable(mc, fc, "baz");
+ Thread[] threads=new Thread[n];
+ for (int i=0; i<n; i++)
+ (threads[i]=new Thread(r)).start();
+ for (int i=0; i<n; i++)
+ threads[i].join();
+ }
+
+ public void testCollapsing() throws Exception {
+ int n=10;
+ MyPromotingContextualiser mpc=new MyPromotingContextualiser("baz");
+ testCollapsing(mpc, n);
+ assertTrue(mpc._counter==1);
+
+ MyActiveContextualiser mxc=new MyActiveContextualiser("baz");
+ testCollapsing(mxc, n);
+ assertTrue(mxc._counter==n);
+ }
+
+ // reusable components...
+ // shared
+ protected final Streamer _streamer=new SimpleStreamer();
+ protected final Contextualiser _dummyContextualiser=new DummyContextualiser();
+ protected final File _dir=new File("c:/geronimo/tmp/wadi-test-"+System.currentTimeMillis());
+ protected final Collapser _collapser=new HashingCollapser(1, 2000);
+ protected final SessionWrapperFactory _sessionWrapperFactory=new StandardSessionWrapperFactory();
+ protected final SessionIdFactory _sessionIdFactory=new TomcatSessionIdFactory();
+ protected final boolean _accessOnLoad=true;
+ protected final Router _router=new DummyRouter();
+
+ // standard
+ protected final SessionPool _standardSessionPool=new SimpleSessionPool(new StandardSessionFactory());
+ protected final ContextPool _standardContextPool=new SessionToContextPoolAdapter(_standardSessionPool);
+ protected final AttributesFactory _standardAttributesFactory=new StandardAttributesFactory();
+ protected final ValuePool _standardValuePool=new SimpleValuePool(new StandardValueFactory());
+
+ // distributable
+ protected final SessionPool _distributableSessionPool=new SimpleSessionPool(new DistributableSessionFactory());
+ protected final ContextPool _distributableContextPool=new SessionToContextPoolAdapter(_distributableSessionPool);
+ protected final AttributesFactory _distributableAttributesFactory=new DistributableAttributesFactory();
+ protected final ValuePool _distributableValuePool=new SimpleValuePool(new DistributableValueFactory());
+
+ public void testExpiry() throws Exception {
+ Map m=new HashMap();
+ Evicter memoryEvicter=new NeverEvicter(30, true);
+ MemoryContextualiser memory=new MemoryContextualiser(_dummyContextualiser, memoryEvicter, m, _streamer, _standardContextPool, _requestPool);
+ StandardManager manager=new StandardManager(_standardSessionPool, _standardAttributesFactory, _standardValuePool, _sessionWrapperFactory, _sessionIdFactory, memory, m, _router, true);
+ manager.init(new DummyManagerConfig());
+ Session session=manager.create();
+ session.setMaxInactiveInterval(1);
+ assertTrue(m.size()==1); // in memory
+ memoryEvicter.evict();
+ assertTrue(m.size()==1); // still in memory
+ Thread.sleep(1000);
+ memoryEvicter.evict();
+ assertTrue(m.size()==0); // no longer in memory - expired
+ }
+
+ public void testDemotionAndExpiry() throws Exception {
+ Map d=new HashMap();
+ Evicter discEvicter=new NeverEvicter(30, true);
+ Contextualiser disc=new ExclusiveStoreContextualiser(_dummyContextualiser, _collapser, true, discEvicter, d, _streamer, _dir);
+ Map m=new HashMap();
+ Contextualiser serial=new SerialContextualiser(disc, _collapser, m);
+ Evicter memoryEvicter=new AbsoluteEvicter(30, true, 1);
+ Contextualiser memory=new MemoryContextualiser(serial, memoryEvicter, m, _streamer, _distributableContextPool, _requestPool);
+ PartitionManager partitionManager=new DummyPartitionManager(24);
+ Dispatcher dispatcher=new ActiveClusterDispatcher(_nodeName, _clusterName, _clusterUri, 5000L);
+ StandardManager manager=new ClusteredManager(_distributableSessionPool, _distributableAttributesFactory, _distributableValuePool, _sessionWrapperFactory, _sessionIdFactory, memory, m, _router, true, _streamer, _accessOnLoad, new DummyReplicaterFactory(), _location, _httpProxy, dispatcher, partitionManager, _collapser);
+ manager.init(new DummyManagerConfig());
+
+ Session session=manager.create();
+ session.setMaxInactiveInterval(2);// times out 2 seconds from now...
+
+ assertTrue(m.size()==1); // in memory
+ assertTrue(d.size()==0); // not on disc
+
+ memoryEvicter.evict();
+ assertTrue(m.size()==1); // still in memory
+ assertTrue(d.size()==0); // still not on disc
+
+ Thread.sleep(1000);
+ memoryEvicter.evict();
+ assertTrue(m.size()==0); // no longer in memory
+ assertTrue(d.size()==1); // now on disc
+
+ discEvicter.evict();
+ assertTrue(m.size()==0); // no longer in memory
+ assertTrue(d.size()==1); // still on disc
+
+ Thread.sleep(1000);
+ discEvicter.evict();
+ assertTrue(m.size()==0); // no longer in memory
+ assertTrue(d.size()==0); // no longer on disc - expired
+ }
+
+ public void testDemotionAndPromotion() throws Exception {
+ Map d=new HashMap();
+ Evicter discEvicter=new NeverEvicter(30, true);
+ Contextualiser disc=new ExclusiveStoreContextualiser(_dummyContextualiser, _collapser, true, discEvicter, d, _streamer, _dir);
+ Map m=new HashMap();
+ Contextualiser serial=new SerialContextualiser(disc, _collapser, m);
+ Evicter memoryEvicter=new AbsoluteEvicter(30, true, 1);
+ Contextualiser memory=new MemoryContextualiser(serial, memoryEvicter, m, _streamer, _distributableContextPool, _requestPool);
+ PartitionManager partitionManager=new DummyPartitionManager(24);
+ Dispatcher dispatcher=new ActiveClusterDispatcher(_nodeName, _clusterName, _clusterUri, 5000L);
+ StandardManager manager=new ClusteredManager(_distributableSessionPool, _distributableAttributesFactory, _distributableValuePool, _sessionWrapperFactory, _sessionIdFactory, memory, m, _router, true, _streamer, _accessOnLoad, new DummyReplicaterFactory(), _location, _httpProxy, dispatcher, partitionManager, _collapser);
+ manager.init(new DummyManagerConfig());
+
+ Session session=manager.create();
+ String id=session.getName();
+ session.setMaxInactiveInterval(2);// times out 2 seconds from now...
+
+ Thread.sleep(1000);
+ memoryEvicter.evict();
+ assertTrue(m.size()==0); // no longer in memory
+ assertTrue(d.size()==1); // now on disc
+
+ memory.contextualise(new WebInvocationContext(null,null,new MyFilterChain()),id, null, null, false);
+ assertTrue(m.size()==1); // promoted back into memory
+ assertTrue(d.size()==0); // no longer on disc
+ }
+
+ static class MyLocation extends SimpleEvictable implements Location, Serializable {
+
+ public void proxy(InvocationContext invocationContext) throws ProxyingException {
+ System.out.println("PROXYING");
+ }
+
+ public Destination getDestination(){return null;}
+ }
+
+ public void testCluster() throws Exception {
+ ConnectionFactory connectionFactory = Utils.getConnectionFactory();
+ ((ActiveMQConnectionFactory)connectionFactory).setBrokerContainerFactory(new BrokerContainerFactoryImpl(new VMPersistenceAdapter()));
+ ClusterFactory clusterFactory = new CustomClusterFactory(connectionFactory);
+ String clusterName = "ORG.CODEHAUS.WADI.TEST.CLUSTER";
+ CustomCluster cluster0 = (CustomCluster)clusterFactory.createCluster(clusterName);
+ CustomCluster cluster1 = (CustomCluster)clusterFactory.createCluster(clusterName);
+
+ cluster0.start();
+ cluster1.start();
+ //-------------------
+ // do the test
+
+ //Location location0=new MyLocation();
+ //Map c0=new HashMap();
+ Relocater relocater0=new WebHybridRelocater(5000L, 5000L, true);
+ Collapser collapser0=new HashingCollapser(10, 2000);
+ ClusterContextualiser clstr0=new ClusterContextualiser(new DummyContextualiser(), collapser0, relocater0);
+ Map m0=new HashMap();
+ m0.put("foo", new MyContext("foo", "1"));
+ Contextualiser memory0=new MemoryContextualiser(clstr0, new NeverEvicter(30000, true), m0, new GZIPStreamer(), new MyContextPool(), _requestPool);
+ memory0.init(new DummyDistributableContextualiserConfig(cluster0));
+
+ //Location location1=new MyLocation();
+ //Map c1=new HashMap();
+ Relocater relocater1=new WebHybridRelocater(5000L, 5000L, true);
+ Collapser collapser1=new HashingCollapser(10, 2000);
+ ClusterContextualiser clstr1=new ClusterContextualiser(new DummyContextualiser(), collapser1, relocater1);
+ Map m1=new HashMap();
+ m1.put("bar", new MyContext("bar", "2"));
+ Contextualiser memory1=new MemoryContextualiser(clstr1, new NeverEvicter(30000, true), m1, new GZIPStreamer(), new MyContextPool(), _requestPool);
+ memory1.init(new DummyDistributableContextualiserConfig(cluster1));
+
+ Thread.sleep(2000); // activecluster needs a little time to sort itself out...
+ _log.info("STARTING NOW!");
+ FilterChain fc=new MyFilterChain();
+
+ assertTrue(!m0.containsKey("bar"));
+ assertTrue(!m1.containsKey("foo"));
+ // not sure what these were testing - if Context not available, these will return false...
+// assertTrue(memory0.contextualise(null,null,fc,"bar", null, null, false));
+// assertTrue(memory0.contextualise(null,null,fc,"bar", null, null, false));
+// assertTrue(memory1.contextualise(null,null,fc,"foo", null, null, false));
+// assertTrue(memory1.contextualise(null,null,fc,"foo", null, null, false));
+ assertTrue(!memory0.contextualise(new WebInvocationContext(null,null,fc),"baz", null, null, false));
+ assertTrue(!memory1.contextualise(new WebInvocationContext(null,null,fc),"baz", null, null, false));
+
+ Thread.sleep(2000);
+ _log.info("STOPPING NOW!");
+ // ------------------
+ cluster1.stop();
+ cluster1=null;
+ cluster0.stop();
+ cluster0=null;
+ clusterFactory=null;
+ connectionFactory=null;
+ }
+
+}
Added: incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestDIndex.java
URL: http://svn.apache.org/viewcvs/incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestDIndex.java?rev=356933&view=auto
==============================================================================
--- incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestDIndex.java (added)
+++ incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestDIndex.java Wed Dec 14 15:32:56 2005
@@ -0,0 +1,111 @@
+/**
+ *
+ * Copyright 2003-2005 Core Developers Network Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.codehaus.wadi.test;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.codehaus.wadi.Motable;
+import org.codehaus.wadi.dindex.impl.DIndex;
+import org.codehaus.wadi.dindex.impl.PartitionFacade;
+import org.codehaus.wadi.impl.DistributableSession;
+import org.codehaus.wadi.impl.FixedWidthSessionIdFactory;
+
+import junit.framework.TestCase;
+
+public class TestDIndex extends TestCase {
+
+ protected final Log _log=LogFactory.getLog(getClass().getName());
+
+ public TestDIndex(String name) {
+ super(name);
+ }
+
+ protected final int _numPartitions=3;
+ protected final FixedWidthSessionIdFactory _factory=new FixedWidthSessionIdFactory(5, "0123456789".toCharArray(), _numPartitions);
+
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ class Foo implements Runnable {
+
+ protected final PartitionFacade _facade;
+
+ public Foo(PartitionFacade facade) {
+ _facade=facade;
+ }
+
+ public void run() {
+
+ }
+
+ }
+
+ protected long heartbeatTimeout=5*1000;
+ protected long responseTimeout=30*60*1000;
+
+
+ public void testDindex() throws Exception {
+ assertTrue(true);
+
+ DIndexNode red=new DIndexNode("red", _numPartitions, _factory, heartbeatTimeout);
+ DIndexNode green=new DIndexNode("green", _numPartitions, _factory, heartbeatTimeout);
+ DIndexNode blue=new DIndexNode("blue", _numPartitions, _factory, heartbeatTimeout);
+
+
+ red.start();
+ green.start();
+ blue.start();
+
+ while(red.getCluster().getNodes().size()<2 ||
+ green.getCluster().getNodes().size()<2 ||
+ blue.getCluster().getNodes().size()<2 ||
+ green.getDIndex().getPartitionManager().getPartitionKeys().size()==0 ||
+ blue.getDIndex().getPartitionManager().getPartitionKeys().size()==0)
+ Thread.sleep(1000);
+
+ _log.info("partitions distributed");
+
+ String name=_factory.create(1); // blue
+ _log.info("inserting: "+name);
+ Motable motable=new DistributableSession(new DummyDistributableSessionConfig());
+ motable.init(0, 0, 100000, name);
+ red.insert(name, motable, 30*1000L);
+ _log.info("inserted: "+name);
+ _log.info("fetching: "+name);
+ DIndex g=green.getDIndex();
+ g.relocate2(name, "green", 1, false, responseTimeout);
+ // Motable motable2=(Motable)
+ green.get(name);
+ _log.info("fetched: "+name);
+ //green.getDIndex().remove(name);
+ //blue.getDIndex().put(name, name);
+
+ Thread.sleep(10*1000);
+ blue.stop();
+ green.stop();
+ red.stop();
+ Thread.sleep(6000);
+ _log.info("0 nodes running");
+ }
+
+}
Added: incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestEvicters.java
URL: http://svn.apache.org/viewcvs/incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestEvicters.java?rev=356933&view=auto
==============================================================================
--- incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestEvicters.java (added)
+++ incubator/wadi/trunk/modules/core/src/test/java/org/codehaus/wadi/test/TestEvicters.java Wed Dec 14 15:32:56 2005
@@ -0,0 +1,171 @@
+/**
+ *
+ * Copyright 2003-2005 Core Developers Network Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.codehaus.wadi.test;
+
+import java.io.File;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Timer;
+
+import junit.framework.TestCase;
+
+import org.codehaus.wadi.AttributesFactory;
+import org.codehaus.wadi.Collapser;
+import org.codehaus.wadi.ContextPool;
+import org.codehaus.wadi.Contextualiser;
+import org.codehaus.wadi.Emoter;
+import org.codehaus.wadi.Evicter;
+import org.codehaus.wadi.EvicterConfig;
+import org.codehaus.wadi.InvocationProxy;
+import org.codehaus.wadi.Motable;
+import org.codehaus.wadi.PoolableInvocationWrapperPool;
+import org.codehaus.wadi.ProxiedLocation;
+import org.codehaus.wadi.SessionIdFactory;
+import org.codehaus.wadi.SessionPool;
+import org.codehaus.wadi.SessionWrapperFactory;
+import org.codehaus.wadi.Streamer;
+import org.codehaus.wadi.ValuePool;
+import org.codehaus.wadi.gridstate.Dispatcher;
+import org.codehaus.wadi.gridstate.PartitionManager;
+import org.codehaus.wadi.gridstate.activecluster.ActiveClusterDispatcher;
+import org.codehaus.wadi.gridstate.impl.DummyPartitionManager;
+import org.codehaus.wadi.http.HTTPProxiedLocation;
+import org.codehaus.wadi.impl.AbsoluteEvicter;
+import org.codehaus.wadi.impl.AbstractExclusiveContextualiser;
+import org.codehaus.wadi.impl.ClusteredManager;
+import org.codehaus.wadi.impl.DistributableAttributesFactory;
+import org.codehaus.wadi.impl.DistributableSessionFactory;
+import org.codehaus.wadi.impl.DistributableValueFactory;
+import org.codehaus.wadi.impl.DummyContextualiser;
+import org.codehaus.wadi.impl.DummyReplicaterFactory;
+import org.codehaus.wadi.impl.DummyRouter;
+import org.codehaus.wadi.impl.DummyStatefulHttpServletRequestWrapperPool;
+import org.codehaus.wadi.impl.ExclusiveStoreContextualiser;
+import org.codehaus.wadi.impl.HashingCollapser;
+import org.codehaus.wadi.impl.MemoryContextualiser;
+import org.codehaus.wadi.impl.NeverEvicter;
+import org.codehaus.wadi.impl.SessionToContextPoolAdapter;
+import org.codehaus.wadi.impl.SimpleSessionPool;
+import org.codehaus.wadi.impl.SimpleStreamer;
+import org.codehaus.wadi.impl.SimpleValuePool;
+import org.codehaus.wadi.impl.StandardHttpProxy;
+import org.codehaus.wadi.impl.StandardSessionWrapperFactory;
+import org.codehaus.wadi.impl.TomcatSessionIdFactory;
+import org.codehaus.wadi.impl.Utils;
+
+import EDU.oswego.cs.dl.util.concurrent.NullSync;
+import EDU.oswego.cs.dl.util.concurrent.Sync;
+
+public class TestEvicters extends TestCase {
+
+ protected final String _clusterUri=Utils.getClusterUri();
+ protected final String _clusterName="WADI.TEST";
+
+ public TestEvicters(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public static class MyEvicterConfig implements EvicterConfig {
+
+ protected final Timer _timer=new Timer();
+ public Timer getTimer() {return _timer;}
+
+ protected final Map _map=new HashMap();
+ public Map getMap() {return _map;}
+
+ protected final Sync _sync=new NullSync();
+ public Sync getEvictionLock(String id, Motable motable) {return _sync;}
+
+ protected final Emoter _emoter=new EtherEmoter();
+ public Emoter getEvictionEmoter() {return _emoter;}
+
+ protected final int _maxInactiveInterval=4;
+ public int getMaxInactiveInterval() {return _maxInactiveInterval;}
+
+ protected int _demotions;
+ public int getDemotions() {return _demotions;}
+ public void demote(Motable motable) {_demotions++;}
+
+ protected int _expirations;
+ public int getExpirations() {return _expirations;}
+ public void expire(Motable motable) {_expirations++; _map.remove(motable.getName());}
+ }
+
+ public void testExpiryFromStorage() throws Exception {
+ // Contextualiser
+ Contextualiser next=new DummyContextualiser();
+ int sweepInterval=1;
+ boolean strictOrdering=true;
+ Evicter devicter=new NeverEvicter(sweepInterval, strictOrdering);
+ Map dmap=new HashMap();
+ Streamer streamer=new SimpleStreamer();
+ // (Contextualiser next, Collapser collapser, Evicter evicter, Map map, StreamingStrategy streamer, File dir) {
+ Collapser collapser=new HashingCollapser(100, 1000);
+ File dir=new File("/tmp/wadi-"+System.currentTimeMillis());
+ dir.mkdir();
+ Contextualiser disc=new ExclusiveStoreContextualiser(next, collapser, true, devicter, dmap, streamer, dir);
+ Map mmap=new HashMap();
+ int inactivityInterval=1; // second
+ Evicter mevicter=new AbsoluteEvicter(sweepInterval, strictOrdering, inactivityInterval);
+ SessionPool sessionPool=new SimpleSessionPool(new DistributableSessionFactory());
+ ContextPool contextPool=new SessionToContextPoolAdapter(sessionPool);
+ PoolableInvocationWrapperPool requestPool=new DummyStatefulHttpServletRequestWrapperPool();
+ AbstractExclusiveContextualiser memory=new MemoryContextualiser(disc, mevicter, mmap, streamer, contextPool, requestPool);
+ // Manager
+ AttributesFactory attributesFactory=new DistributableAttributesFactory();
+ ValuePool valuePool=new SimpleValuePool(new DistributableValueFactory());
+ SessionWrapperFactory wrapperFactory=new StandardSessionWrapperFactory();
+ SessionIdFactory idFactory=new TomcatSessionIdFactory();
+ InvocationProxy proxy=new StandardHttpProxy("jsessionid");
+ ProxiedLocation location= new HTTPProxiedLocation(new InetSocketAddress(InetAddress.getLocalHost(), 8888));
+ String nodeName="node0";
+ PartitionManager partitionManager=new DummyPartitionManager(24);
+ Dispatcher dispatcher=new ActiveClusterDispatcher(nodeName, _clusterName, _clusterUri, 5000L);
+ ClusteredManager manager=new ClusteredManager(sessionPool, attributesFactory, valuePool, wrapperFactory, idFactory, memory, memory.getMap(), new DummyRouter(), true, streamer, true, new DummyReplicaterFactory(), location, proxy, dispatcher, partitionManager, collapser);
+ manager.setMaxInactiveInterval(2);
+ manager.init(new DummyManagerConfig());
+ //manager.start();
+ //mevicter.stop(); // we'll run it by hand...
+ //devicter.stop();
+
+ manager.create();
+ assertTrue(mmap.size()==1);
+ assertTrue(dmap.size()==0);
+ Thread.sleep(1100);
+ mevicter.evict();
+ assertTrue(mmap.size()==0);
+ assertTrue(dmap.size()==1);
+ Thread.sleep(1100);
+ devicter.evict();
+ assertTrue(mmap.size()==0);
+ assertTrue(dmap.size()==0);
+ manager.stop();
+
+ // rename/use IdGenerator and StreamingStrategy...
+ dir.delete();
+ }
+}