July 18, 2016
Category: Testing concurrent Java
@Path("counter") public class Counter { private static int i = 0; @POST @Produces(MediaType.TEXT_PLAIN) public String addOne() { return new Integer(i++).toString(); } }This is clearly not thread safe. The access to the variable counter is not synchronized, which will lead to a race condition, if the method "addOne" is called from too many threads in parallel. Let us see, if we can detect this bug with a test.
@RunWith(ConcurrentTestRunner.class) public class CounterTest { private HttpServer server; private WebTarget target; @Before public void setUp() throws Exception { server = Main.startServer(); Client c = ClientBuilder.newClient(); target = c.target(Main.BASE_URI); } @After public void tearDown() throws Exception { server.shutdown(); } @Test public void testAddOne() { String responseMsg = target.path("counter").request().post(Entity.json(null) , String.class); /* * * Checking the responseMsg left out for brevity... * */ } }The concurrent test runner runs the test method in parallel with THREAD_COUNT threads. To detect race conditions, we need a tool, which can detect race conditions during tests. One such tool is vmlens. We can enable it, by adding the vmlens agent path to the vm arguments. After running we will see the race condition in vmlens:
After we have found the race condition, we want to fix the race.
@Path("counter") public class Counter { private static AtomicInteger i = new AtomicInteger(); @POST @Produces(MediaType.TEXT_PLAIN) public String addOne() { return new Integer(i.incrementAndGet()).toString(); } }
© 2020 vmlens Legal Notice Privacy Policy