Implement LeastLoadedCandidateSelectionStrategy
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / selectionstrategy / LeastLoadedCandidateSelectionStrategyTest.java
1 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
2
3 import static org.junit.Assert.assertEquals;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.Map;
8 import org.junit.Test;
9
10 public class LeastLoadedCandidateSelectionStrategyTest {
11
12     @Test
13     public void testLeastLoadedStrategy(){
14         LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(0L);
15
16         String owner = strategy.newOwner(prepareViableCandidates(3), new HashMap<String, Long>());
17         assertEquals("member-1", owner);
18
19         // member-2 has least load
20         owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(5,2,4));
21         assertEquals("member-2", owner);
22
23         // member-3 has least load
24         owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(5,7,4));
25         assertEquals("member-3", owner);
26
27         // member-1 has least load
28         owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(1,7,4));
29         assertEquals("member-1", owner);
30
31     }
32
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]);
37         }
38         return statistics;
39     }
40
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));
45         }
46         return viableCandidates;
47     }
48 }