8000 Add timeout to scripted statements by eatkins · Pull Request #5151 · sbt/sbt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add timeout to scripted statements #5151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
package sbt
package scriptedtest

import scala.collection.mutable
import java.util.concurrent.{ Executors, TimeUnit, TimeoutException }

import scala.collection.mutable
import scala.concurrent.duration._
import sbt.internal.scripted._

private[sbt] object BatchScriptRunner {
type States = mutable.HashMap[StatementHandler, StatementHandler#State]
}

/** Defines an alternative script runner that allows batch execution. */
private[sbt] class BatchScriptRunner extends ScriptRunner {
private[sbt] class BatchScriptRunner extends ScriptRunner with AutoCloseable {
import BatchScriptRunner.States
private[this] val service = Executors.newCachedThreadPool()

/** Defines a method to run batched execution.
*
Expand All @@ -40,26 +43,35 @@ private[sbt] class BatchScriptRunner extends ScriptRunner {
}
}

private val timeout = 5.minutes
def processStatement(handler: StatementHandler, statement: Statement, states: States): Unit = {
val state = states(handler).asInstanceOf[handler.State]
val nextState =
try Right(handler(statement.command, statement.arguments, state))
catch { case e: Exception => Left(e) }
nextState match {
case Left(err) =>
if (statement.successExpected) {
err match {
case t: TestFailed =>
throw new TestException(statement, "Command failed: " + t.getMessage, null)
case _ => throw new TestException(statement, "Command failed", err)
}
} else
()
case Right(s) =>
if (statement.successExpected)
states(handler) = s
else
throw new TestException(statement, "Command succeeded but failure was expected", null)
val nextStateFuture = service.submit(
() =>
try Right(handler(statement.command, statement.arguments, state))
catch { case e: Exception => Left(e) }
)
try {
nextStateFuture.get(timeout.toMillis, TimeUnit.MILLISECONDS) match {
case Left(err) =>
if (statement.successExpected) {
err match {
case t: TestFailed =>
throw new TestException(statement, "Command failed: " + t.getMessage, null)
case _ => throw new TestException(statement, "Command failed", err)
}
} else
()
case Right(s) =>
if (statement.successExpected)
states(handler) = s
else
throw new TestException(statement, "Command succeeded but failure was expected", null)
}
} catch {
case e: TimeoutException => throw new TestException(statement, "Command timed out", e)
}
}

override def close(): Unit = service.shutdown()
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ final class ScriptedTests(
createScriptedHandlers(testDirectory, buffer, RemoteSbtCreatorKind.LauncherBased)
val runner = new BatchScriptRunner
val states = new mutable.HashMap[StatementHandler, StatementHandler#State]()
commonRunTest(label, testDirectory, prescripted, handlers, runner, states, buffer)
try commonRunTest(label, testDirectory, prescripted, handlers, runner, states, buffer)
finally runner.close()
}
runOrHandleDisabled(label, testDirectory, singleTestRunner, buffer)
}
Expand Down Expand Up @@ -362,7 +363,10 @@ final class ScriptedTests(
}

try runBatchTests
finally runner.cleanUpHandlers(seqHandlers, states)
finally {
runner.cleanUpHandlers(seqHandlers, states)
runner.close()
}
}

private def runOrHandleDisabled(
Expand Down Expand Up @@ -415,6 +419,7 @@ final class ScriptedTests(
case null | _: SocketException => log.error(s" Cause of test exception: ${t.getMessage}")
case _ => t.printStackTrace()
}
log.play()
}
if (pending) None else Some(label)
}
Expand Down
0