[Jboss-cvs] JBossAS SVN: r54939 - trunk/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 16:07:46 EDT 2006


Author: bstansberry at jboss.com
Date: 2006-07-31 16:07:45 -0400 (Mon, 31 Jul 2006)
New Revision: 54939

Added:
   trunk/testsuite/src/main/org/jboss/test/cluster/test/FamilyClusterInfoUnitTestCase.java
Log:
[JBAS-2071] Test for controlled access to FamilyClusterInfo

Added: trunk/testsuite/src/main/org/jboss/test/cluster/test/FamilyClusterInfoUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/cluster/test/FamilyClusterInfoUnitTestCase.java	2006-07-31 19:53:58 UTC (rev 54938)
+++ trunk/testsuite/src/main/org/jboss/test/cluster/test/FamilyClusterInfoUnitTestCase.java	2006-07-31 20:07:45 UTC (rev 54939)
@@ -0,0 +1,199 @@
+package org.jboss.test.cluster.test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+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());
+      
+   }
+      
+      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