[Jboss-cvs] JBossAS SVN: r54949 - branches/Branch_4_0/testsuite/src/main/org/jboss/test/cluster/test

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Jul 31 17:54:16 EDT 2006


Author: bstansberry at jboss.com
Date: 2006-07-31 17:54:15 -0400 (Mon, 31 Jul 2006)
New Revision: 54949

Added:
   branches/Branch_4_0/testsuite/src/main/org/jboss/test/cluster/test/FamilyClusterInfoUnitTestCase.java
Log:
[JBAS-2225] Test target list is immutable

Added: branches/Branch_4_0/testsuite/src/main/org/jboss/test/cluster/test/FamilyClusterInfoUnitTestCase.java
===================================================================
--- branches/Branch_4_0/testsuite/src/main/org/jboss/test/cluster/test/FamilyClusterInfoUnitTestCase.java	2006-07-31 21:48:38 UTC (rev 54948)
+++ branches/Branch_4_0/testsuite/src/main/org/jboss/test/cluster/test/FamilyClusterInfoUnitTestCase.java	2006-07-31 21:54:15 UTC (rev 54949)
@@ -0,0 +1,353 @@
+package org.jboss.test.cluster.test;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository;
+import org.jboss.ha.framework.interfaces.FamilyClusterInfo;
+
+import junit.framework.TestCase;
+
+/**
+ * Test of FamilyClusterInfoImpl.
+ * 
+ * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
+ * @version $Revision: 1.1 $
+ */
+public class FamilyClusterInfoUnitTestCase extends TestCase
+{
+   private abstract class FCIChanger extends Thread
+   {
+      private boolean started;
+      private boolean stopped;
+      private Throwable caught;
+      
+      protected FamilyClusterInfo fci;
+      
+      FCIChanger(FamilyClusterInfo fci)
+      {
+         this.fci = fci;
+      }
+      
+      abstract void update();
+      
+      
+      public void run()
+      {
+         try
+         {
+            started = true;
+            
+            while (!stopped)
+            {
+               if (interrupted())
+                  throw new InterruptedException();
+               
+               update();
+            }
+            
+         }
+         catch (InterruptedException ie)
+         {
+            if (!stopped)
+               caught = ie;
+         }
+         catch (Throwable t)
+         {
+            caught = t;
+         }
+         finally
+         {
+            started = false;
+         }
+      }
+      
+      void beginUpdates()
+      {
+         this.start();
+         while (!started)
+         {
+            try
+            {
+               sleep(10);
+            }
+            catch (InterruptedException e)
+            {
+               caught = e;
+            }
+         }
+      }
+      
+      void endUpdates()
+      {
+         long start = System.currentTimeMillis();
+         
+         while (started && (System.currentTimeMillis() - start) < 100)
+         {
+            stopped = true;
+         }
+         
+         if (started)
+            interrupt();
+      }
+      
+      Throwable getThrowable()
+      {
+         return caught;
+      }
+   }
+   
+   private class Updater extends FCIChanger
+   {
+      private ArrayList targets = new ArrayList();
+      private int viewId;
+      
+      Updater(FamilyClusterInfo fci)
+      {
+         super(fci);
+      }
+      
+      void update()
+      {
+         targets.add(new Object());
+         viewId++;
+         fci.updateClusterInfo((ArrayList) targets.clone(), viewId);         
+      }
+   }
+   
+   private class Remover extends FCIChanger
+   {
+      Remover(FamilyClusterInfo fci)
+      {
+         super(fci);
+      }
+      
+      void update()
+      {
+         List targets = fci.getTargets();
+         if (targets.size() > 0)
+            fci.removeDeadTarget(targets.get(0));
+      }
+      
+   }
+   
+   private class Resetter extends FCIChanger
+   {
+      Resetter(FamilyClusterInfo fci)
+      {
+         super(fci);
+      }
+      
+      void update()
+      {
+         fci.resetView();
+      }      
+   }
+   
+   public void testSynchronization() throws Exception
+   {
+      ClusteringTargetsRepository.initTarget("testSynchronization", new ArrayList(), 0);
+      FamilyClusterInfo fci = ClusteringTargetsRepository.getFamilyClusterInfo("testSynchronization");
+      
+      Updater updater = new Updater(fci);
+      Remover remover = new Remover(fci);
+      Resetter resetter = new Resetter(fci);
+      
+      updater.beginUpdates();
+      
+      checkFCIConsistency(fci, 150, false);
+      
+      remover.beginUpdates();
+      
+      checkFCIConsistency(fci, 150, true);
+      
+      resetter.beginUpdates();
+      
+      checkFCIConsistency(fci, 150, true);
+      
+      updater.endUpdates();
+      remover.endUpdates();
+      resetter.endUpdates();
+      
+      assertNull("Updater had no exceptions", updater.getThrowable());
+      assertNull("Remover had no exceptions", remover.getThrowable());
+      assertNull("Resetter had no exceptions", resetter.getThrowable());
+      
+   }
+   
+   public void testTargetListImmutability() throws Exception
+   {
+      ArrayList targets = new ArrayList();
+      targets.add("ONE");
+      targets.add("TWO");
+      ClusteringTargetsRepository.initTarget("testSynchronization", targets, 0);
+      FamilyClusterInfo fci = ClusteringTargetsRepository.getFamilyClusterInfo("testSynchronization");
+      
+      ArrayList fciTargets = fci.getTargets();
+      
+      try 
+      { 
+         fciTargets.add("FAIL");
+         fail("add call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      try 
+      { 
+         fciTargets.addAll(targets);
+         fail("addAll call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      try 
+      { 
+         fciTargets.clear();
+         fail("clear call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      try 
+      { 
+         fciTargets.ensureCapacity(5);
+         fail("ensureCapacity call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      try 
+      { 
+         fciTargets.remove(0);
+         fail("remove call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      try 
+      { 
+         fciTargets.set(0, "FAIL");
+         fail("set call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      try 
+      { 
+         fciTargets.trimToSize();
+         fail("trimToSize call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      try 
+      { 
+         fciTargets.removeAll(targets);
+         fail("removeAll call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      try 
+      { 
+         fciTargets.retainAll(targets);
+         fail("retainAll call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      Iterator iter = fciTargets.iterator();
+      int count = 0;
+      while (iter.hasNext())
+      {
+         iter.next();
+         count++;         
+      }
+      assertEquals("Correct count", 2, count);
+      try 
+      { 
+         iter.remove();
+         fail("Iterator.remove call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      ListIterator listIter = fciTargets.listIterator();
+      count = 0;
+      while (listIter.hasNext())
+      {
+         listIter.next();
+         count++;         
+      }
+      assertEquals("Correct count", 2, count);
+      try 
+      { 
+         listIter.remove();
+         fail("Iterator.remove call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      while (listIter.hasPrevious())
+      {
+         listIter.previous();
+         count--;
+      }
+      assertEquals("Correct count", 0, count);
+      
+      listIter = fciTargets.listIterator(1);
+      while (listIter.hasNext())
+      {
+         listIter.next();
+         count++;         
+      }
+      assertEquals("Correct count", 1, count);
+      try 
+      { 
+         listIter.remove();
+         fail("Iterator.remove call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      while (listIter.hasPrevious())
+      {
+         listIter.previous();
+         count--;
+      }
+      assertEquals("Correct count", -1, count);
+      
+      // Check the lists returned by other methods
+      fciTargets = fci.updateClusterInfo(targets, 0);
+
+      try 
+      { 
+         fciTargets.add("FAIL");
+         fail("add call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+      fciTargets = fci.removeDeadTarget(targets.get(0));
+
+      try 
+      { 
+         fciTargets.add("FAIL");
+         fail("add call did not fail");
+      } 
+      catch (UnsupportedOperationException good) {}
+      
+   }
+      
+   private void checkFCIConsistency(FamilyClusterInfo fci, int checks, boolean allowOutOfSync)
+   {
+      
+      for (int i = 0; i < checks; i++)
+      {
+         synchronized (fci)
+         {
+            if (fci.currentMembershipInSyncWithViewId())
+            {
+               List targets = fci.getTargets();
+               long vid = fci.getCurrentViewId();
+               assertEquals("targets and vid match", vid, targets.size());
+            }
+            else
+            {
+               assertTrue("OK for FCI view to be out of sync", allowOutOfSync);
+            }
+         }
+      }
+      
+   }
+}




More information about the jboss-cvs-commits mailing list