Disributed Junit3 Example With @GridifyTest Annotation
This example will demonstrate how GridGain can distribute your long running JUnit3 tests or test suites across grid using @GridifyTest ![]()
To try this example you will need to open GridifyJunit3ExampleTestSuite in IDEA, Eclipse or any other IDE and run this JUnit3 suite using standard IDE JUnit integration. You will observe how execution of the tests is offloaded to remote nodes and then the results are seen in the IDE just as if it was a local run. All you had to do is attach @GridifyTest ![]()
Package:
org.gridgain.examples.junit.junit3
There is one class worth noting in this example:
Configuration
In order to enable @GridifyTest ![]()
JBoss AOP
Note that GridGain is not shipped with JBoss and doesn't include necessary JBoss libraries. We assume that if you choose to use JBoss AOP you would have these libraries anyways. The following configuration needs to be applied to enable JBoss byte code weaving:
- The following JVM configuration must be present:
- -javaagent:[path to jboss-aop-jdk50-4.x.x.jar]
- -Djboss.aop.class.path=[path to gridgain.jar]
- -Djboss.aop.exclude=org,com -Djboss.aop.include=org.gridgain.examples
- The following JARs should be in a classpath:
- javassist-4.x.x.jar
- jboss-aop-jdk50-4.x.x.jar
- jboss-aspect-library-jdk50-4.x.x.jar
- jboss-common-4.x.x.jar
- trove-1.0.x.jar
AspectJ AOP
The following configuration needs to be applied to enable AspectJ byte code weaving.
- JVM configuration should include: -javaagent:[GRIDGAIN_HOME]/libs/aspectjweaver-1.5.3.jar
- Classpath should contain the [GRIDGAIN_HOME]/config/aop/aspectj folder.
For more information see AOP Configuration
Running Grid Node
To run this example you need to start one or more additional grid nodes. For simplicity, you can start these nodes on the same box on which you are running the example.
To start a remote node 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.
GridifyJunit3ExampleTestSuite.java
This class represents a standard JUnit3 suite with a static suite()} method. Grid-enabling is achieved by attaching {{@GridifyTest ![]()
Attach @GridifyTest Annotation
The only difference from standard JUnit3 suites is that we need to attach @GridifyTest ![]()
@GridifyTest public static TestSuite suite() { ... }
You can pass configuration parameters into GridifyTest annotation. Refer to @GridifyTest ![]()
Running Tests Sequentially
Sometimes it is desired that certain tests run in sequence, yet parallel with other tests. For that you simply need to create a nested suite, then the whole suite will be executed remotely. For example, the following lines of code will guarantee that TestA and TestB always run in sequence.
// 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);
Full Source Code.
/**
* Regular JUnit3 suite. Note that because of {@link GridifyTest} annotation,
* all tests will execute in parallel on the grid.
* <p>
* Note that since {@link TestA} and {@link TestB} are added to this
* suite from within another nested suite and not directly, they will
* always execute sequentially, however still in parallel with other
* tests.
*/
public final class GridifyJunit3ExampleTestSuite {
/**
* Standard JUnit3 static <tt>suite()</tt> method. Note we attach {@link GridifyTest}
* annotation to it, so it will be grid-enabled.
*
* @return JUnit3 suite.
*/
@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;
}
}
