aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/tests/com/yahoo/jrt/SlobrokTest.java
blob: a0ee4a471dbc00c9f6cb539d03ae2102ddf0a009 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;

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

import com.yahoo.jrt.slobrok.api.SlobrokList;
import com.yahoo.jrt.slobrok.api.Mirror;
import com.yahoo.jrt.slobrok.api.Register;
import com.yahoo.jrt.slobrok.api.Mirror.Entry;
import com.yahoo.jrt.slobrok.server.Slobrok;
import org.junit.After;
import org.junit.Before;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

public class SlobrokTest {

    private static class SpecList extends ArrayList<Mirror.Entry> {
        public SpecList add(String name, String spec) {
            add(new Mirror.Entry(name, spec));
            return this;
        }
    }

    String[]   slobroks;
    boolean    error    = false;
    Supervisor server   = new Supervisor(new Transport());
    Supervisor client   = new Supervisor(new Transport());
    Acceptor   acceptor = null;
    Mirror     mirror   = null;
    Register   register = null;
    String     mySpec   = null;
    Slobrok    slobrok;

    @Before
    public void setUp() throws ListenFailedException {
        slobrok = new Slobrok();
        slobroks = new String[1];
        slobroks[0] = new Spec("localhost", slobrok.port()).toString();
        SlobrokList slobroklist = new SlobrokList();
        slobroklist.setup(slobroks);
        acceptor = server.listen(new Spec(0));
        mirror = new Mirror(client, slobroklist);
        register = new Register(server, slobroklist,
                                "localhost", acceptor.port());
        mySpec = new Spec("localhost", acceptor.port()).toString();
    }

    @After
    public void tearDown() {
        register.shutdown();
        mirror.shutdown();
        acceptor.shutdown();
        client.transport().shutdown();
        server.transport().shutdown();
        slobrok.stop();
    }

    void check(String pattern, ArrayList<Entry> result) {
        if (error) {
            err("already failed, skipping test");
            return;
        }
        Comparator<Entry> cmp = new Comparator<Entry>() {
            public int compare(Entry a, Entry b) {
                return a.compareTo(b);
            }
        };
        List<Entry> expect = result;
        expect.sort(cmp);
        List<Entry> actual = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            actual = mirror.lookup(pattern);
            actual.sort(cmp);
            if (actual.equals(expect)) {
                // err("lookup successful for pattern: " + pattern);
                return;
            }
            try { Thread.sleep(10); } catch (InterruptedException e) {}
        }
        error = true;
        err("lookup failed for pattern: " + pattern);
        err("actual values:");
        if (actual.isEmpty()) {
            err("  { EMPTY }");
        }
        for (Entry e : actual) {
            err("  {" + e.getName() + ", " + e.getSpecString() + "}");
        }
        err("expected values:");
        if (expect.isEmpty()) {
            err("  { EMPTY }");
        }
        for (Entry e : expect) {
            err("  {" + e.getName() + ", " + e.getSpecString() + "}");
        }
    }

    @org.junit.Test
    public void testSlobrok() {
        String wantName = "A/x/w";
        register.registerName(wantName);
        check(wantName, new SpecList().add(wantName, mySpec));
        check("*/*", new SpecList());
        check("*/*/*", new SpecList().add(wantName, mySpec));

        assertTrue(mirror.ready());
        assertTrue(mirror.updates() > 0);

        mirror.dumpState();
        List<Entry> oneArr = mirror.lookup("*/*/*");
        mirror.dumpState();
        assertEquals(1, oneArr.size());
        Mirror.Entry one = oneArr.get(0);
        assertEquals(one, new Entry(wantName, mySpec));
        assertNotEquals(one, new Entry("B/x/w", mySpec));
        assertNotEquals(one, new Entry(wantName, "foo:99"));
        assertNotEquals(one, null);
        assertNotEquals(one, register);
        assertEquals(one.getName(), wantName);
        assertEquals(one.getSpecString(), mySpec);

        register.registerName("B/x");
        check("B/x", new SpecList().add("B/x", mySpec));
        check("*/*", new SpecList().add("B/x", mySpec));
        check("*/*/*", new SpecList().add("A/x/w", mySpec));

        register.registerName("C/x/z");
        check("C/x/z", new SpecList().add("C/x/z", mySpec));
        check("*/*", new SpecList().add("B/x", mySpec));
        check("*/*/*", new SpecList()
              .add("A/x/w", mySpec)
              .add("C/x/z", mySpec));

        register.registerName("D/y/z");
        check("D/y/z", new SpecList().add("D/y/z", mySpec));
        check("*/*", new SpecList().add("B/x", mySpec));
        check("*/*/*", new SpecList()
              .add("A/x/w", mySpec)
              .add("C/x/z", mySpec)
              .add("D/y/z", mySpec));

        register.registerName("E/y");
        check("E/y", new SpecList().add("E/y", mySpec));
        check("*/*", new SpecList()
              .add("B/x", mySpec)
              .add("E/y", mySpec));
        check("*/*/*", new SpecList()
              .add("A/x/w", mySpec)
              .add("C/x/z", mySpec)
              .add("D/y/z", mySpec));

        register.registerName("F/y/w");
        check("F/y/w", new SpecList().add("F/y/w", mySpec));
        check("*/*", new SpecList()
              .add("B/x", mySpec)
              .add("E/y", mySpec));
        check("*/*/*", new SpecList()
              .add("A/x/w", mySpec)
              .add("C/x/z", mySpec)
              .add("D/y/z", mySpec)
              .add("F/y/w", mySpec));

        check("*", new SpecList());

        check("B/*", new SpecList()
              .add("B/x", mySpec));

        check("*/y", new SpecList()
              .add("E/y", mySpec));

        check("*/x/*", new SpecList()
              .add("A/x/w", mySpec)
              .add("C/x/z", mySpec));

        check("*/*/z", new SpecList()
              .add("C/x/z", mySpec)
              .add("D/y/z", mySpec));

        check("A/*/z", new SpecList());

        check("A/*/w", new SpecList()
              .add("A/x/w", mySpec));

        register.unregisterName("E/y");
        register.unregisterName("C/x/z");
        register.unregisterName("F/y/w");
        check("*/*", new SpecList()
              .add("B/x", mySpec));
        check("*/*/*", new SpecList()
              .add("A/x/w", mySpec)
              .add("D/y/z", mySpec));

        register.registerName("E/y");
        register.registerName("C/x/z");
        register.registerName("F/y/w");
        check("*/*", new SpecList()
              .add("B/x", mySpec)
              .add("E/y", mySpec));
        check("*/*/*", new SpecList()
              .add("A/x/w", mySpec)
              .add("C/x/z", mySpec)
              .add("D/y/z", mySpec)
              .add("F/y/w", mySpec));

        register.unregisterName("E/y");
        register.unregisterName("C/x/z");
        register.unregisterName("F/y/w");
        check("*/*", new SpecList()
              .add("B/x", mySpec));
        check("*/*/*", new SpecList()
              .add("A/x/w", mySpec)
              .add("D/y/z", mySpec));

        register.registerName("E/y");
        register.registerName("C/x/z");
        register.registerName("F/y/w");
        check("*/*", new SpecList()
              .add("B/x", mySpec)
              .add("E/y", mySpec));
        check("*/*/*", new SpecList()
              .add("A/x/w", mySpec)
              .add("C/x/z", mySpec)
              .add("D/y/z", mySpec)
              .add("F/y/w", mySpec));

        assertFalse(error);
    }

    public static void err(String msg) {
        System.err.println(msg);
    }

}