private void warmUp(ServerSocket serverSocket, DefaultAsyncHttpClient client) { try { Future<?> future = client .prepareGet(StringUtils.format("http://localhost:%d/", serverSocket.getLocalPort())) .setRequestTimeout(100) .execute(); future.get(); } catch (Exception e) { // ignore } } }
@Test public void testRequestTimeout() throws IOException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> responseFuture = client.prepareGet(getTargetUrl()).setRequestTimeout(100).execute(); Response response = responseFuture.get(2000, TimeUnit.MILLISECONDS); assertNull(response); } catch (InterruptedException e) { fail("Interrupted.", e); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof TimeoutException); checkTimeoutMessage(e.getCause().getMessage(), true); } catch (TimeoutException e) { fail("Timeout.", e); } }
@Test public void testGlobalDefaultPerRequestInfiniteTimeout() throws IOException { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100))) { Future<Response> responseFuture = client.prepareGet(getTargetUrl()).setRequestTimeout(-1).execute(); Response response = responseFuture.get(); assertNotNull(response); } catch (InterruptedException e) { fail("Interrupted.", e); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof TimeoutException); checkTimeoutMessage(e.getCause().getMessage(), true); } }
Future<Response> responseFuture = null; try { responseFuture = client.prepareGet(getTargetUrl()).setRequestTimeout(SLEEPTIME_MS / 2) .execute(new AsyncCompletionHandler<Response>() {
Future<?> future = client .prepareGet(StringUtils.format("http://localhost:%d/", serverSocket.getLocalPort())) .setRequestTimeout(2000) .execute(); System.out.println("created future in: " + (System.currentTimeMillis() - requestStart));
final Response response = asyncHttpClient .prepareGet(publicKeyUrl) .setRequestTimeout(5000) .execute() .get();
private void warmUp(ServerSocket serverSocket, DefaultAsyncHttpClient client) { try { Future<?> future = client .prepareGet(String.format("http://localhost:%d/", serverSocket.getLocalPort())) .setRequestTimeout(100) .execute(); future.get(); } catch (Exception e) { // ignore } } }
@Test public void shoulNotCallRepositoryWhenServiceResponsesWithStatusOtherThan200() throws ExecutionException, InterruptedException { // given when(asyncHttpClient.prepareGet(publicKeyUrl)).thenReturn(boundRequestBuilder); when(boundRequestBuilder.setRequestTimeout(anyInt())).thenReturn(boundRequestBuilder); when(boundRequestBuilder.execute()).thenReturn(future); when(future.get()).thenReturn(response); when(response.getStatusCode()).thenReturn(500); keyStore.retrieveApiOauthPublicKey(); //then verifyZeroInteractions(oAuthPublicKeyRepository); }
@Test public void shoulNotCallRepositoryWithEmptyKeyList() throws ExecutionException, InterruptedException { // given final ZonedDateTime now = ZonedDateTime.now(); final ZonedDateTime oneDayAgo = now.minusDays(1); final ZonedDateTime twoDaysAgo = now.minusDays(2); when(asyncHttpClient.prepareGet(publicKeyUrl)).thenReturn(boundRequestBuilder); when(boundRequestBuilder.setRequestTimeout(anyInt())).thenReturn(boundRequestBuilder); when(boundRequestBuilder.execute()).thenReturn(future); when(future.get()).thenReturn(response); when(response.getStatusCode()).thenReturn(200); when(response.getResponseBody()).thenReturn("[\n" + "{\n" + "\"publicKey\": \"publicKeyTwo\",\n" + "\"publicKeyFingerprint\": \"fingerPrintTwo\",\n" + "\"validFrom\": \"" + twoDaysAgo.toString() + "\",\n" + "\"validUntil\": \"" + oneDayAgo.toString() + "\"\n" + "}\n" + "]"); keyStore.retrieveApiOauthPublicKey(); //then verifyZeroInteractions(oAuthPublicKeyRepository); }
Future<?> future = client .prepareGet(String.format("http://localhost:%d/", serverSocket.getLocalPort())) .setRequestTimeout(2000) .execute(); System.out.println("created future in: " + (System.currentTimeMillis() - requestStart));
when(boundRequestBuilder.setRequestTimeout(anyInt())).thenReturn(boundRequestBuilder); when(boundRequestBuilder.execute()).thenReturn(future); when(future.get()).thenReturn(response);
when(boundRequestBuilder.setRequestTimeout(anyInt())).thenReturn(boundRequestBuilder); when(boundRequestBuilder.execute()).thenReturn(future); when(future.get()).thenReturn(response);