Skip to content

Commit

Permalink
Unit tests for limiter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkozlowski committed Jul 19, 2021
1 parent 9b29a89 commit 72b43a7
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ public void onSuccess_dropsIfResponseIndicatesQosOrError_endpoint() {
assertThat(limiter.getLimit()).as("For status %d", code).isCloseTo(max * 0.9, Percentage.withPercentage(5));
}

@Test
public void onSuccess_releasesSuccessfullyIfResponseIndicatesQosOrError_sticky() {
CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter = limiter(Behavior.STICKY);
int code = 429;
Response response = mock(Response.class);
when(response.code()).thenReturn(code);

double max = limiter.getLimit();
limiter.acquire(LimitEnforcement.DEFAULT_ENABLED).get().onSuccess(response);
assertThat(limiter.getLimit()).isEqualTo(max);
}

@Test
public void onSuccess_ignoresIfResponseIndicatesUnknownServerError_endpoint() {
CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter = limiter(Behavior.ENDPOINT_LEVEL);
Expand All @@ -221,6 +233,18 @@ public void onSuccess_ignoresIfResponseIndicatesUnknownServerError_endpoint() {
assertThat(limiter.getLimit()).isEqualTo(max);
}

@Test
public void onSuccess_ignoresIfResponseIndicatesUnknownServerError_sticky() {
CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter = limiter(Behavior.STICKY);
int code = 599;
Response response = mock(Response.class);
when(response.code()).thenReturn(code);

double max = limiter.getLimit();
limiter.acquire(LimitEnforcement.DEFAULT_ENABLED).get().onSuccess(response);
assertThat(limiter.getLimit()).isEqualTo(max);
}

@Test
public void onSuccess_dropsIfResponseIndicatesUnknownServerError_host() {
CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter = limiter(Behavior.HOST_LEVEL);
Expand Down Expand Up @@ -253,6 +277,16 @@ public void onFailure_ignoresIfIoException_endpoint() {
assertThat(limiter.getLimit()).isEqualTo(max);
}

@Test
public void onFailure_ignoresIfIoException_sticky() {
CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter = limiter(Behavior.STICKY);
IOException exception = new IOException();

double max = limiter.getLimit();
limiter.acquire(LimitEnforcement.DEFAULT_ENABLED).get().onFailure(exception);
assertThat(limiter.getLimit()).isEqualTo(max);
}

@ParameterizedTest
@EnumSource(Behavior.class)
public void onFailure_ignoresForNonIoExceptions(Behavior behavior) {
Expand Down

0 comments on commit 72b43a7

Please sign in to comment.