Is there a way to identify forked vms during test execution

I have a set of testNG tests that I want to execute in parallel. I’m using the maxParallelForks property and setting it to 4 maxParallelForks=4

The problem is that the tests in question use ports for testing network code. I need a way to guarantee to each forked vm a set of port numbers that it can use. Unfortunately, I don’t see any way to either identify forked vms separately or send different system properties to each forked vm.

So now I have the scenario where two test classes will execute in parallel at the same time. Both will think that say port 5000 is available and try to use it.

The cleanest solution would be to say pass a system property of “portRange” to a forked vm. So for forked vm 1 it could be portRange=5000-5010 and for forked vm 2 it could be portRange=5011-5020

Then I could read that within the test code by using System.getProperty()

Does anyone know if anything like this exists in gradle?

Then only way currently is to use the ‘org.gradle.test.worker’ system property that is set inside the JVM.

Here’s an example: