Rename parameter names from 'duration' -> 'interval' just like our ActorSystem.schedule() names the parameter (#2005)

This commit is contained in:
Chris Stewart 2020-09-11 11:10:59 -05:00 committed by GitHub
parent 522d80ed2f
commit 39ef35b9ee
14 changed files with 61 additions and 64 deletions

View File

@ -113,13 +113,13 @@ object EclairBench extends App with EclairRpcTestUtil {
_ <- sendPayments(network, PaymentAmount, PaymentCount)
_ <- TestAsyncUtil.retryUntilSatisfied(
condition = paymentLog.size() == NetworkSize * PaymentCount,
duration = 1.second,
interval = 1.second,
maxTries = 100)
_ <-
TestAsyncUtil
.retryUntilSatisfied(
condition = EclairBenchUtil.paymentLogValues().forall(_.completed),
duration = 1.second,
interval = 1.second,
maxTries = 100)
.recover { case ex: Throwable => ex.printStackTrace() }
_ = println("\nDone!")

View File

@ -40,7 +40,7 @@ class TestRpcUtilTest extends BitcoindRpcTest {
it should "complete immediately if condition is true" in {
AsyncUtil
.retryUntilSatisfiedF(conditionF = () => Future.successful(true),
duration = 0.millis)
interval = 0.millis)
.map { _ =>
succeed
}
@ -50,7 +50,7 @@ class TestRpcUtilTest extends BitcoindRpcTest {
recoverToSucceededIf[RpcRetryException] {
AsyncUtil.retryUntilSatisfiedF(conditionF =
() => Future.successful(false),
duration = 0.millis)
interval = 0.millis)
}
}
@ -71,7 +71,7 @@ class TestRpcUtilTest extends BitcoindRpcTest {
it should "timeout if condition is false" in {
recoverToSucceededIf[RpcRetryException] {
AsyncUtil
.awaitCondition(condition = () => false, duration = 0.millis)
.awaitCondition(condition = () => false, interval = 0.millis)
.map(_ => succeed)
}
}

View File

@ -142,7 +142,7 @@ trait Client extends BitcoinSLogger with StartStopAsync[BitcoindRpcClient] {
for {
_ <- awaitCookie(instance.authCredentials)
_ <- AsyncUtil.retryUntilSatisfiedF(() => isStartedF,
duration = 1.seconds,
interval = 1.seconds,
maxTries = 60)
} yield this.asInstanceOf[BitcoindRpcClient]
}

View File

@ -1,13 +1,12 @@
package org.bitcoins.rpc.util
import akka.actor.ActorSystem
import org.bitcoins.core.util.BitcoinSLogger
import org.bitcoins.core.util.{BitcoinSLogger, FutureUtil}
import scala.concurrent._
import scala.concurrent.duration.{DurationInt, FiniteDuration}
abstract class AsyncUtil extends BitcoinSLogger {
import AsyncUtil.DEFAULT_INTERNVAL
import AsyncUtil.DEFAULT_MAX_TRIES
private def retryRunnable(
@ -23,11 +22,11 @@ abstract class AsyncUtil extends BitcoinSLogger {
def retryUntilSatisfied(
condition: => Boolean,
duration: FiniteDuration = DEFAULT_INTERNVAL,
interval: FiniteDuration = AsyncUtil.DEFAULT_INTERVAL,
maxTries: Int = DEFAULT_MAX_TRIES)(implicit
system: ActorSystem): Future[Unit] = {
val f = () => Future.successful(condition)
retryUntilSatisfiedF(f, duration, maxTries)
retryUntilSatisfiedF(f, interval, maxTries)
}
/**
@ -40,14 +39,14 @@ abstract class AsyncUtil extends BitcoinSLogger {
*/
def retryUntilSatisfiedF(
conditionF: () => Future[Boolean],
duration: FiniteDuration = DEFAULT_INTERNVAL,
interval: FiniteDuration = AsyncUtil.DEFAULT_INTERVAL,
maxTries: Int = DEFAULT_MAX_TRIES)(implicit
system: ActorSystem): Future[Unit] = {
val stackTrace: Array[StackTraceElement] =
Thread.currentThread().getStackTrace
retryUntilSatisfiedWithCounter(conditionF = conditionF,
duration = duration,
interval = interval,
maxTries = maxTries,
stackTrace = stackTrace)
}
@ -79,7 +78,7 @@ abstract class AsyncUtil extends BitcoinSLogger {
// Has a different name so that default values are permitted
protected def retryUntilSatisfiedWithCounter(
conditionF: () => Future[Boolean],
duration: FiniteDuration,
interval: FiniteDuration,
counter: Int = 0,
maxTries: Int,
stackTrace: Array[StackTraceElement])(implicit
@ -89,26 +88,25 @@ abstract class AsyncUtil extends BitcoinSLogger {
conditionF().flatMap { condition =>
if (condition) {
Future.successful(())
FutureUtil.unit
} else if (counter == maxTries) {
Future.failed(
RpcRetryException(
s"Condition timed out after $maxTries attempts with $duration waiting periods",
stackTrace))
Future.failed(RpcRetryException(
s"Condition timed out after $maxTries attempts with interval=$interval waiting periods",
stackTrace))
} else {
val p = Promise[Boolean]()
val runnable = retryRunnable(condition, p)
system.scheduler.scheduleOnce(duration, runnable)
system.scheduler.scheduleOnce(delay = interval, runnable = runnable)
p.future.flatMap {
case true => Future.successful(())
case true => FutureUtil.unit
case false =>
retryUntilSatisfiedWithCounter(conditionF,
duration,
counter + 1,
maxTries,
stackTrace)
retryUntilSatisfiedWithCounter(conditionF = conditionF,
interval = interval,
counter = counter + 1,
maxTries = maxTries,
stackTrace = stackTrace)
}
}
}
@ -124,7 +122,7 @@ abstract class AsyncUtil extends BitcoinSLogger {
*/
def awaitCondition(
condition: () => Boolean,
duration: FiniteDuration = DEFAULT_INTERNVAL,
interval: FiniteDuration = AsyncUtil.DEFAULT_INTERVAL,
maxTries: Int = DEFAULT_MAX_TRIES)(implicit
system: ActorSystem): Future[Unit] = {
@ -134,17 +132,17 @@ abstract class AsyncUtil extends BitcoinSLogger {
val conditionF: () => Future[Boolean] = () =>
Future.successful(conditionDef)
awaitConditionF(conditionF, duration, maxTries)
awaitConditionF(conditionF, interval, maxTries)
}
def awaitConditionF(
conditionF: () => Future[Boolean],
duration: FiniteDuration = DEFAULT_INTERNVAL,
interval: FiniteDuration = AsyncUtil.DEFAULT_INTERVAL,
maxTries: Int = DEFAULT_MAX_TRIES)(implicit
system: ActorSystem): Future[Unit] = {
retryUntilSatisfiedF(conditionF = conditionF,
duration = duration,
interval = interval,
maxTries = maxTries)
}
@ -155,7 +153,7 @@ object AsyncUtil extends AsyncUtil {
/**
* The default interval between async attempts
*/
private[bitcoins] val DEFAULT_INTERNVAL: FiniteDuration = 100.milliseconds
private[bitcoins] val DEFAULT_INTERVAL: FiniteDuration = 100.milliseconds
/**
* The default number of async attempts before timing out

View File

@ -188,7 +188,7 @@ class EclairRpcClientTest extends BitcoinSAsyncTest {
}
AsyncUtil
.awaitConditionF(hasRoute, duration = 1.second, maxTries = 60)
.awaitConditionF(hasRoute, interval = 1.second, maxTries = 60)
.map(_ => succeed)
}
@ -207,7 +207,7 @@ class EclairRpcClientTest extends BitcoinSAsyncTest {
}
AsyncUtil
.awaitConditionF(hasRoute, duration = 1.second, maxTries = 60)
.awaitConditionF(hasRoute, interval = 1.second, maxTries = 60)
.map(_ => succeed)
}
@ -228,7 +228,7 @@ class EclairRpcClientTest extends BitcoinSAsyncTest {
.awaitUntilIncomingPaymentStatus[IncomingPaymentStatus.Received](
client4,
invoice.lnTags.paymentHash.hash,
duration = 1.second)
interval = 1.second)
_ <- EclairRpcTestUtil.awaitUntilPaymentSucceeded(client1,
paymentId,
@ -417,13 +417,13 @@ class EclairRpcClientTest extends BitcoinSAsyncTest {
}
_ <- TestAsyncUtil.retryUntilSatisfiedF(conditionF =
() => eclair.isStarted(),
duration = 1.second,
interval = 1.second,
maxTries = 60)
_ = EclairRpcTestUtil.shutdown(eclair)
_ <-
TestAsyncUtil.retryUntilSatisfiedF(conditionF =
() => eclair.isStarted().map(!_),
duration = 1.second,
interval = 1.second,
maxTries = 60)
} yield succeed
}
@ -1127,7 +1127,7 @@ class EclairRpcClientTest extends BitcoinSAsyncTest {
AsyncUtil
.retryUntilSatisfiedF((() => checkUpdates()),
duration = 1.second,
interval = 1.second,
maxTries = 60)
.transform(_ => succeed, ex => ex)
}

View File

@ -778,7 +778,7 @@ class EclairRpcClient(
val started: Future[EclairRpcClient] = {
for {
_ <- AsyncUtil.retryUntilSatisfiedF(() => isStarted,
duration = 1.seconds,
interval = 1.seconds,
maxTries = 60)
} yield this
}

View File

@ -72,7 +72,7 @@ class BroadcastTransactionTest extends NodeUnitTest {
txOpt.isDefined,
"Transaction was not added to BroadcastableTransaction database")
_ <- TestAsyncUtil.awaitConditionF(() => hasSeenTx(tx),
duration = 1.second,
interval = 1.second,
maxTries = 25)
} yield ()
}

View File

@ -131,7 +131,7 @@ class NeutrinoNodeTest extends NodeUnitTest {
.chainApiFromDb()
.flatMap(_.getBlockCount.map(_ == ExpectedCount))
},
duration = 1000.millis)
interval = 1000.millis)
def hasFilterHeadersF =
RpcUtil.retryUntilSatisfiedF(
@ -140,7 +140,7 @@ class NeutrinoNodeTest extends NodeUnitTest {
.chainApiFromDb()
.flatMap(_.getFilterHeaderCount.map(_ == ExpectedCount))
},
duration = 1000.millis)
interval = 1000.millis)
def hasFiltersF =
RpcUtil.retryUntilSatisfiedF(
@ -149,7 +149,7 @@ class NeutrinoNodeTest extends NodeUnitTest {
.chainApiFromDb()
.flatMap(_.getFilterCount.map(_ == ExpectedCount))
},
duration = 1000.millis)
interval = 1000.millis)
for {
_ <- hasBlocksF

View File

@ -90,7 +90,7 @@ class SpvNodeTest extends NodeUnitTest {
val has6BlocksF = RpcUtil.retryUntilSatisfiedF(
conditionF =
() => spvNode.chainApiFromDb().flatMap(_.getBlockCount.map(_ == 6)),
duration = 250.millis)
interval = 250.millis)
has6BlocksF.map { _ =>
val isCanceled = cancel.cancel()

View File

@ -183,7 +183,7 @@ class P2PClientTest extends BitcoindRpcTest with CachedBitcoinSAppConfig {
_ = p2pClient.actor ! Tcp.Abort
isDisconnected <-
TestAsyncUtil.retryUntilSatisfiedF(p2pClient.isDisconnected,
duration = 1.seconds)
interval = 1.seconds)
} yield isDisconnected
isDisconnectedF.map { _ =>

View File

@ -36,7 +36,7 @@ class PeerMessageHandlerTest extends NodeUnitTest {
val isConnectedF = TestAsyncUtil.retryUntilSatisfiedF(
() => p2pClientF.flatMap(_.isConnected),
duration = 500.millis
interval = 500.millis
)
val isInitF = isConnectedF.flatMap { _ =>

View File

@ -125,7 +125,7 @@ trait Node extends NodeApi with ChainQueryApi with P2PLogger {
val isInitializedF = for {
_ <- peerMsgSenderF.map(_.connect())
_ <- AsyncUtil.retryUntilSatisfiedF(() => isInitialized,
duration = 250.millis)
interval = 250.millis)
} yield ()
isInitializedF.failed.foreach(err =>

View File

@ -273,7 +273,7 @@ trait EclairRpcTestUtil extends BitcoinSLogger {
}
TestAsyncUtil.retryUntilSatisfiedF(conditionF = () => isState(),
duration = 1.seconds)
interval = 1.seconds)
}
def awaitUntilPaymentSucceeded(
@ -305,7 +305,7 @@ trait EclairRpcTestUtil extends BitcoinSLogger {
private def awaitUntilOutgoingPaymentStatus[T <: OutgoingPaymentStatus](
client: EclairApi,
paymentId: PaymentId,
duration: FiniteDuration,
interval: FiniteDuration,
maxTries: Int,
failFast: Boolean)(implicit
system: ActorSystem,
@ -341,14 +341,14 @@ trait EclairRpcTestUtil extends BitcoinSLogger {
}
TestAsyncUtil.retryUntilSatisfiedF(conditionF = () => isInState(),
duration = duration,
interval = interval,
maxTries = maxTries)
}
def awaitUntilIncomingPaymentStatus[T <: IncomingPaymentStatus](
client: EclairApi,
paymentHash: Sha256Digest,
duration: FiniteDuration = 1.second,
interval: FiniteDuration = 1.second,
maxTries: Int = 60)(implicit
system: ActorSystem,
tag: ClassTag[T]): Future[Unit] = {
@ -374,7 +374,7 @@ trait EclairRpcTestUtil extends BitcoinSLogger {
}
TestAsyncUtil.retryUntilSatisfiedF(conditionF = () => isInState(),
duration = duration,
interval = interval,
maxTries = maxTries)
}
@ -609,7 +609,7 @@ trait EclairRpcTestUtil extends BitcoinSLogger {
logger.debug(s"Awaiting connection between clients")
val connected = TestRpcUtil.retryUntilSatisfiedF(conditionF =
() => isConnected(),
duration = 1.second)
interval = 1.second)
connected.map(_ => logger.debug(s"Successfully connected two clients"))
@ -751,7 +751,7 @@ trait EclairRpcTestUtil extends BitcoinSLogger {
TestAsyncUtil.retryUntilSatisfiedF(conditionF =
() => clientInSync(eclair, bitcoind),
duration = 1.seconds)
interval = 1.seconds)
}
/** Shuts down an eclair daemon and the bitcoind daemon it is associated with

View File

@ -55,7 +55,6 @@ import scala.util._
//noinspection AccessorLikeMethodIsEmptyParen
trait BitcoindRpcTestUtil extends BitcoinSLogger {
import BitcoindRpcTestUtil.DEFAULT_LONG_DURATION
type RpcClientAccum =
mutable.Builder[BitcoindRpcClient, Vector[BitcoindRpcClient]]
@ -344,7 +343,7 @@ trait BitcoindRpcTestUtil extends BitcoinSLogger {
def awaitConnection(
from: BitcoindRpcClient,
to: BitcoindRpcClient,
duration: FiniteDuration = 100.milliseconds,
interval: FiniteDuration = 100.milliseconds,
maxTries: Int = 50)(implicit system: ActorSystem): Future[Unit] = {
import system.dispatcher
@ -357,7 +356,7 @@ trait BitcoindRpcTestUtil extends BitcoinSLogger {
}
AsyncUtil.retryUntilSatisfiedF(conditionF = isConnected,
duration = duration,
interval = interval,
maxTries = maxTries)
}
@ -442,7 +441,7 @@ trait BitcoindRpcTestUtil extends BitcoinSLogger {
def awaitSynced(
client1: BitcoindRpcClient,
client2: BitcoindRpcClient,
duration: FiniteDuration = DEFAULT_LONG_DURATION,
interval: FiniteDuration = BitcoindRpcTestUtil.DEFAULT_LONG_INTERVAL,
maxTries: Int = 50)(implicit system: ActorSystem): Future[Unit] = {
implicit val ec: ExecutionContextExecutor = system.dispatcher
@ -455,14 +454,14 @@ trait BitcoindRpcTestUtil extends BitcoinSLogger {
}
AsyncUtil.retryUntilSatisfiedF(conditionF = () => isSynced(),
duration = duration,
interval = interval,
maxTries = maxTries)
}
def awaitSameBlockHeight(
client1: BitcoindRpcClient,
client2: BitcoindRpcClient,
duration: FiniteDuration = DEFAULT_LONG_DURATION,
interval: FiniteDuration = BitcoindRpcTestUtil.DEFAULT_LONG_INTERVAL,
maxTries: Int = 50)(implicit system: ActorSystem): Future[Unit] = {
import system.dispatcher
@ -475,14 +474,14 @@ trait BitcoindRpcTestUtil extends BitcoinSLogger {
}
AsyncUtil.retryUntilSatisfiedF(conditionF = () => isSameBlockHeight(),
duration = duration,
interval = interval,
maxTries = maxTries)
}
def awaitDisconnected(
from: BitcoindRpcClient,
to: BitcoindRpcClient,
duration: FiniteDuration = 100.milliseconds,
interval: FiniteDuration = 100.milliseconds,
maxTries: Int = 50)(implicit system: ActorSystem): Future[Unit] = {
import system.dispatcher
@ -500,7 +499,7 @@ trait BitcoindRpcTestUtil extends BitcoinSLogger {
}
AsyncUtil.retryUntilSatisfiedF(conditionF = () => isDisconnected(),
duration = duration,
interval = interval,
maxTries = maxTries)
}
@ -559,7 +558,7 @@ trait BitcoindRpcTestUtil extends BitcoinSLogger {
val futures = pairs.map {
case (first, second) =>
BitcoindRpcTestUtil
.awaitConnection(first, second, duration = 10.second)
.awaitConnection(first, second, interval = 10.second)
}
Future.sequence(futures)
}
@ -984,7 +983,7 @@ trait BitcoindRpcTestUtil extends BitcoinSLogger {
val blocksGeneratedF = generatedF.flatMap { _ =>
AsyncUtil.retryUntilSatisfiedF(
() => areBlocksGenerated(),
duration = DEFAULT_LONG_DURATION
interval = BitcoindRpcTestUtil.DEFAULT_LONG_INTERVAL
)
}
@ -999,7 +998,7 @@ object BitcoindRpcTestUtil extends BitcoindRpcTestUtil {
/**
* Used for long running async tasks
*/
private val DEFAULT_LONG_DURATION = {
private val DEFAULT_LONG_INTERVAL = {
if (EnvUtil.isMac && EnvUtil.isCI) 10.seconds
else 3.seconds
}