KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > buddyreplication > BuddyReplicationContentTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.buddyreplication;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jboss.cache.CacheImpl;
12 import org.jboss.cache.misc.TestingUtil;
13
14 /**
15  * Teststhe transfer of content under *normal* operation
16  *
17  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
18  */

19 public class BuddyReplicationContentTest extends BuddyReplicationTestsBase
20 {
21    private String JavaDoc key = "key";
22    private String JavaDoc value = "value";
23    private CacheImpl[] caches;
24    private Log log = LogFactory.getLog(BuddyGroupAssignmentTest.class);
25
26
27    private void assertNoStaleLocks(CacheImpl[] caches)
28    {
29       for (CacheImpl cache : caches) assertNoStaleLocks(cache);
30    }
31
32    private void assertNoStaleLocks(CacheImpl cache)
33    {
34       assertEquals("Number of locks in cache instance " + cache.toString(true) + " should be 0", 0, cache.getNumberOfLocksHeld());
35    }
36
37    protected void setUp() throws Exception JavaDoc
38    {
39       log.debug("Starting setUp()");
40       super.setUp();
41       log.debug("Finishing setUp()");
42    }
43
44    protected void tearDown() throws Exception JavaDoc
45    {
46       super.tearDown();
47       log.debug("Starting tearDown()");
48       cleanup(caches);
49       log.debug("Finishing tearDown()");
50    }
51
52    public void testSimplePut() throws Exception JavaDoc
53    {
54       log.debug("Running testSimplePut");
55       caches = createCaches(3, false);
56
57       String JavaDoc fqn = "/test";
58       String JavaDoc backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn;
59
60       assertNoStaleLocks(caches);
61
62       // put something in cache 1
63
caches[0].put(fqn, key, value);
64
65       assertNoStaleLocks(caches);
66
67       // this should be in neither of the other caches' "main" trees
68
assertEquals(value, caches[0].get(fqn, key));
69       assertNull("Should be null", caches[1].get(fqn, key));
70       assertNull("Should be null", caches[2].get(fqn, key));
71
72       // check the backup trees
73

74       assertEquals("Buddy should have data in backup tree", value, caches[1].get(backupFqn, key));
75       assertNull("Should be null", caches[2].get(backupFqn, key));
76
77       assertNoStaleLocks(caches);
78    }
79
80    public void testPutAndRemove() throws Exception JavaDoc
81    {
82       log.debug("Running testPutAndRemove");
83       caches = createCaches(3, false);
84
85       String JavaDoc fqn = "/test";
86       String JavaDoc backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn;
87
88       assertNoStaleLocks(caches);
89
90       // put something in cache 1
91
caches[0].put(fqn, key, value);
92
93       assertNoStaleLocks(caches);
94
95       // this should be in neither of the other caches' "main" trees
96
assertEquals(value, caches[0].get(fqn, key));
97       assertNull("Should be null", caches[1].get(fqn, key));
98       assertNull("Should be null", caches[2].get(fqn, key));
99
100       // check the backup trees
101

102       assertEquals("Buddy should have data in backup tree", value, caches[1].get(backupFqn, key));
103       assertNull("Should be null", caches[2].get(backupFqn, key));
104
105       assertNoStaleLocks(caches);
106
107       // now remove
108
caches[0].remove(fqn);
109       assertNoStaleLocks(caches);
110
111
112       assertNull("Should be null", caches[0].get(fqn, key));
113       assertNull("Should be null", caches[1].get(fqn, key));
114       assertNull("Should be null", caches[2].get(fqn, key));
115
116       // check the backup trees
117
assertNull("Should be null", caches[0].get(backupFqn, key));
118       assertNull("Should be null", caches[1].get(backupFqn, key));
119       assertNull("Should be null", caches[2].get(backupFqn, key));
120
121       assertNoStaleLocks(caches);
122    }
123
124    public void testPutAndRemove2() throws Exception JavaDoc
125    {
126       log.debug("Running testPutAndRemove2");
127       caches = createCaches(2, 4, false);
128
129       String JavaDoc fqn = "/test";
130       String JavaDoc backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn;
131
132       // put something in cache 1
133
assertNoStaleLocks(caches);
134
135       caches[0].put(fqn, key, value);
136
137       assertNoStaleLocks(caches);
138
139       // this should be in neither of the other caches' "main" trees
140
assertEquals(value, caches[0].get(fqn, key));
141       assertNull("Should be null", caches[1].get(fqn, key));
142       assertNull("Should be null", caches[2].get(fqn, key));
143       assertNull("Should be null", caches[3].get(fqn, key));
144
145       // check the backup trees
146
assertEquals("Buddy should have data in backup tree", value, caches[1].get(backupFqn, key));
147       assertEquals("Buddy should have data in backup tree", value, caches[2].get(backupFqn, key));
148       assertNull("Should be null", caches[3].get(backupFqn, key));
149
150       assertNoStaleLocks(caches);
151
152       // now remove
153
caches[0].remove(fqn);
154       assertNoStaleLocks(caches);
155
156       assertNull("Should be null", caches[0].get(fqn, key));
157       assertNull("Should be null", caches[1].get(fqn, key));
158       assertNull("Should be null", caches[2].get(fqn, key));
159       assertNull("Should be null", caches[3].get(fqn, key));
160
161       // check the backup trees
162
assertNull("Should be null", caches[0].get(backupFqn, key));
163       assertNull("Should be null", caches[1].get(backupFqn, key));
164       assertNull("Should be null", caches[2].get(backupFqn, key));
165       assertNull("Should be null", caches[3].get(backupFqn, key));
166
167       assertNoStaleLocks(caches);
168    }
169
170    public void testBuddyJoin() throws Exception JavaDoc
171    {
172       log.debug("Running testBuddyJoin");
173       caches = createCaches(2, false);
174       CacheImpl cache2 = null;
175
176       try
177       {
178          String JavaDoc fqn = "/test";
179          String JavaDoc backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[1].getLocalAddress()) + fqn;
180
181          assertNoStaleLocks(caches);
182
183          // put something in cache 1
184
caches[1].put(fqn, key, value);
185
186          assertNoStaleLocks(caches);
187
188          // this should be in neither of the other caches' "main" trees
189
assertEquals(value, caches[1].get(fqn, key));
190          assertNull("Should be null", caches[0].get(fqn, key));
191
192          // check the backup trees
193
assertEquals("Buddy should have data in backup tree", value, caches[0].get(backupFqn, key));
194
195          assertNoStaleLocks(caches);
196
197          // now add a new cache to the cluster
198
cache2 = createCache(1, null);
199
200          // allow this cache a few msecs to join
201
TestingUtil.sleepThread(500);
202
203          // now caches[1]'s buddy should be cache2, not cache[0]
204
assertIsBuddy(caches[1], cache2, true);
205          // this should still be the same
206
assertIsBuddy(caches[0], caches[1], true);
207          // and cache2's buddy should be cache[0]
208
assertIsBuddy(cache2, caches[0], true);
209
210          // so now the backup data we saw on cache[0] should have been removed.
211
assertNull("This backup data should have been removed", caches[0].get(backupFqn, key));
212
213          // while cache2 should now posess this backup (due to a state transfer)
214
assertEquals("Backup state should have been transferred to this new cache instance", value, cache2.get(backupFqn, key));
215
216          caches[1].remove(fqn);
217          assertNoStaleLocks(caches);
218
219
220          assertNull("Should be null", caches[0].get(fqn, key));
221          assertNull("Should be null", caches[1].get(fqn, key));
222          assertNull("Should be null", cache2.get(fqn, key));
223
224          // check the backup trees
225
assertNull("Should be null", caches[0].get(backupFqn, key));
226          assertNull("Should be null", caches[1].get(backupFqn, key));
227          assertNull("Should be null", cache2.get(backupFqn, key));
228
229          assertNoStaleLocks(caches);
230
231       }
232       finally
233       {
234          if (cache2 != null) cache2.stop();
235       }
236    }
237
238    public void testCompleteStateSurvival() throws Exception JavaDoc
239    {
240       log.debug("Running testCompleteStateSurvival");
241       caches = null;
242
243       caches = createCaches(3, false, true);
244       caches[0].put("/0", "key", "value");
245       caches[1].put("/1", "key", "value");
246       caches[2].put("/2", "key", "value");
247
248       TestingUtil.sleepThread(getSleepTimeout());
249
250       caches[2].stop();
251
252       System.out.println("*** " + caches[0].printLockInfo());
253       System.out.println("*** " + caches[1].printLockInfo());
254       System.out.println("*** " + caches[2].printLockInfo());
255
256
257       TestingUtil.sleepThread(getSleepTimeout());
258
259       assertEquals("value", caches[0].get("/2", "key"));
260
261       TestingUtil.sleepThread(getSleepTimeout());
262
263       caches[1].stop();
264
265       // cache[0] is all thats left!!
266

267
268       assertEquals("value", caches[0].get("/0", "key"));
269       assertEquals("value", caches[1].get("/1", "key"));
270       assertEquals("value", caches[2].get("/2", "key"));
271    }
272 }
273
Popular Tags