Integrate MRI projects for Neon
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / selectionstrategy / LeastLoadedCandidateSelectionStrategyTest.java
index 50db591a19e86307db54784afd4f1f82331828dd..a7f1657906b92a9f32552dd4d21c75eff34de0f9 100644 (file)
@@ -8,8 +8,10 @@
 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
 
 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;
@@ -17,39 +19,71 @@ import org.junit.Test;
 public class LeastLoadedCandidateSelectionStrategyTest {
 
     @Test
-    public void testLeastLoadedStrategy(){
-        LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(0L);
+    public void testLeastLoadedStrategy() {
+        LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(
+                0L, Collections.<String, Long>emptyMap());
 
-        String owner = strategy.newOwner(prepareViableCandidates(3), new HashMap<String, Long>());
+        String owner = strategy.newOwner(null, prepareViableCandidates(3));
         assertEquals("member-1", owner);
 
+        Map<String, Long> 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 Map<String, Long> prepareStatistics(long... count){
+    private static Map<String, Long> prepareStatistics(long... count) {
         Map<String, Long> statistics = new HashMap<>();
-        for(int i=0;i<count.length;i++){
-            statistics.put("member-" + (i+1), count[i]);
+        for (int i = 0; i < count.length; i++) {
+            statistics.put("member-" + (i + 1), count[i]);
         }
         return statistics;
     }
 
-    private Collection<String> prepareViableCandidates(int count){
+    private static Collection<String> prepareViableCandidates(int count) {
         Collection<String> viableCandidates = new ArrayList<>();
-        for(int i=0;i<count;i++){
-            viableCandidates.add("member-" + (i+1));
+        for (int i = 0; i < count; i++) {
+            viableCandidates.add("member-" + (i + 1));
         }
         return viableCandidates;
     }
-}
\ No newline at end of file
+
+    private static void assertStatistics(Map<String, Long> statistics, long... count) {
+        for (int i = 0; i < count.length; i++) {
+            assertEquals(count[i], (long) statistics.get("member-" + (i + 1)));
+        }
+    }
+}