Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a different system property for each jmv gradle spawns when testing in parallel?

Tags:

gradle

testing

Gradle allows me to start multiple jvms for testing like so:

test {
   maxParallelForks = 10
}

Some of the tests for an application I have requires a fake ftp server which needs a port. This is quite easy to do with one jvm:

test {
   systemProperty 'ftpPort', 10000
}

However, when running in parallel I would need to start 10 fake ftp servers. How do I add a custom system property for each jvm spawned by gradle?

Something like:

test {
   maxParallelForks 10
   customizeForks { index ->
       systemProperty 'ftpPort', 10000 + index
   }
}
like image 549
Knut Saua Mathiesen Avatar asked Oct 27 '17 10:10

Knut Saua Mathiesen


1 Answers

There's no setup task to do before an after the fork. However, you can override the test task in your project to achieve the behavior (here in Java), as the Test task is simply a class:

  public class ForkTest extends org.gradle.api.tasks.testing.Test {
        private final AtomicInteger nextPort = new AtomicInteger(10000); 
        public Test copyTo(JavaForkOptions target) {
            super.copyTo(target);
            target.systemProperty("ftpPort", nextPort.getAndAIncrement());
            return this;
        }
    }

Then in your build.gradle :

task testFork(Type: ForkTest){
   forkEvery = 1
   maxParallelForks = 10
   ... 
}
like image 102
Tony Farias Avatar answered Oct 11 '22 08:10

Tony Farias