3

I want to be able to switch between parallel and serial execution of scala tests using command line.

Working example with "test.par" system property:

val parallelTestOpt = Option(System.getProperty("test.par"))

testOptions in IntegrationTest += Tests.Argument(
  //Configure distributor's pool size
  parallelTestOpt.map(count =>s"-P$count").getOrElse("-P1")
)

lazy val root = (project in file("."))
  .configs(IntegrationTest)
  .settings(Defaults.itSettings,
      //If suites are executed in parallel
      IntegrationTest / parallelExecution := parallelTestOpt.exists(_ != "1"),
      IntegrationTest / testForkedParallel := parallelTestOpt.exists(_ != "1")
  )

The "problematic" part is the parallelTestOpt.map(count =>s"-P$count").getOrElse("-P1"). I don't want to provide default value "-P1" when the "test.par" property was not specified. What is the best practice to achieve that ?

Maybe the whole concept is wrong and I should do it in a different way ?

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
Tomas Bartalos
  • 1,256
  • 12
  • 29

1 Answers1

2

As an alternative approach consider separating the parallelism concern into a single-argument custom command

commands += Command.single("testpar") { (state, numOfThreads) =>
  s"""set IntegrationTest / testOptions  += Tests.Argument("-P$numOfThreads")"""::
     "set IntegrationTest / parallelExecution := true" ::
     "set IntegrationTest / testForkedParallel := true" ::
     "IntegrationTest / test" :: state
}

and execute with, say, testpar 6 to run with pool of 6 threads.


Addressing the comment, for compile-time safety try

commands += Command.single("testpar") { (state, numOfThreads) =>
  val extracted = Project.extract(state)
  val stateWithParallel= extracted.appendWithSession(
    Seq(
      IntegrationTest / testOptions  += Tests.Argument(s"-P$numOfThreads"),
      IntegrationTest / parallelExecution := true,
      IntegrationTest / testForkedParallel := true,
    ),
    state
  )
  extracted.runTask(IntegrationTest / test, stateWithParallel)
  state
}
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • Thank you for you answer, I like the idea of separation of concerns. But since the statements are generated as strings, my IDE can't help with syntax checking and highlighting, is there a way around this ? – Tomas Bartalos Feb 17 '20 at 15:57