aboutsummaryrefslogtreecommitdiffstats
path: root/jrt_test/src/tests/slobrok-api/SlobrokAPITest.java
blob: 50d2fa5f92ccaedbdc02ccd6567cdf289d55d307 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.


import com.yahoo.jrt.*;
import com.yahoo.jrt.slobrok.api.*;
import java.util.Comparator;
import java.util.Arrays;
import java.util.ArrayList;


public class SlobrokAPITest {

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

    String[]   slobroks;
    SlobrokList slist = new SlobrokList();
    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;

    public SlobrokAPITest(String slobrokSpec) throws ListenFailedException {
	slobroks = new String[1];
	slobroks[0] = slobrokSpec;
        slist.setup(slobroks);
	acceptor = server.listen(new Spec(0));
	mirror = new Mirror(client, slist);
	register = new Register(server, slist,
				"localhost", acceptor.port());
	mySpec = new Spec("localhost", acceptor.port()).toString();
    }

    void shutdown() {
	register.shutdown();
	mirror.shutdown();
	acceptor.shutdown();
	client.transport().shutdown();
	server.transport().shutdown();
    }

    void check(String pattern, ArrayList result) {
	Comparator cmp = new Comparator() {
		public int compare(Object a, Object b) {
		    Mirror.Entry x = (Mirror.Entry) a;
		    Mirror.Entry y = (Mirror.Entry) b;
                    return x.compareTo(y);
		}
	    };
	Mirror.Entry[] expect
	    = (Mirror.Entry[]) result.toArray(new Mirror.Entry[result.size()]);
	Arrays.sort(expect, cmp);
	Mirror.Entry[] actual = new Mirror.Entry[0];
	for (int i = 0; i < 600; i++) {
	    actual = mirror.lookup(pattern);
	    Arrays.sort(actual, cmp);
	    if (Arrays.equals(actual, expect)) {
		err("lookup successful for pattern: " + pattern);
		return;
	    }
	    try { Thread.sleep(100); } catch (InterruptedException e) {}
	}
	error = true;
	err("lookup failed for pattern: " + pattern);
	err("actual values:");
	if (actual.length == 0) {
	    err("  { EMPTY }");
	}
	for (int i = 0; i < actual.length; i++) {
	    err("  {" + actual[i].getName() + ", " + actual[i].getSpec() + "}");
	}
	err("expected values:");
	if (expect.length == 0) {
	    err("  { EMPTY }");
	}
	for (int i = 0; i < expect.length; i++) {
	    err("  {" + expect[i].getName() + ", " + expect[i].getSpec() + "}");
	}
    }

    public void runTests() throws Exception {
	try {
	    register.registerName("A/x/w");
	    check("A/x/w", new SpecList().add("A/x/w", mySpec));
	    check("*/*", new SpecList());
	    check("*/*/*", new SpecList().add("A/x/w", 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));

	    if (error) {
		throw new Exception("Test failed");
	    }
	} finally {
	    shutdown();
	}
    }

    public static void main(String[] args) {
	if (args.length != 1) {
	    err("usage: SlobrokAPITest slobrok-spec");
	    System.exit(1);
	}
	try {
	    new SlobrokAPITest(args[0]).runTests();
	} catch (Exception e) {
	    e.printStackTrace();
	    System.exit(1);
	}
    }

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