Skip to content

Instantly share code, notes, and snippets.

@jsuereth
Created August 1, 2014 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsuereth/cec11c14412e365e90bf to your computer and use it in GitHub Desktop.
Save jsuereth/cec11c14412e365e90bf to your computer and use it in GitHub Desktop.
Test concurrency fun
import sbt._
import Keys._
object TestBuild extends Build {
lazy val root = Project(id = "root", base = file(".")).aggregate(project1, project2).settings(defaultSettings:_*).settings(
concurrentRestrictions in Global := Seq(
Tags.limit(Tags.ForkedTestGroup, 1)
)
)
val okToRun = new java.util.concurrent.atomic.AtomicBoolean(true)
lazy val project1 = Project("project1", file("project1")).settings(defaultSettings:_*).settings(
libraryDependencies += "org.specs2" %% "specs2" % "2.1.1" % "test"
)
lazy val project2 = Project("project2", file("project2")).settings(defaultSettings:_*).settings(
libraryDependencies += "org.specs2" %% "specs2" % "2.1.1" % "test"
)
lazy val defaultSettings = Seq(
parallelExecution in Test := false,
testOptions in Test += Tests.Setup( () => startupDb(name.value) ),
testOptions in Test += Tests.Cleanup( () => shutdownDb(name.value) )
)
def startupDb(name: String) = {
System.err.println(s"startup ${name}, thread name = " + Thread.currentThread().getName)
assert(okToRun.compareAndSet(true, false), "Failed to safely grab the ok to run lock!")
}
def shutdownDb(name: String) = {
System.err.println(s"shutdown ${name}, thread name = " + Thread.currentThread().getName)
assert(okToRun.compareAndSet(false, true), "Failed to safely release the ok to run lock!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment