Class ConcurrencyTester<F,​T>


  • public final class ConcurrencyTester<F,​T>
    extends java.lang.Object
    Utility class to test concurrency.

    This class is used to check whether or not a set of threads violate a concurrency contract (e.g., whether they run concurrently when they are not allowed to, or vice versa).

    The following is a usage scenario of this class. Suppose we have a method that accepts an action (of type Runnable or the like) as an argument (e.g., methodUnderTest(..., actionUnderTest)) and at some point during the method's execution, the method will invoke the action exactly once. When several threads are concurrently calling the method (possibly with different parameter values), the method under test may make a contract that all the threads can execute the corresponding actions concurrently, or it may make a contract that all the threads cannot execute the actions concurrently. To check whether the method under test meets the concurrency contract, we can write a test as follows.

    
     ConcurrencyTester tester = new ConcurrencyTester();
     for (...) {
         Function actionUnderTest = (input) -> { ... };
         tester.addMethodInvocationFromNewThread(
                 (Function instrumentedActionUnderTest) -> {
                     // instrumentedActionUnderTest is actionUnderTest plus the instrumented code by
                     // ConcurrencyTester, so that the tester can perform necessary concurrency
                     // checks when the action starts and finishes.
                     // The client will need to call methodUnderTest with this instrumented action.
                     methodUnderTest(..., instrumentedActionUnderTest);
                 },
                 actionUnderTest);
     }
     

    Then, if the actions are allowed to run concurrently, we can make the following assertion:

    
     tester.assertThatActionsCanRunConcurrently();
     

    If the actions are not allowed to run concurrently, we can make the following assertion:

    
     tester.assertThatActionsCannotRunConcurrently();
     
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addMethodInvocationFromNewThread​(java.util.function.Consumer<java.util.function.Function<F,​T>> methodUnderTestInvocation, java.util.function.Function<F,​T> actionUnderTest)
      Adds a new invocation of the method under test to this ConcurrencyTester instance.
      void assertThatActionsCannotRunConcurrently()
      Executes the invocations of the method under test in separate threads and asserts that all the actions ran sequentially.
      void assertThatActionsCanRunConcurrently()
      Executes the invocations of the method under test in separate threads and asserts that all the actions ran concurrently.
      void assertThatOnlyOneActionIsExecuted()
      Executes the invocations of the method under test in separate threads and asserts that one and only one of the actions was executed.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ConcurrencyTester

        public ConcurrencyTester()
    • Method Detail

      • addMethodInvocationFromNewThread

        public void addMethodInvocationFromNewThread​(@NonNull
                                                     java.util.function.Consumer<java.util.function.Function<F,​T>> methodUnderTestInvocation,
                                                     @NonNull
                                                     java.util.function.Function<F,​T> actionUnderTest)
        Adds a new invocation of the method under test to this ConcurrencyTester instance. The ConcurrencyTester will execute each invocation in a separate thread and check whether the corresponding actions under test meet the concurrency requirement.
        Parameters:
        methodUnderTestInvocation - the invocation of the method under test which will be executed from a new thread
        actionUnderTest - the action under test
      • assertThatActionsCanRunConcurrently

        public void assertThatActionsCanRunConcurrently()
        Executes the invocations of the method under test in separate threads and asserts that all the actions ran concurrently. Note that a failed assertion means that either the actions were not allowed to run concurrently (which violates the concurrency requirement) or the actions accidentally ran sequentially. However, while the latter case is possible, the implementation of this method makes sure that it is unlikely to happen.
      • assertThatActionsCannotRunConcurrently

        public void assertThatActionsCannotRunConcurrently()
        Executes the invocations of the method under test in separate threads and asserts that all the actions ran sequentially. Note that a successful assertion means that either the actions were not allowed to run concurrently (which meets the concurrency requirement) or the actions accidentally ran sequentially. However, while the latter case is possible, the implementation of this method makes sure that it is unlikely to happen.
      • assertThatOnlyOneActionIsExecuted

        public void assertThatOnlyOneActionIsExecuted()
        Executes the invocations of the method under test in separate threads and asserts that one and only one of the actions was executed.