Search code examples
javaseleniumgradlequeuejunit4

How does Gradle's java plugin run JUnit tests in parallel: one queue per fork or a single queue and multiple forks?


I have been recently trying Gradle, I didn't have any prior experience with it, and so far I have been able to do the things I wanted to and am satisfied with the results. However, in my case I have to run Selenium tests with JUnit and some of them are disproportionately larger than others (i.e.: 25min vs 4min).

When using the maxParallelForks option, sometimes it takes longer than I would expect, as the tests seem to be assigned beforehand to the forks and sometimes I end up with iddle forks, while one of them is stuck with a long test, and when it finishes other shorter tests run after it (which could have run in any of the other available forks).

TL;DR: When running tests in parallel, Gradle seems to assign tests as if there were multiple queues (one per fork) and I would like it to be like a single queue where the forks take the next test in the queue.

As an off-topic example, my situation is like being stuck in a queue at the supermarket, when the ones next to you are empty, but you can't change.


Solution

  • it's the latter. gradle uses one queue and distributes each entry clockwise to the running processes. Say you have 4 tests:

    • Test1 taking 10s
    • Test2 taking 1s
    • Test3 taking 10s
    • Test4 taking 1s

    and using maxParallelForks = 2 the overall test task execution would be around 20s. I guess we need to discuss if this can be improved by getting notified about "free" processes to assign Test3 directly to test worker process 2 after Test2 comes back after 1s.