Change TaskRunner to throw an exception instead of using Either

This commit is contained in:
Mark Harrah 2009-08-16 20:33:46 -04:00
parent 65fc0e0453
commit 56b047035a
6 changed files with 15 additions and 17 deletions

2
cache/Cache.scala vendored
View File

@ -9,7 +9,7 @@ trait Cache[I,O]
}
trait SBinaryFormats extends CollectionTypes with JavaFormats with NotNull
{
//TODO: add basic types minus FileFormat
//TODO: add basic types from SBinary minus FileFormat
}
object Cache extends BasicCacheImplicits with SBinaryFormats with HListCacheImplicits
{

View File

@ -2,16 +2,17 @@ package xsbt
object TaskRunner
{
def apply[T](node: Task[T]): Either[ List[WorkFailure[Task[_]]] , T ] = apply(node, Runtime.getRuntime.availableProcessors)
def apply[T](node: Task[T]): T = apply(node, Runtime.getRuntime.availableProcessors)
/** Executes work for nodes in a directed acyclic graph with root node `node`.
* The maximum number of tasks to execute simultaneously is `maximumTasks`. */
def apply[T](node: Task[T], maximumTasks: Int): Either[ List[WorkFailure[Task[_]]] , T ] =
def apply[T](node: Task[T], maximumTasks: Int): T =
{
require(maximumTasks > 0)
val compute = new Compute[Work.Job, Result] { def apply[A](w: Work.Job[A]) = w.apply }
val strategy = new SimpleStrategy[Work[_,_]]
val scheduler = new TaskScheduler(node, strategy, new BasicTaskListener)
val distributor = new Distributor[ Either[ List[WorkFailure[Task[_]]], T ] , Work.Job, Result](scheduler, compute, maximumTasks)
distributor.run()
distributor.run().fold(failures => throw new TasksFailed(failures), identity[T])
}
}
}
final case class TasksFailed(failures: List[WorkFailure[Task[_]]]) extends RuntimeException(failures.length + " failed")

View File

@ -41,7 +41,8 @@ object TaskRunnerCircularTest extends Properties("TaskRunner Circular")
} named("it_" + i)
it
}
TaskRunner(top, workers).fold(_.exists(_.exception.isInstanceOf[CircularDependency]), x => false)
try { TaskRunner(top, workers); false }
catch { case TasksFailed(failures) => failures.exists(_.exception.isInstanceOf[CircularDependency]) }
}
final def checkRootComplete(intermediate: Int, workers: Int) =
{

View File

@ -11,7 +11,7 @@ object TaskRunnerCallTest extends Properties("TaskRunner Call")
("Workers: " + workers) |: ("i: " + i) |: ("fib(i): " + f) |:
{
def result = TaskRunner( fibTask(i), workers)
("Result: " + result) |: (result == Right(f))
checkResult(result, f)
}
}
)

View File

@ -12,7 +12,7 @@ object TaskRunnerSortTest extends Properties("TaskRunnerSort")
("Workers: " + workers) |: ("Array: " + a.toList) |:
{
def result = TaskRunner( sort(a.toArray), workers)
checkResult(result.right.map(_.toList), sorted.toList)
checkResult(result.toList, sorted.toList)
}
}
)

View File

@ -4,23 +4,19 @@ import org.scalacheck.Prop._
object checkResult
{
def apply[T](run: => Either[List[WorkFailure[Task[_]]],T], expected: T) =
def apply[T](run: => T, expected: T) =
{
("Expected: " + expected) |:
(try
{
val actual = run
("Actual: " + actual) |:
(actual match
{
case Right(a) => a == expected
case Left(failures) =>
failures.foreach(f => f.exception.printStackTrace)
false
})
("Actual: " + actual) |: (actual == expected)
}
catch
{
case TasksFailed(failures) =>
failures.foreach(f => f.exception.printStackTrace)
"One or more tasks failed" |: false
case e =>
e.printStackTrace
"Error in framework" |: false