KickJava   Java API By Example, From Geeks To Geeks.

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


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 EDU.oswego.cs.dl.util.concurrent.Latch;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.jboss.cache.CacheImpl;
13 import org.jboss.cache.misc.TestingUtil;
14
15 import java.util.Map JavaDoc;
16
17 /**
18  * Tests basic group membership semantics
19  *
20  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
21  */

22 public class BuddyPoolBroadcastTest extends BuddyReplicationTestsBase
23 {
24
25    private CacheImpl[] caches;
26    private Log log = LogFactory.getLog(BuddyPoolBroadcastTest.class);
27
28    private void checkConsistentPoolState(CacheImpl[] caches)
29    {
30       for (int i = 0; i < caches.length; i++)
31       {
32          Map JavaDoc groupMap = caches[i].getBuddyManager().buddyPool;
33          for (int j = 0; j < caches.length; j++)
34          {
35             if (i != j)
36             {
37                Map JavaDoc groupMap2 = caches[j].getBuddyManager().buddyPool;
38                for (CacheImpl cache : caches)
39                {
40                   assertEquals("Comparing contents of cache " + (i + 1) + " pool map with cache " + (j + 1), groupMap.get(cache), groupMap2.get(cache));
41                }
42             }
43          }
44       }
45    }
46
47
48    protected void setUp() throws Exception JavaDoc
49    {
50       log.debug("Starting setUp()");
51       super.setUp();
52       log.debug("Finishing setUp()");
53    }
54
55    protected void tearDown() throws Exception JavaDoc
56    {
57       log.debug("Starting tearDown()");
58       super.tearDown();
59       cleanup(caches);
60       log.debug("Finishing tearDown()");
61    }
62
63
64    public void test2CachesWithPoolNames() throws Exception JavaDoc
65    {
66       log.debug("Running test2CachesWithPoolNames");
67       caches = createCaches(2, true);
68
69       BuddyManager m = caches[0].getBuddyManager();
70       Map JavaDoc groupMap = m.buddyPool;
71
72       assertEquals("A", groupMap.get(caches[0].getLocalAddress()));
73       assertEquals("B", groupMap.get(caches[1].getLocalAddress()));
74    }
75
76    public void test3CachesWithPoolNames() throws Exception JavaDoc
77    {
78       log.debug("Running test3CachesWithPoolNames");
79       caches = createCaches(3, true);
80
81       BuddyManager m = caches[0].getBuddyManager();
82       Map JavaDoc groupMap = m.buddyPool;
83
84       assertEquals("A", groupMap.get(caches[0].getLocalAddress()));
85       assertEquals("B", groupMap.get(caches[1].getLocalAddress()));
86       assertEquals("C", groupMap.get(caches[2].getLocalAddress()));
87    }
88
89    public void testBuddyPoolSync() throws Exception JavaDoc
90    {
91       log.debug("Running testBuddyPoolSync");
92       caches = createCaches(3, true);
93
94       Map JavaDoc map = caches[0].getBuddyManager().buddyPool;
95
96       // first test the values
97
assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress()));
98       assertEquals("Failed on cache 1", "B", map.get(caches[1].getLocalAddress()));
99       assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress()));
100
101       // now test against each other
102
checkConsistentPoolState(caches);
103    }
104
105    public void testChangingBuddyPoolMembership() throws Exception JavaDoc
106    {
107       log.debug("Running testChangingBuddyPoolMembership");
108       caches = createCaches(3, true);
109
110       Map JavaDoc map = caches[0].getBuddyManager().buddyPool;
111
112       // first test the values
113
assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress()));
114       assertEquals("Failed on cache 1", "B", map.get(caches[1].getLocalAddress()));
115       assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress()));
116
117       // now test against each other
118
checkConsistentPoolState(caches);
119
120       caches[1].stop();
121       caches[1] = createCache(1, "Z");
122
123       TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
124       TestingUtil.sleepThread(getSleepTimeout());
125
126       // first test the values
127
assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress()));
128       assertEquals("Failed on cache 1", "Z", map.get(caches[1].getLocalAddress()));
129       assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress()));
130
131       // now test against each other
132
checkConsistentPoolState(caches);
133    }
134
135    public void testConcurrency() throws Exception JavaDoc
136    {
137       log.debug("Running testConcurrency");
138       int numCaches = 15;
139       caches = new CacheImpl[numCaches];
140       Latch latch = new Latch();
141       CacheStarter[] starters = new CacheStarter[numCaches];
142
143       for (int i = 0; i < numCaches; i++)
144       {
145          caches[i] = createCache(1, new String JavaDoc(new char[]{(char) ('A' + i)}), false, false);
146          starters[i] = new CacheStarter(latch, caches[i]);
147          starters[i].start();
148       }
149
150       // now have the lot start simultaneously
151
TestingUtil.sleepThread(500);
152       latch.release();
153
154       // allow a generous sleep time
155
TestingUtil.blockUntilViewsReceived(caches, 240000);
156       TestingUtil.sleepThread(1000 * numCaches); // the max timeout we can expect is 2500ms * 10 nodes
157

158       // and now look at the state of things.
159
Map JavaDoc map = caches[0].getBuddyManager().buddyPool;
160       System.out.println(map);
161       for (int i = 0; i < numCaches; i++)
162       {
163          assertEquals("Failed on cache " + i + "(" + caches[i].getLocalAddress() + ")", new String JavaDoc(new char[]{(char) ('A' + i)}), map.get(caches[i].getLocalAddress()));
164       }
165
166       checkConsistentPoolState(caches);
167    }
168
169    public class CacheStarter extends Thread JavaDoc
170    {
171       private Latch latch;
172       private CacheImpl cache;
173
174       public CacheStarter(Latch latch, CacheImpl cache)
175       {
176          this.latch = latch;
177          this.cache = cache;
178       }
179
180       public void run()
181       {
182          try
183          {
184             latch.acquire();
185             cache.start();
186          }
187          catch (Exception JavaDoc e)
188          {
189             e.printStackTrace();
190          }
191       }
192    }
193
194 }
195
Popular Tags