public static Client createRefreshingClient(Supplier<Client> baseClientSupplier) { return new ExceptionCountingRefreshingClient(baseClientSupplier, DEFAULT_EXCEPTION_COUNT_BEFORE_REFRESH); }
@Test public void reinvokesSupplierAfterTheSpecifiedNumberOfExceptionsThrown() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenThrow(CLIENT_EXCEPTION); Client refreshingClient = new ExceptionCountingRefreshingClient(clientSupplier, EXCEPTION_COUNT_BEFORE_REFRESH); int numberOfExecutions = EXCEPTION_COUNT_BEFORE_REFRESH + 1; callExecute(refreshingClient, numberOfExecutions); verify(clientSupplier, times(numberOfExecutions / EXCEPTION_COUNT_BEFORE_REFRESH + 1)).get(); verify(client, times(numberOfExecutions)).execute(request, options); verifyNoMoreInteractions(clientSupplier, client); }
@Test public void requestsContinueWithOldClientIfDelegateSupplierThrows() throws IOException { when(clientSupplier.get()).thenReturn(client).thenThrow(new IllegalStateException("bad")); when(client.execute(request, options)).thenThrow(CLIENT_EXCEPTION); Client refreshingClient = new ExceptionCountingRefreshingClient(clientSupplier, EXCEPTION_COUNT_BEFORE_REFRESH); int numberOfExecutions = EXCEPTION_COUNT_BEFORE_REFRESH + 1; callExecute(refreshingClient, numberOfExecutions); verify(clientSupplier, times(numberOfExecutions / EXCEPTION_COUNT_BEFORE_REFRESH + 1)).get(); verify(client, times(numberOfExecutions)).execute(request, options); verifyNoMoreInteractions(clientSupplier, client); }
@Test public void doesNotRefreshClientIfExceptionsAreNotThrownInARow() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)) .thenThrow(CLIENT_EXCEPTION) .thenReturn(response) .thenThrow(CLIENT_EXCEPTION) .thenReturn(response); Client refreshingClient = new ExceptionCountingRefreshingClient(clientSupplier, EXCEPTION_COUNT_BEFORE_REFRESH); int numberOfExecutions = 4; callExecute(refreshingClient, numberOfExecutions); verify(clientSupplier, times(1)).get(); verify(client, times(numberOfExecutions)).execute(request, options); }
@Test public void doesRefreshClientIfExceptionsAreThrownInARow() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)) .thenReturn(response) .thenThrow(CLIENT_EXCEPTION) .thenThrow(CLIENT_EXCEPTION) .thenReturn(response); Client refreshingClient = new ExceptionCountingRefreshingClient(clientSupplier, EXCEPTION_COUNT_BEFORE_REFRESH); int numberOfExecutions = 4; callExecute(refreshingClient, numberOfExecutions); verify(clientSupplier, times(2)).get(); verify(client, times(numberOfExecutions)).execute(request, options); }
public static Client createRefreshingClient(Supplier<Client> baseClientSupplier) { return new ExceptionCountingRefreshingClient(baseClientSupplier, DEFAULT_EXCEPTION_COUNT_BEFORE_REFRESH); }