X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fentityownership%2Fselectionstrategy%2FLeastLoadedCandidateSelectionStrategyTest.java;h=d0cd32f1b01f7ec529b2874fa6db023a56f0a0b0;hp=1054f346f9ba5f77dee9a97ab8c6c2fe251b4c3e;hb=bb1891e328feee08ccd29c96034e967f1eeccece;hpb=2f562353a2f92b4f53d23c3926522ad39d165dac diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/LeastLoadedCandidateSelectionStrategyTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/LeastLoadedCandidateSelectionStrategyTest.java index 1054f346f9..d0cd32f1b0 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/LeastLoadedCandidateSelectionStrategyTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/LeastLoadedCandidateSelectionStrategyTest.java @@ -10,6 +10,7 @@ package org.opendaylight.controller.cluster.datastore.entityownership.selections import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.junit.Test; @@ -18,23 +19,48 @@ public class LeastLoadedCandidateSelectionStrategyTest { @Test public void testLeastLoadedStrategy(){ - LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(0L); + LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(0L, Collections.emptyMap()); - String owner = strategy.newOwner(prepareViableCandidates(3), new HashMap()); + String owner = strategy.newOwner(null, prepareViableCandidates(3)); assertEquals("member-1", owner); + Map localStatistics = strategy.getLocalStatistics(); + assertEquals(1L, (long) localStatistics.get("member-1")); + // member-2 has least load - owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(5,2,4)); + strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(5,2,4)); + owner = strategy.newOwner(null, prepareViableCandidates(3)); assertEquals("member-2", owner); + assertStatistics(strategy.getLocalStatistics(), 5,3,4); + // member-3 has least load - owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(5,7,4)); + strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(5,7,4)); + owner = strategy.newOwner(null, prepareViableCandidates(3)); assertEquals("member-3", owner); + assertStatistics(strategy.getLocalStatistics(), 5,7,5); + // member-1 has least load - owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(1,7,4)); + strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(1,7,4)); + owner = strategy.newOwner(null, prepareViableCandidates(3)); + assertEquals("member-1", owner); + + assertStatistics(strategy.getLocalStatistics(), 2,7,4); + + // Let member-3 become the owner + strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(3,3,0)); + owner = strategy.newOwner(null, prepareViableCandidates(3)); + assertEquals("member-3", owner); + + assertStatistics(strategy.getLocalStatistics(), 3,3,1); + + // member-3 is no longer viable so choose a new owner + owner = strategy.newOwner("member-3", prepareViableCandidates(2)); assertEquals("member-1", owner); + assertStatistics(strategy.getLocalStatistics(), 4,3,0); + } private static Map prepareStatistics(long... count){ @@ -52,4 +78,10 @@ public class LeastLoadedCandidateSelectionStrategyTest { } return viableCandidates; } + + private void assertStatistics(Map statistics, long... count){ + for(int i=0;i