[jboss-cvs] JBossAS SVN: r73569 - in projects/cluster/ha-client/trunk/src: main/java/org/jboss/ha/client/loadbalance/aop and 4 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed May 21 19:22:01 EDT 2008


Author: bstansberry at jboss.com
Date: 2008-05-21 19:22:01 -0400 (Wed, 21 May 2008)
New Revision: 73569

Added:
   projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/
   projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/FirstAvailable.java
   projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/FirstAvailableIdenticalAllProxies.java
   projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/
   projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/
   projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/FirstAvailableIdenticalAllProxiesUnitTestCase.java
   projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/FirstAvailableUnitTestCase.java
   projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/
   projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/FirstAvailableIdenticalAllProxiesUnitTestCase.java
   projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/FirstAvailableUnitTestCase.java
   projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/MockInvocation.java
Modified:
   projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/FirstAvailable.java
   projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/FirstAvailableIdenticalAllProxies.java
Log:
[JBCLUSTER-199] AOP-based version of FirstAvailable that checks invocation metadata for initial target

Modified: projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/FirstAvailable.java
===================================================================
--- projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/FirstAvailable.java	2008-05-21 19:37:33 UTC (rev 73568)
+++ projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/FirstAvailable.java	2008-05-21 23:22:01 UTC (rev 73569)
@@ -61,7 +61,7 @@
 
    // Attributes ----------------------------------------------------
    
-   private Object electedTarget = null;
+   protected Object electedTarget = null;
    
    // Static --------------------------------------------------------
    
@@ -81,9 +81,14 @@
       }
       else
       {
-         int cursor = RandomRobin.localRandomizer.nextInt(targets.size());
-         this.electedTarget = targets.get(cursor);
+         this.electedTarget = electTarget(targets);
          return this.electedTarget;
       }
    }
+
+   protected Object electTarget(List targets)
+   {
+      int cursor = RandomRobin.localRandomizer.nextInt(targets.size());
+      return targets.get(cursor);
+   }
 }

Modified: projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/FirstAvailableIdenticalAllProxies.java
===================================================================
--- projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/FirstAvailableIdenticalAllProxies.java	2008-05-21 19:37:33 UTC (rev 73568)
+++ projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/FirstAvailableIdenticalAllProxies.java	2008-05-21 23:22:01 UTC (rev 73569)
@@ -70,11 +70,16 @@
       }
       else
       {
-         int cursor = RandomRobin.localRandomizer.nextInt (targets.size());
-         target = targets.get(cursor);
+         target = electTarget(targets);
          clusterFamily.setObject (target);
          return target;
       }   
    }
 
+   protected Object electTarget(List targets)
+   {
+      int cursor = RandomRobin.localRandomizer.nextInt(targets.size());
+      return targets.get(cursor);
+   }
+
 }

Added: projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/FirstAvailable.java
===================================================================
--- projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/FirstAvailable.java	                        (rev 0)
+++ projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/FirstAvailable.java	2008-05-21 23:22:01 UTC (rev 73569)
@@ -0,0 +1,95 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2008, Red Hat Middleware, LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ha.client.loadbalance.aop;
+
+import java.util.List;
+
+import org.jboss.aop.joinpoint.Invocation;
+import org.jboss.aspects.remoting.ClusterConstants;
+import org.jboss.ha.client.loadbalance.AopLoadBalancePolicy;
+import org.jboss.ha.framework.interfaces.FamilyClusterInfo;
+
+
+/**
+ * LoadBalancePolicy implementation that, once a target is chosen, always favors 
+ * that same target; i.e. no further load balancing occurs. Useful in cases where
+ * "sticky session" behavior is desired. Differs from the 
+ * {@link org.jboss.ha.client.loadbalance.FirstAvailable superclass} in the way
+ * that the {@link #chooseTarget(FamilyClusterInfo, Invocation)} method 
+ * initially selects the target. Instead of always randomly selecting the target,
+ * the method will check the passed invocation's metadata for the 
+ * {@link ClusterConstants#HA_TARGET} key in the 
+ * {@link ClusterConstants#CLUSTERED_REMOTING} group.  If an object is found 
+ * there and it is a valid target associated with the given FamilyClusterInfo,
+ * that target will be used. Otherwise the target will be randomly chosen.
+ * This mechanism allows the proxy that contains this policy to specify the 
+ * preferred target via invocation metadata.
+ * <p> 
+ * Each proxy using this policy will choose its own
+ * preferred target: the target is not shared accross the proxy family. For 
+ * shared behaviour please take a look at 
+ * {@link FirstAvailableIdenticalAllProxies}.
+ * </p>
+ * 
+ * @author Brian Stansberry
+ *  
+ * @version $Revision: 57642 $
+ */
+public class FirstAvailable 
+extends org.jboss.ha.client.loadbalance.FirstAvailable
+implements AopLoadBalancePolicy
+{
+   // Constants -----------------------------------------------------
+   
+   private static final long serialVersionUID = 2008524502721775114L;
+
+   // Static --------------------------------------------------------
+   
+   // Constructors --------------------------------------------------
+       
+    // Public --------------------------------------------------------
+
+   public Object chooseTarget (FamilyClusterInfo clusterFamily, Invocation invocation)
+   {
+      List targets = clusterFamily.getTargets ();
+      if (targets.size () == 0)
+         return null;
+
+      if ( (this.electedTarget != null) && targets.contains (this.electedTarget) )
+      {
+         return this.electedTarget;
+      }
+      else
+      {
+         this.electedTarget = electTarget(targets, invocation);
+         return this.electedTarget;
+      }
+   }
+
+   private Object electTarget(List targets, Invocation invocation)
+   {
+      Object target = invocation.getMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.HA_TARGET);
+      if (target == null || !targets.contains(target))
+         target = electTarget(targets);
+      return target;
+   }
+}


Property changes on: projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/FirstAvailable.java
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/FirstAvailableIdenticalAllProxies.java
===================================================================
--- projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/FirstAvailableIdenticalAllProxies.java	                        (rev 0)
+++ projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/FirstAvailableIdenticalAllProxies.java	2008-05-21 23:22:01 UTC (rev 73569)
@@ -0,0 +1,90 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ha.client.loadbalance.aop;
+
+import java.util.List;
+
+import org.jboss.aop.joinpoint.Invocation;
+import org.jboss.aspects.remoting.ClusterConstants;
+import org.jboss.ha.client.loadbalance.AopLoadBalancePolicy;
+import org.jboss.ha.framework.interfaces.FamilyClusterInfo;
+
+
+
+/**
+ * Extends the "sticky session" behavior of {@link FirstAvailable} by caching 
+ * the favored target in the {@link FamilyClusterInfo}, thus allowing different 
+ * proxies for the same family to use the same favored target.
+ * <p>
+ * See {@link FirstAvailable} for the basic behavior.
+ * </p>
+ * 
+ * @see org.jboss.ha.framework.interfaces.FamilyClusterInfo#getObject()
+ *
+ * @author Brian Stansberry
+ * 
+ * @version $Revision: 57642 $
+ */
+public class FirstAvailableIdenticalAllProxies 
+extends org.jboss.ha.client.loadbalance.FirstAvailableIdenticalAllProxies 
+implements AopLoadBalancePolicy
+{
+   // Constants -----------------------------------------------------
+   private static final long serialVersionUID = 2910756623413400467L;
+
+   // Attributes ----------------------------------------------------
+   
+   // Static --------------------------------------------------------
+   
+   // Constructors --------------------------------------------------
+       
+   // Public --------------------------------------------------------
+
+   public Object chooseTarget (FamilyClusterInfo clusterFamily, Invocation invocation)
+   {
+      Object target = clusterFamily.getObject ();
+      List targets = clusterFamily.getTargets ();
+
+      if (targets.size () == 0)
+         return null;
+      
+      if (target != null && targets.contains (target) )
+      {
+         return target;
+      }
+      else
+      {
+         target = electTarget(targets, invocation);
+         clusterFamily.setObject (target);
+         return target;
+      }   
+   }
+
+   private Object electTarget(List targets, Invocation invocation)
+   {
+      Object target = invocation.getMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.HA_TARGET);
+      if (target == null || !targets.contains(target))
+         target = electTarget(targets);
+      return target;
+   }
+
+}


Property changes on: projects/cluster/ha-client/trunk/src/main/java/org/jboss/ha/client/loadbalance/aop/FirstAvailableIdenticalAllProxies.java
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/FirstAvailableIdenticalAllProxiesUnitTestCase.java
===================================================================
--- projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/FirstAvailableIdenticalAllProxiesUnitTestCase.java	                        (rev 0)
+++ projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/FirstAvailableIdenticalAllProxiesUnitTestCase.java	2008-05-21 23:22:01 UTC (rev 73569)
@@ -0,0 +1,98 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.test.ha.client.loadbalance;
+
+import java.util.Arrays;
+
+import org.jboss.ha.client.loadbalance.FirstAvailableIdenticalAllProxies;
+import org.jboss.ha.client.loadbalance.LoadBalancePolicy;
+import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository;
+import org.jboss.ha.framework.interfaces.FamilyClusterInfo;
+
+/**
+ * Tests of the FirstAvailable load balance policy.
+ * 
+ * @author Brian Stansberry
+ *
+ */
+public class FirstAvailableIdenticalAllProxiesUnitTestCase extends FirstAvailableUnitTestCase
+{
+
+   /**
+    * Create a new FirstAvailableIdenticalAllProxiesUnitTestCase.
+    * 
+    * @param name
+    */
+   public FirstAvailableIdenticalAllProxiesUnitTestCase(String name)
+   {
+      super(name);
+   }
+   
+   
+
+   @Override
+   protected LoadBalancePolicy getLoadBalancePolicy()
+   {
+      return new FirstAvailableIdenticalAllProxies();
+   }
+
+   @Override
+   public void testInitialSelection()
+   {
+      testCount++;
+      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, Arrays.asList(TARGETS));
+      
+      LoadBalancePolicy lbp = getLoadBalancePolicy();
+      Object target = lbp.chooseTarget(fci);
+      
+      for (int i = 0; i < 10000; i++)
+      {
+         assertSame(target, getLoadBalancePolicy().chooseTarget(fci));
+      }
+      
+   }
+   
+   public void testIdenticalAfterFailover()
+   {
+      testCount++;
+      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, Arrays.asList(TARGETS));
+      
+      LoadBalancePolicy lbp1 = getLoadBalancePolicy();
+      Object target1 = lbp1.chooseTarget(fci);
+      
+      LoadBalancePolicy lbp2 = getLoadBalancePolicy();
+      
+      assertSame(target1, lbp2.chooseTarget(fci));
+      
+      fci.removeDeadTarget(target1);
+      
+      Object target3 = lbp1.chooseTarget(fci);
+      
+      assertFalse(target1.equals(target3));
+      
+      assertSame(target3, lbp2.chooseTarget(fci));
+      
+   }
+
+   
+}

Added: projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/FirstAvailableUnitTestCase.java
===================================================================
--- projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/FirstAvailableUnitTestCase.java	                        (rev 0)
+++ projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/FirstAvailableUnitTestCase.java	2008-05-21 23:22:01 UTC (rev 73569)
@@ -0,0 +1,149 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.test.ha.client.loadbalance;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.jboss.ha.client.loadbalance.FirstAvailable;
+import org.jboss.ha.client.loadbalance.LoadBalancePolicy;
+import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository;
+import org.jboss.ha.framework.interfaces.FamilyClusterInfo;
+
+/**
+ * Tests of the FirstAvailable load balance policy.
+ * 
+ * @author Brian Stansberry
+ */
+public class FirstAvailableUnitTestCase extends TestCase
+{
+   protected static final String[] TARGETS = { "A", "B", "C" };
+   protected static final String FAMILY_BASE = "testFamily";
+   
+   protected static int testCount = 0;
+   
+   /**
+    * Create a new FirstAvailableUnitTestCase.
+    * 
+    * @param name
+    */
+   public FirstAvailableUnitTestCase(String name)
+   {
+      super(name);
+   }
+   
+   protected LoadBalancePolicy getLoadBalancePolicy()
+   {
+      return new FirstAvailable();
+   }
+   
+   public void testBasicStickiness()
+   {
+      testCount++;
+      List<String> targets = Arrays.asList(TARGETS);
+      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, targets);
+      
+      LoadBalancePolicy lbp = getLoadBalancePolicy();
+      Object target = lbp.chooseTarget(fci);
+      
+      assertTrue(targets.contains(target));
+      
+      for (int i = 0; i < 10; i++)
+         assertEquals(target, lbp.chooseTarget(fci));
+   }
+   
+   public void testInitialSelection()
+   {
+      testCount++;
+      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, Arrays.asList(TARGETS));
+      
+      Set<Object> selected = new HashSet<Object>();
+      
+      for (int i = 0; i < 10000; i++)
+      {
+         LoadBalancePolicy lbp = getLoadBalancePolicy();
+         selected.add(lbp.chooseTarget(fci));
+         if (selected.size() == TARGETS.length)
+            break;
+      }
+      
+      assertEquals(TARGETS.length, selected.size());
+   }
+   
+   public void testStickinessAfterFailover()
+   {
+      testCount++;
+      List<String> targets = Arrays.asList(TARGETS);
+      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, targets);
+      
+      LoadBalancePolicy lbp = getLoadBalancePolicy();
+      Object firstTarget;
+      Object target = firstTarget = lbp.chooseTarget(fci);
+      
+      assertTrue(targets.contains(target));
+      
+      for (int i = 0; i < 5; i++)
+         assertEquals(target, lbp.chooseTarget(fci));
+      
+      fci.removeDeadTarget(target);
+      Object secondTarget;
+      target = secondTarget = lbp.chooseTarget(fci);
+      
+      assertFalse(firstTarget.equals(secondTarget));
+      assertTrue(targets.contains(target));
+      
+      for (int i = 0; i < 5; i++)
+         assertEquals(target, lbp.chooseTarget(fci));
+      
+      fci.removeDeadTarget(target);
+      Object thirdTarget;
+      target = thirdTarget = lbp.chooseTarget(fci);
+      
+      assertFalse(firstTarget.equals(thirdTarget));
+      assertFalse(secondTarget.equals(thirdTarget));
+      assertTrue(targets.contains(target));
+      
+      for (int i = 0; i < 5; i++)
+         assertEquals(target, lbp.chooseTarget(fci));
+      
+      fci.removeDeadTarget(target);
+      
+      assertNull(lbp.chooseTarget(fci));
+   }
+   
+   public void testNoAvailableTargets()
+   {
+      testCount++;
+      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, new ArrayList<Object>());
+      
+      assertNull(getLoadBalancePolicy().chooseTarget(fci));
+   }
+   
+   
+
+}

Added: projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/FirstAvailableIdenticalAllProxiesUnitTestCase.java
===================================================================
--- projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/FirstAvailableIdenticalAllProxiesUnitTestCase.java	                        (rev 0)
+++ projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/FirstAvailableIdenticalAllProxiesUnitTestCase.java	2008-05-21 23:22:01 UTC (rev 73569)
@@ -0,0 +1,74 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.test.ha.client.loadbalance.aop;
+
+import org.jboss.ha.client.loadbalance.AopLoadBalancePolicy;
+import org.jboss.ha.client.loadbalance.LoadBalancePolicy;
+import org.jboss.ha.client.loadbalance.aop.FirstAvailableIdenticalAllProxies;
+
+/**
+ * Tests of the FirstAvailable aop load balance policy.
+ * 
+ * @author Brian Stansberry
+ *
+ */
+public class FirstAvailableIdenticalAllProxiesUnitTestCase 
+extends org.jboss.test.ha.client.loadbalance.FirstAvailableIdenticalAllProxiesUnitTestCase
+{
+   /**
+    * Create a new FirstAvailableIdenticalAllProxiesUnitTestCase.
+    * 
+    * @param name
+    */
+   public FirstAvailableIdenticalAllProxiesUnitTestCase(String name)
+   {
+      super(name);
+   }
+
+   @Override
+   protected LoadBalancePolicy getLoadBalancePolicy()
+   {
+      return new FirstAvailableIdenticalAllProxies();
+   }
+   
+   protected AopLoadBalancePolicy getAopLoadBalancePolicy()
+   {
+      return (AopLoadBalancePolicy) getLoadBalancePolicy();
+   }
+   
+   public void testSelectProvidedTarget()
+   {
+      FirstAvailableUnitTestCase.selectProvidedTargetTest(getAopLoadBalancePolicy());
+   }
+   
+   public void testInvalidProvidedTarget()
+   {
+      FirstAvailableUnitTestCase.invalidProvidedTargetTest(getAopLoadBalancePolicy());
+   }
+   
+   public void testStickinessTrumpsProvidedTarget()
+   {
+      FirstAvailableUnitTestCase.stickinessTrumpsProvidedTargetTest(getAopLoadBalancePolicy());
+   }
+   
+}

Added: projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/FirstAvailableUnitTestCase.java
===================================================================
--- projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/FirstAvailableUnitTestCase.java	                        (rev 0)
+++ projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/FirstAvailableUnitTestCase.java	2008-05-21 23:22:01 UTC (rev 73569)
@@ -0,0 +1,135 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.test.ha.client.loadbalance.aop;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.util.PayloadKey;
+import org.jboss.aspects.remoting.ClusterConstants;
+import org.jboss.ha.client.loadbalance.AopLoadBalancePolicy;
+import org.jboss.ha.client.loadbalance.LoadBalancePolicy;
+import org.jboss.ha.client.loadbalance.aop.FirstAvailable;
+import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository;
+import org.jboss.ha.framework.interfaces.FamilyClusterInfo;
+
+/**
+ * Tests of the FirstAvailable aop load balance policy.
+ * 
+ * @author Brian Stansberry
+ */
+public class FirstAvailableUnitTestCase 
+extends org.jboss.test.ha.client.loadbalance.FirstAvailableUnitTestCase
+{
+   /**
+    * Create a new FirstAvailableUnitTestCase.
+    * 
+    * @param name
+    */
+   public FirstAvailableUnitTestCase(String name)
+   {
+      super(name);
+   }
+   
+   protected LoadBalancePolicy getLoadBalancePolicy()
+   {
+      return new FirstAvailable();
+   }
+   
+   protected AopLoadBalancePolicy getAopLoadBalancePolicy()
+   {
+      return (AopLoadBalancePolicy) getLoadBalancePolicy();
+   }
+   
+   public void testSelectProvidedTarget()
+   {
+      selectProvidedTargetTest(getAopLoadBalancePolicy());
+   }
+   
+   public static void selectProvidedTargetTest(AopLoadBalancePolicy lbp)
+   {
+      testCount++;
+      List<String> targets = Arrays.asList(TARGETS);
+      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, targets);
+      
+      Object target = targets.get(1);
+      MockInvocation invocation = new MockInvocation(new Interceptor[0]);
+      invocation.getMetaData().addMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.HA_TARGET, target, PayloadKey.TRANSIENT);
+      assertSame(target, lbp.chooseTarget(fci, invocation));
+   }
+   
+   public void testInvalidProvidedTarget()
+   {
+      invalidProvidedTargetTest(getAopLoadBalancePolicy());
+   }
+   
+   public static void invalidProvidedTargetTest(AopLoadBalancePolicy lbp)
+   {
+      testCount++;
+      List<String> targets = Arrays.asList(TARGETS);
+      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, targets);
+      
+      Object target = new Object();
+      MockInvocation invocation = new MockInvocation(new Interceptor[0]);
+      invocation.getMetaData().addMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.HA_TARGET, target, PayloadKey.TRANSIENT);
+      
+      Object selected = lbp.chooseTarget(fci, invocation);
+      assertFalse(target.equals(selected));
+      
+      assertTrue(targets.contains(selected));      
+   }
+   
+   public void testStickinessTrumpsProvidedTarget()
+   {
+      stickinessTrumpsProvidedTargetTest(getAopLoadBalancePolicy());
+   }
+   
+   public static void stickinessTrumpsProvidedTargetTest(AopLoadBalancePolicy lbp)
+   {
+      testCount++;
+      List<String> targets = Arrays.asList(TARGETS);
+      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, targets);
+      
+      Object selected = lbp.chooseTarget(fci);
+      Object different = null;
+      for (Object target : targets)
+      {
+         if (!target.equals(selected))
+         {
+            different = target;
+            break;
+         }
+      }
+      
+      assertNotSame(selected, different);
+      
+      MockInvocation invocation = new MockInvocation(new Interceptor[0]);
+      invocation.getMetaData().addMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.HA_TARGET, different, PayloadKey.TRANSIENT);
+      
+      assertSame(selected, lbp.chooseTarget(fci, invocation));
+      
+      
+   }
+
+}

Added: projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/MockInvocation.java
===================================================================
--- projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/MockInvocation.java	                        (rev 0)
+++ projects/cluster/ha-client/trunk/src/test/java/org/jboss/test/ha/client/loadbalance/aop/MockInvocation.java	2008-05-21 23:22:01 UTC (rev 73569)
@@ -0,0 +1,91 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.test.ha.client.loadbalance.aop;
+
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.joinpoint.Invocation;
+import org.jboss.aop.joinpoint.InvocationBase;
+import org.jboss.aop.metadata.SimpleMetaData;
+
+/**
+ * Mock invocation; just to pass metadata.
+ * 
+ * @author Brian Stansberry
+ *
+ */
+public class MockInvocation extends InvocationBase
+{
+   /** The serialVersionUID */
+   private static final long serialVersionUID = 1L;
+
+   /**
+    * Create a new MockInvocation.
+    * 
+    */
+   public MockInvocation()
+   {
+      // TODO Auto-generated constructor stub
+   }
+
+   /**
+    * Create a new MockInvocation.
+    * 
+    * @param interceptors
+    */
+   public MockInvocation(Interceptor[] interceptors)
+   {
+      super(interceptors);
+   }
+
+   /**
+    * Create a new MockInvocation.
+    * 
+    * @param invocation
+    */
+   public MockInvocation(Invocation invocation)
+   {
+      super(invocation);
+   }
+
+   /**
+    * Create a new MockInvocation.
+    * 
+    * @param interceptors
+    * @param meta
+    */
+   public MockInvocation(Interceptor[] interceptors, SimpleMetaData meta)
+   {
+      super(interceptors, meta);
+   }
+
+   public Invocation copy()
+   {
+      return null;
+   }
+
+   public Invocation getWrapper(Interceptor[] ainterceptor)
+   {
+      return null;
+   }
+
+}




More information about the jboss-cvs-commits mailing list