aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/test/java/com/yahoo/config/subscription/impl/JRTConfigRequesterTest.java
blob: dca0c2d00184d23bcab70c077729f19bd516bf5e (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.subscription.impl;

import com.yahoo.config.subscription.ConfigSourceSet;
import com.yahoo.foo.SimpletypesConfig;
import com.yahoo.jrt.Request;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.ConnectionPool;
import com.yahoo.vespa.config.ErrorCode;
import com.yahoo.vespa.config.PayloadChecksums;
import com.yahoo.vespa.config.TimingValues;
import com.yahoo.vespa.config.protocol.JRTServerConfigRequestV3;
import org.junit.Test;

import java.util.Random;

import static com.yahoo.config.subscription.impl.JRTConfigRequester.calculateFailedRequestDelay;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

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

    @Test
    public void testDelayCalculation() {
        TimingValues defaultTimingValues = new TimingValues();
        Random random = new Random(0); // Use seed to make delays deterministic
        TimingValues timingValues = new TimingValues(defaultTimingValues, random);

        int failures = 1;
        // First time failure
        long delay = calculateFailedRequestDelay(failures, timingValues);
        assertEquals(10924, delay);

        failures++;
        // 2nd time failure
        delay = calculateFailedRequestDelay(failures, timingValues);
        assertEquals(22652, delay);

        failures++;
        // 3rd time failure
        delay = calculateFailedRequestDelay(failures, timingValues);
        assertEquals(35849, delay);
    }

    @Test
    public void testFirstRequestAfterSubscribing() {
        TimingValues timingValues = getTestTimingValues();
        MockConnection connection = new MockConnection();
        JRTConfigRequester requester = new JRTConfigRequester(connection, timingValues);
        JRTConfigSubscription<SimpletypesConfig> sub = createSubscription(requester, timingValues);

        assertEquals(requester.getConnectionPool(), connection);
        requester.request(sub);
        final Request request = connection.getRequest();
        assertNotNull(request);
        assertEquals(1, connection.getNumberOfRequests());
        JRTServerConfigRequestV3 receivedRequest = JRTServerConfigRequestV3.createFromRequest(request);
        assertTrue(receivedRequest.validateParameters());
        assertEquals(timingValues.getSubscribeTimeout(), receivedRequest.getTimeout());
        assertEquals(0, requester.getFailures());
    }

    @Test
    public void testFatalError() {
        final TimingValues timingValues = getTestTimingValues();

        final MockConnection connection = new MockConnection(new ErrorResponseHandler());
        JRTConfigRequester requester = new JRTConfigRequester(connection, timingValues);
        requester.request(createSubscription(requester, timingValues));
        waitUntilResponse(connection);
        assertEquals(1, requester.getFailures());
    }

    @Test
    public void testFatalErrorSubscribed() {
        TimingValues timingValues = getTestTimingValues();
        MockConnection connection = new MockConnection(new ErrorResponseHandler());
        JRTConfigRequester requester = new JRTConfigRequester(connection, timingValues);

        JRTConfigSubscription<SimpletypesConfig> sub = createSubscription(requester, timingValues);
        sub.setConfig(1L, false, config(), PayloadChecksums.empty());

        requester.request(sub);
        waitUntilResponse(connection);
        assertEquals(1, requester.getFailures());
    }

    @Test
    public void testTransientError() {
        TimingValues timingValues = getTestTimingValues();

        MockConnection connection = new MockConnection(new ErrorResponseHandler(com.yahoo.jrt.ErrorCode.TIMEOUT));
        JRTConfigRequester requester = new JRTConfigRequester(connection, timingValues);
        requester.request(createSubscription(requester, timingValues));
        waitUntilResponse(connection);
        assertEquals(1, requester.getFailures());
    }

    @Test
    public void testTransientErrorSubscribed() {
        TimingValues timingValues = getTestTimingValues();
        MockConnection connection = new MockConnection(new ErrorResponseHandler(com.yahoo.jrt.ErrorCode.TIMEOUT));
        JRTConfigRequester requester = new JRTConfigRequester(connection, timingValues);
        JRTConfigSubscription<SimpletypesConfig> sub = createSubscription(requester, timingValues);
        sub.setConfig(1L, false, config(), PayloadChecksums.empty());

        requester.request(sub);
        waitUntilResponse(connection);
        assertEquals(1, requester.getFailures());
    }

    @Test
    public void testUnknownConfigDefinitionError() {
        TimingValues timingValues = getTestTimingValues();
        MockConnection connection = new MockConnection(new ErrorResponseHandler(ErrorCode.UNKNOWN_DEFINITION));
        JRTConfigRequester requester = new JRTConfigRequester(connection, timingValues);
        JRTConfigSubscription<SimpletypesConfig> sub = createSubscription(requester, timingValues);
        sub.setConfig(1L, false, config(), PayloadChecksums.empty());

        assertEquals(requester.getConnectionPool(), connection);
        requester.request(sub);
        waitUntilResponse(connection);
        assertEquals(1, requester.getFailures());
    }

    @Test
    public void testClosedSubscription() {
        TimingValues timingValues = getTestTimingValues();
        MockConnection connection = new MockConnection(new MockConnection.OKResponseHandler());
        JRTConfigRequester requester = new JRTConfigRequester(connection, timingValues);
        JRTConfigSubscription<SimpletypesConfig> sub = createSubscription(requester, timingValues);
        sub.close();

        requester.request(sub);
        assertEquals(1, connection.getNumberOfRequests());
        // Check that no further request was sent?
        try {
            Thread.sleep(timingValues.getFixedDelay()*2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        assertEquals(1, connection.getNumberOfRequests());
    }

    @Test
    public void testTimeout() {
        TimingValues timingValues = getTestTimingValues();
        MockConnection connection = new MockConnection(new DelayedResponseHandler(timingValues.getSubscribeTimeout()),
                                                       2); // fake that we have more than one source
        JRTConfigRequester requester = new JRTConfigRequester(connection, timingValues);
        JRTConfigSubscription<SimpletypesConfig> sub = createSubscription(requester, timingValues);
        sub.close();

        requester.request(createSubscription(requester, timingValues));
        // Check that no further request was sent?
        try {
            Thread.sleep(timingValues.getFixedDelay()*2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private JRTConfigSubscription<SimpletypesConfig> createSubscription(JRTConfigRequester requester, TimingValues timingValues) {
        return new JRTConfigSubscription<>(new ConfigKey<>(SimpletypesConfig.class, "testid"),
                                           requester,
                                           timingValues);
    }

    private SimpletypesConfig config() {
        SimpletypesConfig.Builder builder = new SimpletypesConfig.Builder();
        return new SimpletypesConfig(builder);
    }

    private void waitUntilResponse(MockConnection connection) {
        int i = 0;
        while (i < 1000 && connection.getRequest() == null) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i++;
        }
    }

    public static TimingValues getTestTimingValues() { return new TimingValues(
            1000,  // successTimeout
            500,   // errorTimeout
            500,   // initialTimeout
            2000,  // subscribeTimeout
            250);   // fixedDelay
    }

    private static class ErrorResponseHandler extends MockConnection.OKResponseHandler {
        private final int errorCode;

        public ErrorResponseHandler() {
            this(ErrorCode.INTERNAL_ERROR);
        }

        public ErrorResponseHandler(int errorCode) {
            this.errorCode = errorCode;
        }

        @Override
        public void run() {
            System.out.println("Running error response handler");
            request().setError(errorCode, "error");
            requestWaiter().handleRequestDone(request());
        }
    }

    private static class DelayedResponseHandler extends MockConnection.OKResponseHandler {
        private final long waitTimeMilliSeconds;

        public DelayedResponseHandler(long waitTimeMilliSeconds) {
            this.waitTimeMilliSeconds = waitTimeMilliSeconds;
        }

        @Override
        public void run() {
            System.out.println("Running delayed response handler (waiting " + waitTimeMilliSeconds +
            ") before responding");
            try {
                Thread.sleep(waitTimeMilliSeconds);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            request().setError(com.yahoo.jrt.ErrorCode.TIMEOUT, "error");
            requestWaiter().handleRequestDone(request());
        }
    }

    @Test
    public void testManagedPool() {
        ConfigSourceSet sourceSet = ConfigSourceSet.createDefault();
        TimingValues timingValues = new TimingValues();
        JRTConfigRequester requester1 = JRTConfigRequester.create(sourceSet, timingValues);
        JRTConfigRequester requester2 = JRTConfigRequester.create(sourceSet, timingValues);
        assertNotSame(requester1, requester2);
        assertSame(requester1.getConnectionPool(), requester2.getConnectionPool());
        ConnectionPool firstPool = requester1.getConnectionPool();
        requester1.close();
        requester2.close();
        requester1 = JRTConfigRequester.create(sourceSet, timingValues);
        assertNotSame(firstPool, requester1.getConnectionPool());
        requester2 = JRTConfigRequester.create(new ConfigSourceSet("test-managed-pool-2"), timingValues);
        assertNotSame(requester1.getConnectionPool(), requester2.getConnectionPool());
        requester1.close();
        requester2.close();
    }

}