简体   繁体   中英

Cats OptionT future not completing

I'm having an issue where my future (wrapped with OptionT) doesn't complete.

When I explicitly await the result withoutn OptionT, I can see the correct output (See logs below Some(2,2)). But in my for comprehension, we never see the "Ids: " logs. and in the Await OptionT println, we never see the output.

Obviously without the Duration.Inf, the futures timeout.

The functionality here is that if the Brand and Country for the Ids provided do not exist, we should fail. If they exist, we can continue to create the link between them.

  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)
  }

The log output here is

[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)

then it waits forever (Duration.Inf)

这里的问题与Cats或OptionT无关,而与scalatest提供的默认ExecutionContext相关,后者正在执行代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM