Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make virtual time pass in a test in Cats Effect 3?

I am trying to convert Mules to Cats Effect 3 (CE3). Since it is a caching library, in its tests it needs (virtual) time to pass to test whether items will be expired. It is currently making fairly heavy use of cats.effect.laws.util.TestContext, which allows advancing the virtual clock without actually sleeping via tick. TestContext does not (as far as I have been able to discover) exist in CE3, but I am always loathe to puts sleep calls into a test. Rolling my own IO for this specific case though is a daunting prospect; surely this is a more general problem people have. I was not able to find a reference to TestContext in the migration guide, however.

Is there a known upgrade path for this case?

like image 772
Keith Pinson Avatar asked May 18 '21 17:05

Keith Pinson


People also ask

How to pass the cat in 3 months?

Candidates who want to pass the CAT in three months must perform a chapter-specific study to cover all of the topics that will be included in the next CAT exam. The Common Admission Test (CAT) is a national-level entrance exam for admission to elite B-schools such as IIMs.

What is cats effect?

These effects may be asynchronous (callback-driven) or synchronous (directly returning values); they may return within microseconds or run infinitely. Even more importantly, Cats Effect defines a set of typeclasses which define what it means to be a purely functional runtime system.

How do you pass time in a math class?

Luckily, if you can find a way to get engaged, that hour-long math lecture will be over before you know it. While it may sound counterintuitive, focusing on what you’re learning is one of the best ways to pass time in class. If you’ve given it your best shot and you just can’t do it, you can try to find a productive way to spend your time.


Video Answer


2 Answers

As mentioned by Victor in comments about v3.3.0, this version is released about a day ago and you can see examples for virtual time in Mocking time paragraph in docs

like image 133
hnaderi Avatar answered Oct 08 '22 14:10

hnaderi


With some help from Chris Davenport on Discord, I found that TestContext is still there, but has been moved from cats.effect.laws.util to cats.effect.kernel.testkit. It lives in its own project and artifact, so you will need to add a dependency (SBT syntax):

    "org.typelevel" %% "cats-effect-kernel-testkit" % catsEffectV % Test,

Also if you were not using anything else from the laws package than you will want to remove that dependency, which would look something like:

    "org.typelevel" %% "cats-effect-laws"           % catsEffectV % Test,

Presumably this is why they moved it: because it is not specific to laws.


However, I was not successful in getting virtual time to pass in my tests with this, and have sadly brute-forced it with IO.sleeps for now. :/

like image 30
Keith Pinson Avatar answered Oct 08 '22 15:10

Keith Pinson