Skip to content

Commit

Permalink
Override blocking inside CatsMonadError
Browse files Browse the repository at this point in the history
Fix #1991
  • Loading branch information
flipp5b committed Nov 7, 2023
1 parent d986659 commit 3d50b74
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ class CatsMonadError[F[_]](implicit F: Sync[F]) extends MonadError[F] {
override def flatten[T](ffa: F[F[T]]): F[T] = F.flatten(ffa)

override def ensure[T](f: F[T], e: => F[Unit]): F[T] = F.guaranteeCase(f)(_ => e)

override def blocking[T](t: => T): F[T] = F.blocking(t)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package sttp.client3.impl.cats

import cats.effect.IO
import cats.effect.unsafe.{IORuntime, IORuntimeConfig}
import org.scalatest.freespec.AsyncFreeSpec

class CatsMonadErrorTest extends AsyncFreeSpec {
import scala.concurrent.duration._

"blocking" - {
"should shift to blocking execution context " in {
implicit val ioRuntime: IORuntime = createIORuntime(computePoolSize = 1)
val monad = new CatsMonadError[IO]

val program = monad
.blocking(Thread.sleep(100.millis.toMillis))
.background
.use(getOutcome => IO.race(getOutcome, IO.unit))

program
.flatMap(either => IO.delay(assert(either.isRight)))
.unsafeToFuture()
}
}

private def createIORuntime(computePoolSize: Int): IORuntime = {
val (compute, _) = IORuntime.createWorkStealingComputeThreadPool(computePoolSize)
val (blocking, _) = IORuntime.createDefaultBlockingExecutionContext()

IORuntime(compute, blocking, compute, () => (), IORuntimeConfig())
}
}

0 comments on commit 3d50b74

Please sign in to comment.