The following example shows how to test multi-threaded, concurrent Java with vmlens:
import com.vmlens.api.AllInterleavings;
public class TestNonVolatileField {
private int j = 0;
@Test
public void testUpdate() throws InterruptedException {
try(AllInterleavings allInterleavings = new AllInterleavings("testNonVolatileField")) {
while (allInterleavings.hasNext()) {
Thread first = new Thread() {
@Override
public void run() {
j++;
}
};
first.start();
j++;
first.join();
}
}
}
}
VMLens detects the data race and generates the following report:
See test-vmlens-maven-plugin for more examples.
To use vmlens with Maven, configure a plugin tag to tell Maven that the vmlens plugin should be executed at the test phase. And include the jar com.vmlens.api as test dependency.
<project>
<!-- to include the class AllInterleavings into the test class path. -->
<dependency>
<groupId>com.vmlens</groupId>
<artifactId>api</artifactId>
<version>1.2.6</version>
<scope>test</scope>
</dependency>
<build>
<plugins>
<!-- to run the vmlens maven plugin during the maven test phase -->
<plugin>
<groupId>com.vmlens</groupId>
<artifactId>vmlens-maven-plugin</artifactId>
<version>1.2.6</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
...
</project>
See pom.xml for an example.
To use VMLens as a standalone tool:
- Include com.vmlens.api from the Maven Repository as a test jar in your project.
- Download the jar standalone-1.2.6.jar from the Maven Repository
- Run java -jar standalone-1.2.6.jar install. This creates the agent directory and prints the vm parameter to System.out
- Add this vm parameter when you run your test
- Run java -jar standalone-1.2.6.jar report. This checks for data races and creates the report
Read the documentation here.
Contact me at thomas.krieger@vmlens.com