/*
* 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));
}
}