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.CacheException;
021: import net.sf.ehcache.CacheManager;
022: import net.sf.ehcache.Element;
023: import net.sf.ehcache.event.CountingCacheEventListener;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import java.util.Date;
028:
029: /**
030: * @author Greg Luck
031: * @version $Id: RMIBootstrapCacheLoaderTest.java 519 2007-07-27 07:11:45Z gregluck $
032: */
033: public class RMIBootstrapCacheLoaderTest extends TestCase {
034:
035: /**
036: * A value to represent replicate asynchronously
037: */
038: protected static final boolean ASYNCHRONOUS = true;
039:
040: /**
041: * A value to represent replicate synchronously
042: */
043: protected static final boolean SYNCHRONOUS = false;
044:
045: private static final Log LOG = LogFactory
046: .getLog(RMICacheReplicatorTest.class.getName());
047:
048: /**
049: * CacheManager 1 in the cluster
050: */
051: protected CacheManager manager1;
052: /**
053: * CacheManager 2 in the cluster
054: */
055: protected CacheManager manager2;
056: /**
057: * CacheManager 3 in the cluster
058: */
059: protected CacheManager manager3;
060: /**
061: * CacheManager 4 in the cluster
062: */
063: protected CacheManager manager4;
064: /**
065: * CacheManager 5 in the cluster
066: */
067: protected CacheManager manager5;
068: /**
069: * CacheManager 6 in the cluster
070: */
071: protected CacheManager manager6;
072:
073: /**
074: * The name of the cache under test
075: */
076: protected String cacheName = "sampleCache1";
077:
078: /**
079: * {@inheritDoc}
080: * Sets up two caches: cache1 is local. cache2 is to be receive updates
081: *
082: * @throws Exception
083: */
084: protected void setUp() throws Exception {
085: if (JVMUtil.isSingleRMIRegistryPerVM()) {
086: return;
087: }
088:
089: MulticastKeepaliveHeartbeatSender.setHeartBeatInterval(1000);
090:
091: CountingCacheEventListener.resetCounters();
092: manager1 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR
093: + "distribution/ehcache-distributed1.xml");
094: manager2 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR
095: + "distribution/ehcache-distributed2.xml");
096:
097: //manager6 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed-jndi6.xml");
098:
099: //allow cluster to be established
100: Thread.sleep(3000);
101: }
102:
103: /**
104: * Force the VM to grow to its full size. This stops SoftReferences from being reclaimed in favour of
105: * Heap growth. Only an issue when a VM is cold.
106: */
107: protected void forceVMGrowth() {
108: byte[] forceVMGrowth = new byte[50000000];
109: }
110:
111: /**
112: * {@inheritDoc}
113: *
114: * @throws Exception
115: */
116: protected void tearDown() throws Exception {
117:
118: if (JVMUtil.isSingleRMIRegistryPerVM()) {
119: return;
120: }
121:
122: if (manager1 != null) {
123: manager1.shutdown();
124: }
125: if (manager2 != null) {
126: manager2.shutdown();
127: }
128: if (manager3 != null) {
129: manager3.shutdown();
130: }
131: if (manager4 != null) {
132: manager4.shutdown();
133: }
134: if (manager5 != null) {
135: manager5.shutdown();
136: }
137: if (manager6 != null) {
138: manager6.shutdown();
139: }
140: }
141:
142: /**
143: * Tests loading from bootstrap
144: */
145: public void testBootstrapFromClusterWithAsyncLoader()
146: throws CacheException, InterruptedException {
147:
148: if (JVMUtil.isSingleRMIRegistryPerVM()) {
149: return;
150: }
151:
152: forceVMGrowth();
153:
154: //Give everything a chance to startup
155: Integer index = null;
156: for (int i = 0; i < 2; i++) {
157: for (int j = 0; j < 1000; j++) {
158: index = new Integer(((1000 * i) + j));
159: manager1
160: .getCache("sampleCache1")
161: .put(
162: new Element(
163: index,
164: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
165: + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
166: + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
167: + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
168: + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
169: }
170:
171: }
172: assertEquals(2000, manager1.getCache("sampleCache1").getSize());
173:
174: Thread.sleep(8000);
175: assertEquals(2000, manager2.getCache("sampleCache1").getSize());
176:
177: manager3 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR
178: + "distribution/ehcache-distributed3.xml");
179: Thread.sleep(5000);
180: assertEquals(2000, manager3.getCache("sampleCache1").getSize());
181:
182: }
183:
184: /**
185: * Tests loading from bootstrap
186: */
187: public void testBootstrapFromClusterWithSyncLoader()
188: throws CacheException, InterruptedException {
189:
190: if (JVMUtil.isSingleRMIRegistryPerVM()) {
191: return;
192: }
193:
194: forceVMGrowth();
195:
196: //Give everything a chance to startup
197: Integer index = null;
198: for (int i = 0; i < 2; i++) {
199: for (int j = 0; j < 1000; j++) {
200: index = new Integer(((1000 * i) + j));
201: manager1
202: .getCache("sampleCache2")
203: .put(
204: new Element(
205: index,
206: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
207: + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
208: + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
209: + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
210: + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
211: }
212:
213: }
214:
215: assertEquals(2000, manager1.getCache("sampleCache2").getSize());
216:
217: Thread.sleep(8000);
218: assertEquals(2000, manager2.getCache("sampleCache2").getSize());
219:
220: manager3 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR
221: + "distribution/ehcache-distributed3.xml");
222: //Should not need to wait because the load is synchronous
223: //Thread.sleep(10000);
224: assertEquals(2000, manager3.getCache("sampleCache2").getSize());
225:
226: }
227:
228: /**
229: * Create the same named cache in two CacheManagers. Populate the first one. Check that the second one gets the
230: * entries.
231: */
232: public void testAddCacheAndBootstrapOccurs()
233: throws InterruptedException {
234:
235: manager1.addCache("testBootstrap1");
236: Cache testBootstrap1 = manager1.getCache("testBootstrap1");
237: for (int i = 0; i < 1000; i++) {
238: testBootstrap1.put(new Element("key" + i, new Date()));
239: }
240:
241: manager2.addCache("testBootstrap1");
242: Cache testBootstrap2 = manager2.getCache("testBootstrap1");
243: //wait for async bootstrap
244: Thread.sleep(3000);
245: assertEquals(1000, testBootstrap2.getSize());
246:
247: }
248:
249: }
|