summaryrefslogtreecommitdiffstats
path: root/config-proxy/src/test/java/com/yahoo/vespa/config/proxy/ConfigProxyRpcServerTest.java
blob: db4a0dc357f3e52c4a77a68b869d3d04e5eba08e (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.proxy;

import com.yahoo.config.subscription.ConfigSourceSet;
import com.yahoo.jrt.Request;
import com.yahoo.jrt.Spec;
import com.yahoo.jrt.StringValue;
import com.yahoo.vespa.config.RawConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;

/**
 * @author hmusum
 * @since 5.1.9
 */
public class ConfigProxyRpcServerTest {
    private static final String hostname = "localhost";
    private static final int port = 12345;
    private static final String address = "tcp/" + hostname + ":" + port;
    private ProxyServer proxyServer;
    private ConfigProxyRpcServer rpcServer;

    @Before
    public void setup() {
        proxyServer = ProxyServer.createTestServer(new ConfigSourceSet(address));
        rpcServer = new ConfigProxyRpcServer(proxyServer, null);
    }

    @After
    public void teardown() {
        rpcServer.shutdown();
    }

    @Test
    public void basic() {
        ConfigSourceSet configSources = new ConfigSourceSet();
        ProxyServer proxy = ProxyServer.createTestServer(configSources);
        Spec spec = new Spec("localhost", 12345);
        ConfigProxyRpcServer server = new ConfigProxyRpcServer(proxy, spec);
        assertThat(server.getSpec(), is(spec));
    }

    /**
     * Tests ping RPC command
     */
    @Test
    public void testRpcMethodPing() {
        Request req = new Request("ping");
        rpcServer.ping(req);

        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        assertThat(req.returnValues().get(0).asInt32(), is(0));
    }

    /**
     * Tests listCachedConfig RPC command
     */
    @Test
    public void testRpcMethodListCachedConfig() {
        Request req = new Request("listCachedConfig");
        rpcServer.listCachedConfig(req);

        assertFalse(req.errorMessage(), req.isError());
        String[] ret = req.returnValues().get(0).asStringArray();
        assertThat(req.returnValues().size(), is(1));
        assertThat(ret.length, is(0));

        final RawConfig config = ProxyServerTest.fooConfig;
        proxyServer.getMemoryCache().put(config);
        req = new Request("listCachedConfig");
        rpcServer.listCachedConfig(req);
        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        ret = req.returnValues().get(0).asStringArray();
        assertThat(ret.length, is(1));
        assertThat(ret[0], is(config.getNamespace() + "." + config.getName() + "," +
                config.getConfigId() + "," +
                config.getGeneration() + "," +
                config.getConfigMd5()));
    }

    /**
     * Tests listCachedConfig RPC command
     */
    @Test
    public void testRpcMethodListCachedConfigFull() {
        Request req = new Request("listCachedConfigFull");
        rpcServer.listCachedConfigFull(req);

        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        String[] ret = req.returnValues().get(0).asStringArray();
        assertThat(ret.length, is(0));

        final RawConfig config = ProxyServerTest.fooConfig;
        proxyServer.getMemoryCache().put(config);
        req = new Request("listCachedConfigFull");
        rpcServer.listCachedConfigFull(req);
        assertFalse(req.errorMessage(), req.isError());
        ret = req.returnValues().get(0).asStringArray();
        assertThat(ret.length, is(1));
        assertThat(ret[0], is(config.getNamespace() + "." + config.getName() + "," +
                config.getConfigId() + "," +
                config.getGeneration() + "," +
                config.getConfigMd5() + "," +
                config.getPayload().getData()));
    }

    /**
     * Tests printStatistics RPC command
     */
    @Test
    public void testRpcMethodListSourceConnections() {
        Request req = new Request("listSourceConnections");
        rpcServer.listSourceConnections(req);

        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        final String[] ret = req.returnValues().get(0).asStringArray();
        assertThat(ret.length, is(2));
        assertThat(ret[0], is("Current source: " + address));
        assertThat(ret[1], is("All sources:\n" + address + "\n"));
    }

    /**
     * Tests printStatistics RPC command
     */
    @Test
    public void testRpcMethodPrintStatistics() {
        Request req = new Request("printStatistics");
        rpcServer.printStatistics(req);
        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        assertThat(req.returnValues().get(0).asString(), is("\n" +
                "Delayed responses queue size: 0\n" +
                "Contents: "));
    }

    /**
     * Tests invalidateCache RPC command
     */
    @Test
    public void testRpcMethodInvalidateCache() {
        Request req = new Request("invalidateCache");
        rpcServer.invalidateCache(req);

        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        final String[] ret = req.returnValues().get(0).asStringArray();
        assertThat(ret.length, is(2));
        assertThat(ret[0], is("0"));
        assertThat(ret[1], is("success"));
    }

    /**
     * Tests getMode and setMode RPC commands
     */
    @Test
    public void testRpcMethodGetModeAndSetMode() {
        Request req = new Request("getMode");
        rpcServer.getMode(req);
        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        assertThat(req.returnValues().get(0).asString(), is("default"));

        req = new Request("setMode");
        String mode = "memorycache";
        req.parameters().add(new StringValue(mode));
        rpcServer.setMode(req);
        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        String[] ret = req.returnValues().get(0).asStringArray();
        assertThat(ret.length, is(2));
        assertThat(ret[0], is("0"));
        assertThat(ret[1], is("success"));
        assertThat(proxyServer.getMode().name(), is(mode));

        req = new Request("getMode");
        rpcServer.getMode(req);
        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        assertThat(req.returnValues().get(0).asString(), is(mode));

        req = new Request("setMode");
        String oldMode = mode;
        mode = "invalid";
        req.parameters().add(new StringValue(mode));
        rpcServer.setMode(req);

        assertFalse(req.errorMessage(), req.isError());
        ret = req.returnValues().get(0).asStringArray();
        assertThat(ret.length, is(2));
        assertThat(ret[0], is("1"));
        assertThat(ret[1], is("Could not set mode to '" + mode + "'. Legal modes are '" + Mode.modes() + "'"));
        assertThat(proxyServer.getMode().name(), is(oldMode));
    }

    /**
     * Tests updateSources RPC command
     */
    @Test
    public void testRpcMethodUpdateSources() {
        Request req = new Request("updateSources");
        String spec1 = "tcp/a:19070";
        String spec2 = "tcp/b:19070";
        req.parameters().add(new StringValue(spec1 + "," + spec2));
        rpcServer.updateSources(req);
        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        assertThat(req.returnValues().get(0).asString(), is("Updated config sources to: " + spec1 + "," + spec2));


        proxyServer.setMode(Mode.ModeName.MEMORYCACHE.name());

        req = new Request("updateSources");
        req.parameters().add(new StringValue(spec1 + "," + spec2));
        rpcServer.updateSources(req);
        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        assertThat(req.returnValues().get(0).asString(), is("Cannot update sources when in '" + Mode.ModeName.MEMORYCACHE.name().toLowerCase() + "' mode"));

        // TODO source connections needs to have deterministic order to work
        /*req = new Request("listSourceConnections");
        rpcServer.listSourceConnections(req);
        assertFalse(req.errorMessage(), req.isError());
        final String[] ret = req.returnValues().get(0).asStringArray();
        assertThat(ret.length, is(2));
        assertThat(ret[0], is("Current source: " + spec1));
        assertThat(ret[1], is("All sources:\n" + spec2 + "\n" + spec1 + "\n"));
        */
    }

    /**
     * Tests dumpCache RPC command
     */
    @Test
    public void testRpcMethodDumpCache() {
        Request req = new Request("dumpCache");
        String path = "/tmp";
        req.parameters().add(new StringValue(path));
        rpcServer.dumpCache(req);
        assertFalse(req.errorMessage(), req.isError());
        assertThat(req.returnValues().size(), is(1));
        assertThat(req.returnValues().get(0).asString(), is("success"));
    }

}