88package sbt
99package scriptedtest
1010
11- import scala . collection . mutable
11+ import java . util . concurrent .{ Executors , TimeUnit , TimeoutException }
1212
13+ import scala .collection .mutable
14+ import scala .concurrent .duration ._
1315import sbt .internal .scripted ._
1416
1517private [sbt] object BatchScriptRunner {
1618 type States = mutable.HashMap [StatementHandler , StatementHandler # State ]
1719}
1820
1921/** Defines an alternative script runner that allows batch execution. */
20- private [sbt] class BatchScriptRunner extends ScriptRunner {
22+ private [sbt] class BatchScriptRunner extends ScriptRunner with AutoCloseable {
2123 import BatchScriptRunner .States
24+ private [this ] val service = Executors .newCachedThreadPool()
2225
2326 /** Defines a method to run batched execution.
2427 *
@@ -40,26 +43,35 @@ private[sbt] class BatchScriptRunner extends ScriptRunner {
4043 }
4144 }
4245
46+ private val timeout = 5 .minutes
4347 def processStatement (handler : StatementHandler , statement : Statement , states : States ): Unit = {
4448 val state = states(handler).asInstanceOf [handler.State ]
45- val nextState =
46- try Right (handler(statement.command, statement.arguments, state))
47- catch { case e : Exception => Left (e) }
48- nextState match {
49- case Left (err) =>
50- if (statement.successExpected) {
51- err match {
52- case t : TestFailed =>
53- throw new TestException (statement, " Command failed: " + t.getMessage, null )
54- case _ => throw new TestException (statement, " Command failed" , err)
55- }
56- } else
57- ()
58- case Right (s) =>
59- if (statement.successExpected)
60- states(handler) = s
61- else
62- throw new TestException (statement, " Command succeeded but failure was expected" , null )
49+ val nextStateFuture = service.submit(
50+ () =>
51+ try Right (handler(statement.command, statement.arguments, state))
52+ catch { case e : Exception => Left (e) }
53+ )
54+ try {
55+ nextStateFuture.get(timeout.toMillis, TimeUnit .MILLISECONDS ) match {
56+ case Left (err) =>
57+ if (statement.successExpected) {
58+ err match {
59+ case t : TestFailed =>
60+ throw new TestException (statement, " Command failed: " + t.getMessage, null )
61+ case _ => throw new TestException (statement, " Command failed" , err)
62+ }
63+ } else
64+ ()
65+ case Right (s) =>
66+ if (statement.successExpected)
67+ states(handler) = s
68+ else
69+ throw new TestException (statement, " Command succeeded but failure was expected" , null )
70+ }
71+ } catch {
72+ case e : TimeoutException => throw new TestException (statement, " Command timed out" , e)
6373 }
6474 }
75+
76+ override def close (): Unit = service.shutdown()
6577}
0 commit comments