Skip to content

Commit

Permalink
#504 Status 500 is now really considered as a server error (#509)
Browse files Browse the repository at this point in the history
We recently noticed that the DatafeedLoop was crashing with `500` errors returned from Agent. This is something we initially stated that we did not wanted to retry on this specific status. Now we do, as it has actually been raised by some customers.
  • Loading branch information
thibauult authored Apr 30, 2021
1 parent d8ae546 commit 67eab48
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void testSupplierWithExceptionAndNoRetryShouldFailWithException() throws Throwab
ofMinimalInterval(), supplier,
(t) -> false, Collections.emptyList());

assertThrows(ApiException.class, () -> r.execute());
assertThrows(ApiException.class, r::execute);
verify(supplier, times(1)).get();
}

Expand All @@ -108,7 +108,7 @@ void testMaxAttemptsReachedShouldFailWithException() throws ApiException {
retryConfig, supplier, (t) -> true,
Collections.emptyList());

assertThrows(ApiException.class, () -> r.execute());
assertThrows(ApiException.class, r::execute);
verify(supplier, times(retryConfig.getMaxAttempts())).get();
}

Expand All @@ -122,7 +122,7 @@ void testExceptionNotMatchingRetryPredicateShouldBeForwarded() throws ApiExcepti
(t) -> t instanceof ApiException && ((ApiException) t).isServerError(),
Collections.emptyList());

assertThrows(ApiException.class, () -> r.execute());
assertThrows(ApiException.class, r::execute);
verify(supplier, times(1)).get();
}

Expand Down Expand Up @@ -166,13 +166,13 @@ void testNonMatchingExceptionShouldNotTriggerRecoveryAndRetry() throws Throwable
final String value = "string";

SupplierWithApiException<String> supplier = mock(ConcreteSupplier.class);
when(supplier.get()).thenThrow(new ApiException(500, "error")).thenReturn(value);
when(supplier.get()).thenThrow(new ApiException(400, "error")).thenReturn(value);

ConcreteConsumer consumer = mock(ConcreteConsumer.class);

Resilience4jRetryWithRecovery<String> r = new Resilience4jRetryWithRecovery<>("name", "localhost.symphony.com",
ofMinimalInterval(), supplier, (t) -> true,
Collections.singletonList(new RecoveryStrategy(ApiException.class, e -> e.isClientError(), consumer)));
Collections.singletonList(new RecoveryStrategy(ApiException.class, ApiException::isServerError, consumer)));

assertEquals(value, r.execute());
verify(supplier, times(2)).get();
Expand All @@ -195,7 +195,7 @@ void testThrowableInRecoveryAndNotMatchingRetryPredicateShouldBeForwarded() thro
(t) -> t instanceof ApiException,
Collections.singletonList(new RecoveryStrategy(ApiException.class, ApiException::isClientError, consumer)));

assertThrows(IndexOutOfBoundsException.class, () -> r.execute());
assertThrows(IndexOutOfBoundsException.class, r::execute);

InOrder inOrder = inOrder(supplier, consumer);
inOrder.verify(supplier).get();
Expand Down Expand Up @@ -241,7 +241,7 @@ void testExecuteAndRetrySucceeds() throws Throwable {
@Test
void testExecuteAndRetryShouldConvertApiExceptionIntoApiRuntimeException() throws Throwable {
SupplierWithApiException<String> supplier = mock(ConcreteSupplier.class);
when(supplier.get()).thenThrow(new ApiException(500, ""));
when(supplier.get()).thenThrow(new ApiException(400, ""));

assertThrows(ApiRuntimeException.class,
() -> Resilience4jRetryWithRecovery.executeAndRetry(new RetryWithRecoveryBuilder<String>(), "test", "serviceName", supplier));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void startTestRecreateDatafeedError() throws ApiException {
when(datafeedApi.v4DatafeedIdReadGet("persisted-id", "1234", "1234", null))
.thenThrow(new ApiException(400, "expired DF id"));
when(datafeedApi.v4DatafeedCreatePost("1234", "1234"))
.thenThrow(new ApiException(500, "unhandled exception"));
.thenThrow(new ApiException(404, "unhandled exception"));

assertThrows(NestedRetryException.class, () -> this.datafeedService.start());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ void testStartInternalServerErrorReadDatafeedShouldNotBeRetried() throws ApiExce
AckId ackId = datafeedService.getAckId();
when(datafeedApi.listDatafeed("1234", "1234", "tibot")).thenReturn(
Collections.singletonList(new V5Datafeed().id("test-id")));
when(datafeedApi.readDatafeed("test-id", "1234", "1234", ackId)).thenThrow(new ApiException(500, "client-error"));
when(datafeedApi.readDatafeed("test-id", "1234", "1234", ackId)).thenThrow(new ApiException(404, "client-error"));

assertThrows(ApiException.class, this.datafeedService::start);
verify(datafeedApi, times(1)).listDatafeed("1234", "1234", "tibot");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public boolean isClientError() {
* @return true if response status strictly greater than 500, false otherwise
*/
public boolean isServerError() {
return this.code > HttpURLConnection.HTTP_INTERNAL_ERROR;
return this.code >= HttpURLConnection.HTTP_INTERNAL_ERROR;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.symphony.bdk.http.api;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class ApiExceptionTest {

@Test
void isServerError() {
assertFalse(new ApiException(499, "An error").isServerError());
assertTrue(new ApiException(500, "Internal Server Error").isServerError());
assertTrue(new ApiException(502, "Bad Gateway").isServerError());
assertTrue(new ApiException(503, "Service Unavailable").isServerError());
}

@Test
void isUnauthorized() {
assertTrue(new ApiException(401, "Unauthorized").isUnauthorized());
}

@Test
void isClientError() {
assertTrue(new ApiException(400, "Bad Request").isClientError());
}

@Test
void isTooManyRequestsError() {
assertTrue(new ApiException(429, "Too Many Requests").isTooManyRequestsError());
}
}

0 comments on commit 67eab48

Please sign in to comment.