KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > test > FamilyClusterInfoUnitTestCase


1 package org.jboss.test.cluster.test;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.ListIterator JavaDoc;
7
8 import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository;
9 import org.jboss.ha.framework.interfaces.FamilyClusterInfo;
10
11 import junit.framework.TestCase;
12
13 /**
14  * Test of FamilyClusterInfoImpl.
15  *
16  * @author <a HREF="brian.stansberry@jboss.com">Brian Stansberry</a>
17  * @version $Revision: 1.1 $
18  */

19 public class FamilyClusterInfoUnitTestCase extends TestCase
20 {
21    private abstract class FCIChanger extends Thread JavaDoc
22    {
23       private boolean started;
24       private boolean stopped;
25       private Throwable JavaDoc caught;
26       
27       protected FamilyClusterInfo fci;
28       
29       FCIChanger(FamilyClusterInfo fci)
30       {
31          this.fci = fci;
32       }
33       
34       abstract void update();
35       
36       
37       public void run()
38       {
39          try
40          {
41             started = true;
42             
43             while (!stopped)
44             {
45                if (interrupted())
46                   throw new InterruptedException JavaDoc();
47                
48                update();
49             }
50             
51          }
52          catch (InterruptedException JavaDoc ie)
53          {
54             if (!stopped)
55                caught = ie;
56          }
57          catch (Throwable JavaDoc t)
58          {
59             caught = t;
60          }
61          finally
62          {
63             started = false;
64          }
65       }
66       
67       void beginUpdates()
68       {
69          this.start();
70          while (!started)
71          {
72             try
73             {
74                sleep(10);
75             }
76             catch (InterruptedException JavaDoc e)
77             {
78                caught = e;
79             }
80          }
81       }
82       
83       void endUpdates()
84       {
85          long start = System.currentTimeMillis();
86          
87          while (started && (System.currentTimeMillis() - start) < 100)
88          {
89             stopped = true;
90          }
91          
92          if (started)
93             interrupt();
94       }
95       
96       Throwable JavaDoc getThrowable()
97       {
98          return caught;
99       }
100    }
101    
102    private class Updater extends FCIChanger
103    {
104       private ArrayList JavaDoc targets = new ArrayList JavaDoc();
105       private int viewId;
106       
107       Updater(FamilyClusterInfo fci)
108       {
109          super(fci);
110       }
111       
112       void update()
113       {
114          targets.add(new Object JavaDoc());
115          viewId++;
116          fci.updateClusterInfo((ArrayList JavaDoc) targets.clone(), viewId);
117       }
118    }
119    
120    private class Remover extends FCIChanger
121    {
122       Remover(FamilyClusterInfo fci)
123       {
124          super(fci);
125       }
126       
127       void update()
128       {
129          List JavaDoc targets = fci.getTargets();
130          if (targets.size() > 0)
131             fci.removeDeadTarget(targets.get(0));
132       }
133       
134    }
135    
136    private class Resetter extends FCIChanger
137    {
138       Resetter(FamilyClusterInfo fci)
139       {
140          super(fci);
141       }
142       
143       void update()
144       {
145          fci.resetView();
146       }
147    }
148    
149    public void testSynchronization() throws Exception JavaDoc
150    {
151       ClusteringTargetsRepository.initTarget("testSynchronization", new ArrayList JavaDoc(), 0);
152       FamilyClusterInfo fci = ClusteringTargetsRepository.getFamilyClusterInfo("testSynchronization");
153       
154       Updater updater = new Updater(fci);
155       Remover remover = new Remover(fci);
156       Resetter resetter = new Resetter(fci);
157       
158       updater.beginUpdates();
159       
160       checkFCIConsistency(fci, 150, false);
161       
162       remover.beginUpdates();
163       
164       checkFCIConsistency(fci, 150, true);
165       
166       resetter.beginUpdates();
167       
168       checkFCIConsistency(fci, 150, true);
169       
170       updater.endUpdates();
171       remover.endUpdates();
172       resetter.endUpdates();
173       
174       assertNull("Updater had no exceptions", updater.getThrowable());
175       assertNull("Remover had no exceptions", remover.getThrowable());
176       assertNull("Resetter had no exceptions", resetter.getThrowable());
177       
178    }
179    
180    public void testTargetListImmutability() throws Exception JavaDoc
181    {
182       ArrayList JavaDoc targets = new ArrayList JavaDoc();
183       targets.add("ONE");
184       targets.add("TWO");
185       ClusteringTargetsRepository.initTarget("testSynchronization", targets, 0);
186       FamilyClusterInfo fci = ClusteringTargetsRepository.getFamilyClusterInfo("testSynchronization");
187       
188       List JavaDoc fciTargets = fci.getTargets();
189       
190       try
191       {
192          fciTargets.add("FAIL");
193          fail("add call did not fail");
194       }
195       catch (UnsupportedOperationException JavaDoc good) {}
196       
197       try
198       {
199          fciTargets.addAll(targets);
200          fail("addAll call did not fail");
201       }
202       catch (UnsupportedOperationException JavaDoc good) {}
203       
204       try
205       {
206          fciTargets.clear();
207          fail("clear call did not fail");
208       }
209       catch (UnsupportedOperationException JavaDoc good) {}
210       
211       try
212       {
213          fciTargets.remove(0);
214          fail("remove call did not fail");
215       }
216       catch (UnsupportedOperationException JavaDoc good) {}
217       
218       try
219       {
220          fciTargets.set(0, "FAIL");
221          fail("set call did not fail");
222       }
223       catch (UnsupportedOperationException JavaDoc good) {}
224       
225       try
226       {
227          fciTargets.removeAll(targets);
228          fail("removeAll call did not fail");
229       }
230       catch (UnsupportedOperationException JavaDoc good) {}
231       
232       try
233       {
234          fciTargets.retainAll(targets);
235          fail("retainAll call did not fail");
236       }
237       catch (UnsupportedOperationException JavaDoc good) {}
238       
239       Iterator JavaDoc iter = fciTargets.iterator();
240       int count = 0;
241       while (iter.hasNext())
242       {
243          iter.next();
244          count++;
245       }
246       assertEquals("Correct count", 2, count);
247       try
248       {
249          iter.remove();
250          fail("Iterator.remove call did not fail");
251       }
252       catch (UnsupportedOperationException JavaDoc good) {}
253       
254       ListIterator JavaDoc listIter = fciTargets.listIterator();
255       count = 0;
256       while (listIter.hasNext())
257       {
258          listIter.next();
259          count++;
260       }
261       assertEquals("Correct count", 2, count);
262       try
263       {
264          listIter.remove();
265          fail("Iterator.remove call did not fail");
266       }
267       catch (UnsupportedOperationException JavaDoc good) {}
268       
269       while (listIter.hasPrevious())
270       {
271          listIter.previous();
272          count--;
273       }
274       assertEquals("Correct count", 0, count);
275       
276       listIter = fciTargets.listIterator(1);
277       while (listIter.hasNext())
278       {
279          listIter.next();
280          count++;
281       }
282       assertEquals("Correct count", 1, count);
283       try
284       {
285          listIter.remove();
286          fail("Iterator.remove call did not fail");
287       }
288       catch (UnsupportedOperationException JavaDoc good) {}
289       
290       while (listIter.hasPrevious())
291       {
292          listIter.previous();
293          count--;
294       }
295       assertEquals("Correct count", -1, count);
296       
297       // Check the lists returned by other methods
298
fciTargets = fci.updateClusterInfo(targets, 0);
299
300       try
301       {
302          fciTargets.add("FAIL");
303          fail("add call did not fail");
304       }
305       catch (UnsupportedOperationException JavaDoc good) {}
306       
307       fciTargets = fci.removeDeadTarget(targets.get(0));
308
309       try
310       {
311          fciTargets.add("FAIL");
312          fail("add call did not fail");
313       }
314       catch (UnsupportedOperationException JavaDoc good) {}
315       
316    }
317       
318    private void checkFCIConsistency(FamilyClusterInfo fci, int checks, boolean allowOutOfSync)
319    {
320       
321       for (int i = 0; i < checks; i++)
322       {
323          synchronized (fci)
324          {
325             if (fci.currentMembershipInSyncWithViewId())
326             {
327                List JavaDoc targets = fci.getTargets();
328                long vid = fci.getCurrentViewId();
329                assertEquals("targets and vid match", vid, targets.size());
330             }
331             else
332             {
333                assertTrue("OK for FCI view to be out of sync", allowOutOfSync);
334             }
335          }
336       }
337       
338    }
339 }
340
Popular Tags