Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

surefire parallel threadCount is ignored (JUnit 4)

I've seen quite a few topics dealing with running JUnit tests in parallel with maven surefire, but I haven't seen an answer dealing with this particular issue.

In short: test methods are executing in parallel (good news there), but the prop doesn't serve to limit my threads. The ultimate goal is to parallelize my web driver and appium runs, but I'd like to be able to control the total possible threads first.

The code snippets below are from a dummy project just for demonstration's sake. Following the apache documentation, it doesn't look like there's a whole lot more to it than the below.

pom.xml

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <parallel>methods</parallel>
                <threadCountMethods>2</threadCountMethods>
            </configuration>
        </plugin>
    </plugins>
</build>

ExampleTest.class

public class ExampleTest {

    @Test
    public void one() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void two() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void three() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void four() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void five() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void six() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void seven() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void eight() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void nine() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void ten() throws InterruptedException {
        Thread.sleep(5000);
    }
}

I'd expect a full run of this class with the provided pom config to take a little longer than 25 seconds (10 sleeps for 5 seconds running two at a time). The actual runtime is a little more than 5 seconds:

Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.022 sec - in ExampleTest

Changing that threadCountMethods prop has no effect on the runtime (it looks like all methods are going in parallel without regard for the threadCount prop).

Any input would be greatly appreciated; thanks!

like image 855
pappy-o Avatar asked Mar 13 '23 23:03

pappy-o


1 Answers

So after some more digging, I found a tiny little property called perCoreThreadCount found here. If not specified, that prop defaults to true. With a total of 8 cores on my machine, 2 threads per core (from the original pom config in the question) would mean up to 16 of those sample tests could run in 5ish seconds. Adding that prop and setting value to false in pom fixed all the trouble!

like image 155
pappy-o Avatar answered Apr 02 '23 11:04

pappy-o