Add integration test with cluster-singleton
[controller.git] / opendaylight / md-sal / sal-distributed-eos / src / test / java / org / opendaylight / controller / cluster / 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.entityownership.selectionstrategy;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.Map;
17 import org.junit.Test;
18
19 public class LeastLoadedCandidateSelectionStrategyTest {
20
21     @Test
22     public void testLeastLoadedStrategy() {
23         LeastLoadedCandidateSelectionStrategy strategy = new LeastLoadedCandidateSelectionStrategy(
24                 0L, Collections.<String, Long>emptyMap());
25
26         String owner = strategy.newOwner(null, prepareViableCandidates(3));
27         assertEquals("member-1", owner);
28
29         Map<String, Long> localStatistics = strategy.getLocalStatistics();
30         assertEquals(1L, (long) localStatistics.get("member-1"));
31
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);
36
37         assertStatistics(strategy.getLocalStatistics(), 5,3,4);
38
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);
43
44         assertStatistics(strategy.getLocalStatistics(), 5,7,5);
45
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);
50
51         assertStatistics(strategy.getLocalStatistics(), 2,7,4);
52
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);
57
58         assertStatistics(strategy.getLocalStatistics(), 3,3,1);
59
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);
63
64         assertStatistics(strategy.getLocalStatistics(), 4,3,0);
65
66     }
67
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]);
72         }
73         return statistics;
74     }
75
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));
80         }
81         return viableCandidates;
82     }
83
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)));
87         }
88     }
89 }