Cats OptionT未来未完成

问题描述 投票:0回答:1

我遇到了一个问题,我的未来(用OptionT包装)没有完成。

当我在没有OptionT的情况下明确等待结果时,我可以看到正确的输出(参见下面的日志Some(2,2))。但在我的理解中,我们从未看到“Ids:”日志。在Await OptionT println中,我们从未看到输出。

显然没有Duration.Inf,期货超时。

这里的功能是,如果提供的Ids的品牌和国家不存在,我们应该失败。如果它们存在,我们可以继续创建它们之间的链接。

  def save(brandId: Int, countryId: Int): Future[Option[Int]] = {
    val now = DateTime.now(DateTimeZone.UTC)
    println(s"Await: ${Await.result(getIds(brandId, countryId), Duration.Inf)}")
    println(s"Await OptionT: ${Await.result(OptionT(getIds(brandId, countryId)).map(identity).value, Duration.Inf)}")
    for {
      ids <- OptionT(getIds(brandId, countryId))
      _ = logger.info(s"Ids: $ids")
      brand = BrandCountry(None, ids._2, ids._1, now, "admin", now, "admin")
      _ = logger.info(s"Brand: $brand")
      insertStmt = BrandCountrys.returning(BrandCountrys.map(_.id)) += brand
      _ = logger.info(s"Executing SQL:\n${insertStmt.statements.mkString(";\n")}")
      result <- OptionT.liftF(db.run(insertStmt))
    } yield result
  }.value

  private def getIds(brandId: Int, countryId: Int) = {
    val ids = for {
      bId <- Brands.filter(_.id === brandId).map(_.id)
      cId <- Countries.filter(_.id === countryId).map(_.id)
    } yield (bId, cId)
    logger.info(s"Executing SQL:\n${ids.result.statements.mkString(";\n")}")
    db.run(ids.result.headOption)
  }

这里的日志输出是

[error] c.d.d.v.d.BrandCountrysDao - Executing SQL:
select x2."id", x3."id" from "configurations_v2"."brands" x2, "configurations_v2"."countries" x3 where (x2."id" = 2) and (x3."id" = 2)
Await: Some((2,2))
[error] c.d.d.v.d.BrandCountrysDao - Executing SQL:
select x2."id", x3."id" from "configurations_v2"."brands" x2, "configurations_v2"."countries" x3 where (x2."id" = 2) and (x3."id" = 2)

然后它永远等待(Duration.Inf)

scala slick future option scala-cats
1个回答
1
投票

这里的问题与Cats或OptionT无关,而是与执行代码的scalatest提供的默认ExecutionContext有关。

© www.soinside.com 2019 - 2024. All rights reserved.