aboutsummaryrefslogtreecommitdiffstats
path: root/zkfacade/src/test/java/com/yahoo/vespa/curator/ConnectionSpecTest.java
blob: 6100a302b283aae4633085b62d26ec3a7c8f1205 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.curator;

import com.yahoo.net.HostName;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;

/**
 * @author mpolden
 */
public class ConnectionSpecTest {

    @Test
    public void create() {
        HostName.setHostNameForTestingOnly("host2");
        Config config = new Config(List.of(new Config.Server("host1", 10001),
                                           new Config.Server("host2", 10002),
                                           new Config.Server("host3", 10003)));

        {
            ConnectionSpec spec = ConnectionSpec.create(config.servers, Config.Server::hostname, Config.Server::port, false);
            assertEquals("host1:10001,host2:10002,host3:10003", spec.local());
            assertEquals("host1:10001,host2:10002,host3:10003", spec.ensemble());
            assertEquals(3, spec.ensembleSize());
        }

        {
            ConnectionSpec specLocalAffinity = ConnectionSpec.create(config.servers, Config.Server::hostname, Config.Server::port, true);
            assertEquals("host2:10002", specLocalAffinity.local());
            assertEquals("host1:10001,host2:10002,host3:10003", specLocalAffinity.ensemble());
            assertEquals(3, specLocalAffinity.ensembleSize());
        }

        {
            ConnectionSpec specFromString = ConnectionSpec.create("host1:10001", "host1:10001,host2:10002");
            assertEquals("host1:10001", specFromString.local());
            assertEquals("host1:10001,host2:10002", specFromString.ensemble());
            assertEquals(2, specFromString.ensembleSize());
        }
    }

    private static class Config {

        private final List<Server> servers;

        public Config(List<Server> servers) {
            this.servers = servers;
        }

        private static class Server {

            private final String hostname;
            private final int port;

            public Server(String hostname, int port) {
                this.hostname = hostname;
                this.port = port;
            }

            public String hostname() {
                return hostname;
            }

            public int port() {
                return port;
            }
        }

    }

}