aboutsummaryrefslogtreecommitdiffstats
path: root/http-utils/src/test/java/ai/vespa/util/http/hc4/retry/DelayedConnectionLevelRetryHandlerTest.java
blob: 0b83227d5383f0821d1a2dc183cce1df8302c0cf (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.util.http.hc4.retry;

import org.apache.http.client.protocol.HttpClientContext;
import org.junit.jupiter.api.Test;

import javax.net.ssl.SSLException;
import java.io.IOException;
import java.net.ConnectException;
import java.time.Duration;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * @author bjorncs
 */
public class DelayedConnectionLevelRetryHandlerTest {

    @SuppressWarnings("unchecked")
    @Test
    void retry_consumers_are_invoked() {
        RetryConsumer<IOException> retryConsumer = (RetryConsumer<IOException>) mock(RetryConsumer.class);
        RetryFailedConsumer<IOException> retryFailedConsumer = (RetryFailedConsumer<IOException>) mock(RetryFailedConsumer.class);

        Duration delay = Duration.ofSeconds(10);
        int maxRetries = 5;

        DelayedConnectionLevelRetryHandler handler = DelayedConnectionLevelRetryHandler.Builder
                .withFixedDelay(delay, maxRetries)
                .withSleeper(mock(Sleeper.class))
                .onRetry(retryConsumer)
                .onRetryFailed(retryFailedConsumer)
                .build();

        IOException exception = new IOException();
        HttpClientContext ctx = new HttpClientContext();
        int lastExecutionCount = maxRetries + 1;
        for (int i = 1; i <= lastExecutionCount; i++) {
            handler.retryRequest(exception, i, ctx);
        }

        verify(retryFailedConsumer).onRetryFailed(exception, lastExecutionCount, ctx);
        for (int i = 1; i < lastExecutionCount; i++) {
            verify(retryConsumer).onRetry(exception, delay, i, ctx);
        }
    }

    @Test
    void retry_with_fixed_delay_sleeps_for_expected_duration() {
        Sleeper sleeper = mock(Sleeper.class);

        Duration delay = Duration.ofSeconds(2);
        int maxRetries = 2;

        DelayedConnectionLevelRetryHandler handler = DelayedConnectionLevelRetryHandler.Builder
                .withFixedDelay(delay, maxRetries)
                .withSleeper(sleeper)
                .build();

        IOException exception = new IOException();
        HttpClientContext ctx = new HttpClientContext();
        int lastExecutionCount = maxRetries + 1;
        for (int i = 1; i <= lastExecutionCount; i++) {
            handler.retryRequest(exception, i, ctx);
        }

        verify(sleeper, times(2)).sleep(delay);
    }

    @Test
    void retry_with_fixed_backoff_sleeps_for_expected_durations() {
        Sleeper sleeper = mock(Sleeper.class);

        Duration startDelay = Duration.ofMillis(500);
        Duration maxDelay = Duration.ofSeconds(5);
        int maxRetries = 10;

        DelayedConnectionLevelRetryHandler handler = DelayedConnectionLevelRetryHandler.Builder
                .withExponentialBackoff(startDelay, maxDelay, maxRetries)
                .withSleeper(sleeper)
                .build();

        IOException exception = new IOException();
        HttpClientContext ctx = new HttpClientContext();
        int lastExecutionCount = maxRetries + 1;
        for (int i = 1; i <= lastExecutionCount; i++) {
            handler.retryRequest(exception, i, ctx);
        }

        verify(sleeper).sleep(startDelay);
        verify(sleeper).sleep(Duration.ofSeconds(1));
        verify(sleeper).sleep(Duration.ofSeconds(2));
        verify(sleeper).sleep(Duration.ofSeconds(4));
        verify(sleeper, times(6)).sleep(Duration.ofSeconds(5));
    }

    @Test
    void retries_for_listed_exceptions_until_max_retries_exceeded() {
        int maxRetries = 2;

        DelayedConnectionLevelRetryHandler handler = DelayedConnectionLevelRetryHandler.Builder
                .withFixedDelay(Duration.ofSeconds(2), maxRetries)
                .retryForExceptions(Arrays.asList(SSLException.class, ConnectException.class))
                .withSleeper(mock(Sleeper.class))
                .build();

        SSLException sslException = new SSLException("ssl error");
        HttpClientContext ctx = new HttpClientContext();
        int lastExecutionCount = maxRetries + 1;
        for (int i = 1; i < lastExecutionCount; i++) {
            assertTrue(handler.retryRequest(sslException, i, ctx));
        }
        assertFalse(handler.retryRequest(sslException, lastExecutionCount, ctx));
    }

    @Test
    void does_not_retry_for_non_listed_exception() {
        DelayedConnectionLevelRetryHandler handler = DelayedConnectionLevelRetryHandler.Builder
                .withFixedDelay(Duration.ofSeconds(2), 2)
                .retryForExceptions(Arrays.asList(SSLException.class, ConnectException.class))
                .withSleeper(mock(Sleeper.class))
                .build();

        IOException ioException = new IOException();
        HttpClientContext ctx = new HttpClientContext();
        assertFalse(handler.retryRequest(ioException, 1, ctx));
    }

}