summaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/handler/VipStatusTestCase.java
blob: d3479936544add967e9a8e9927c36871d2d7b52c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler;

import static org.junit.Assert.*;

import com.yahoo.container.QrSearchersConfig;
import com.yahoo.container.jdisc.state.StateMonitor;
import com.yahoo.jdisc.core.SystemTimer;
import org.junit.Test;

/**
 * Smoke test that VipStatus has the right basic logic.
 *
 * @author steinar
 */
public class VipStatusTestCase {
    private static final String [] clusters = {"cluster1", "cluster2", "cluster3"};

    private static QrSearchersConfig getSearchersCfg() {
        var b = new QrSearchersConfig.Builder();
        var searchClusterB = new QrSearchersConfig.Searchcluster.Builder();
        for (String cluster : clusters) {
            searchClusterB.name(cluster);
        }
        b.searchcluster(searchClusterB);
        return b.build();
    }
    private static VipStatus getVipStatus(StateMonitor.Status startState) {
        return new VipStatus(getSearchersCfg(), new ClustersStatus(), new StateMonitor(1000, startState, new SystemTimer(), runnable -> {
            Thread thread = new Thread(runnable, "StateMonitor");
            thread.setDaemon(true);
            return thread;
        }));
    }

    private static void removeAll(VipStatus v) {
        for (String s : clusters) {
            v.removeFromRotation(s);
        }
    }
    private static void addAll(VipStatus v) {
        for (String s : clusters) {
            v.addToRotation(s);
        }
    }

    private static void verifyUpOrDown(StateMonitor.Status status) {
        VipStatus v = getVipStatus(status);
        removeAll(v);
        // initial state
        assertFalse(v.isInRotation());
        v.addToRotation(clusters[0]);
        assertFalse(v.isInRotation());
        v.addToRotation(clusters[1]);
        assertFalse(v.isInRotation());
        v.addToRotation(clusters[2]);
        assertTrue(v.isInRotation());
    }

    @Test
    public void testInitializingOrDownRequireAllUp() {
        verifyUpOrDown(StateMonitor.Status.initializing);
        verifyUpOrDown(StateMonitor.Status.down);
    }

    @Test
    public void testUpRequireAllDown() {
        VipStatus v = getVipStatus(StateMonitor.Status.initializing);
        assertFalse(v.isInRotation());
        addAll(v);
        assertTrue(v.isInRotation());

        v.removeFromRotation(clusters[0]);
        assertTrue(v.isInRotation());
        v.removeFromRotation(clusters[1]);
        assertTrue(v.isInRotation());
        v.removeFromRotation(clusters[2]);
        assertFalse(v.isInRotation());  // All down
        v.addToRotation(clusters[1]);
        assertFalse(v.isInRotation());
        v.addToRotation(clusters[0]);
        v.addToRotation(clusters[2]);
        assertTrue(v.isInRotation());   // All up
        v.removeFromRotation(clusters[0]);
        v.removeFromRotation(clusters[2]);
        assertTrue(v.isInRotation());
        v.addToRotation(clusters[0]);
        v.addToRotation(clusters[2]);
        assertTrue(v.isInRotation());
    }

}