Description
Distributed JUnit support has been added starting GridGain 1.6.0 release.
You can distribute your JUnit3 test suites in 2 different ways. One way is to use GridJunit3TestSuite directly instead of the usual JUnit3 TestSuite. This is perhaps the easiest and most straight forward way to grid-enable your JUnit3 test suites.
Another way is by attaching @GridifyTest to your static suite() methods on JUnit3 test suites. The advantage of this approach is that you can provide test configuration parameters, such as timeout or custom test router right as annotation parameter in code, without having to change your existing test suites or having to pass extra VM arguments.
Package
org.gridgain.grid.test.junit3 Javadoc![]()
GridJunit3TestSuite
GridJunit3TestSuite ![]()
Bellow is an example of distributed JUnit3 test suite:
public class GridJunit3ExampleTestSuite { // Standard JUnit3 static suite method. public static TestSuite suite() { TestSuite suite = new GridJunit3TestSuite("Example Grid Test Suite"); // Add tests. suite.addTestSuite(TestA.class); suite.addTestSuite(TestB.class); suite.addTestSuite(TestC.class); return suite; } }
If you have four tests A, B, C, and D, and if you need to run A and B sequentially, then you should create a nested test suite with test A and B as follows:
public class GridJunit3ExampleTestSuite { // Standard JUnit3 static suite method. public static TestSuite suite() { TestSuite suite = new GridJunit3TestSuite("Example Grid Test Suite"); // Nested test suite to run tests A and B sequentially. TestSuite nested = new TestSuite("Example Nested Sequential Suite"); nested.addTestSuite(TestA.class); nested.addTestSuite(TestB.class); // Add tests A and B. suite.addTest(nested); // Add other tests. suite.addTestSuite(TestC.class); return suite; } }
GridJunit3LocalTestSuite
Some tests can only be executed locally mostly due to some environment issues. However they still can benefit from parallel execution with other tests. GridGain supports it via GridJunit3LocalTestSuite ![]()
![]()
To use local test suite within distributed test suite, simply add it to distributed test suite as follows:
public class GridJunit3ExampleTestSuite { // Local test suite example. public static TestSuite suite() { TestSuite suite = new GridJunit3TestSuite("Example Grid Test Suite"); // Local nested test suite to always run tests A and B // on the local node. TestSuite nested = new GridJunit3LocalTestSuite("Example Nested Sequential Suite"); nested.addTestSuite(TestA.class); nested.addTestSuite(TestB.class); // Add local tests A and B. suite.addTest(nested); // Add other tests. suite.addTestSuite(TestC.class); suite.addTestSuite(TestD.class); return suite; } }
Logging
When running distributed JUnit, all the logging that is done to System.out or System.err is preserved. GridGain will accumulate all logging that is done on remote nodes, send them back to originating node and associate all log statements with their corresponding tests. This way, for example, if you are running tests from and IDEA or Eclipse (or any other IDE) you would still see the logs as if it was a local run. However, since remote nodes keep all log statements done within a single individual test case in memory, you must make sure that enough memory is allocated on every node and that individual test cases do not spit out GigaBytes of log statements. Also note, that logs will be sent back to originating node upon completion of every test, so don't be alarmed if you don't see any log statements for a while and then all of them appear at once.
GridGain achieves such log transparency via reassigning System.out or System.err to internal PrintStream implementation. However, when using Log4J (or any other logging framework) within your tests you must make sure that it is configured with ConsoleAppender and that ConsoleAppender.setFollow(boolean) attribute is set to true. Logging to files is not supported yet and is planned for future releases.
Test Nesting
GridJunit3TestSuite ![]()
GridifyTest Annotation
To enable JUnit3 tests using @GridifyTest ![]()
public class GridifyJunit3ExampleTestSuite { // Standard JUnit3 suite method. Note we attach @GridifyTest // annotation to it, so it will be grid-enabled. @GridifyTest public static TestSuite suite() { TestSuite suite = new TestSuite("Example Test Suite"); // Nested test suite to run tests A and B sequentially. TestSuite nested = new TestSuite("Example Nested Sequential Suite"); nested.addTestSuite(TestA.class); nested.addTestSuite(TestB.class); // Add tests A and B. suite.addTest(nested); // Add other tests. suite.addTestSuite(TestC.class); suite.addTestSuite(TestD.class); return suite; } }
For information on how to start GridGain with AOP support refer to AOP Configuration documentation. Note that only AspectJ and JBoss AOP are supported. Spring AOP is not supported as it is proxy-based and only works with interfaces.
Configuration
To run distributed JUnit tests you need to start other instances of GridGain. You can do so by running GRIDGAIN_HOME/bin/gridgain-junit.{sh|bat} script, which will start default configuration. If configuration other than default is required, then use regular GRIDGAIN_HOME/bin/gridgain.{sh|bat} script and pass your own Spring configuration file as a parameter to the script.
You can use the following configuration parameters to configure distributed test suite locally. Note that many parameters can be overridden by setting corresponding JVM parameters defined in GridTestVmParameters ![]()
| Configuration Method | Default Value | Description |
|---|---|---|
| setDisabled(boolean) |
false | If true then GridGain will be turned off and suite will run locally. This value can be overridden by setting GridTestVmParameters.GRID_DISABLED |
| setConfigurationPath(String) |
config/junit/junit-spring.xml | Optional path to GridGain Spring configuration file for running JUnit tests. This property can be overridden by setting GridTestVmParameters.GRID_CONFIG |
| setRouterClassName(String) |
GridTestRouterAdapter |
Optional name of test router class that implements GridTestRouter |
| GRID_ROUTER_PREFER_REMOTE |
false | This value can only be set as VM parameter. Set it to true, e.g. -DGRID_ROUTER_PREFER_REMOTE=true, if you would like test router to not route tests to local node if there are remote nodes present. Note that this property works only with default test router. |
| setRouterClass(Class) |
null | Same as setRouterClassName(String) |
| setTimeout(long) |
0 which means that tests will never timeout. | Maximum timeout value in milliseconds after which test suite will return without waiting for the remaining tests to complete. This value can be overridden by setting GridTestVmParameters.GRID_TEST_TIMEOUT |
Test Scheduling
With GridGain you can configure how many tests you can run in parallel by specifying parallelJobsNumber configuration parameter on GridCollisionSpi ![]()
<property name="collisionSpi"> <bean class="org.gridgain.grid.spi.collision.fifoqueue.GridFifoQueueCollisionSpi"> <property name="parallelJobsNumber" value="1"/> </bean> </property>
The XML configuration above will guarantee that only 1 test can run at a time on local or remote nodes. You can ensure this way that although your tests run in parallel on different nodes, within a single node only one test can be running and all other ones are waiting.
Starting Grid Node
To start a remote node for JUnit tests, open the terminal window on Linux/Mac OS X or Command Prompt on Windows, change directory to ${GRIDGAIN_HOME}/bin and run the gridgain.{sh|bat} script. However, distributed JUnits have to use GridTestExecutorService ![]()
gridgain.bat config/junit/junit-spring.xml
or starting from GridGain 1.6.1, simply execute gridgain-junit.{sh|bat} script.
gridgain-junit.bat
It takes 2-3 seconds for grid node to start and if everything worked fine you should see starting log ending with successful start acknowledgment.
Examples
GridGain comes with several examples for distributed JUnit3. Please refer to JUnit3 - Distributed JUnit Example for example of how to use GridJunit3TestSuite ![]()
![]()
