50db591a19e86307db54784afd4f1f82331828dd
[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.HashMap;
14 import java.util.Map;
15 import org.junit.Test;
16
17 public class LeastLoadedCandidateSelectionStrategyTest {
18
19     @Test
20     public void testLeastLoadedStrategy(){
21         LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(0L);
22
23         String owner = strategy.newOwner(prepareViableCandidates(3), new HashMap<String, Long>());
24         assertEquals("member-1", owner);
25
26         // member-2 has least load
27         owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(5,2,4));
28         assertEquals("member-2", owner);
29
30         // member-3 has least load
31         owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(5,7,4));
32         assertEquals("member-3", owner);
33
34         // member-1 has least load
35         owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(1,7,4));
36         assertEquals("member-1", owner);
37
38     }
39
40     private Map<String, Long> prepareStatistics(long... count){
41         Map<String, Long> statistics = new HashMap<>();
42         for(int i=0;i<count.length;i++){
43             statistics.put("member-" + (i+1), count[i]);
44         }
45         return statistics;
46     }
47
48     private Collection<String> prepareViableCandidates(int count){
49         Collection<String> viableCandidates = new ArrayList<>();
50         for(int i=0;i<count;i++){
51             viableCandidates.add("member-" + (i+1));
52         }
53         return viableCandidates;
54     }
55 }