DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers
  • Top 7 Mistakes When Testing JavaFX Applications

Trending

  • Part 1: Building Governed MCP Tool Services With Quarkus LangChain4j and Goose
  • OpenCode: Setting Up an Open-Source AI Coding Assistant
  • To Switch, or Not to Switch the AI Model
  • How You Clear Your HTML5 Canvas Matters

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. How to Test if Your Multi-Threaded Java Rest Service Is Thread Safe

How to Test if Your Multi-Threaded Java Rest Service Is Thread Safe

Thomas Krieger shows how to test if your rest service is thread safe by analyzing a counter with a race condition and using a multi-threaded test runner.

By 
Thomas Krieger
·
Jul. 19, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
46.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In the following article, you will see how to test if your rest service is thread safe. Let us start with a simple example—a counter which contains a race condition.

The Not-Thread Safe Counter

As an example, we use the following jersey rest service. It consists of a resource that increments a counter for each post call and returns the new value:

@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.

The Test

To test if this service is thread safe, we need a multi-threaded test, like the following:

@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:

Race Condition in REST Service

After we have found the race condition, we want to fix the race.

Making the Rest Service Thread Safe

The easiest way to do this is to use a java.util.concurrent.atomic.AtomicInteger. AtomicInteger uses a volatile field internally, making updates visible to all threads. And the used method “addAndGet” is made atomic by using compareAndSet.

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

Conclusion

To test a multi threaded java rest service, we needed two things—a multi-threaded test runner and a tool that can detect Java race conditions. For the multi-threaded test I used concurrent-junit, for the race condition detection I used vmlens. If you have a question or remark, please add a comment in the comments section.

Testing Java (programming language)

Published at DZone with permission of Thomas Krieger. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers
  • Top 7 Mistakes When Testing JavaFX Applications

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook