mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2024-11-19 09:52:09 +01:00
transformRetryToTestFailure in scala 2.11.12 (#346)
* Made transformRetryToTestFailure compile in scala 2.11.12 * Fixed RpcUtilTest * Replaced match with if/else because match wasn't working
This commit is contained in:
parent
7633bf6179
commit
abc6f55a08
@ -4,8 +4,8 @@ import java.io.File
|
||||
|
||||
import akka.actor.ActorSystem
|
||||
import akka.stream.ActorMaterializer
|
||||
import org.bitcoins.rpc.util.RpcUtil.RpcRetryException
|
||||
import org.bitcoins.rpc.client.BitcoindRpcClient
|
||||
import org.scalatest.exceptions.TestFailedException
|
||||
import org.scalatest.{AsyncFlatSpec, BeforeAndAfterAll}
|
||||
|
||||
import scala.concurrent.duration.DurationInt
|
||||
@ -45,7 +45,7 @@ class RpcUtilTest extends AsyncFlatSpec with BeforeAndAfterAll {
|
||||
}
|
||||
|
||||
it should "fail if condition is false" in {
|
||||
recoverToSucceededIf[RpcRetryException] {
|
||||
recoverToSucceededIf[TestFailedException] {
|
||||
RpcUtil.retryUntilSatisfiedF(conditionF = () => Future.successful(false),
|
||||
duration = 0.millis)
|
||||
}
|
||||
@ -60,7 +60,7 @@ class RpcUtilTest extends AsyncFlatSpec with BeforeAndAfterAll {
|
||||
|
||||
it should "fail if there is a delay and duration is zero" in {
|
||||
val boolLater = trueLater(delay = 250)
|
||||
recoverToSucceededIf[RpcRetryException] {
|
||||
recoverToSucceededIf[TestFailedException] {
|
||||
RpcUtil.retryUntilSatisfiedF(boolLaterDoneAndTrue(boolLater),
|
||||
duration = 0.millis)
|
||||
}
|
||||
@ -72,7 +72,7 @@ class RpcUtilTest extends AsyncFlatSpec with BeforeAndAfterAll {
|
||||
}
|
||||
|
||||
it should "timeout if condition is false" in {
|
||||
assertThrows[RpcRetryException] {
|
||||
assertThrows[TestFailedException] {
|
||||
RpcUtil.awaitCondition(condition = () => false, duration = 0.millis)
|
||||
}
|
||||
}
|
||||
@ -87,7 +87,7 @@ class RpcUtilTest extends AsyncFlatSpec with BeforeAndAfterAll {
|
||||
|
||||
it should "timeout if there is a delay and duration is zero" in {
|
||||
val boolLater = trueLater(delay = 250)
|
||||
assertThrows[RpcRetryException] {
|
||||
assertThrows[TestFailedException] {
|
||||
RpcUtil.awaitConditionF(boolLaterDoneAndTrue(boolLater),
|
||||
duration = 0.millis)
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import org.scalatest.exceptions.{StackDepthException, TestFailedException}
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
import scala.concurrent.duration.FiniteDuration
|
||||
import scala.util.{Failure, Success}
|
||||
|
||||
abstract class AsyncUtil extends org.bitcoins.rpc.util.AsyncUtil {
|
||||
override protected def retryUntilSatisfiedWithCounter(
|
||||
@ -34,9 +33,10 @@ object AsyncUtil extends AsyncUtil {
|
||||
* conveniently mention the line that called the AsyncUtil method.
|
||||
*/
|
||||
def transformRetryToTestFailure[T](fut: Future[T])(implicit ec: ExecutionContext): Future[T] = {
|
||||
fut.transform {
|
||||
case Failure(RpcRetryException(message, caller)) =>
|
||||
val relevantStackTrace = caller.tail
|
||||
def transformRetry(err: Throwable): Throwable = {
|
||||
if (err.isInstanceOf[RpcRetryException]) {
|
||||
val retryErr = err.asInstanceOf[RpcRetryException]
|
||||
val relevantStackTrace = retryErr.caller.tail
|
||||
.dropWhile(_.getFileName == "AsyncUtil.scala")
|
||||
.takeWhile(!_.getFileName.contains("TestSuite"))
|
||||
val stackElement = relevantStackTrace.head
|
||||
@ -44,13 +44,16 @@ object AsyncUtil extends AsyncUtil {
|
||||
val path = stackElement.getClassName
|
||||
val line = stackElement.getLineNumber
|
||||
val pos = org.scalactic.source.Position(file, path, line)
|
||||
val err = new TestFailedException({ _: StackDepthException =>
|
||||
Some(message)
|
||||
val newErr = new TestFailedException({ _: StackDepthException =>
|
||||
Some(retryErr.message)
|
||||
}, None, pos)
|
||||
err.setStackTrace(relevantStackTrace)
|
||||
Failure(err)
|
||||
case Failure(err) => Failure(err)
|
||||
case Success(elem) => Success(elem)
|
||||
newErr.setStackTrace(relevantStackTrace)
|
||||
newErr
|
||||
} else {
|
||||
err
|
||||
}
|
||||
}
|
||||
|
||||
fut.transform({ elem: T => elem }, transformRetry)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user