KickJava   Java API By Example, From Geeks To Geeks.

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


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.jboss.cache.CacheImpl;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.misc.TestingUtil;
12
13 import java.io.File JavaDoc;
14
15 /**
16  * Tests how groups are formed and disbanded
17  *
18  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
19  */

20 public class BuddyAssignmentStateTransferTest extends BuddyReplicationTestsBase
21 {
22
23    protected int timeout = 10000; // !!!
24

25    protected int getSleepTimeout()
26    {
27       return timeout;
28    }
29
30    public void testNonRegionBasedStateTransfer() throws Exception JavaDoc
31    {
32       CacheImpl[] caches = new CacheImpl[2];
33       try
34       {
35          caches[0] = createCache(1, "TEST", false, true);
36
37          Fqn main = Fqn.fromString("/a/b/c");
38          caches[0].put(main, "name", "Joe");
39
40          caches[1] = createCache(1, "TEST", false, true);
41
42          TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
43          TestingUtil.sleepThread(getSleepTimeout());
44
45          Fqn test = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN,
46                  BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()));
47          test = new Fqn(test, main);
48
49          assertEquals("State transferred", "Joe", caches[1].get(test, "name"));
50
51          CacheImpl[] old = caches;
52          caches = new CacheImpl[3];
53          System.arraycopy(old, 0, caches, 0, old.length);
54          caches[2] = createCache(1, "TEST", false, true);
55
56          TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
57          TestingUtil.sleepThread(getSleepTimeout());
58
59          assertNull("State not transferred", caches[2].get(test, "name"));
60
61          // Make 2 the buddy of 0
62
caches[1].stop();
63          caches[1] = null;
64
65          TestingUtil.sleepThread(getSleepTimeout());
66
67          assertEquals("State transferred", "Joe", caches[2].get(test, "name"));
68       }
69       finally
70       {
71          cleanup(caches);
72       }
73    }
74
75    public void testRegionBasedStateTransfer() throws Exception JavaDoc
76    {
77
78       // TODO: Reinstate once we have proper FLUSH working.
79
// This test relies on calls to inactive regions being queued and re-run
80
// when the region is activated. After discussions with Brian, the queueing
81
// was removed, presuming FLUSH will fix this. Need to test with FLUSH.
82
// - Manik Surtani (16 Oct 2006)
83
/*
84       CacheImpl[] caches = new CacheImpl[3];
85
86       try
87       {
88          caches[0] = createCache(1, "TEST", false, false);
89          caches[1] = createCache(1, "TEST", false, false);
90          caches[2] = createCache(1, "TEST", false, false);
91          caches[0].getConfiguration().setInactiveOnStartup(true);
92          caches[1].getConfiguration().setInactiveOnStartup(true);
93          caches[2].getConfiguration().setInactiveOnStartup(true);
94          caches[0].getConfiguration().setUseRegionBasedMarshalling(true);
95          caches[1].getConfiguration().setUseRegionBasedMarshalling(true);
96          caches[2].getConfiguration().setUseRegionBasedMarshalling(true);
97          caches[0].start();
98          caches[1].start();
99          caches[2].start();
100
101          TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
102          TestingUtil.sleepThread(getSleepTimeout());
103
104          caches[0].activateRegion("/a");
105          caches[1].activateRegion("/a");
106          caches[2].activateRegion("/a");
107
108          caches[0].activateRegion("/d");
109          caches[1].activateRegion("/d");
110
111          Fqn mainA = Fqn.fromString("/a/b/c");
112          caches[0].put(mainA, "name", "Joe");
113
114          Fqn mainD = Fqn.fromString("/d/e/f");
115          caches[0].put(mainD, "name", "Joe");
116
117          Fqn group = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN,
118                  BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()));
119          Fqn testA = new Fqn(group, mainA);
120          assertEquals("/a replicated", "Joe", caches[1].get(testA, "name"));
121          assertNull("No backup of /a", caches[2].get(testA, "name"));
122
123          Fqn testD = new Fqn(group, mainD);
124          assertEquals("/d replicated", "Joe", caches[1].get(testD, "name"));
125          assertNull("No backup of /d", caches[2].get(testD, "name"));
126
127          // Make 2 the buddy of 0
128          caches[1].stop();
129          caches[1] = null;
130
131          TestingUtil.sleepThread(getSleepTimeout());
132
133          assertEquals("/a state transferred", "Joe", caches[2].get(testA, "name"));
134          assertNull("/d state not transferred", caches[2].get(testD, "name"));
135       }
136       finally
137       {
138          cleanup(caches);
139       }
140       */

141    }
142
143    public void testPersistentStateTransfer() throws Exception JavaDoc
144    {
145       String JavaDoc tmpLoc = System.getProperty("java.io.tmpdir", "/tmp");
146       tmpLoc += File.separator + "BuddyReplicationTestsBase-";
147       String JavaDoc tmpLoc0 = tmpLoc + 0;
148       String JavaDoc tmpLoc1 = tmpLoc + 1;
149       CacheImpl[] caches = new CacheImpl[2];
150
151       try
152       {
153          caches[0] = createCacheWithCacheLoader(tmpLoc0, false, false, false, true, false);
154          caches[0].getConfiguration().setFetchInMemoryState(false);
155
156          caches[0].start();
157
158          Fqn main = Fqn.fromString("/a/b/c");
159          caches[0].put(main, "name", "Joe");
160
161          caches[1] = createCacheWithCacheLoader(tmpLoc1, false, false, false, true, false);
162          caches[1].getConfiguration().setFetchInMemoryState(false);
163
164          caches[1].start();
165
166          TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
167          TestingUtil.sleepThread(getSleepTimeout());
168
169          Fqn group = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN,
170                  BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()));
171          Fqn test = new Fqn(group, main);
172
173          assertFalse("/a/b/c not in memory", caches[1].exists(test));
174          assertEquals("/a/b/c in cache loader", "Joe", caches[1].get(test, "name"));
175       }
176       finally
177       {
178          cleanup(caches);
179       }
180    }
181 }
182
Popular Tags