1

For a given method I want to write test cased to see how it behaves when multiple thread running simultaneously. Might sound foolish but just wondering if it is possible to create any such scenario ?

2
  • Maybe not a real answer to your question but a general note: When using multiple threads your code will not be deterministic. It can happen that your test runs fine 1000 times but fails during the 1001th execution without any code change.
    – micha
    Sep 2, 2013 at 21:31
  • Agreed, that's one big factor but how to check for the thread safety if all I am writing is supposed to run in a multi-threaded environment? It is very critical for me Sep 2, 2013 at 21:34

3 Answers 3

2

This is how I implemented this in some code recently:

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.junit.Assert.*;
public class UtilTest
{
    private final static int TEST_THREAD_COUNT = 10;
    private volatile int threadsRun = 0;
    private volatile List<String> nonUniqueIds = new ArrayList<>();

    @Test
    public void testUniqueIdGeneration()
    {
        final ExecutorService pool = Executors.newFixedThreadPool(TEST_THREAD_COUNT);
        Runnable r = new Runnable()
        {
            @Override
            public void run()
            {
                List<String> doneIds = new ArrayList<>();
                for (int i = 1; i <= 100; i++)
                {
                    String id = Util.generateId();
                    if (doneIds.contains(id))
                    {
                        nonUniqueIds.add(id);
                        break;
                    }
                    doneIds.add(id);
                }
                threadsRun++;
                if (threadsRun >= TEST_THREAD_COUNT)
                    pool.shutdown();
            }
        };

        for (int i = 0; i < TEST_THREAD_COUNT; i++)
        {
            pool.submit(r);
        }
        while (! pool.isShutdown())
        {
            //stay alive
        }

        if (nonUniqueIds.isEmpty() )
            return;

        for (String id: nonUniqueIds)
        {
            System.out.println("Non unique id: " + id);
        }
        assertTrue("Non unique ids found", false);
    }
}
1
  • Aren't there third party libraries that can abstract the concurrent execution? I have come across contiPerf but it isn't active now. Let me know if you know of any other library that can call a test cases from multiple threads and abstract this through annotation like contiPerf did. Thanks ! Oct 31, 2016 at 13:50
1

You can actually just start your thread in the test case, but I am pretty sure that it won't do what you want.Execution is non-deterministic.

There seem to be some concurrency testing frameworks that might help. They use special techniques to control the behavior of the threads to cause race conditions and other bad things.

I did a quick check and found MultithreadedTC.

Perhaps this puts you on the right track.

1
  • That looks very relevant, definitely give it a try and let you know Sep 2, 2013 at 21:44
1

Consider your unit test code as a thread too. When I tried to test something in multi-threaded environment, generally speaking I did the following:

  • I started the work threads (the thread(s) containing the tested method and the other threads) in the unit test thread manually and
  • let the unit test thread sleep for a given time, or wait for the other threads and then I checked my results.

The thing is, the unit test thread may not give up execution to the others and that is why you have to force it someway. You cannot "directly" control how the processor time is assigned to threads, so that is why you have to start them and let them "work out" the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.