aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/test/java/com/yahoo/messagebus/network/rpc/SlobrokTestCase.java
blob: 0badfd5892958b716ac32eefa6e8c4d8486085d0 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.network.rpc;


import com.yahoo.jrt.ListenFailedException;
import com.yahoo.jrt.Spec;
import com.yahoo.jrt.slobrok.api.Mirror;
import com.yahoo.jrt.slobrok.server.Slobrok;
import com.yahoo.messagebus.network.Identity;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertTrue;


/**
 * @author havardpe
 */
public class SlobrokTestCase {

    private static class Res {
        private List<Mirror.Entry> lst = new ArrayList<>();
        public Res add(String fullName, String spec) {
            lst.add(new Mirror.Entry(fullName, spec));
            return this;
        }
        public List<Mirror.Entry> toArray() {
            return lst;
        }
    }

    Slobrok    slobrok;
    RPCNetwork net1;
    RPCNetwork net2;
    RPCNetwork net3;
    int        port1;
    int        port2;
    int        port3;

    void check(RPCNetwork net, String pattern, List<Mirror.Entry> expect) {
        Comparator<Mirror.Entry> cmp = Comparator.naturalOrder();
        expect.sort(cmp);
        List<Mirror.Entry> actual = null;
        for (int i = 0; i < 1000; i++) {
            actual = net.getMirror().lookup(pattern);
            actual.sort(cmp);
            if (actual.equals(expect)) {
                System.out.printf("lookup successful for pattern: %s\n", pattern);
                return;
            }
            try { Thread.sleep(10); } catch (InterruptedException e) {
                //
            }
        }
        System.out.printf("lookup failed for pattern: %s\n", pattern);
        System.out.printf("actual values:\n");
        if (actual == null || actual.isEmpty()) {
            System.out.printf("  { EMPTY }\n");
        } else {
            for (Mirror.Entry entry : actual) {
                System.out.printf("  { %s, %s }\n", entry.getName(), entry.getSpecString());
            }
        }
        System.out.printf("expected values:\n");
        if (expect.isEmpty()) {
            System.out.printf("  { EMPTY }\n");
        } else {
            for (Mirror.Entry entry : expect) {
                System.out.printf("  { %s, %s }\n", entry.getName(),  entry.getSpecString());
            }
        }
        assertTrue(false);
    }

    @BeforeEach
    public void setUp() throws ListenFailedException {
        slobrok = new Slobrok();
        String slobrokCfgId = "raw:slobrok[1]\nslobrok[0].connectionspec \"" + new Spec("localhost", slobrok.port()).toString() + "\"\n";
        net1 = new RPCNetwork(new RPCNetworkParams().setNumNetworkThreads(1).setIdentity(new Identity("net/a")).setSlobrokConfigId(slobrokCfgId));
        net2 = new RPCNetwork(new RPCNetworkParams().setNumNetworkThreads(1).setIdentity(new Identity("net/b")).setSlobrokConfigId(slobrokCfgId));
        net3 = new RPCNetwork(new RPCNetworkParams().setNumNetworkThreads(1).setIdentity(new Identity("net/c")).setSlobrokConfigId(slobrokCfgId));
        port1 = net1.getPort();
        port2 = net2.getPort();
        port3 = net3.getPort();
    }

    @AfterEach
    public void tearDown() {
        net3.shutdown();
        net2.shutdown();
        net1.shutdown();
        slobrok.stop();
    }

    @Test
    void testSlobrok() {
        net1.registerSession("foo");
        net2.registerSession("foo");
        net2.registerSession("bar");
        net3.registerSession("foo");
        net3.registerSession("bar");
        net3.registerSession("baz");

        check(net1, "*/*/*", new Res()
                .add("net/a/foo", net1.getConnectionSpec())
                .add("net/b/foo", net2.getConnectionSpec())
                .add("net/b/bar", net2.getConnectionSpec())
                .add("net/c/foo", net3.getConnectionSpec())
                .add("net/c/bar", net3.getConnectionSpec())
                .add("net/c/baz", net3.getConnectionSpec()).toArray());
        check(net2, "*/*/*", new Res()
                .add("net/a/foo", net1.getConnectionSpec())
                .add("net/b/foo", net2.getConnectionSpec())
                .add("net/b/bar", net2.getConnectionSpec())
                .add("net/c/foo", net3.getConnectionSpec())
                .add("net/c/bar", net3.getConnectionSpec())
                .add("net/c/baz", net3.getConnectionSpec()).toArray());
        check(net3, "*/*/*", new Res()
                .add("net/a/foo", net1.getConnectionSpec())
                .add("net/b/foo", net2.getConnectionSpec())
                .add("net/b/bar", net2.getConnectionSpec())
                .add("net/c/foo", net3.getConnectionSpec())
                .add("net/c/bar", net3.getConnectionSpec())
                .add("net/c/baz", net3.getConnectionSpec()).toArray());

        net2.unregisterSession("bar");
        net3.unregisterSession("bar");
        net3.unregisterSession("baz");

        check(net1, "*/*/*", new Res()
                .add("net/a/foo", net1.getConnectionSpec())
                .add("net/b/foo", net2.getConnectionSpec())
                .add("net/c/foo", net3.getConnectionSpec()).toArray());
        check(net2, "*/*/*", new Res()
                .add("net/a/foo", net1.getConnectionSpec())
                .add("net/b/foo", net2.getConnectionSpec())
                .add("net/c/foo", net3.getConnectionSpec()).toArray());
        check(net3, "*/*/*", new Res()
                .add("net/a/foo", net1.getConnectionSpec())
                .add("net/b/foo", net2.getConnectionSpec())
                .add("net/c/foo", net3.getConnectionSpec()).toArray());
    }

}