1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
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 |
| import java.util.Map; |
15 |
| import java.util.concurrent.CountDownLatch; |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| public class BuddyPoolBroadcastTest extends BuddyReplicationTestsBase |
23 |
| { |
24 |
| |
25 |
| private CacheImpl[] caches; |
26 |
| private Log log = LogFactory.getLog(BuddyPoolBroadcastTest.class); |
27 |
| |
28 |
4
| private void checkConsistentPoolState(CacheImpl[] caches)
|
29 |
| { |
30 |
4
| for (int i = 0; i < caches.length; i++)
|
31 |
| { |
32 |
24
| Map groupMap = caches[i].getBuddyManager().buddyPool;
|
33 |
24
| for (int j = 0; j < caches.length; j++)
|
34 |
| { |
35 |
252
| if (i != j)
|
36 |
| { |
37 |
228
| Map groupMap2 = caches[j].getBuddyManager().buddyPool;
|
38 |
228
| for (CacheImpl cache : caches)
|
39 |
| { |
40 |
3204
| 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 |
5
| protected void setUp() throws Exception
|
49 |
| { |
50 |
5
| log.debug("Starting setUp()");
|
51 |
5
| super.setUp();
|
52 |
5
| log.debug("Finishing setUp()");
|
53 |
| } |
54 |
| |
55 |
5
| protected void tearDown() throws Exception
|
56 |
| { |
57 |
5
| log.debug("Starting tearDown()");
|
58 |
5
| super.tearDown();
|
59 |
5
| cleanup(caches);
|
60 |
5
| log.debug("Finishing tearDown()");
|
61 |
| } |
62 |
| |
63 |
| |
64 |
1
| public void test2CachesWithPoolNames() throws Exception
|
65 |
| { |
66 |
1
| log.debug("Running test2CachesWithPoolNames");
|
67 |
1
| caches = createCaches(2, true);
|
68 |
| |
69 |
1
| BuddyManager m = caches[0].getBuddyManager();
|
70 |
1
| Map groupMap = m.buddyPool;
|
71 |
| |
72 |
1
| assertEquals("A", groupMap.get(caches[0].getLocalAddress()));
|
73 |
1
| assertEquals("B", groupMap.get(caches[1].getLocalAddress()));
|
74 |
| } |
75 |
| |
76 |
1
| public void test3CachesWithPoolNames() throws Exception
|
77 |
| { |
78 |
1
| log.debug("Running test3CachesWithPoolNames");
|
79 |
1
| caches = createCaches(3, true);
|
80 |
| |
81 |
1
| BuddyManager m = caches[0].getBuddyManager();
|
82 |
1
| Map groupMap = m.buddyPool;
|
83 |
| |
84 |
1
| assertEquals("A", groupMap.get(caches[0].getLocalAddress()));
|
85 |
1
| assertEquals("B", groupMap.get(caches[1].getLocalAddress()));
|
86 |
1
| assertEquals("C", groupMap.get(caches[2].getLocalAddress()));
|
87 |
| } |
88 |
| |
89 |
1
| public void testBuddyPoolSync() throws Exception
|
90 |
| { |
91 |
1
| log.debug("Running testBuddyPoolSync");
|
92 |
1
| caches = createCaches(3, true);
|
93 |
| |
94 |
1
| Map map = caches[0].getBuddyManager().buddyPool;
|
95 |
| |
96 |
| |
97 |
1
| assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress()));
|
98 |
1
| assertEquals("Failed on cache 1", "B", map.get(caches[1].getLocalAddress()));
|
99 |
1
| assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress()));
|
100 |
| |
101 |
| |
102 |
1
| checkConsistentPoolState(caches);
|
103 |
| } |
104 |
| |
105 |
1
| public void testChangingBuddyPoolMembership() throws Exception
|
106 |
| { |
107 |
1
| log.debug("Running testChangingBuddyPoolMembership");
|
108 |
1
| caches = createCaches(3, true);
|
109 |
| |
110 |
1
| Map map = caches[0].getBuddyManager().buddyPool;
|
111 |
| |
112 |
| |
113 |
1
| assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress()));
|
114 |
1
| assertEquals("Failed on cache 1", "B", map.get(caches[1].getLocalAddress()));
|
115 |
1
| assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress()));
|
116 |
| |
117 |
| |
118 |
1
| checkConsistentPoolState(caches);
|
119 |
| |
120 |
1
| caches[1].stop();
|
121 |
1
| caches[1] = createCache(1, "Z");
|
122 |
| |
123 |
1
| TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
|
124 |
1
| TestingUtil.sleepThread(getSleepTimeout());
|
125 |
| |
126 |
| |
127 |
1
| assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress()));
|
128 |
1
| assertEquals("Failed on cache 1", "Z", map.get(caches[1].getLocalAddress()));
|
129 |
1
| assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress()));
|
130 |
| |
131 |
| |
132 |
1
| checkConsistentPoolState(caches);
|
133 |
| } |
134 |
| |
135 |
1
| public void testConcurrency() throws Exception
|
136 |
| { |
137 |
1
| log.debug("Running testConcurrency");
|
138 |
1
| int numCaches = 15;
|
139 |
1
| caches = new CacheImpl[numCaches];
|
140 |
1
| CountDownLatch latch = new CountDownLatch(1);
|
141 |
1
| CacheStarter[] starters = new CacheStarter[numCaches];
|
142 |
| |
143 |
1
| for (int i = 0; i < numCaches; i++)
|
144 |
| { |
145 |
15
| caches[i] = createCache(1, new String(new char[]{(char) ('A' + i)}), false, false);
|
146 |
15
| starters[i] = new CacheStarter(latch, caches[i]);
|
147 |
15
| starters[i].start();
|
148 |
| } |
149 |
| |
150 |
| |
151 |
1
| TestingUtil.sleepThread(500);
|
152 |
1
| latch.countDown();
|
153 |
| |
154 |
| |
155 |
1
| TestingUtil.blockUntilViewsReceived(caches, 240000);
|
156 |
1
| TestingUtil.sleepThread(1000 * numCaches);
|
157 |
| |
158 |
| |
159 |
1
| Map map = caches[0].getBuddyManager().buddyPool;
|
160 |
1
| System.out.println(map);
|
161 |
1
| for (int i = 0; i < numCaches; i++)
|
162 |
| { |
163 |
15
| assertEquals("Failed on cache " + i + "(" + caches[i].getLocalAddress() + ")", new String(new char[]{(char) ('A' + i)}), map.get(caches[i].getLocalAddress()));
|
164 |
| } |
165 |
| |
166 |
1
| checkConsistentPoolState(caches);
|
167 |
| } |
168 |
| |
169 |
| public class CacheStarter extends Thread |
170 |
| { |
171 |
| private CountDownLatch latch; |
172 |
| private CacheImpl cache; |
173 |
| |
174 |
15
| public CacheStarter(CountDownLatch latch, CacheImpl cache)
|
175 |
| { |
176 |
15
| this.latch = latch;
|
177 |
15
| this.cache = cache;
|
178 |
| } |
179 |
| |
180 |
15
| public void run()
|
181 |
| { |
182 |
15
| try
|
183 |
| { |
184 |
15
| latch.await();
|
185 |
15
| cache.start();
|
186 |
| } |
187 |
| catch (Exception e) |
188 |
| { |
189 |
0
| e.printStackTrace();
|
190 |
| } |
191 |
| } |
192 |
| } |
193 |
| |
194 |
| } |