cats-effect icon indicating copy to clipboard operation
cats-effect copied to clipboard

`Spawn[F[_]].background(fa)` does not evaluate `fa` under `TestContext`

Open seigert opened this issue 4 years ago • 1 comments

It could be shown by a simple test harness that

  1. IO.pure(1).start.map(_.join.flatMap(_.embedNever)) === IO(IO.pure(1)), but
  2. IO.pure(1).background.map(_.flatMap(_.embedNever)) !== Resource.pure(IO.pure(1)).

I believe that with test Eq[Resource[F, *] instance like Eq.by(_.use(F.pure)) above should be equivalent. Moreso, IO.pure(1).background.map(_.flatMap(_.embedNever)).use(identity) === Resource.pure(IO.pure(1)).use(identity) under TestContext.

Also, there is more complex example with Scala 3 that attempts to create new Applicative instance. ApplicativeTests laws such as homomorphism law fail for it exactly by the same cause.

seigert avatar Sep 07 '21 09:09 seigert

Pulling this into 3.3.0 to evaluate. I suspect the test harness is the problem (more specifically, the Eq as you called out), but this needs to be tracked down and evaluated.

djspiewak avatar Sep 07 '21 15:09 djspiewak

(1) and (2) are not equivalent. .background cancels the fiber when you exit the use block. So in fact you are leaking the join of a canceled fiber outside of the use block.

If you insert a cede to give a chance for the background fiber to actually run then the test passes.

a.background.evalTap(_ *> IO.cede).map(_.flatMap(_.embedNever))

armanbilge avatar May 06 '23 02:05 armanbilge