Skip to content

Commit

Permalink
Merge 39c46ec into 35dd477
Browse files Browse the repository at this point in the history
  • Loading branch information
szmg committed Jul 3, 2018
2 parents 35dd477 + 39c46ec commit 993ecba
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void enqueue(final Callback<T> callback) {
CircuitBreakerUtils.isCallPermitted(circuitBreaker);
} catch (CircuitBreakerOpenException cb) {
callback.onFailure(call, cb);
return;
}

final StopWatch stopWatch = StopWatch.start(circuitBreaker.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -34,6 +36,7 @@

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

/**
* Tests the integration of the Retrofit HTTP client and {@link CircuitBreaker}
Expand All @@ -50,14 +53,16 @@ public class RetrofitCircuitBreakerTest {

private CircuitBreaker circuitBreaker = CircuitBreaker.of("test", circuitBreakerConfig);

private OkHttpClient client;

private RetrofitService service;

@Before
public void setUp() {
this.circuitBreaker = CircuitBreaker.of("test", circuitBreakerConfig);

final long TIMEOUT = 300; // ms
final OkHttpClient client = new OkHttpClient.Builder()
this.client = new OkHttpClient.Builder()
.connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
.writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
Expand Down Expand Up @@ -212,6 +217,27 @@ public void decorateUnsuccessfulEnqueuedCall() throws Throwable {
assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}

@Test
public void shouldNotCallServiceOnEnqueueWhenOpen() throws Throwable {
stubFor(get(urlPathEqualTo("/greeting"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/plain")
.withBody("hello world")));

circuitBreaker.transitionToOpenState();

try {
EnqueueDecorator.enqueue(service.greeting());
fail("CircuitBreakerOpenException was expected");
} catch (CircuitBreakerOpenException ignore) {

}

ensureAllRequestsAreExecuted(Duration.ofSeconds(1));
verify(0, getRequestedFor(urlPathEqualTo("/greeting")));
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowOnBadService() {
BadRetrofitService badService = new Retrofit.Builder()
Expand All @@ -222,4 +248,16 @@ public void shouldThrowOnBadService() {

badService.greeting();
}

private void ensureAllRequestsAreExecuted(Duration timeout) throws InterruptedException {
long end = System.nanoTime() + timeout.toNanos();
Dispatcher dispatcher = client.dispatcher();
while (System.nanoTime() < end) {
if (dispatcher.queuedCallsCount() <= 0 && dispatcher.runningCallsCount() <= 0) {
return;
}
Thread.sleep(10);
}
fail("Timeout exceeded while waiting for requests to be finished");
}
}

0 comments on commit 993ecba

Please sign in to comment.