Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to allow retry on IO & WebClientRequest exceptions. #358

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -9,10 +9,12 @@
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.io.IOException;
import java.time.Duration;

@Service
@@ -47,7 +49,7 @@ public <T> T get(String url, Class<T> clazz) {
// only does retry if initial error was 5xx as service may be temporarily down
// 4xx errors will always happen if 404, 401, 403 etc, so does not retry
.retryWhen(Retry.backoff(3, Duration.ofSeconds(2))
.filter(ServiceException.class::isInstance)
.filter(ex -> ex instanceof ServiceException || ex instanceof IOException || ex instanceof WebClientRequestException)
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> {
throw new ServiceException(getErrorMessage(url, SERVICE_FAILED_ERROR), HttpStatus.SERVICE_UNAVAILABLE.value());
}))
@@ -74,7 +76,7 @@ public <T> T post(String url, Object body, Class<T> clazz) {
clientResponse -> Mono.error(new ServiceException(getErrorMessage(url, ERROR_5xx), clientResponse.statusCode().value())))
.bodyToMono(clazz)
.retryWhen(Retry.backoff(3, Duration.ofSeconds(2))
.filter(ServiceException.class::isInstance)
.filter(ex -> ex instanceof ServiceException || ex instanceof IOException || ex instanceof WebClientRequestException)
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> {
throw new ServiceException(getErrorMessage(url, SERVICE_FAILED_ERROR), HttpStatus.SERVICE_UNAVAILABLE.value());
}))
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package ca.bc.gov.educ.api.course.service;

import ca.bc.gov.educ.api.course.exception.ServiceException;
import io.netty.channel.ConnectTimeoutException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;
@@ -106,4 +110,24 @@ public void testGetOverride_Given4xxErrorFromService_ExpectServiceError(){
this.restService.get(TEST_URL_403, String.class);
}

@Test(expected = ServiceException.class)
public void testGet_Given5xxErrorFromService_ExpectConnectionError(){
when(requestBodyUriMock.uri(TEST_URL_503)).thenReturn(requestBodyMock);
when(requestBodyMock.retrieve()).thenReturn(responseMock);

Throwable cause = new RuntimeException("Simulated cause");
when(responseMock.bodyToMono(String.class)).thenReturn(Mono.error(new ConnectTimeoutException("Connection closed")));
restService.get(TEST_URL_503, String.class);
}

@Test(expected = ServiceException.class)
public void testGet_Given5xxErrorFromService_ExpectWebClientRequestError(){
when(requestBodyUriMock.uri(TEST_URL_503)).thenReturn(requestBodyMock);
when(requestBodyMock.retrieve()).thenReturn(responseMock);

Throwable cause = new RuntimeException("Simulated cause");
when(responseMock.bodyToMono(String.class)).thenReturn(Mono.error(new WebClientRequestException(cause, HttpMethod.GET, null, new HttpHeaders())));
restService.get(TEST_URL_503, String.class);
}

}
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import ca.bc.gov.educ.api.course.exception.ServiceException;
import ca.bc.gov.educ.api.course.util.ThreadLocalStateUtil;
import io.netty.channel.ConnectTimeoutException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -12,13 +13,16 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;
@@ -94,14 +98,39 @@ public void testPostOverride_GivenProperData_Expect200Response(){

@Test(expected = ServiceException.class)
public void testPost_Given4xxErrorFromService_ExpectServiceError() {
ThreadLocalStateUtil.setCorrelationID("test-correlation-id");
ThreadLocalStateUtil.setCurrentUser("test-user");
when(this.responseMock.onStatus(any(), any())).thenThrow(new ServiceException());
this.restService.post(TEST_URL, TEST_BODY, byte[].class);
}

@Test(expected = ServiceException.class)
public void testPostOverride_Given4xxErrorFromService_ExpectServiceError() {
ThreadLocalStateUtil.setCorrelationID("test-correlation-id");
ThreadLocalStateUtil.setCurrentUser("test-user");
when(this.responseMock.onStatus(any(), any())).thenThrow(new ServiceException());
this.restService.post(TEST_URL, TEST_BODY, byte[].class);
}

@Test(expected = ServiceException.class)
public void testPost_Given5xxErrorFromService_ExpectConnectionError(){
when(requestBodyUriMock.uri(TEST_URL)).thenReturn(requestBodyMock);
when(requestBodyMock.retrieve()).thenReturn(responseMock);

when(responseMock.bodyToMono(byte[].class)).thenReturn(Mono.error(new ConnectTimeoutException("Connection closed")));
this.restService.post(TEST_URL, TEST_BODY, byte[].class);
}

@Test(expected = ServiceException.class)
public void testPost_Given5xxErrorFromService_ExpectWebClientRequestError(){
ThreadLocalStateUtil.setCorrelationID("test-correlation-id");
ThreadLocalStateUtil.setCurrentUser("test-user");
when(requestBodyUriMock.uri(TEST_URL)).thenReturn(requestBodyMock);
when(requestBodyMock.retrieve()).thenReturn(responseMock);

Throwable cause = new RuntimeException("Simulated cause");
when(responseMock.bodyToMono(byte[].class)).thenReturn(Mono.error(new WebClientRequestException(cause, HttpMethod.POST, null, new HttpHeaders())));
this.restService.post(TEST_URL, TEST_BODY, byte[].class);
}

}