aboutsummaryrefslogtreecommitdiffstats
path: root/jaxrs_client_utils/src/test/java/com/yahoo/vespa/jaxrs/client/RetryingJaxRsStrategyTest.java
blob: dbe886b7896cf0eb0ec669465ad07ef6221225a8 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.jaxrs.client;

import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.defaults.Defaults;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.OngoingStubbing;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.ProcessingException;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class RetryingJaxRsStrategyTest {
    private static final String API_PATH = "/";

    @Captor
    ArgumentCaptor<JaxRsClientFactory.Params<TestJaxRsApi>> paramsCaptor;

    @Path(API_PATH)
    private interface TestJaxRsApi {
        @GET
        @Path("/foo/bar")
        String doSomething();
    }

    private static final Set<HostName> SERVER_HOSTS = new HashSet<>(Arrays.asList(
            new HostName("host-1"),
            new HostName("host-2"),
            new HostName("host-3")));
    private static final int REST_PORT = Defaults.getDefaults().vespaWebServicePort();

    private final JaxRsClientFactory jaxRsClientFactory = mock(JaxRsClientFactory.class);
    private final TestJaxRsApi mockApi = mock(TestJaxRsApi.class);
    private final RetryingJaxRsStrategy<TestJaxRsApi> jaxRsStrategy = new RetryingJaxRsStrategy<>(
            SERVER_HOSTS, REST_PORT, jaxRsClientFactory, TestJaxRsApi.class, API_PATH, "http");

    @Before
    public void setup() {
        when(jaxRsClientFactory.createClient(any())).thenReturn(mockApi);
    }

    @Test
    public void noRetryIfNoFailure() throws Exception {
        jaxRsStrategy.apply(TestJaxRsApi::doSomething);

        verify(mockApi, times(1)).doSomething();

        verify(jaxRsClientFactory, times(1)).createClient(paramsCaptor.capture());
        JaxRsClientFactory.Params<TestJaxRsApi> params = paramsCaptor.getValue();
        assertEquals(REST_PORT, params.uri().getPort());
        assertEquals(API_PATH, params.uri().getPath());
        assertEquals("http", params.uri().getScheme());
        assertThat(SERVER_HOSTS, hasItem(new HostName(params.uri().getHost())));
    }

    @Test
    public void testRetryAfterSingleFailure() throws Exception {
        // Make the first attempt fail.
        when(mockApi.doSomething())
                .thenThrow(new ProcessingException("Fake timeout induced by test"))
                .thenReturn("a response");

        jaxRsStrategy.apply(TestJaxRsApi::doSomething);

        // Check that there was a second attempt.
        verify(mockApi, times(2)).doSomething();
    }

    @Test
    public void testRetryUsesAllAvailableServers() throws Exception {
        when(mockApi.doSomething())
                .thenThrow(new ProcessingException("Fake timeout 1 induced by test"))
                .thenThrow(new ProcessingException("Fake timeout 2 induced by test"))
                .thenReturn("a response");

        jaxRsStrategy.apply(TestJaxRsApi::doSomething);

        verify(mockApi, times(3)).doSomething();
        verifyAllServersContacted(jaxRsClientFactory);
    }

    @Test
    public void testRetryLoopsOverAvailableServers() throws Exception {
        when(mockApi.doSomething())
                .thenThrow(new ProcessingException("Fake socket timeout 1 induced by test"))
                .thenThrow(new ProcessingException("Fake socket timeout 2 induced by test"))
                .thenThrow(new ProcessingException("Fake socket timeout 3 induced by test"))
                .thenThrow(new ProcessingException("Fake socket timeout 4 induced by test"))
                .thenReturn("a response");

        jaxRsStrategy.apply(TestJaxRsApi::doSomething);

        verify(mockApi, times(5)).doSomething();
        verifyAllServersContacted(jaxRsClientFactory);
    }

    @Test
    public void testRetryGivesUpAfterOneLoopOverAvailableServers() {
        jaxRsStrategy.setMaxIterations(1);
        testRetryGivesUpAfterXIterations(1);
    }

    @Test
    public void testRetryGivesUpAfterTwoLoopsOverAvailableServers() {
        testRetryGivesUpAfterXIterations(2);
    }

    private void testRetryGivesUpAfterXIterations(int iterations) {
        OngoingStubbing<String> stub = when(mockApi.doSomething());
        for (int i = 0; i < iterations; ++i) {
            stub = stub
                    .thenThrow(new ProcessingException("Fake timeout 1 iteration " + i))
                    .thenThrow(new ProcessingException("Fake timeout 2 iteration " + i))
                    .thenThrow(new ProcessingException("Fake timeout 3 iteration " + i));
        }

        try {
            jaxRsStrategy.apply(TestJaxRsApi::doSomething);
            fail("Exception should be thrown from above statement");
        } catch (IOException e) {
            // As expected.
        }

        verify(mockApi, times(iterations * 3)).doSomething();
        verifyAllServersContacted(jaxRsClientFactory);
    }

    private void verifyAllServersContacted(final JaxRsClientFactory jaxRsClientFactory) {
        verify(jaxRsClientFactory, atLeast(SERVER_HOSTS.size())).createClient(paramsCaptor.capture());
        final Set<JaxRsClientFactory.Params<TestJaxRsApi>> actualServerHostsContacted = new HashSet<>(paramsCaptor.getAllValues());
        assertEquals(actualServerHostsContacted.stream().map(x -> new HostName(x.uri().getHost())).collect(Collectors.toSet()), SERVER_HOSTS);
    }
}