cats-effect
cats-effect copied to clipboard
`Spawn[F[_]].background(fa)` does not evaluate `fa` under `TestContext`
It could be shown by a simple test harness that
IO.pure(1).start.map(_.join.flatMap(_.embedNever)) === IO(IO.pure(1)), butIO.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.
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.
(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))