summaryrefslogtreecommitdiffstats
path: root/config-proxy/src/test/java/com/yahoo/vespa/config/proxy/RpcConfigSourceClientTest.java
blob: ada98f4b30e5e75214d40829f266a1ee7182ff3e (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
// Copyright Yahoo. 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.vespa.config.ConfigKey;
import com.yahoo.vespa.config.RawConfig;
import com.yahoo.vespa.config.protocol.JRTConfigRequestFactory;
import com.yahoo.vespa.config.protocol.JRTServerConfigRequestV3;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.junit.Assert.assertEquals;

/**
 * @author hmusum
 */
public class RpcConfigSourceClientTest {

    private ResponseHandler responseHandler;
    private RpcConfigSourceClient rpcConfigSourceClient;

    @Rule
    public TemporaryFolder tempFolder = new TemporaryFolder();


    @Before
    public void setup() {
        responseHandler = new ResponseHandler(true);
        rpcConfigSourceClient = new RpcConfigSourceClient(responseHandler, new MockConfigSource());
    }

    @Test
    public void basic() {
        final RawConfig fooConfig = ProxyServerTest.fooConfig;
        configUpdatedSendResponse(fooConfig);
        // Nobody asked for the config, so no response sent
        assertSentResponses(0);

        simulateClientRequestingConfig(fooConfig);
        configUpdatedSendResponse(fooConfig);
        assertSentResponses(1);

        // Nobody asked for 'bar' config
        RawConfig barConfig = new RawConfig(new ConfigKey<>("bar", "id", "namespace"), fooConfig.getDefMd5());
        configUpdatedSendResponse(barConfig);
        assertSentResponses(1);
    }

    @Test
    public void errorResponse() {
        configUpdatedSendResponse(ProxyServerTest.errorConfig);
        assertSentResponses(0);
    }

    @Test
    public void it_does_not_send_old_config_in_response() {
        RawConfig fooConfigOldGeneration = ProxyServerTest.fooConfig;

        RawConfig fooConfig = createConfigWithNextConfigGeneration(fooConfigOldGeneration);
        configUpdatedSendResponse(fooConfig);

        // Nobody asked for the config
        assertSentResponses(0);

        simulateClientRequestingConfig(fooConfig);
        configUpdatedSendResponse(fooConfig);
        assertSentResponses(1);

        simulateClientRequestingConfig(fooConfig);
        configUpdatedSendResponse(fooConfigOldGeneration);
        // Old config generation, so no response returned
        assertSentResponses(1);
    }

    @Test
    public void it_does_send_config_with_generation_0_in_response() {
        RawConfig fooConfigOldGeneration = ProxyServerTest.fooConfig;

        RawConfig fooConfig = createConfigWithNextConfigGeneration(fooConfigOldGeneration, 1);

        simulateClientRequestingConfig(fooConfig);
        configUpdatedSendResponse(fooConfig);
        assertSentResponses(1);

        RawConfig fooConfig2 = createConfigWithNextConfigGeneration(fooConfigOldGeneration, 0);
        simulateClientRequestingConfig(fooConfig2);
        configUpdatedSendResponse(fooConfig2);
        assertSentResponses(2);
    }

    private void assertSentResponses(int expected) {
        assertEquals(expected, responseHandler.sentResponses());
    }

    private void simulateClientRequestingConfig(RawConfig config) {
        rpcConfigSourceClient.delayedResponses().add(new DelayedResponse(JRTServerConfigRequestV3.createFromRequest(JRTConfigRequestFactory.createFromRaw(config, -10L).getRequest())));
    }

    private void configUpdatedSendResponse(RawConfig config) {
        rpcConfigSourceClient.updateSubscribers(config);
    }

    private RawConfig createConfigWithNextConfigGeneration(RawConfig config) {
        return createConfigWithNextConfigGeneration(config, config.getGeneration() + 1);
    }

    private RawConfig createConfigWithNextConfigGeneration(RawConfig config, long newConfigGeneration) {
        final int errorCode = 0;
        return ProxyServerTest.createConfigWithNextConfigGeneration(config, errorCode, ProxyServerTest.fooConfig.getPayload(), newConfigGeneration);
    }

}