1 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
3 import static org.junit.Assert.assertEquals;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.HashMap;
10 public class LeastLoadedCandidateSelectionStrategyTest {
13 public void testLeastLoadedStrategy(){
14 LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(0L);
16 String owner = strategy.newOwner(prepareViableCandidates(3), new HashMap<String, Long>());
17 assertEquals("member-1", owner);
19 // member-2 has least load
20 owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(5,2,4));
21 assertEquals("member-2", owner);
23 // member-3 has least load
24 owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(5,7,4));
25 assertEquals("member-3", owner);
27 // member-1 has least load
28 owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(1,7,4));
29 assertEquals("member-1", owner);
33 private Map<String, Long> prepareStatistics(long... count){
34 Map<String, Long> statistics = new HashMap<>();
35 for(int i=0;i<count.length;i++){
36 statistics.put("member-" + (i+1), count[i]);
41 private Collection<String> prepareViableCandidates(int count){
42 Collection<String> viableCandidates = new ArrayList<>();
43 for(int i=0;i<count;i++){
44 viableCandidates.add("member-" + (i+1));
46 return viableCandidates;