How to index forked JVM processes during test execution?


(Pavlo Vlasenko) #1

I am looking for a way to adopt maven-surefire-plugin mechanizm, which allows to pass unique surefire.forkNumber property counting from 1 to effective number of parallel forks into application. I want to use such property as index of array down in my code.

With Java Gradle plugin I can use maxParallelForks property in Test task, which allow to generate unique org.gradle.test.worker system properties and use them in code. Almost what I need, but these properties generated randomly, and do not start from 1, however seems they go in sequence:

build.gradle:

test {
    maxParallelForks = 4
}

Foobar.java:

String gradleTestWorker = System.getProperty("org.gradle.test.worker", "1"); // e.g. will be "33", or "34", or "35", or "36" per fork

My best try to get each forked JVM process indexed in this situation is to assume that values of org.gradle.test.worker are always serial, so I can use a division by modulo and convert them into actual indicies:

build.gradle:

test {
    maxParallelForks = 4
    systemProperty 'forkCount', maxParallelForks
}

Foobar.java:

    Integer gradleTestWorker = Integer.valueOf(System.getProperty("org.gradle.test.worker")); // will be 33, or 34, or 35 or 36
    Integer totalForks = Integer.valueOf(System.getProperty("forkCount")); // will be 4
    Integer index = gradleTestWorker % totalForks; // will be 1 or 2 or 3 or 0 - exactly what I need

Can someone please help me with the questions below:

  1. How values of org.gradle.test.worker are generated? Can I rely that they will be always serial?
  2. Is there a more correct way to index forked JVM threads?