/
ConcurrencyLimitingInterceptorTest.java
158 lines (140 loc) · 5.92 KB
/
ConcurrencyLimitingInterceptorTest.java
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
/*
* (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.conjure.java.okhttp;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import com.google.common.util.concurrent.Futures;
import com.netflix.concurrency.limits.Limiter;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public final class ConcurrencyLimitingInterceptorTest {
private static final ConcurrencyLimitingInterceptor interceptor = new ConcurrencyLimitingInterceptor();
@Mock
private BufferedSource mockSource;
@Mock
private Interceptor.Chain chain;
@Mock
private Limiter.Listener listener;
private Request request;
private Response response;
@BeforeEach
public void before() {
request = new Request.Builder()
.url("https://localhost:1234/call")
.tag(
ConcurrencyLimiterListener.class,
ConcurrencyLimiterListener.create().setLimiterListener(Futures.immediateFuture(listener)))
.get()
.build();
response = new Response.Builder()
.code(200)
.request(request)
.message("message")
.protocol(Protocol.HTTP_2)
.build();
when(chain.request()).thenReturn(request);
}
@Test
public void dropsIfRateLimited() throws IOException {
Response rateLimited = response.newBuilder().code(429).build();
when(chain.proceed(request)).thenReturn(rateLimited);
assertThat(interceptor.intercept(chain)).isEqualTo(rateLimited);
verify(listener).onDropped();
}
@Test
public void dropsIfUnavailable() throws IOException {
Response unavailable = response.newBuilder().code(503).build();
when(chain.proceed(request)).thenReturn(unavailable);
assertThat(interceptor.intercept(chain)).isEqualTo(unavailable);
verify(listener).onDropped();
}
@Test
public void ignoresIfRedirect() throws IOException {
Response redirect = response.newBuilder().code(308).build();
when(chain.proceed(request)).thenReturn(redirect);
assertThat(interceptor.intercept(chain)).isEqualTo(redirect);
verify(listener).onIgnore();
}
@Test
public void ignoresIfUnsuccessful() throws IOException {
Response unsuccessful = response.newBuilder().code(500).build();
when(chain.proceed(request)).thenReturn(unsuccessful);
assertThat(interceptor.intercept(chain)).isEqualTo(unsuccessful);
verify(listener).onIgnore();
}
@Test
public void ignoresIfIoException() throws IOException {
IOException exception = new IOException();
when(chain.proceed(request)).thenThrow(exception);
assertThatThrownBy(() -> interceptor.intercept(chain)).isEqualTo(exception);
verify(listener).onIgnore();
}
@Test
public void wrapsResponseBody() throws IOException {
String data = "data";
ResponseBody body = ResponseBody.create(MediaType.parse("application/json"), data);
when(chain.proceed(request)).thenReturn(response.newBuilder().body(body).build());
Response wrappedResponse = interceptor.intercept(chain);
verifyNoMoreInteractions(listener);
assertThat(wrappedResponse.body().string()).isEqualTo(data);
verify(listener).onSuccess();
}
@Test
public void proxyHandlesExceptions() throws IOException {
ResponseBody body = ResponseBody.create(MediaType.parse("application/json"), -1, mockSource);
when(chain.proceed(request)).thenReturn(response.newBuilder().body(body).build());
IOException exception = new IOException();
when(mockSource.readByteArray()).thenThrow(exception);
Response erroneousResponse = interceptor.intercept(chain);
assertThatThrownBy(() -> erroneousResponse.body().source().readByteArray())
.isEqualTo(exception);
}
@Test
public void ignoresIfNoContent() throws IOException {
Response noContent = response.newBuilder().code(204).build();
when(chain.proceed(request)).thenReturn(noContent);
assertThat(interceptor.intercept(chain)).isEqualTo(noContent);
verify(listener).onIgnore();
}
@Test
public void marksSuccessIfContentEmpty() throws IOException {
Response empty = response.newBuilder()
.code(204)
.body(ResponseBody.create(MediaType.parse("application/json"), new byte[0]))
.build();
when(chain.proceed(request)).thenReturn(empty);
assertThat(interceptor.intercept(chain)).isEqualTo(empty);
verify(listener).onSuccess();
}
}