From 7b8ed4d13f26bec33a46693ba84f0f45633a8190 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Wed, 30 Jan 2019 18:27:39 -0800 Subject: [PATCH] Allow sbt scripted tests to run in parallel I'm not sure if this is a huge benefit or not, but it's nice to have the option to run the scripted tests in parallel. The default behavior should be the same as before. --- project/Scripted.scala | 10 +++++++++- .../main/scala/sbt/scriptedtest/ScriptedTests.scala | 5 ++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/project/Scripted.scala b/project/Scripted.scala index cd7095799..2b4276a8d 100644 --- a/project/Scripted.scala +++ b/project/Scripted.scala @@ -115,6 +115,7 @@ object Scripted { bootProperties: File, launchOpts: Array[String], prescripted: java.util.List[File], + instances: Int ): Unit } @@ -123,7 +124,6 @@ object Scripted { Thread.currentThread.setContextClassLoader(loader) val bridge = bridgeClass.getDeclaredConstructor().newInstance().asInstanceOf[SbtScriptedRunner] - try { // Using java.util.List to encode File => Unit. val callback = new java.util.AbstractList[File] { @@ -131,6 +131,13 @@ object Scripted { def get(x: Int): sbt.File = ??? def size(): Int = 0 } + val instances: Int = (System.getProperty("sbt.scripted.parallel.instances") match { + case null => 1 + case i => scala.util.Try(i.toInt).getOrElse(1) + }) match { + case i if i > 0 => i + case _ => 1 + } import scala.language.reflectiveCalls bridge.runInParallel( sourcePath, @@ -139,6 +146,7 @@ object Scripted { launcher, launchOpts.toArray, callback, + instances ) } catch { case ite: InvocationTargetException => throw ite.getCause } } finally { diff --git a/scripted-sbt-redux/src/main/scala/sbt/scriptedtest/ScriptedTests.scala b/scripted-sbt-redux/src/main/scala/sbt/scriptedtest/ScriptedTests.scala index 91373e1d0..8375bf835 100644 --- a/scripted-sbt-redux/src/main/scala/sbt/scriptedtest/ScriptedTests.scala +++ b/scripted-sbt-redux/src/main/scala/sbt/scriptedtest/ScriptedTests.scala @@ -125,7 +125,10 @@ final class ScriptedTests( if (labelsAndDirs.isEmpty) List() else { val totalSize = labelsAndDirs.size - val batchSize = totalSize / sbtInstances + val batchSize = totalSize / sbtInstances match { + case 0 => 1 + case s => s + } val (launcherBasedTests, runFromSourceBasedTests) = labelsAndDirs.partition { case (testName, _) =>