Vm options not working in gradle build


(martin wignall) #1

Hello, I have a junit test that works when executed from within Eclipse, but fails when executed by Gradle at the command line. My test is making an http request, and to make this work I’m setting the jvm property:

-Djava.net.preferIPv4Stack=true

From within Eclipse, I do this as a VM option in ‘Run Configurations’. And my test works fine, when executed from within Eclipse when this property is set.

To set the jvm property for gradle, I’ve set my build.gradle to contain:

test {

allJvmArgs = [ “-Djava.net.preferIPv4Stack=true” ] }

Now, the failing junit test code that makes the http request looks like this:

ExecutorService executor = Executors.newCachedThreadPool(); Callable task = new Callable() {

Client client = Client.create();

WebResource resource = client.resource(some-URL);

public Object call() {

return resource.accept(MediaType.TEXT_PLAIN).get(String.class);

} }; Future future = executor.submit(task); return ((String)future.get(1000, TimeUnit.MILLISECONDS)).split("\n");

I think the issue is something to do with how the ExecutorService works. I suspect the jvm parameter (mentioned above) is not being passed through to the thread that executes my http call. I say this, because the following code works fine:

Client client = Client.create(); WebResource resource = client.resource(some-URL); return resource.accept(MediaType.TEXT_PLAIN).get(String.class).split("\n");

However, I cannot use the above code, as I want my call to time out after 1000ms if there is no response (hence using the ExecutorService approach).

Am I right in my suspicion that the jvm parameter is not being passed through to the thread executed by the ExecutorService? If so, how can I rectify this?

Many thanks in advance for any help!

Martin


(René Groeschke) #2

I’m wondering why your setup with:

test {
     allJvmArgs = [ "-Djava.net.preferIPv4Stack=true" ]
 }

does not work. Can you alternatively try to pass the property using the systemProperty method of the Test task:

test{
 systemProperty 'java.net.preferIPv4Stack', 'true'
}

For further debugging, you might write a custom test that prints the jvm args using “System.getProperties()” to double check, that the property is passed correctly.

regards, René