限制 Scalatest 并行执行线程数 [英] Limit scalatest parallel execution thread number

查看:49
本文介绍了限制 Scalatest 并行执行线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试并行执行部分测试,因此我使用 ParallelTestExecution 特性扩展了这些测试类,唯一的问题是它一次运行太多测试.据我所知,它最多运行 2 * number_of_cpu_cores 所以在我的情况下是 2*8 测试.它的方式太多了,我想将其限制为最多 4 个线程.我尝试使用 SBT concurentRestrictions in Test 设置,但它不会改变任何东西(我认为它只影响并发测试类的执行,不影响一个类中的并发测试数).有没有办法强制 scalaTest 并行运行最多 N 个测试?最好是我可以设置每个测试类的最大线程数,因为有些测试消耗的资源较少,而且我可以一次运行 4 个以上.

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.

推荐答案

所以一年多后我又回到了那个问题,因为我以前无法解决它.我在这个项目中标记了一个解决方案:https://github.com/agido/pageobject.它比我需要的要复杂一些,因此基于他们的代码,我创建了更简单的解决方案,其中只有一个特征可以用作标准 ParallelTestExecution 的替代品.:

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)
        )
      )
    ))
}

更多工作原理和一些示例可以在这里找到:https://github.com/mateuszgruszczynski/scaltesttwolevelparallelism

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

这篇关于限制 Scalatest 并行执行线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆