2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
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;
16 import org.junit.Test;
18 public class LeastLoadedCandidateSelectionStrategyTest {
21 public void testLeastLoadedStrategy(){
22 LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(0L, Collections.<String, Long>emptyMap());
24 String owner = strategy.newOwner(null, prepareViableCandidates(3));
25 assertEquals("member-1", owner);
27 Map<String, Long> localStatistics = strategy.getLocalStatistics();
28 assertEquals(1L, (long) localStatistics.get("member-1"));
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);
35 assertStatistics(strategy.getLocalStatistics(), 5,3,4);
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);
42 assertStatistics(strategy.getLocalStatistics(), 5,7,5);
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);
49 assertStatistics(strategy.getLocalStatistics(), 2,7,4);
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);
56 assertStatistics(strategy.getLocalStatistics(), 3,3,1);
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);
62 assertStatistics(strategy.getLocalStatistics(), 4,3,0);
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]);
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));
79 return viableCandidates;
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)));