2010-06-10 14:19:15 +02:00
|
|
|
package sbt
|
2009-08-16 20:29:08 +02:00
|
|
|
|
|
|
|
|
import org.scalacheck._
|
|
|
|
|
import Prop._
|
2009-11-12 00:41:39 +01:00
|
|
|
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")
|
|
|
|
|
{
|
2009-11-12 00:41:39 +01:00
|
|
|
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) =
|
|
|
|
|
{
|
2010-06-10 14:19:15 +02:00
|
|
|
val top = pure("top", intermediate)
|
2009-08-16 20:29:08 +02:00
|
|
|
def iterate(task: Task[Int]): Task[Int] =
|
2010-06-10 14:19:15 +02:00
|
|
|
task flatMap { t =>
|
2009-08-16 20:29:08 +02:00
|
|
|
if(t <= 0)
|
|
|
|
|
top
|
|
|
|
|
else
|
2010-06-10 14:19:15 +02:00
|
|
|
iterate(pure((t-1).toString, t-1) )
|
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) =
|
|
|
|
|
{
|
2010-06-10 14:19:15 +02:00
|
|
|
lazy val top = iterate(pure("bottom", intermediate), intermediate)
|
2009-08-16 20:29:08 +02:00
|
|
|
def iterate(task: Task[Int], i: Int): Task[Int] =
|
2011-08-14 16:53:37 +02:00
|
|
|
task flatMap { t =>
|
|
|
|
|
if(t <= 0)
|
|
|
|
|
top
|
|
|
|
|
else
|
|
|
|
|
iterate(pure((t-1).toString, t-1), 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
|
|
|
}
|