public static Client createRefreshingClient(Supplier<Client> baseClientSupplier) { return new CounterBackedRefreshingClient(baseClientSupplier, DEFAULT_REQUEST_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 { return delegate().execute(request, options); }
@Test public void passesRequestsThroughToTheDelegate() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenReturn( Response.create(204, "no content", ImmutableMap.of(), new byte[0])); Client refreshingClient = CounterBackedRefreshingClient.createRefreshingClient(clientSupplier); refreshingClient.execute(request, options); verify(client, times(1)).execute(request, options); verifyNoMoreInteractions(client); }
@Test public void reinvokesSupplierAfterTheSpecifiedNumberOfRequestsHaveBeenMade() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenReturn( Response.create(204, "no content", ImmutableMap.of(), new byte[0])); Client refreshingClient = new CounterBackedRefreshingClient(clientSupplier, 2); refreshingClient.execute(request, options); refreshingClient.execute(request, options); refreshingClient.execute(request, options); verify(clientSupplier, times(2)).get(); verify(client, times(3)).execute(request, options); verifyNoMoreInteractions(clientSupplier, client); }
@Override public Response execute(Request request, Request.Options options) throws IOException { return delegate().execute(request, options); }
@Test public void createsClientOnlyOnceIfMakingMultipleRequests() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenReturn( Response.create(204, "no content", ImmutableMap.of(), new byte[0])); Client refreshingClient = CounterBackedRefreshingClient.createRefreshingClient(clientSupplier); refreshingClient.execute(request, options); refreshingClient.execute(request, options); refreshingClient.execute(request, options); verify(clientSupplier).get(); verify(client, times(3)).execute(request, options); verifyNoMoreInteractions(clientSupplier, client); }
@Test public void requestsContinueWithOldClientIfDelegateSupplierThrows() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenReturn( Response.create(204, "no content", ImmutableMap.of(), new byte[0])); Client refreshingClient = new CounterBackedRefreshingClient(clientSupplier, 2); refreshingClient.execute(request, options); when(clientSupplier.get()).thenThrow(new IllegalStateException("bad")); refreshingClient.execute(request, options); refreshingClient.execute(request, options); // Creation failed, so we delegate to the old client still. verify(clientSupplier, times(2)).get(); verify(client, times(3)).execute(request, options); verifyNoMoreInteractions(clientSupplier, client); } }
/** * 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); }
public static Client createRefreshingClient(Supplier<Client> baseClientSupplier) { return new CounterBackedRefreshingClient(baseClientSupplier, DEFAULT_REQUEST_COUNT_BEFORE_REFRESH); }