From 63958ac0ff19291407ece1c7d2f6e4e0862df526 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 22 Dec 2015 22:06:11 -0500 Subject: [PATCH] Fix concurrent test failure Since ListenableFuture callbacks are invoked after the future is set, we cannot rely on callbacks having taken place right after a call to future.get(). This change adds a CountdownLatch to detect when the callbacks were invoked. Issue: SPR-12538 --- .../AsyncRestTemplateIntegrationTests.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java b/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java index 86b436ba588..6fef1b39e08 100644 --- a/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java @@ -599,25 +599,20 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa template.setInterceptors(Collections.singletonList(interceptor)); ListenableFuture> future = template.getForEntity("/get", String.class); - ResponseEntity response = future.get(); + interceptor.latch.await(5, TimeUnit.SECONDS); assertNotNull(interceptor.response); assertEquals(HttpStatus.OK, interceptor.response.getStatusCode()); assertNull(interceptor.exception); - assertEquals(helloWorld, response.getBody()); + assertEquals(helloWorld, future.get().getBody()); } @Test public void getAndInterceptError() throws Exception { RequestInterceptor interceptor = new RequestInterceptor(); template.setInterceptors(Collections.singletonList(interceptor)); - ListenableFuture> future = template.getForEntity("/status/notfound", String.class); + template.getForEntity("/status/notfound", String.class); - try { - future.get(); - fail("No exception thrown"); - } catch (ExecutionException ex) { - // expected - } + interceptor.latch.await(5, TimeUnit.SECONDS); assertNotNull(interceptor.response); assertEquals(HttpStatus.NOT_FOUND, interceptor.response.getStatusCode()); assertNull(interceptor.exception); @@ -632,6 +627,8 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa private static class RequestInterceptor implements AsyncClientHttpRequestInterceptor { + private final CountDownLatch latch = new CountDownLatch(1); + private volatile ClientHttpResponse response; private volatile Throwable exception; @@ -654,7 +651,15 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa }; ListenableFuture future = execution.executeAsync(request, body); - future.addCallback(resp -> response = resp, ex -> exception = ex); + future.addCallback( + resp -> { + response = resp; + this.latch.countDown(); + }, + ex -> { + exception = ex; + this.latch.countDown(); + }); return future; } }