/*
* 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.List;
import org.jboss.ha.client.loadbalance.LoadBalancePolicy;
import org.jboss.ha.client.loadbalance.RoundRobin;
import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository;
import org.jboss.ha.framework.interfaces.FamilyClusterInfo;
/**
* Unit tests for {@link RoundRobin}.
*
* @author Brian Stansberry
*
*/
public class RoundRobinUnitTestCase extends AbstractLoadBalancePolicyTestCase<RoundRobin>
{
/**
* Create a new RoundRobinUnitTestCase.
*
*/
public RoundRobinUnitTestCase()
{
}
/**
* Create a new RoundRobinUnitTestCase.
*
* @param name
*/
public RoundRobinUnitTestCase(String name)
{
super(name);
}
protected RoundRobin getLoadBalancePolicy()
{
return new RoundRobin();
}
public void testBasicCycle()
{
testCount++;
FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, Arrays.asList(TARGETS));
LoadBalancePolicy lbp = getLoadBalancePolicy();
int expectedCursor = -1;
for (int i = 0; i < 100; i++)
{
Object target = lbp.chooseTarget(fci);
if (expectedCursor < 0)
{
expectedCursor = fci.getCursor();
}
else
{
expectedCursor = ++expectedCursor % TARGETS.length;
assertEquals(expectedCursor, fci.getCursor());
}
assertEquals(TARGETS[expectedCursor], target);
}
}
public void testFailover()
{
testCount++;
List<String> targets = new ArrayList<String>(Arrays.asList(TARGETS));
FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, targets);
LoadBalancePolicy lbp = getLoadBalancePolicy();
String target = (String) lbp.chooseTarget(fci);
int cursor = fci.getCursor();
assertEquals(target, fci.getTargets().get(cursor));
while (targets.size() > 1)
{
fci.removeDeadTarget(target);
targets.remove(target);
assertEquals(targets, fci.getTargets());
int expectedCursor = ++cursor % targets.size();
target = (String) lbp.chooseTarget(fci);
cursor = fci.getCursor();
assertEquals(expectedCursor, cursor);
assertEquals(target, fci.getTargets().get(cursor));
}
}
public void testTopologyChange()
{
testCount++;
List<String> targets = Arrays.asList(TARGETS);
FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, targets);
LoadBalancePolicy lbp = getLoadBalancePolicy();
String target = (String) lbp.chooseTarget(fci);
int cursor = fci.getCursor();
assertEquals(target, fci.getTargets().get(cursor));
targets = Arrays.asList(new String[]{"W", "X", "Y", "Z"});
fci.updateClusterInfo(targets, 1);
int expectedCursor = ++cursor % targets.size();
target = (String) lbp.chooseTarget(fci);
cursor = fci.getCursor();
assertEquals(expectedCursor, cursor);
assertEquals(target, targets.get(cursor));
}
}