From 5854ea189ea6513fef6bc6593d8535642fe412b6 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 25 Nov 2014 10:52:34 +0000 Subject: [PATCH] Make TestInterceptor thread-safe Previously, TestInterceptor used an int to keep a count of how often it had been called. The count was incremented on one thread and read on another thread. This lead to intermittent test failures as the field was not declared volatile and a stale value would sometimes be returned. This commit updates TestInterceptor to use an AtomicInteger that's held in a final field. This ensures that getCount() will not return stale values and also ensures that the count can safely be incremented concurrently. Closes gh-1997 --- .../actuate/autoconfigure/EndpointMvcIntegrationTests.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 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 cc3f4a82cbd..5f77587c50b 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,6 +23,7 @@ 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 javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -140,16 +141,16 @@ public class EndpointMvcIntegrationTests { protected static class TestInterceptor extends HandlerInterceptorAdapter { - private int count = 0; + private final AtomicInteger count = new AtomicInteger(0); @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { - this.count++; + this.count.incrementAndGet(); } public int getCount() { - return this.count; + return this.count.get(); } }