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;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashMap;
17 import org.junit.Test;
19 public class LeastLoadedCandidateSelectionStrategyTest {
22 public void testLeastLoadedStrategy() {
23 LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(
24 0L, Collections.<String, Long>emptyMap());
26 String owner = strategy.newOwner(null, prepareViableCandidates(3));
27 assertEquals("member-1", owner);
29 Map<String, Long> localStatistics = strategy.getLocalStatistics();
30 assertEquals(1L, (long) localStatistics.get("member-1"));
32 // member-2 has least load
33 strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(5,2,4));
34 owner = strategy.newOwner(null, prepareViableCandidates(3));
35 assertEquals("member-2", owner);
37 assertStatistics(strategy.getLocalStatistics(), 5,3,4);
39 // member-3 has least load
40 strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(5,7,4));
41 owner = strategy.newOwner(null, prepareViableCandidates(3));
42 assertEquals("member-3", owner);
44 assertStatistics(strategy.getLocalStatistics(), 5,7,5);
46 // member-1 has least load
47 strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(1,7,4));
48 owner = strategy.newOwner(null, prepareViableCandidates(3));
49 assertEquals("member-1", owner);
51 assertStatistics(strategy.getLocalStatistics(), 2,7,4);
53 // Let member-3 become the owner
54 strategy = new LeastLoadedCandidateSelectionStrategy(0L, prepareStatistics(3,3,0));
55 owner = strategy.newOwner(null, prepareViableCandidates(3));
56 assertEquals("member-3", owner);
58 assertStatistics(strategy.getLocalStatistics(), 3,3,1);
60 // member-3 is no longer viable so choose a new owner
61 owner = strategy.newOwner("member-3", prepareViableCandidates(2));
62 assertEquals("member-1", owner);
64 assertStatistics(strategy.getLocalStatistics(), 4,3,0);
68 private static Map<String, Long> prepareStatistics(long... count) {
69 Map<String, Long> statistics = new HashMap<>();
70 for (int i = 0; i < count.length; i++) {
71 statistics.put("member-" + (i + 1), count[i]);
76 private static Collection<String> prepareViableCandidates(int count) {
77 Collection<String> viableCandidates = new ArrayList<>();
78 for (int i = 0; i < count; i++) {
79 viableCandidates.add("member-" + (i + 1));
81 return viableCandidates;
84 private static void assertStatistics(Map<String, Long> statistics, long... count) {
85 for (int i = 0; i < count.length; i++) {
86 assertEquals(count[i], (long) statistics.get("member-" + (i + 1)));