Class UnitTest

java.lang.Object
com.tibco.ep.testing.framework.UnitTest

public abstract class UnitTest extends Object

UnitTest accessor functions

Unit test cases can inherit from, or use a delegate of this class to access transactional abort mode, transactional deadlock detection, transactional leak detection and test timeouts. When using in junit test cases, call in @Before and @After functions :

 @Before
 public void initializeTest() {
     this.initialize();
 }

 @After
 public void completeTest() throws TransactionalMemoryLeakException, TransactionalDeadlockDetectedException {
     this.complete();
 }
 
By default, a unit test timeout is enabled with a value of DEFAULT_TIMEOUT_SECONDS seconds. If test execution exceeds this value a node snapshot is created in the node directory of the test node. The behavior associated with a test timeout can be changed using setTimeoutBehavior(Runnable). The test timeout value an be changed using setTimeoutSeconds(long) and timeouts can be disabled using disableTimeout()

To avoid test timeouts during debugging sessions, timeouts are automatically disabled if the JVM was started with a Java agent option (e.g -agentlib...)

  • Field Details

    • DEFAULT_TIMEOUT_SECONDS

      public static final long DEFAULT_TIMEOUT_SECONDS
      Default timeout for test cases in seconds. Value is 300L seconds.
      See Also:
  • Constructor Details

    • UnitTest

      public UnitTest()
      Constructor
  • Method Details

    • initialize

      public void initialize()

      Initialize test settings - must be called once before tests are run. If called more than once, test settings are cleared and re-initialized.

      This operation :

      • Initializes managed object leak detection - counts of existing managed objects is taken in preparation for later comparison.
      • Initializes deadlock leak detection - deadlock statistics are cleared and collection enabled.
      • Starts a test case timeout (default is 300 seconds) - if the test case fails to complete before this time, a node snapshot is taken and the tests aborted.

      Transactional abort mode is disabled by default - to enable call enableTransactionalAbortMode() - then, new one-way (Asynchronous) transactions and timer events are aborted once and re-tried.

      The replaying of transactions (including transactional memory timer events) in abort mode helps to verify that the unit under test handles transaction replays well. However, since non-transactional resources such as logger and Java heap are not rolled back and replayed, effects such as double logging may be observed when abort mode is enabled.

      The system property ignoreLeaks can be set to a comma-separated list of class names - these class names are then ignored in the leak detection report.

    • complete

      Complete tests - should be called once after tests are run. If called before complete, the operation does nothing.

      This operation :

      • Completes managed object leak detection - if object leak are detected, TransactionalMemoryLeakException is thrown
      • Completes deadlock leak detection - if any deadlocks are detected, TransactionalDeadlockDetectedException is thrown
      • Disabled abort mode
      • Cancels test case timeout
      Throws:
      TransactionalMemoryLeakException - One or more leaks were found
      TransactionalDeadlockDetectedException - One or more deadlocks were found
    • disableTransactionalMemoryLeakDetection

      public void disableTransactionalMemoryLeakDetection()

      Leak detection is enabled by default for each test case - use this method to disable :

       @Test
       public void myTestCase() {
         this.disableTransactionalMemoryLeakDetection();
         .. some leaky test case
       }
       

      If already disabled, this operation does nothing.

      Can be called at any time.

      Note that its highly recommended to keep enabled but add classes that are meant to be persistent to the ignoreleak list in jar manifest files. See the ep-maven documentation for details.

    • addVerboseTransactionalMemoryLeakDetection

      public void addVerboseTransactionalMemoryLeakDetection(String verboseClass)

      Add verbose transactional memory leak detection for the specified class. This must be called before the test framework is initialized, for example :

       @Before
       public void initializeTest() {
           this.setVerboseTransactionalMemoryLeakDetection("com.tibco.ep.testing.framework.MO");
           this.setVerboseTransactionalMemoryLeakDetection("com.tibco.ep.testing.framework.ExampleConfig");
           this.initialize();
       }
       

      With this set, the leak reports include the managed object references of leaked objects along with the results of toString() for each of the leaked objects.

      Parameters:
      verboseClass - class name to add to verbose leak report
    • disableTransactionalDeadlockDetection

      public void disableTransactionalDeadlockDetection()

      Disable deadlock detection for this test case :

       @Test
       public void myTestCase() {
         this.disableTransactionalDeadlockDetection();
         .. ignore deadlocks
       }
       

      If already disabled, this operation does nothing.

      Can be called at any time.

    • disableTransactionalAbortMode

      public void disableTransactionalAbortMode()

      Disable abort mode for this test case :

       @Test
       public void myTestCase() {
         this.disableTransactionalAbortMode();
         .. disable abort mode
       }
       

      If already disabled, this operation does nothing.

      Can be called at any time.

    • enableTransactionalAbortMode

      public void enableTransactionalAbortMode()

      Enable abort mode for this test case :

       @Test
       public void myTestCase() {
         this.enableTransactionalAbortMode();
         .. enable abort mode
       }
       

      If already enabled, this operation does nothing.

      Can be called at any time.

    • disableTimeout

      public void disableTimeout()

      Disable timeout for this test case :

       @Test
       public void myTestCase() {
         this.disableTimeout();
         .. disable timeout
       }
       

      If already disabled, this operation does nothing.

      Can be called at any time.

    • setTimeoutSeconds

      public void setTimeoutSeconds(long timeoutSeconds)

      Set the test case timeout - default is 300 seconds. Timeout is stopped and re-started.

       @Test
       public void myTestCase() {
         this.setTimeoutSeconds(400);
         .. 
       }
       
      Parameters:
      timeoutSeconds - Timeout in seconds
    • setTimeoutBehavior

      public void setTimeoutBehavior(Runnable runnable)

      Override the default behavior to create node snapshot and exist the jvm

       @Test
       public void myTestCase() {
         this.setTimeoutBehavior(new Runnable() {
           @Override
           public void run() {
             System.out.println("TIMEOUT!");
             System.exit(-1);
           };
         });
         .. 
       }
       
      Parameters:
      runnable - code to execute on timeout
    • isTransactionalMemoryLeakDetectionEnabled

      public boolean isTransactionalMemoryLeakDetectionEnabled()

      Determine if leak detection is enabled

      Returns:
      true if enabled, false otherwise
    • isTransactionDeadlockDetectionEnabled

      public boolean isTransactionDeadlockDetectionEnabled()

      Determine if deadlock detection is enabled

      Returns:
      true if enabled, false otherwise
    • isTransactionAbortModeEnabled

      public boolean isTransactionAbortModeEnabled()

      Determine if abort mode is enabled

      Returns:
      true if enabled, false otherwise
    • isTimeoutEnabled

      public boolean isTimeoutEnabled()

      Determine if timeout is enabled

      Returns:
      true if enabled, false otherwise
    • getNodes

      public static List<String> getNodes()

      Determine list of nodes that are currently part of this cluster.

      An example use is to make sure all nodes are discovered before starting test case :

       @BeforeClass
       public static void waitForCluster() {
           for (int i = 0; i < 10; i++) {
               if (getNodes().size() == 2) {
                   break;
               }
               try {
                   Thread.sleep(500);
               } catch (InterruptedException e) {
               }
           }
           logger.info("Discovered nodes " + String.join(",", getNodes()));
       }
       
      Returns:
      list of discovered node name