Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > JUnit3 - Distributed JUnit
JUnit3 - Distributed JUnit
Added by morpheus, last edited by morpheus on Oct 24, 2007  (view change)
Labels: 
(None)


Disributed Junit3 Example

This example will demonstrate how GridGain can distribute your long running JUnit3 tests or test suites across grid and hence dramatically speeding up overall execution of all tests.

To try this example you will need to open GridJunit3ExampleTestSuite.java 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.

Package:
org.gridgain.examples.junit.junit3

There is one class worth noting in this example:

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 Javadoc which is pre-configured in ${GRIDGAIN_HOME}/config/junit/junit-spring.xml Spring configuration file. You need to specify a path to this file to the gridgain startup script as follows:

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.

GridJunit3ExampleTestSuite.java

This class represents a standard JUnit3 suite with a {{static suite()} method.

Create GridJunit3TestSuite Suite

The only difference from standard JUnit3 suites is that instead of creating a new TestSuite we create a new GridJunit3TestSuite Javadoc suite.

TestSuite suite = new GridJunit3TestSuite("Example Grid Test Suite");

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);

Running Tests Locally

Certain tests must run locally no matter what, often due to some environmental issues. Yet these tests can benefit from parallel execution with other tests. GridGain supports it via GridJunit3LocalTestSuite Javadoc suite. For example, the code below guarantees that TestC will always run locally.

// Add TestC to execute always on the local node but still in
// parallel with other tests.
suite.addTest(new GridJunit3LocalTestSuite(TestC.class, "Local suite"));

Full Source Code.

GridJunit3ExampleTestSuite.java
public final class GridJunit3ExampleTestSuite {
    /**
     * Standard JUnit3 static suite method.
     *
     * @return JUnit3 suite.
     */
    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 TestC to execute always on the local node but still in
        // parallel with other tests.
        suite.addTest(new GridJunit3LocalTestSuite(TestC.class, "Local suite"));

        // Add other tests.
        suite.addTestSuite(TestD.class);

        return suite;
    }
}

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators