2024 01 31 Refactor BitcoindRpcTestUtil.{startSevers(),stopSevers()} to use Future.traverse() (#5382)

* Refactor BitcoindRpcTestUtil.startServers() to use Future.traverse()

* Refactor BitcoindRpcTestUtil.stopServers() to use Future.traverse()
This commit is contained in:
Chris Stewart 2024-01-31 15:20:36 -06:00 committed by GitHub
parent 451e019225
commit 1685c84eea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -42,12 +42,10 @@ import org.bitcoins.util.ListUtil
import java.io.File
import java.net.{InetSocketAddress, URI}
import java.nio.file.{Files, Path}
import scala.collection.immutable.Map
import scala.collection.mutable
import scala.concurrent._
import scala.concurrent.duration.{DurationInt, FiniteDuration}
import scala.util._
import scala.util.control.NonFatal
//noinspection AccessorLikeMethodIsEmptyParen
trait BitcoindRpcTestUtil extends Logging {
@ -332,7 +330,7 @@ trait BitcoindRpcTestUtil extends Logging {
def startServers(servers: Vector[BitcoindRpcClient])(implicit
ec: ExecutionContext): Future[Unit] = {
val startedServers = servers.map { server =>
val startedServersF = Future.traverse(servers) { server =>
server.start().flatMap { res =>
val createWalletF = for {
version <- server.version
@ -341,16 +339,13 @@ trait BitcoindRpcTestUtil extends Logging {
false
case V23 | V24 => true
}
_ <- res.createWallet("", descriptors = true)
_ <- res.createWallet("", descriptors = descriptors)
} yield res
createWalletF.recoverWith { case NonFatal(_) =>
Future.successful(res)
}
createWalletF
}
}
Future.sequence(startedServers).map(_ => ())
startedServersF.map(_ => ())
}
/** Stops the given servers and deletes their data directories
@ -359,7 +354,7 @@ trait BitcoindRpcTestUtil extends Logging {
system: ActorSystem): Future[Unit] = {
implicit val ec: ExecutionContextExecutor = system.getDispatcher
val serverStops = servers.map { s =>
val serverStopsF = Future.traverse(servers) { s =>
val stopF = s.stop()
stopF.onComplete {
case Failure(exception) =>
@ -372,7 +367,7 @@ trait BitcoindRpcTestUtil extends Logging {
_ <- removeDataDirectory(s)
} yield ()
}
Future.sequence(serverStops).map(_ => ())
serverStopsF.map(_ => ())
}
/** Stops the given server and deletes its data directory