gpt4 book ai didi

scala - Http4s 客户端/资源的集成测试

转载 作者:行者123 更新时间:2023-12-04 15:42:31 24 4
gpt4 key购买 nike

我正在实现 Vault client在 Scala 中使用 Http4s 客户端。

我现在开始编写集成测试。到目前为止我有这个:

abstract class Utils extends AsyncWordSpec with Matchers {
implicit override def executionContext = ExecutionContext.global
implicit val timer: Timer[IO] = IO.timer(executionContext)
implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)

val vaultUri = Uri.unsafeFromString(Properties.envOrElse("VAULT_ADDR", throw IllegalArgumentException))
val vaultToken = Properties.envOrElse("VAULT_TOKEN", throw IllegalArgumentException)
val clientResource = BlazeClientBuilder[IO](global)
.withCheckEndpointAuthentication(false)
.resource

def usingClient[T](f: VaultClient[IO] => IO[Assertion]): Future[Assertion] = {
clientResource.use { implicit client =>
f(new VaultClient[IO](vaultUri, vaultToken))
}.unsafeToFuture()
}
}

然后我的测试看起来像这样(只显示一个测试):

class SysSpec extends Utils {
"The auth endpoint" should {
"successfully mount an authentication method" in {
usingClient { client =>
for {
result <- client.sys.auth.create("test", AuthMethod(
"approle", "some nice description", config = TuneOptions(defaultLeaseTtl = 60.minutes)
))
} yield result should be (())
}
}
}
}

这种方法有效,但感觉不对。对于每个测试,我都会打开连接 (clientResource.use) 并重新创建 VaultClient

有没有办法让我为 SysSpec 中的所有测试重用相同的连接和客户端。

请注意这些是集成测试而不是单元测试。

最佳答案

这是我能想到的最好的办法。

abstract class Utils extends AsyncWordSpec with Matchers with BeforeAndAfterAll {
implicit override def executionContext = ExecutionContext.global
implicit val timer: Timer[IO] = IO.timer(executionContext)
implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)

val (httpClient, finalizer) = BlazeClientBuilder[IO](global)
.withCheckEndpointAuthentication(false)
.resource.allocated.unsafeRunSync()
override protected def afterAll(): Unit = finalizer.unsafeRunSync()

private implicit val c = httpClient
val client = new VaultClient[IO](uri"http://[::1]:8200", "the root token fetched from somewhere")
}

然后测试直接使用客户端:

class SysSpec extends Utils {
"The auth endpoint" should {
"successfully mount an authentication method" in {
client.sys.auth.create("test", AuthMethod(
"approle", "some nice description",
config = TuneOptions(defaultLeaseTtl = 60.minutes))
).map(_ shouldBe ()).unsafeToFuture()
}
}
}

我使用这种方法的两个主要问题是代码中的两个 unsafeRunSync。第一个是创建客户端,第二个是清理资源。然而,与重复创建和销毁客户端相比,这是一种更好的方法。

我也不想使用 unsafeToFuture 但这需要 ScalaTest 直接支持 Cats-Effect。

关于scala - Http4s 客户端/资源的集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57324515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com