Http4s 的集成测试 Client/Resource

Integration tests for Http4s Client/Resource

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

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

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。