1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
34
35 public class RMIBootstrapCacheLoaderTest extends TestCase {
36
37
38
39
40
41 protected static final boolean ASYNCHRONOUS = true;
42
43
44
45
46 protected static final boolean SYNCHRONOUS = false;
47
48 private static final Log LOG = LogFactory.getLog(RMICacheReplicatorTest.class.getName());
49
50
51
52
53 protected CacheManager manager1;
54
55
56
57 protected CacheManager manager2;
58
59
60
61 protected CacheManager manager3;
62
63
64
65 protected CacheManager manager4;
66
67
68
69 protected CacheManager manager5;
70
71
72
73 protected CacheManager manager6;
74
75
76
77
78 protected String cacheName = "sampleCache1";
79
80
81
82
83
84
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
98
99
100
101 Thread.sleep(3000);
102 }
103
104
105
106
107
108 protected void forceVMGrowth() {
109 byte[] forceVMGrowth = new byte[50000000];
110 }
111
112
113
114
115
116
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
146
147 public void testBootstrapFromClusterWithAsyncLoader() throws CacheException, InterruptedException {
148
149 if (JVMUtil.isSingleRMIRegistryPerVM()) {
150 return;
151 }
152
153 forceVMGrowth();
154
155
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
183
184 public void testBootstrapFromClusterWithSyncLoader() throws CacheException, InterruptedException {
185
186 if (JVMUtil.isSingleRMIRegistryPerVM()) {
187 return;
188 }
189
190 forceVMGrowth();
191
192
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
214
215 assertEquals(2000, manager3.getCache("sampleCache2").getSize());
216
217
218 }
219
220
221
222
223
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
236 Thread.sleep(3000);
237 assertEquals(1000, testBootstrap2.getSize());
238
239
240 }
241
242
243 }