Commit 337e9bd0 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent 55fadf12
...@@ -23,7 +23,8 @@ import java.lang.annotation.RetentionPolicy; ...@@ -23,7 +23,8 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.Collections; import java.util.Collections;
import java.util.Map; 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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
...@@ -58,7 +59,6 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -58,7 +59,6 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -81,12 +81,12 @@ public class EndpointMvcIntegrationTests { ...@@ -81,12 +81,12 @@ public class EndpointMvcIntegrationTests {
private TestInterceptor interceptor; private TestInterceptor interceptor;
@Test @Test
public void envEndpointNotHidden() { public void envEndpointNotHidden() throws InterruptedException {
String body = new TestRestTemplate().getForObject("http://localhost:" + this.port String body = new TestRestTemplate().getForObject("http://localhost:" + this.port
+ "/env/user.dir", String.class); + "/env/user.dir", String.class);
assertNotNull(body); assertNotNull(body);
assertTrue("Wrong body: \n" + body, body.contains("spring-boot-actuator")); assertTrue("Wrong body: \n" + body, body.contains("spring-boot-actuator"));
assertEquals(1, this.interceptor.getCount()); assertTrue(this.interceptor.invoked());
} }
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
...@@ -141,16 +141,16 @@ public class EndpointMvcIntegrationTests { ...@@ -141,16 +141,16 @@ public class EndpointMvcIntegrationTests {
protected static class TestInterceptor extends HandlerInterceptorAdapter { protected static class TestInterceptor extends HandlerInterceptorAdapter {
private final AtomicInteger count = new AtomicInteger(0); private final CountDownLatch latch = new CountDownLatch(1);
@Override @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception { Object handler, ModelAndView modelAndView) throws Exception {
this.count.incrementAndGet(); this.latch.countDown();
} }
public int getCount() { public boolean invoked() throws InterruptedException {
return this.count.get(); return this.latch.await(30, TimeUnit.SECONDS);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment