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
This commit is contained in:
Andy Wilkinson 2014-11-25 10:52:34 +00:00
parent 023d5bea3b
commit 5854ea189e
1 changed files with 4 additions and 3 deletions

View File

@ -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();
}
}