001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.distribution;
016:
017: import junit.framework.TestCase;
018: import net.sf.ehcache.AbstractCacheTest;
019: import net.sf.ehcache.Cache;
020: import net.sf.ehcache.CacheManager;
021: import net.sf.ehcache.Ehcache;
022: import net.sf.ehcache.Element;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import java.net.SocketTimeoutException;
027: import java.rmi.RemoteException;
028: import java.rmi.UnmarshalException;
029: import java.util.ArrayList;
030: import java.util.Date;
031: import java.util.List;
032:
033: /**
034: * Unit tests for RMICachePeer
035: *
036: * Note these tests need a live network interface running in multicast mode to work
037: *
038: * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
039: * @version $Id: RMICacheManagerPeerTest.java 519 2007-07-27 07:11:45Z gregluck $
040: */
041: public class RMICacheManagerPeerTest extends TestCase {
042:
043: private static final Log LOG = LogFactory
044: .getLog(RMICacheManagerPeerTest.class.getName());
045:
046: /**
047: * manager
048: */
049: protected CacheManager manager;
050: private String hostName = "localhost";
051: private Integer port = new Integer(40000);
052: private RMICacheManagerPeerListener peerListener;
053: private Cache cache;
054:
055: /**
056: * {@inheritDoc}
057: *
058: * @throws Exception
059: */
060: protected void setUp() throws Exception {
061: manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR
062: + "ehcache.xml");
063: cache = new Cache("test", 10, false, false, 10, 10);
064:
065: peerListener = new RMICacheManagerPeerListener(hostName, port,
066: manager, new Integer(2000));
067: }
068:
069: /**
070: * Shutdown the cache
071: */
072: protected void tearDown() throws InterruptedException {
073: Thread.sleep(20);
074: if (peerListener != null) {
075: peerListener.dispose();
076: }
077: manager.shutdown();
078: }
079:
080: /**
081: * Can we create the peer?
082: */
083: public void testCreatePeer() throws RemoteException {
084: for (int i = 0; i < 10; i++) {
085: new RMICachePeer(cache, hostName, port, new Integer(2000));
086: }
087: }
088:
089: /**
090: * See if socket.setSoTimeout(socketTimeoutMillis) works. Should throw a SocketTimeoutException
091: *
092: * @throws RemoteException
093: */
094: public void testFailsIfTimeoutExceeded() throws Exception {
095:
096: RMICachePeer rmiCachePeer = new SlowRMICachePeer(cache,
097: hostName, port, new Integer(1000));
098: peerListener.addCachePeer(cache.getName(), rmiCachePeer);
099: peerListener.init();
100:
101: try {
102: CachePeer cachePeer = new ManualRMICacheManagerPeerProvider()
103: .lookupRemoteCachePeer(rmiCachePeer.getUrl());
104: cachePeer.put(new Element("1", new Date()));
105: fail();
106: } catch (UnmarshalException e) {
107: assertEquals(SocketTimeoutException.class, e.getCause()
108: .getClass());
109: }
110: }
111:
112: /**
113: * See if socket.setSoTimeout(socketTimeoutMillis) works.
114: * Should not fail because the put takes less than the timeout.
115: *
116: * @throws RemoteException
117: */
118: public void testWorksIfTimeoutNotExceeded() throws Exception {
119:
120: cache = new Cache("test", 10, false, false, 10, 10);
121: RMICachePeer rmiCachePeer = new SlowRMICachePeer(cache,
122: hostName, port, new Integer(2100));
123:
124: peerListener.addCachePeer(cache.getName(), rmiCachePeer);
125: peerListener.init();
126:
127: CachePeer cachePeer = new ManualRMICacheManagerPeerProvider()
128: .lookupRemoteCachePeer(rmiCachePeer.getUrl());
129: cachePeer.put(new Element("1", new Date()));
130: }
131:
132: /**
133: * Test send.
134: * <p/>
135: * This is a unit test because it was throwing AbstractMethodError if a method has changed signature,
136: * or NoSuchMethodError is a new one is added. The problem is that rmic needs
137: * to recompile the stub after any changes are made to the CachePeer source, something done by ant
138: * compile but not by the IDE.
139: */
140: public void testSend() throws Exception {
141:
142: cache = new Cache("test", 10, false, false, 10, 10);
143: RMICachePeer rmiCachePeer = new RMICachePeer(cache, hostName,
144: port, new Integer(2100));
145: manager.addCache(cache);
146:
147: peerListener.addCachePeer(cache.getName(), rmiCachePeer);
148: peerListener.init();
149:
150: CachePeer cachePeer = new ManualRMICacheManagerPeerProvider()
151: .lookupRemoteCachePeer(rmiCachePeer.getUrl());
152: Element element = new Element("1", new Date());
153: EventMessage eventMessage = new EventMessage(EventMessage.PUT,
154: null, element);
155: List eventMessages = new ArrayList();
156: eventMessages.add(eventMessage);
157: cachePeer.send(eventMessages);
158: }
159:
160: /**
161: * RMICachePeer that breaks in lots of interesting ways.
162: */
163: class SlowRMICachePeer extends RMICachePeer {
164:
165: /**
166: * Constructor
167: * @param cache
168: * @param hostName
169: * @param port
170: * @param socketTimeoutMillis
171: * @throws RemoteException
172: */
173: public SlowRMICachePeer(Ehcache cache, String hostName,
174: Integer port, Integer socketTimeoutMillis)
175: throws RemoteException {
176: super (cache, hostName, port, socketTimeoutMillis);
177: }
178:
179: /**
180: * Puts an Element into the underlying cache without notifying listeners or updating statistics.
181: *
182: * @param element
183: * @throws java.rmi.RemoteException
184: * @throws IllegalArgumentException
185: * @throws IllegalStateException
186: */
187: public void put(Element element) throws RemoteException,
188: IllegalArgumentException, IllegalStateException {
189: try {
190: Thread.sleep(2000);
191: } catch (InterruptedException exception) {
192: LOG.debug(exception.getMessage(), exception);
193: }
194: }
195: }
196: }
|