From 337e9bd013b48b8665052cac8a3d30bbafc89dae Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 25 Nov 2014 14:19:07 +0000 Subject: [PATCH] Use and wait for a latch to check that the interceptor is called Spring MVC drives the postHandle method on any interceptors after the response has been sent to the client. This meant that there was a race between the test receiving the response and asserting that the interceptor had been driven and Spring MVC driving the interceptor. This commit updates the interceptor to use a CountDownLatch to track whether or not it's been called. The test now waits for up to 30 seconds for the latch to be decremented. Closes gh-1997 --- .../EndpointMvcIntegrationTests.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java index 5f77587c50b..ec42c628ae4 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java @@ -23,7 +23,8 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.Collections; import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -58,7 +59,6 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -81,12 +81,12 @@ public class EndpointMvcIntegrationTests { private TestInterceptor interceptor; @Test - public void envEndpointNotHidden() { + public void envEndpointNotHidden() throws InterruptedException { String body = new TestRestTemplate().getForObject("http://localhost:" + this.port + "/env/user.dir", String.class); assertNotNull(body); assertTrue("Wrong body: \n" + body, body.contains("spring-boot-actuator")); - assertEquals(1, this.interceptor.getCount()); + assertTrue(this.interceptor.invoked()); } @Target(ElementType.TYPE) @@ -141,16 +141,16 @@ public class EndpointMvcIntegrationTests { protected static class TestInterceptor extends HandlerInterceptorAdapter { - private final AtomicInteger count = new AtomicInteger(0); + private final CountDownLatch latch = new CountDownLatch(1); @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { - this.count.incrementAndGet(); + this.latch.countDown(); } - public int getCount() { - return this.count.get(); + public boolean invoked() throws InterruptedException { + return this.latch.await(30, TimeUnit.SECONDS); } }