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 ofDEFAULT_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 usingsetTimeoutBehavior(Runnable)
. The test timeout value an be changed usingsetTimeoutSeconds(long)
and timeouts can be disabled usingdisableTimeout()
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 Summary
Fields Modifier and Type Field Description static long
DEFAULT_TIMEOUT_SECONDS
Default timeout for test cases in seconds.
-
Constructor Summary
Constructors Constructor Description UnitTest()
Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addVerboseTransactionalMemoryLeakDetection(String verboseClass)
Add verbose transactional memory leak detection for the specified class.void
complete()
Complete tests - should be called once after tests are run.void
disableTimeout()
Disable timeout for this test case :void
disableTransactionalAbortMode()
Disable abort mode for this test case :void
disableTransactionalDeadlockDetection()
Disable deadlock detection for this test case :void
disableTransactionalMemoryLeakDetection()
Leak detection is enabled by default for each test case - use this method to disable :void
enableTransactionalAbortMode()
Enable abort mode for this test case :static List<String>
getNodes()
Determine list of nodes that are currently part of this cluster.void
initialize()
Initialize test settings - must be called once before tests are run.boolean
isTimeoutEnabled()
Determine if timeout is enabledboolean
isTransactionAbortModeEnabled()
Determine if abort mode is enabledboolean
isTransactionalMemoryLeakDetectionEnabled()
Determine if leak detection is enabledboolean
isTransactionDeadlockDetectionEnabled()
Determine if deadlock detection is enabledvoid
setTimeoutBehavior(Runnable runnable)
Override the default behavior to create node snapshot and exist the jvmvoid
setTimeoutSeconds(long timeoutSeconds)
Set the test case timeout - default is 300 seconds.
-
-
-
Field Detail
-
DEFAULT_TIMEOUT_SECONDS
public static final long DEFAULT_TIMEOUT_SECONDS
Default timeout for test cases in seconds. Value is 300L seconds.- See Also:
- Constant Field Values
-
-
Method Detail
-
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
public void complete() throws TransactionalMemoryLeakException, TransactionalDeadlockDetectedException
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 foundTransactionalDeadlockDetectedException
- 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
-
-