forked from higherkindness/rules_scala
-
Notifications
You must be signed in to change notification settings - Fork 5
Execute test classes concurrently #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/scala/higherkindness/rules_scala/common/sbt-testing/BufferedLogger.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package higherkindness.rules_scala.common.sbt_testing | ||
|
|
||
| import sbt.testing.Logger | ||
|
|
||
| import scala.collection.mutable | ||
|
|
||
| private sealed trait SbtLogEntry | ||
| private object SbtLogEntry { | ||
| case class Error(message: String) extends SbtLogEntry | ||
| case class Warn(message: String) extends SbtLogEntry | ||
| case class Info(message: String) extends SbtLogEntry | ||
| case class Debug(message: String) extends SbtLogEntry | ||
| case class Trace(throwable: Throwable) extends SbtLogEntry | ||
| } | ||
|
|
||
| class BufferedLogger(underlying: Logger) extends Logger { | ||
| private val buffer = mutable.ArrayBuffer.empty[SbtLogEntry] | ||
|
jadenPete marked this conversation as resolved.
|
||
|
|
||
| override def ansiCodesSupported(): Boolean = underlying.ansiCodesSupported() | ||
| override def error(message: String): Unit = buffer.addOne(SbtLogEntry.Error(message)) | ||
| override def warn(message: String): Unit = buffer.addOne(SbtLogEntry.Warn(message)) | ||
| override def info(message: String): Unit = buffer.addOne(SbtLogEntry.Info(message)) | ||
| override def debug(message: String): Unit = buffer.addOne(SbtLogEntry.Debug(message)) | ||
| override def trace(throwable: Throwable): Unit = buffer.addOne(SbtLogEntry.Trace(throwable)) | ||
|
|
||
| def flush(): Unit = { | ||
| buffer.foreach { | ||
| case SbtLogEntry.Error(message) => underlying.error(message) | ||
| case SbtLogEntry.Warn(message) => underlying.warn(message) | ||
| case SbtLogEntry.Info(message) => underlying.info(message) | ||
| case SbtLogEntry.Debug(message) => underlying.debug(message) | ||
| case SbtLogEntry.Trace(throwable) => underlying.trace(throwable) | ||
| } | ||
|
jadenPete marked this conversation as resolved.
|
||
|
|
||
| buffer.clear() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/scala/higherkindness/rules_scala/common/sbt-testing/TestTaskExecutor.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package higherkindness.rules_scala.common.sbt_testing | ||
|
|
||
| import java.util.concurrent.ConcurrentLinkedQueue | ||
| import sbt.testing.{Event, Logger, Status, Task} | ||
|
tmccombs marked this conversation as resolved.
|
||
| import scala.collection.{concurrent, mutable} | ||
| import scala.concurrent.{blocking, ExecutionContext, Future} | ||
| import scala.jdk.CollectionConverters.* | ||
|
|
||
| case class TaskExecutorResult(taskEvents: Map[Task, Array[Event]], failures: Array[String]) | ||
|
|
||
| private object TaskExecutorResult { | ||
| private[sbt_testing] case class Mutable( | ||
|
jadenPete marked this conversation as resolved.
|
||
| taskEvents: concurrent.TrieMap[Task, ConcurrentLinkedQueue[Event]], | ||
| failures: ConcurrentLinkedQueue[String], | ||
| ) { | ||
| def clear(): Unit = { | ||
| taskEvents.clear() | ||
| failures.clear() | ||
| } | ||
|
|
||
| def toTaskExecutorResult: TaskExecutorResult = TaskExecutorResult( | ||
| taskEvents.view.map { case task -> events => task -> events.asScala.toArray }.toMap, | ||
| failures.asScala.toArray, | ||
| ) | ||
| } | ||
|
|
||
| private[sbt_testing] object Mutable { | ||
| def empty: Mutable = apply(concurrent.TrieMap.empty, new ConcurrentLinkedQueue()) | ||
| } | ||
| } | ||
|
|
||
| trait TestTaskExecutor { | ||
| def submitTask(task: Task): Unit | ||
| def waitForTasks(): Future[TaskExecutorResult] | ||
| } | ||
|
|
||
| class ConcurrentTestTaskExecutor(logger: Logger) extends TestTaskExecutor { | ||
| private val activeTasks = new ConcurrentLinkedQueue[Future[Unit]]() | ||
| private val currentResult = TaskExecutorResult.Mutable.empty | ||
|
|
||
| override def submitTask(task: Task): Unit = activeTasks.add( | ||
| Future { | ||
| blocking { | ||
| val bufferedLogger = new BufferedLogger(logger) | ||
| val reporter = new TestReporter(bufferedLogger) | ||
|
|
||
| reporter.preTask(task) | ||
|
|
||
| val additionalTasks = task.execute( | ||
| event => { | ||
| currentResult.synchronized { | ||
| currentResult.taskEvents.getOrElseUpdate(task, new ConcurrentLinkedQueue()).add(event) | ||
|
|
||
| event.status match { | ||
| case Status.Failure | Status.Error => currentResult.failures.add(task.taskDef.fullyQualifiedName) | ||
| case _ => | ||
| } | ||
| } | ||
| }, | ||
| Array(new PrefixedTestingLogger(bufferedLogger, " ")), | ||
| ) | ||
|
|
||
| additionalTasks.foreach(submitTask) | ||
|
|
||
| reporter.postTask() | ||
|
|
||
| // Only one task should write to stderr/stdout at a time. Of course, the task implementation could write to | ||
| // stdout/stderr directly, but that's out of our control. | ||
| synchronized { | ||
| bufferedLogger.flush() | ||
| } | ||
| } | ||
| }(ExecutionContext.global), | ||
| ) | ||
|
|
||
| override def waitForTasks(): Future[TaskExecutorResult] = { | ||
| given ExecutionContext = ExecutionContext.global | ||
|
|
||
| Future | ||
| .sequence(activeTasks.asScala) | ||
| .map { _ => | ||
| activeTasks.clear() | ||
| currentResult.toTaskExecutorResult | ||
| }(ExecutionContext.global) | ||
| } | ||
| } | ||
|
|
||
| class SequentialTestTaskExecutor(logger: Logger) extends TestTaskExecutor { | ||
| private val currentResult = TaskExecutorResult.Mutable.empty | ||
|
|
||
| override def submitTask(task: Task): Unit = { | ||
| val reporter = new TestReporter(logger) | ||
|
|
||
| reporter.preTask(task) | ||
|
|
||
| val additionalTasks = task.execute( | ||
| event => { | ||
| currentResult.taskEvents.getOrElseUpdate(task, new ConcurrentLinkedQueue()).add(event) | ||
|
|
||
| event.status match { | ||
| case Status.Failure | Status.Error => currentResult.failures.add(task.taskDef.fullyQualifiedName) | ||
| case _ => | ||
| } | ||
| }, | ||
| Array(new PrefixedTestingLogger(logger, " ")), | ||
| ) | ||
|
|
||
| additionalTasks.foreach(submitTask) | ||
| reporter.postTask() | ||
| } | ||
|
|
||
| override def waitForTasks(): Future[TaskExecutorResult] = { | ||
| val result = currentResult.toTaskExecutorResult | ||
|
|
||
| currentResult.clear() | ||
|
|
||
| Future.successful(result) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.