Skip to content

Instantly share code, notes, and snippets.

@TimWSpence
Last active November 15, 2022 15:44
Show Gist options
  • Save TimWSpence/80ab81dda4fb4675f6e86e0ad0eb443f to your computer and use it in GitHub Desktop.
Save TimWSpence/80ab81dda4fb4675f6e86e0ad0eb443f to your computer and use it in GitHub Desktop.
//> using scala "3.2.1"
//> using lib "org.typelevel::cats-effect-testkit:3.4.0"
//> using lib "org.typelevel::munit-cats-effect-3:1.0.7"
import cats.effect.*
import cats.effect.testkit.TestControl
import munit.CatsEffectSuite
import scala.concurrent.duration.*
class Suite extends CatsEffectSuite:
test("basic usage") {
val run = IO.ref(false).flatMap { ref =>
val background = IO.sleep(5.seconds) >> ref.set(true)
background.start >> IO.sleep(10.seconds) >> ref.get.assert
}
TestControl.executeEmbed(run)
}
test("stepped execution") {
IO.ref(false).flatMap { ref =>
val background = IO.sleep(5.seconds) >> ref.set(true)
val run = background.start >> IO.sleep(10.seconds) >> ref.get.assert
TestControl.execute(run).flatMap { ctrl =>
for
// Schedule initial fibers
_ <- ctrl.tick
_ <- ref.get.assertEquals(false)
// Advance time but do not schedule fibers
_ <- ctrl.advance(5.seconds)
_ <- ref.get.assertEquals(false)
// Now schedule - background fiber should run
_ <- ctrl.tick
// Background fiber has now modified the Ref
_ <- ref.get.assert
// Run to completion
_ <- ctrl.tickAll
yield ()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment