Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3] Override blocking inside CatsMonadError #1992

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,30 @@
package sttp.client3.impl.cats

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

class CatsMonadErrorTest extends AsyncFreeSpec {
"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))
.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())
}
}
Loading