Limit scalatest parallel execution thread number

雨燕双飞 提交于 2019-12-13 15:27:23

问题


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.


回答1:


Try to add this line to your sbt project settings:

testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-P4")

From http://www.scalatest.org/user_guide/using_the_runner:

The -P option may optionally be appended with a number (e.g. "-P10" -- no intervening space) to specify the number of threads to be created in the thread pool. If no number (or 0) is specified, the number of threads will be decided based on the number of processors available.




回答2:


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



来源:https://stackoverflow.com/questions/45169343/limit-scalatest-parallel-execution-thread-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!