summaryrefslogtreecommitdiffstats
path: root/config-proxy/src/test/java/com/yahoo/vespa/config/proxy/MemoryCacheTest.java
blob: b0cba728a0c02d601ff037d329813d85a3861c62 (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
// 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.slime.Slime;
import com.yahoo.vespa.config.ConfigCacheKey;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.ConfigPayload;
import com.yahoo.vespa.config.PayloadChecksums;
import com.yahoo.vespa.config.RawConfig;
import com.yahoo.vespa.config.protocol.Payload;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Optional;

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

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

    private final String defName = "foo";
    private final String configId = "id";
    private final String namespace = "bar";
    private static final String defMd5 = "a";

    private final long generation = 1L;
    private final String defName2 = "baz-quux";
    private final String namespace2 = "search.config";
    // Test with a config id with / in it
    private final String configId2 = "clients/gateways/gateway/component/com.yahoo.feedhandler.VespaFeedHandlerRemoveLocation";
    private static final String defMd52 = "a2";
    private static final String differentDefMd5 = "09ef";
    private static final PayloadChecksums checksums = PayloadChecksums.from("b", "");
    private final ConfigKey<?> configKey = new ConfigKey<>(defName, configId, namespace);
    private final ConfigKey<?> configKey2 = new ConfigKey<>(defName2, configId2, namespace2);
    private ConfigCacheKey cacheKey;
    private ConfigCacheKey cacheKeyDifferentMd5;
    private ConfigCacheKey cacheKey2;
    private RawConfig config;
    private RawConfig config2;
    private RawConfig configDifferentMd5;
    private Payload payload;
    private Payload payload2;
    private Payload payloadDifferentMd5;

    @Before
    public void setup() {
        ArrayList<String> defContent = new ArrayList<>();
        defContent.add("bar string");

        Slime slime = new Slime();
        slime.setString("bar \"value\"");
        payload = Payload.from(new ConfigPayload(slime));

        slime = new Slime();
        slime.setString("bar \"baz\"");
        payload2 = Payload.from(new ConfigPayload(slime));

        slime = new Slime();
        slime.setString("bar \"value2\"");
        payloadDifferentMd5 = Payload.from(new ConfigPayload(slime));

        config = new RawConfig(configKey, defMd5, payload, checksums, generation, false, defContent, Optional.empty());
        config2 = new RawConfig(configKey2, defMd52, payload2, checksums, generation, false, defContent, Optional.empty());
        configDifferentMd5 = new RawConfig(configKey, differentDefMd5, payloadDifferentMd5, checksums, generation, false, defContent, Optional.empty());

        cacheKey = new ConfigCacheKey(configKey, config.getDefMd5());
        cacheKey2 = new ConfigCacheKey(configKey2, config2.getDefMd5());
        cacheKeyDifferentMd5 = new ConfigCacheKey(configKey, differentDefMd5);
    }

    @Test
    public void basic() {
        MemoryCache cache = new MemoryCache();

        cache.update(config);
        cache.update(config2);
        assertEquals(2, cache.size());
        assertTrue(cache.containsKey(cacheKey));
        assertTrue(cache.containsKey(cacheKey2));

        RawConfig response = cache.get(cacheKey).orElseThrow();
        assertEquals(defName, response.getName());
        assertEquals(payload.toString(), response.getPayload().toString());
        assertEquals(generation, response.getGeneration());

        response = cache.get(cacheKey2).orElseThrow();
        assertEquals(defName2, response.getName());
        assertEquals(payload2.toString(), response.getPayload().toString());
        assertEquals(generation, response.getGeneration());

        cache.clear();
    }

    @Test
    public void testSameConfigNameDifferentMd5() {
        MemoryCache cache = new MemoryCache();

        cache.update(config);
        cache.update(configDifferentMd5); // same name, different defMd5
        assertEquals(2, cache.size());
        assertTrue(cache.containsKey(cacheKey));

        RawConfig response = cache.get(cacheKey).orElseThrow();
        assertEquals(defName, response.getName());
        assertEquals(payload.getData(), response.getPayload().getData());
        assertEquals(generation, response.getGeneration());

        response = cache.get(cacheKeyDifferentMd5).orElseThrow();
        assertEquals(defName, response.getName());
        assertEquals(payloadDifferentMd5.getData(), response.getPayload().getData());
        assertEquals(generation, response.getGeneration());

        cache.clear();
        assertEquals(0, cache.size());
    }
}