Make potentially-static methods static
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / selectionstrategy / LeastLoadedCandidateSelectionStrategyTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
9
10 import static org.junit.Assert.assertEquals;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.Map;
16 import org.junit.Test;
17
18 public class LeastLoadedCandidateSelectionStrategyTest {
19
20     @Test
21     public void testLeastLoadedStrategy(){
22         LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(0L, Collections.<String, Long>emptyMap());
23
24         String owner = strategy.newOwner(null, prepareViableCandidates(3));
25         assertEquals("member-1", owner);
26
27         Map<String, Long> localStatistics = strategy.getLocalStatistics();
28         assertEquals(1L, (long) localStatistics.get("member-1"));
29
30         // member-2 has least load
31         strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(5,2,4));
32         owner = strategy.newOwner(null, prepareViableCandidates(3));
33         assertEquals("member-2", owner);
34
35         assertStatistics(strategy.getLocalStatistics(), 5,3,4);
36
37         // member-3 has least load
38         strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(5,7,4));
39         owner = strategy.newOwner(null, prepareViableCandidates(3));
40         assertEquals("member-3", owner);
41
42         assertStatistics(strategy.getLocalStatistics(), 5,7,5);
43
44         // member-1 has least load
45         strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(1,7,4));
46         owner = strategy.newOwner(null, prepareViableCandidates(3));
47         assertEquals("member-1", owner);
48
49         assertStatistics(strategy.getLocalStatistics(), 2,7,4);
50
51         // Let member-3 become the owner
52         strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(3,3,0));
53         owner = strategy.newOwner(null, prepareViableCandidates(3));
54         assertEquals("member-3", owner);
55
56         assertStatistics(strategy.getLocalStatistics(), 3,3,1);
57
58         // member-3 is no longer viable so choose a new owner
59         owner = strategy.newOwner("member-3", prepareViableCandidates(2));
60         assertEquals("member-1", owner);
61
62         assertStatistics(strategy.getLocalStatistics(), 4,3,0);
63
64     }
65
66     private static Map<String, Long> prepareStatistics(long... count){
67         Map<String, Long> statistics = new HashMap<>();
68         for(int i=0;i<count.length;i++){
69             statistics.put("member-" + (i+1), count[i]);
70         }
71         return statistics;
72     }
73
74     private static Collection<String> prepareViableCandidates(int count){
75         Collection<String> viableCandidates = new ArrayList<>();
76         for(int i=0;i<count;i++){
77             viableCandidates.add("member-" + (i+1));
78         }
79         return viableCandidates;
80     }
81
82     private static void assertStatistics(Map<String, Long> statistics, long... count){
83         for(int i=0;i<count.length;i++){
84             assertEquals(count[i], (long) statistics.get("member-" + (i+1)));
85         }
86     }
87 }