1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.distribution;
18  
19  import junit.framework.TestCase;
20  import net.sf.ehcache.AbstractCacheTest;
21  import net.sf.ehcache.Cache;
22  import net.sf.ehcache.CacheException;
23  import net.sf.ehcache.CacheManager;
24  import net.sf.ehcache.Element;
25  import net.sf.ehcache.event.CountingCacheEventListener;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import java.util.Date;
30  
31  /**
32   * @author Greg Luck
33   * @version $Id: RMIBootstrapCacheLoaderTest.java 512 2007-07-10 09:18:45Z gregluck $
34   */
35  public class RMIBootstrapCacheLoaderTest extends TestCase {
36  
37  
38      /**
39       * A value to represent replicate asynchronously
40       */
41      protected static final boolean ASYNCHRONOUS = true;
42  
43      /**
44       * A value to represent replicate synchronously
45       */
46      protected static final boolean SYNCHRONOUS = false;
47  
48      private static final Log LOG = LogFactory.getLog(RMICacheReplicatorTest.class.getName());
49  
50      /**
51       * CacheManager 1 in the cluster
52       */
53      protected CacheManager manager1;
54      /**
55       * CacheManager 2 in the cluster
56       */
57      protected CacheManager manager2;
58      /**
59       * CacheManager 3 in the cluster
60       */
61      protected CacheManager manager3;
62      /**
63       * CacheManager 4 in the cluster
64       */
65      protected CacheManager manager4;
66      /**
67       * CacheManager 5 in the cluster
68       */
69      protected CacheManager manager5;
70      /**
71       * CacheManager 6 in the cluster
72       */
73      protected CacheManager manager6;
74  
75      /**
76       * The name of the cache under test
77       */
78      protected String cacheName = "sampleCache1";
79  
80      /**
81       * {@inheritDoc}
82       * Sets up two caches: cache1 is local. cache2 is to be receive updates
83       *
84       * @throws Exception
85       */
86      protected void setUp() throws Exception {
87          if (JVMUtil.isSingleRMIRegistryPerVM()) {
88              return;
89          }
90  
91          MulticastKeepaliveHeartbeatSender.setHeartBeatInterval(1000);
92  
93          CountingCacheEventListener.resetCounters();
94          manager1 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed1.xml");
95          manager2 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed2.xml");
96  
97          //manager6 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed-jndi6.xml");
98  
99  
100         //allow cluster to be established
101         Thread.sleep(3000);
102     }
103 
104     /**
105      * Force the VM to grow to its full size. This stops SoftReferences from being reclaimed in favour of
106      * Heap growth. Only an issue when a VM is cold.
107      */
108     protected void forceVMGrowth() {
109         byte[] forceVMGrowth = new byte[50000000];
110     }
111 
112 
113     /**
114      * {@inheritDoc}
115      *
116      * @throws Exception
117      */
118     protected void tearDown() throws Exception {
119 
120         if (JVMUtil.isSingleRMIRegistryPerVM()) {
121             return;
122         }
123 
124         if (manager1 != null) {
125             manager1.shutdown();
126         }
127         if (manager2 != null) {
128             manager2.shutdown();
129         }
130         if (manager3 != null) {
131             manager3.shutdown();
132         }
133         if (manager4 != null) {
134             manager4.shutdown();
135         }
136         if (manager5 != null) {
137             manager5.shutdown();
138         }
139         if (manager6 != null) {
140             manager6.shutdown();
141         }
142     }
143 
144     /**
145      * Tests loading from bootstrap
146      */
147     public void testBootstrapFromClusterWithAsyncLoader() throws CacheException, InterruptedException {
148 
149         if (JVMUtil.isSingleRMIRegistryPerVM()) {
150             return;
151         }
152 
153         forceVMGrowth();
154 
155         //Give everything a chance to startup
156         Integer index = null;
157         for (int i = 0; i < 2; i++) {
158             for (int j = 0; j < 1000; j++) {
159                 index = new Integer(((1000 * i) + j));
160                 manager1.getCache("sampleCache1").put(new Element(index,
161                         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
162                                 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
163                                 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
164                                 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
165                                 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
166             }
167 
168         }
169         assertEquals(2000, manager1.getCache("sampleCache1").getSize());
170 
171         Thread.sleep(8000);
172         assertEquals(2000, manager2.getCache("sampleCache1").getSize());
173 
174         manager3 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed3.xml");
175         Thread.sleep(5000);
176         assertEquals(2000, manager3.getCache("sampleCache1").getSize());
177 
178 
179     }
180 
181     /**
182      * Tests loading from bootstrap
183      */
184     public void testBootstrapFromClusterWithSyncLoader() throws CacheException, InterruptedException {
185 
186         if (JVMUtil.isSingleRMIRegistryPerVM()) {
187             return;
188         }
189 
190         forceVMGrowth();
191 
192         //Give everything a chance to startup
193         Integer index = null;
194         for (int i = 0; i < 2; i++) {
195             for (int j = 0; j < 1000; j++) {
196                 index = new Integer(((1000 * i) + j));
197                 manager1.getCache("sampleCache2").put(new Element(index,
198                         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
199                                 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
200                                 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
201                                 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
202                                 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
203             }
204 
205         }
206 
207         assertEquals(2000, manager1.getCache("sampleCache2").getSize());
208 
209         Thread.sleep(8000);
210         assertEquals(2000, manager2.getCache("sampleCache2").getSize());
211 
212         manager3 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed3.xml");
213         //Should not need to wait because the load is synchronous
214         //Thread.sleep(10000);
215         assertEquals(2000, manager3.getCache("sampleCache2").getSize());
216 
217 
218     }
219 
220 
221     /**
222      * Create the same named cache in two CacheManagers. Populate the first one. Check that the second one gets the
223      * entries.
224      */
225     public void testAddCacheAndBootstrapOccurs() throws InterruptedException {
226 
227         manager1.addCache("testBootstrap1");
228         Cache testBootstrap1 = manager1.getCache("testBootstrap1");
229         for (int i = 0; i < 1000; i++) {
230             testBootstrap1.put(new Element("key" + i, new Date()));
231         }
232 
233         manager2.addCache("testBootstrap1");
234         Cache testBootstrap2 = manager2.getCache("testBootstrap1");
235         //wait for async bootstrap
236         Thread.sleep(3000);
237         assertEquals(1000, testBootstrap2.getSize());
238 
239 
240     }
241 
242 
243 }