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.HashMap;
15 import org.junit.Test;
17 public class LeastLoadedCandidateSelectionStrategyTest {
20 public void testLeastLoadedStrategy(){
21 LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(0L);
23 String owner = strategy.newOwner(prepareViableCandidates(3), new HashMap<String, Long>());
24 assertEquals("member-1", owner);
26 // member-2 has least load
27 owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(5,2,4));
28 assertEquals("member-2", owner);
30 // member-3 has least load
31 owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(5,7,4));
32 assertEquals("member-3", owner);
34 // member-1 has least load
35 owner = strategy.newOwner(prepareViableCandidates(3), prepareStatistics(1,7,4));
36 assertEquals("member-1", owner);
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]);
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));
53 return viableCandidates;