package netflix.ocelli.loadbalancer.weighting;
import com.google.common.collect.Lists;
import netflix.ocelli.LoadBalancer;
import netflix.ocelli.loadbalancer.RandomWeightedLoadBalancer;
import netflix.ocelli.retry.RetryFailedTestRule;
import netflix.ocelli.retry.RetryFailedTestRule.Retry;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import rx.subjects.BehaviorSubject;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
public class InverseMaxWeightingStrategyTest extends BaseWeightingStrategyTest {
@Rule
public RetryFailedTestRule retryRule = new RetryFailedTestRule();
BehaviorSubject<List<IntClientAndMetrics>> subject = BehaviorSubject.create();
LoadBalancer<IntClientAndMetrics> lb = LoadBalancer.fromSnapshotSource(subject).build(RandomWeightedLoadBalancer.create(
new InverseMaxWeightingStrategy<IntClientAndMetrics>(IntClientAndMetrics.BY_METRIC)));
@Test(expected=NoSuchElementException.class)
public void testEmptyClients() throws Throwable {
List<IntClientAndMetrics> clients = create();
subject.onNext(clients);
List<Integer> counts = Arrays.<Integer>asList(roundToNearest(simulate(lb, clients.size(), 1000), 100));
Assert.assertEquals(Lists.newArrayList(), counts);
}
@Test
@Retry(5)
public void testOneClient() throws Throwable {
List<IntClientAndMetrics> clients = create(10);
subject.onNext(clients);
List<Integer> counts = Arrays.<Integer>asList(roundToNearest(simulate(lb, clients.size(), 1000), 100));
Assert.assertEquals(Lists.newArrayList(1000), counts);
}
@Test
@Retry(5)
public void testEqualsWeights() throws Throwable {
List<IntClientAndMetrics> clients = create(1,1,1,1);
subject.onNext(clients);
List<Integer> counts = Arrays.<Integer>asList(roundToNearest(simulate(lb, clients.size(), 4000), 100));
Assert.assertEquals(Lists.newArrayList(1000, 1000, 1000, 1000), counts);
}
@Test
@Retry(5)
public void testDifferentWeights() throws Throwable {
List<IntClientAndMetrics> clients = create(1,2,3,4);
subject.onNext(clients);
List<Integer> counts = Arrays.<Integer>asList(roundToNearest(simulate(lb, clients.size(), 4000), 100));
Assert.assertEquals(Lists.newArrayList(1600, 1200, 800, 400), counts);
}
}