Search code examples
scalasbtscalatest

Limit scalatest parallel execution thread number


I'm trying to execute some part of my tests in parallel so I've extended those tests classes with ParallelTestExecution trait, the only problem is that it runs too many tests at once. As I understand it runs up to 2 * number_of_cpu_cores so in my case 2*8 tests. Its way too much and I would like to limit it to 4 threads max. I've tried to use SBT concurentRestrictions in Test settings but it wont change anything (I thing it affects only concurrent test classes execution and do not affect number of concurrent tests in one class). Is there any way to force scalaTest to run max N tests in parallel? It would be best if I could set max number of threads per test class as some tests are less resources consuming and i could run more than 4 of them at once.


Solution

  • So after over a year I came back to that problem, as I was not able to resolve it previously. I stamped on a solution in this project: https://github.com/agido/pageobject. It was a bit more complicated then I needed so based on their code I've created simpler solution with just one trait that may be used as replacement for standard ParallelTestExecution.:

    package org.scalatest
    
    import java.util.concurrent.Executors
    
    import org.scalatest.tools.ConcurrentDistributor
    
    trait FixedThreadPoolParallelExecution extends SuiteMixin with ParallelTestExecution{ this: Suite =>
    
      val threadPoolSize: Int
    
      abstract override def run(testName: Option[String], args: Args): Status =
        super.run(testName, args.copy(
          distributor = Some(
            new ConcurrentDistributor(
              args,
              java.util.concurrent.Executors.newFixedThreadPool(threadPoolSize, Executors.defaultThreadFactory)
            )
          )
        ))
    }
    

    More how is it working and some examples can be found here: https://github.com/mateuszgruszczynski/scalatesttwolevelparallelism