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 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 junit.framework.TestCase.assertNull;
import static org.hamcrest.CoreMatchers.is;
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(CacheManager.createTestCacheManager(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
assertResponseAndCache(rpcServer, memoryCache, fooConfig, 0, 1);
delayedResponses.add(new DelayedResponse(JRTServerConfigRequestV3.createFromRequest(JRTConfigRequestFactory.createFromRaw(fooConfig, -10L).getRequest())));
clientUpdater.updateSubscribers(fooConfig);
assertResponseAndCache(rpcServer, memoryCache, fooConfig, 1, 1);
// Will not find bar config in delayed responses
RawConfig barConfig = new RawConfig(new ConfigKey<>("bar", "id", "namespace"), fooConfig.getDefMd5());
clientUpdater.updateSubscribers(barConfig);
assertResponseAndCache(rpcServer, memoryCache, barConfig, 1, 2);
mode = new Mode(Mode.ModeName.MEMORYCACHE.name());
// Nothing should be returned, so still 1 response
assertResponseAndCache(rpcServer, memoryCache, fooConfig, 1, 2);
assertThat(statistics.errors(), is(0L));
}
@Test
public void memoryCacheMode() {
final RawConfig fooConfig = ProxyServerTest.fooConfig;
mode = new Mode(Mode.ModeName.MEMORYCACHE.name());
clientUpdater = new ClientUpdater(CacheManager.createTestCacheManager(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));
}
private static void assertResponseAndCache(MockRpcServer rpcServer,
MemoryCache memoryCache,
RawConfig expectedConfig,
long expectedResponses,
int cacheSize) {
assertThat(rpcServer.responses, is(expectedResponses));
assertThat(memoryCache.size(), is(cacheSize));
assertThat(memoryCache.get(new ConfigCacheKey(expectedConfig.getKey(), expectedConfig.getDefMd5())), is(expectedConfig));
}
}
|