public static Client createRefreshingClient(Supplier<Client> baseClientSupplier) { return new ExceptionCountingRefreshingClient(baseClientSupplier, DEFAULT_EXCEPTION_COUNT_BEFORE_REFRESH); }
/** * Returns a Feign {@link Client} wrapping an {@link okhttp3.OkHttpClient}. This {@link Client} recreates * itself in the event that either {@link CounterBackedRefreshingClient#DEFAULT_REQUEST_COUNT_BEFORE_REFRESH} * requests have been made, or if {@link ExceptionCountingRefreshingClient#DEFAULT_EXCEPTION_COUNT_BEFORE_REFRESH} * consecutive exceptions have been thrown by the underlying client. */ public static Client newRefreshingOkHttpClient( Optional<TrustContext> trustContext, Optional<ProxySelector> proxySelector, String userAgent) { Supplier<Client> clientSupplier = () -> CounterBackedRefreshingClient.createRefreshingClient( () -> newOkHttpClient(trustContext, proxySelector, userAgent)); return ExceptionCountingRefreshingClient.createRefreshingClient(clientSupplier); }
@Override public Response execute(Request request, Request.Options options) throws IOException { try { Response response = delegate().execute(request, options); counter.set(0); return response; } catch (Exception e) { counter.incrementAndGet(); throw e; } }
@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 createsClientOnlyOnceIfMakingMultipleRequests() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenReturn(response); Client refreshingClient = ExceptionCountingRefreshingClient.createRefreshingClient(clientSupplier); callExecute(refreshingClient, 3); verify(clientSupplier, times(1)).get(); verify(client, times(3)).execute(request, options); verifyNoMoreInteractions(clientSupplier, client); }
@Override public Response execute(Request request, Request.Options options) throws IOException { try { Response response = delegate().execute(request, options); counter.set(0); return response; } catch (Exception e) { counter.incrementAndGet(); throw e; } }
@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 passesRequestsThroughToTheDelegate() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenReturn(response); Client refreshingClient = ExceptionCountingRefreshingClient.createRefreshingClient(clientSupplier); callExecute(refreshingClient, 1); verify(client, times(1)).execute(request, options); verifyNoMoreInteractions(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); }
/** * Returns a Feign {@link Client} wrapping an {@link okhttp3.OkHttpClient}. This {@link Client} recreates * itself in the event that either {@link CounterBackedRefreshingClient#DEFAULT_REQUEST_COUNT_BEFORE_REFRESH} * requests have been made, or if {@link ExceptionCountingRefreshingClient#DEFAULT_EXCEPTION_COUNT_BEFORE_REFRESH} * consecutive exceptions have been thrown by the underlying client. */ public static Client newRefreshingOkHttpClient( Optional<TrustContext> trustContext, Optional<ProxySelector> proxySelector, String userAgent) { Supplier<Client> clientSupplier = () -> CounterBackedRefreshingClient.createRefreshingClient( () -> newOkHttpClient(trustContext, proxySelector, userAgent)); return ExceptionCountingRefreshingClient.createRefreshingClient(clientSupplier); }
@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); }