summaryrefslogtreecommitdiffstats
path: root/config-proxy/src/test/java/com/yahoo/vespa/config/proxy/ClientUpdaterTest.java
blob: 0abc63d089c007399d1ef9b48a118a537fe99066 (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
// 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.vespa.config.ConfigCacheKey;
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.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

/**
 * @author hmusum
 */
public class ClientUpdaterTest {
    private MockRpcServer rpcServer;
    private ConfigProxyStatistics statistics;
    private DelayedResponses delayedResponses;
    private Mode mode;
    private MemoryCache memoryCache;
    private ClientUpdater clientUpdater;

    @Rule
    public TemporaryFolder tempFolder = new TemporaryFolder();


    @Before
    public void setup() {
        rpcServer = new MockRpcServer();
        statistics = new ConfigProxyStatistics();
        delayedResponses = new DelayedResponses(statistics);
        mode = new Mode();
        memoryCache = new MemoryCache();
        clientUpdater = new ClientUpdater(memoryCache, rpcServer, statistics, delayedResponses, mode);
    }

    @Test
    public void basic() {
        assertThat(rpcServer.responses, is(0L));

        final RawConfig fooConfig = ProxyServerTest.fooConfig;
        clientUpdater.updateSubscribers(fooConfig);

        // No delayed response, so not returned
        assertEquals(0, rpcServer.responses);

        delayedResponses.add(new DelayedResponse(JRTServerConfigRequestV3.createFromRequest(JRTConfigRequestFactory.createFromRaw(fooConfig, -10L).getRequest())));
        clientUpdater.updateSubscribers(fooConfig);
        assertEquals(1, rpcServer.responses);

        // Will not find bar config in delayed responses
        RawConfig barConfig = new RawConfig(new ConfigKey<>("bar", "id", "namespace"), fooConfig.getDefMd5());
        clientUpdater.updateSubscribers(barConfig);
        assertEquals(1, rpcServer.responses);


        mode = new Mode(Mode.ModeName.MEMORYCACHE);
        // Nothing should be returned, so still 1 response
        assertEquals(1, rpcServer.responses);
        assertThat(statistics.errors(), is(0L));
    }

    @Test
    public void memoryCacheMode() {
        final RawConfig fooConfig = ProxyServerTest.fooConfig;
        mode = new Mode(Mode.ModeName.MEMORYCACHE);
        clientUpdater = new ClientUpdater(memoryCache, rpcServer, statistics,delayedResponses, mode);
        memoryCache.clear();
        assertThat(rpcServer.responses, is(0L));

        clientUpdater.updateSubscribers(fooConfig);
        assertNull(memoryCache.get(new ConfigCacheKey(fooConfig.getKey(), fooConfig.getDefMd5())));
        assertThat(memoryCache.size(), is(0));
        assertThat(rpcServer.responses, is(0L));
    }

    @Test
    public void errorResponse() {
        assertThat(rpcServer.responses, is(0L));

        final RawConfig errorConfig = ProxyServerTest.errorConfig;

        clientUpdater.updateSubscribers(errorConfig);
        // Error response, so not put into cache
        assertNull(memoryCache.get(new ConfigCacheKey(errorConfig.getKey(), errorConfig.getDefMd5())));
        assertThat(rpcServer.responses, is(0L));
        assertThat(statistics.errors(), is(1L));
    }


}