sbt/tasks/standard/src/test/scala/TaskRunnerCircular.scala

39 lines
1.3 KiB
Scala
Raw Normal View History

2010-06-10 14:19:15 +02:00
package sbt
2009-08-16 20:29:08 +02:00
import org.scalacheck._
import Prop._
import TaskGen._
2010-06-10 14:19:15 +02:00
import Task._
2009-08-16 20:29:08 +02:00
object TaskRunnerCircularTest extends Properties("TaskRunner Circular")
{
property("Catches circular references") = forAll(MaxTasksGen, MaxWorkersGen) { checkCircularReferences _ }
property("Allows references to completed tasks") = forAllNoShrink(MaxTasksGen, MaxWorkersGen) { allowedReference _ }
2009-08-16 20:29:08 +02:00
final def allowedReference(intermediate: Int, workers: Int) =
{
val top = task(intermediate).named("top")
def iterate(tk: Task[Int]): Task[Int] =
tk flatMap { t =>
2009-08-16 20:29:08 +02:00
if(t <= 0)
top
else
iterate(task(t-1).named((t-1).toString) )
2009-08-16 20:29:08 +02:00
}
2010-06-10 14:19:15 +02:00
try { checkResult(tryRun(iterate(top), true, workers), intermediate) }
catch { case i: Incomplete if cyclic(i) => ("Unexpected cyclic exception: " + i) |: false }
2009-08-16 20:29:08 +02:00
}
final def checkCircularReferences(intermediate: Int, workers: Int) =
{
lazy val top = iterate(task(intermediate).named("bottom"), intermediate)
def iterate(tk: Task[Int], i: Int): Task[Int] =
tk flatMap { t =>
if(t <= 0)
top
else
iterate(task(t-1).named((t-1).toString), i-1)
}
2010-06-10 14:19:15 +02:00
try { tryRun(top, true, workers); false }
catch { case i: Incomplete => cyclic(i) }
2009-08-16 20:29:08 +02:00
}
2010-06-10 14:19:15 +02:00
def cyclic(i: Incomplete) = Incomplete.allExceptions(i).exists(_.isInstanceOf[Execute[Task]#CyclicException[_]])
2009-08-16 20:29:08 +02:00
}