BmcGenericRetrier { public <REQUEST, RESPONSE> RESPONSE execute( @NonNull final REQUEST requestToUse, @NonNull final Function<REQUEST, RESPONSE> functionCall) { MutableObject<BmcException> lastKnownException = new MutableObject<>(); final Optional<RESPONSE> response = waiter.execute( Suppliers.ofInstance(requestToUse), (request) -> { if (lastKnownException.getValue() != null) { LOG.debug( "Retrying: {}", lastKnownException.getValue().getMessage()); } try { return doFunctionCall(request, functionCall); } catch (BmcException e) { if (!retryCondition.shouldBeRetried(e)) { LOG.debug("Not retrying, not retriable: {}", e.getMessage()); throw e; } lastKnownException.setValue(e); } return null; }, Objects::nonNull); if (response.isPresent()) { return response.get(); } throw lastKnownException.getValue(); } BmcGenericRetrier(@NonNull final RetryConfiguration retryConfiguration); RESPONSE execute(
            @NonNull final REQUEST requestToUse,
            @NonNull final Function<REQUEST, RESPONSE> functionCall);  }
@Test public void defaultRetrierFailure() { final Supplier<String> request = setupMockRequest(500, "InternalServerError"); final BmcGenericRetrier defaultRetrier = new BmcGenericRetrier(RetryConfiguration.NO_RETRY_CONFIGURATION); try { defaultRetrier.execute(request, Supplier::get); fail("Should have thrown"); } catch (BmcException e) { assertEquals(500, e.getStatusCode()); } verify(request, times(1)).get(); }
@Test public void defaultRetrierSuccess() { final Supplier<String> request = mock(Supplier.class); doReturn("success").when(request).get(); final BmcGenericRetrier defaultRetrier = new BmcGenericRetrier(RetryConfiguration.NO_RETRY_CONFIGURATION); defaultRetrier.execute(request, Supplier::get); verify(request, times(1)).get(); }
@Test public void retryForInternalServerError() { final Supplier<String> request = setupMockRequest(500, "InternalServerError"); final BmcGenericRetrier retrier = new BmcGenericRetrier(CUSTOM_RETRY_CONFIGURATION); retrier.execute(request, Supplier::get); verify(request, times(2)).get(); }
@Test public void noRetryForInvalidParameter() { final Supplier<String> request = setupMockRequest(400, "InvalidParameter"); final BmcGenericRetrier retrier = new BmcGenericRetrier(CUSTOM_RETRY_CONFIGURATION); try { retrier.execute(request, Supplier::get); fail("Should have thrown"); } catch (BmcException e) { assertEquals(400, e.getStatusCode()); } verify(request, times(1)).get(); }
@Test public void retryForRelatedResourceNotAuthorizedOrNotFound() { final Supplier<String> request = setupMockRequest(400, "RelatedResourceNotAuthorizedOrNotFound"); final BmcGenericRetrier retrier = new BmcGenericRetrier(CUSTOM_RETRY_CONFIGURATION); retrier.execute(request, Supplier::get); verify(request, times(2)).get(); }