Created
March 3, 2022 15:46
-
-
Save kamilkloch/0c67e94f33b7a0e23b4ff543f2abb1dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[2022-03-03 16:37:09,171] INFO [io-compute-1] c.s.r.AsyncTests:24 - internalLegacyAPI begin | |
[2022-03-03 16:37:09,368] INFO [io-compute-2] c.s.r.AsyncTests:45 - cancelEff: finished | |
[2022-03-03 16:37:09,379] INFO [io-compute-2] c.s.r.AsyncTests:51 - nestedAsync: onAsyncCancel | |
[2022-03-03 16:37:09,380] INFO [io-compute-2] c.s.r.AsyncTests:32 - AutoCloseable.close() | |
[2022-03-03 16:37:09,796] INFO [scala-execution-context-global-25] c.s.r.AsyncTests:27 - internalLegacyAPI end | |
[2022-03-03 16:37:11,382] INFO [io-compute-4] c.s.r.AsyncTests:59 - ****************************** | |
[2022-03-03 16:37:11,386] INFO [io-compute-6] c.s.r.AsyncTests:15 - legacyAPI begin | |
[2022-03-03 16:37:11,387] INFO [io-compute-6] c.s.r.AsyncTests:24 - internalLegacyAPI begin | |
[2022-03-03 16:37:11,584] INFO [io-compute-7] c.s.r.AsyncTests:45 - cancelEff: finished | |
[2022-03-03 16:37:11,887] INFO [scala-execution-context-global-25] c.s.r.AsyncTests:18 - legacyAPI end | |
[2022-03-03 16:37:11,988] INFO [scala-execution-context-global-26] c.s.r.AsyncTests:27 - internalLegacyAPI end | |
[2022-03-03 16:37:11,991] INFO [io-compute-3] c.s.r.AsyncTests:55 - nestedAsync: completed: 6 | |
[2022-03-03 16:37:11,996] INFO [io-compute-3] c.s.r.AsyncTests:41 - eff1: onAsyncCancel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.effect.{IO, IOApp} | |
import com.typesafe.scalalogging.Logger | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
import scala.concurrent.duration._ | |
object AsyncTests extends IOApp.Simple { | |
private val log = Logger[this.type] | |
def legacyAPI(onDone: Either[Throwable, Int] => Unit): Unit = { | |
log.info("legacyAPI begin") | |
Future { | |
Thread.sleep(500) | |
log.info("legacyAPI end") | |
onDone(Right(5)) | |
} | |
} | |
def internalLegacyAPI(onDone: Either[Throwable, Int] => Unit): AutoCloseable = { | |
log.info("internalLegacyAPI begin") | |
Future { | |
Thread.sleep(600) | |
log.info("internalLegacyAPI end") | |
onDone(Right(6)) | |
} | |
new AutoCloseable { | |
def close(): Unit = { | |
log.info("AutoCloseable.close()") | |
} | |
} | |
} | |
val eff1 = IO.async[Int] { cb => | |
IO(legacyAPI(cb)) >> | |
nestedAsync >> | |
IO(Some(IO { | |
log.info("eff1: onAsyncCancel") | |
})) | |
}.flatMap(x => IO(log.info(s"eff1: completed: $x"))) | |
val cancelEff = IO.sleep(200.millis) >> IO(log.info("cancelEff: finished")) | |
val nestedAsync = IO.async[Int] { cb => | |
IO { | |
val handle = internalLegacyAPI(cb) | |
Some(IO { | |
log.info("nestedAsync: onAsyncCancel") | |
handle.close() | |
}) | |
} | |
}.flatMap(x => IO(log.info(s"nestedAsync: completed: $x"))) | |
def run: IO[Unit] = { | |
IO.race(nestedAsync, cancelEff).void >> IO.sleep(2000.millis) >> | |
IO(log.info("******************************")) >> | |
IO.race(eff1, cancelEff).void >> IO.sleep(2000.millis) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment