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 package org.apache.hc.client5.http.impl.classic;
28
29
30 import static org.junit.jupiter.api.Assertions.assertFalse;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32
33 import java.net.ConnectException;
34 import java.net.SocketTimeoutException;
35
36 import org.apache.hc.core5.http.ConnectionRequestTimeoutException;
37 import org.apache.hc.core5.http.HttpResponse;
38 import org.apache.hc.core5.http.HttpStatus;
39 import org.apache.hc.core5.http.message.BasicHttpResponse;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42
43
44 class TestDefaultBackoffStrategy {
45
46 private DefaultBackoffStrategy impl;
47
48 @BeforeEach
49 void setUp() {
50 impl = new DefaultBackoffStrategy();
51 }
52
53 @Test
54 void backsOffForSocketTimeouts() {
55 assertTrue(impl.shouldBackoff(new SocketTimeoutException()));
56 }
57
58 @Test
59 void backsOffForConnectionTimeouts() {
60 assertTrue(impl.shouldBackoff(new ConnectException()));
61 }
62
63 @Test
64 void doesNotBackOffForConnectionManagerTimeout() {
65 assertFalse(impl.shouldBackoff(new ConnectionRequestTimeoutException()));
66 }
67
68 @Test
69 void backsOffForServiceUnavailable() {
70 final HttpResponse resp = new BasicHttpResponse(HttpStatus.SC_SERVICE_UNAVAILABLE, "Service Unavailable");
71 assertTrue(impl.shouldBackoff(resp));
72 }
73
74 @Test
75 void backsOffForTooManyRequests() {
76 final HttpResponse resp = new BasicHttpResponse(HttpStatus.SC_TOO_MANY_REQUESTS, "Too Many Requests");
77 assertTrue(impl.shouldBackoff(resp));
78 }
79
80 @Test
81 void doesNotBackOffForNon429And503StatusCodes() {
82 for(int i = 100; i <= 599; i++) {
83 if (i== HttpStatus.SC_TOO_MANY_REQUESTS || i == HttpStatus.SC_SERVICE_UNAVAILABLE) {
84 continue;
85 }
86 final HttpResponse resp = new BasicHttpResponse(i, "Foo");
87 assertFalse(impl.shouldBackoff(resp));
88 }
89 }
90 }