summaryrefslogtreecommitdiffstats
path: root/config-proxy/src/test/java/com/yahoo/vespa/config/proxy/UpstreamConfigSubscriberTest.java
blob: 01733d0fd39e16ef33f8a443b06cec6affb54e0c (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
// 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.config.subscription.impl.JRTConfigRequester;
import com.yahoo.slime.Slime;
import com.yahoo.vespa.config.*;
import com.yahoo.vespa.config.protocol.*;
import com.yahoo.vespa.config.util.ConfigUtils;
import org.junit.*;
import org.junit.rules.TemporaryFolder;

import java.time.Duration;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

import static junit.framework.TestCase.assertNotNull;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;

/**
 * @author hmusum
 */
public class UpstreamConfigSubscriberTest {
    private final ConfigSourceSet sourceSet = new ConfigSourceSet("tcp/foo:78");
    private final TimingValues timingValues = ProxyServer.defaultTimingValues();

    private MapBackedConfigSource sourceResponses;
    private MockClientUpdater clientUpdater;
    private MockConnection mockConnection;
    private long generation = 1;


    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Before
    public void setup() {
        clientUpdater = MockClientUpdater.create(new MemoryCache());
        sourceResponses = new MapBackedConfigSource(clientUpdater);
        mockConnection = new MockConnection(sourceResponses);
    }

    @Test
    public void basic() {
        RawConfig fooConfig = Helper.fooConfig;
        sourceResponses.put(fooConfig.getKey(), fooConfig);

        UpstreamConfigSubscriber subscriber = createUpstreamConfigSubscriber(fooConfig);
        waitForConfigGeneration(clientUpdater, fooConfig.getKey(), generation);
        assertThat(clientUpdater.getLastConfig(), is(fooConfig));
        subscriber.cancel();
    }

    @Test
    public void require_that_reconfiguration_works() {
        RawConfig fooConfig = Helper.fooConfig;
        sourceResponses.put(fooConfig.getKey(), fooConfig);

        UpstreamConfigSubscriber subscriber = createUpstreamConfigSubscriber(fooConfig);
        waitForConfigGeneration(clientUpdater, fooConfig.getKey(), generation);
        assertThat(clientUpdater.getLastConfig(), is(fooConfig));

        // Update payload in config
        generation++;
        final ConfigPayload payload = Helper.createConfigPayload("bar", "value2");
        RawConfig fooConfig2 = createRawConfig(fooConfig, payload);
        sourceResponses.put(fooConfig2.getKey(), fooConfig2);

        waitForConfigGeneration(clientUpdater, fooConfig2.getKey(), generation);
        assertFalse(clientUpdater.getLastConfig().equals(fooConfig));
        subscriber.cancel();
    }

    @Test
    public void require_that_error_response_is_handled() {
        // Create config with error based on fooConfig
        RawConfig fooConfig = Helper.fooConfig;
        ConfigPayload errorConfigPayload = new ConfigPayload(new Slime());
        Payload errorPayload = Payload.from(errorConfigPayload);
        RawConfig errorConfig = new RawConfig(fooConfig.getKey(), fooConfig.getDefMd5(), errorPayload,
                                              ConfigUtils.getMd5(errorConfigPayload), generation,
                                              ErrorCode.UNKNOWN_DEFINITION, fooConfig.getDefContent(), Optional.empty());
        sourceResponses.put(fooConfig.getKey(), errorConfig);

        UpstreamConfigSubscriber subscriber = createUpstreamConfigSubscriber(errorConfig);
        waitForConfigGeneration(clientUpdater, fooConfig.getKey(), generation);
        RawConfig lastConfig = clientUpdater.getLastConfig();
        assertEquals(errorConfig, lastConfig);
        assertThat(lastConfig.errorCode(), is(ErrorCode.UNKNOWN_DEFINITION));
        subscriber.cancel();
    }

    private Map<ConfigSourceSet, JRTConfigRequester> createRequesterPool() {
        JRTConfigRequester request = JRTConfigRequester.get(mockConnection, timingValues);

        Map<ConfigSourceSet, JRTConfigRequester> requesterPool = new LinkedHashMap<>();
        requesterPool.put(sourceSet, request);
        return requesterPool;
    }

    private void waitForConfigGeneration(MockClientUpdater clientUpdater, ConfigKey<?> configKey, long expectedGeneration) {
        Instant end = Instant.now().plus(Duration.ofSeconds(60));
        RawConfig lastConfig;
        do {
            lastConfig = clientUpdater.getLastConfig();
            System.out.println("config=" + lastConfig + (lastConfig == null ? "" : ",generation=" + lastConfig.getGeneration()));
            if (lastConfig != null && lastConfig.getKey().equals(configKey) &&
                    lastConfig.getGeneration() == expectedGeneration) {
                break;
            } else {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        } while (Instant.now().isBefore(end));

        assertNotNull(lastConfig);
        assertThat(lastConfig.getKey(), is(configKey));
        assertThat(lastConfig.getGeneration(), is(expectedGeneration));
    }

    static class MockClientUpdater extends ClientUpdater {
        private RawConfig lastConfig;

        private MockClientUpdater(ConfigProxyStatistics statistics, Mode mode, MemoryCache memoryCache) {
            super(memoryCache, new MockRpcServer(), statistics, new DelayedResponses(statistics), mode);
        }

        static MockClientUpdater create(MemoryCache memoryCache) {
            Mode mode = new Mode();
            ConfigProxyStatistics statistics = new ConfigProxyStatistics();
            return new MockClientUpdater(statistics, mode, memoryCache);
        }

        @Override
        public void updateSubscribers(RawConfig newConfig) {
            lastConfig = newConfig;
        }

        RawConfig getLastConfig() {
            return lastConfig;
        }
    }

    private UpstreamConfigSubscriber createUpstreamConfigSubscriber(RawConfig config) {
        UpstreamConfigSubscriber subscriber = new UpstreamConfigSubscriber(config, clientUpdater, sourceSet, timingValues, createRequesterPool());
        subscriber.subscribe();
        new Thread(subscriber).start();
        return subscriber;
    }

    // Create new config based on another one
    private RawConfig createRawConfig(RawConfig config, ConfigPayload configPayload) {
        final int errorCode = 0;
        Payload fooPayload = Payload.from(configPayload);
        return new RawConfig(config.getKey(), config.getDefMd5(), fooPayload,
                             ConfigUtils.getMd5(configPayload), generation, errorCode,
                             config.getDefContent(), Optional.empty());
    }

}