21
21
import com .github .tomakehurst .wiremock .junit .WireMockRule ;
22
22
import io .github .resilience4j .circuitbreaker .CircuitBreaker ;
23
23
import io .github .resilience4j .circuitbreaker .CircuitBreakerConfig ;
24
+ import io .github .resilience4j .circuitbreaker .CircuitBreakerOpenException ;
25
+ import okhttp3 .Dispatcher ;
24
26
import okhttp3 .OkHttpClient ;
25
27
import org .junit .Before ;
26
28
import org .junit .Rule ;
34
36
35
37
import static com .github .tomakehurst .wiremock .client .WireMock .*;
36
38
import static org .assertj .core .api .Assertions .assertThat ;
39
+ import static org .assertj .core .api .Assertions .fail ;
37
40
38
41
/**
39
42
* Tests the integration of the Retrofit HTTP client and {@link CircuitBreaker}
@@ -50,14 +53,16 @@ public class RetrofitCircuitBreakerTest {
50
53
51
54
private CircuitBreaker circuitBreaker = CircuitBreaker .of ("test" , circuitBreakerConfig );
52
55
56
+ private OkHttpClient client ;
57
+
53
58
private RetrofitService service ;
54
59
55
60
@ Before
56
61
public void setUp () {
57
62
this .circuitBreaker = CircuitBreaker .of ("test" , circuitBreakerConfig );
58
63
59
64
final long TIMEOUT = 300 ; // ms
60
- final OkHttpClient client = new OkHttpClient .Builder ()
65
+ this . client = new OkHttpClient .Builder ()
61
66
.connectTimeout (TIMEOUT , TimeUnit .MILLISECONDS )
62
67
.readTimeout (TIMEOUT , TimeUnit .MILLISECONDS )
63
68
.writeTimeout (TIMEOUT , TimeUnit .MILLISECONDS )
@@ -212,6 +217,27 @@ public void decorateUnsuccessfulEnqueuedCall() throws Throwable {
212
217
assertThat (metrics .getNumberOfFailedCalls ()).isEqualTo (1 );
213
218
}
214
219
220
+ @ Test
221
+ public void shouldNotCallServiceOnEnqueueWhenOpen () throws Throwable {
222
+ stubFor (get (urlPathEqualTo ("/greeting" ))
223
+ .willReturn (aResponse ()
224
+ .withStatus (200 )
225
+ .withHeader ("Content-Type" , "text/plain" )
226
+ .withBody ("hello world" )));
227
+
228
+ circuitBreaker .transitionToOpenState ();
229
+
230
+ try {
231
+ EnqueueDecorator .enqueue (service .greeting ());
232
+ fail ("CircuitBreakerOpenException was expected" );
233
+ } catch (CircuitBreakerOpenException ignore ) {
234
+
235
+ }
236
+
237
+ ensureAllRequestsAreExecuted (Duration .ofSeconds (1 ));
238
+ verify (0 , getRequestedFor (urlPathEqualTo ("/greeting" )));
239
+ }
240
+
215
241
@ Test (expected = IllegalArgumentException .class )
216
242
public void shouldThrowOnBadService () {
217
243
BadRetrofitService badService = new Retrofit .Builder ()
@@ -222,4 +248,16 @@ public void shouldThrowOnBadService() {
222
248
223
249
badService .greeting ();
224
250
}
251
+
252
+ private void ensureAllRequestsAreExecuted (Duration timeout ) throws InterruptedException {
253
+ long end = System .nanoTime () + timeout .toNanos ();
254
+ Dispatcher dispatcher = client .dispatcher ();
255
+ while (System .nanoTime () < end ) {
256
+ if (dispatcher .queuedCallsCount () <= 0 && dispatcher .runningCallsCount () <= 0 ) {
257
+ return ;
258
+ }
259
+ Thread .sleep (10 );
260
+ }
261
+ fail ("Timeout exceeded while waiting for requests to be finished" );
262
+ }
225
263
}
0 commit comments