com.thoughtworks.zerocost.resourcet
The data structure that provides automatic resource management.
ResourceT can be used as a monad transformer for scala.Function0
ResourceT
import scala.Function0 import cats.instances.function._ import com.thoughtworks.zerocost.resourcet._ type RAII[A] = ResourceT[Function0, A]
Given a resource that creates temporary files
trait MyResource extends AutoCloseable { def inUse(): Unit } val myResourceStub0 = stub[MyResource] val myResourceStub1 = stub[MyResource] val myResourceFactoryMock = mockFunction[MyResource] myResourceFactoryMock.expects().returns(myResourceStub0) myResourceFactoryMock.expects().returns(myResourceStub1) val resource: RAII[MyResource] = ResourceT.autoCloseable[Function0, MyResource](myResourceFactoryMock)
when using temporary file created by resouce in a for / yield block, those temporary files should be available.
resouce
for
yield
import cats.syntax.all._ val usingResouce = for { tmpFile0 <- resource tmpFile1 <- resource } yield { tmpFile0 should be(myResourceStub0) tmpFile1 should be(myResourceStub1) tmpFile1.inUse() }
and those files should have been deleted after the for / yield block.
usingResouce.run.apply() ((myResourceStub0.inUse _): () => Unit).verify().never() ((myResourceStub0.close _): () => Unit).verify().once() ((myResourceStub1.inUse _): () => Unit).verify().once() ((myResourceStub1.close _): () => Unit).verify().once()
This ResourceT type is an opacity alias to F[Resource[F, A]]. All type classes and helper functions for this ResourceT type are defined in the companion object ResourceT
F[Resource[F, A]]
The data structure that provides automatic resource management.
ResourceT
can be used as a monad transformer for scala.Function0Given a resource that creates temporary files
when using temporary file created by
resouce
in afor
/yield
block, those temporary files should be available.and those files should have been deleted after the
for
/yield
block.This
ResourceT
type is an opacity alias toF[Resource[F, A]]
. All type classes and helper functions for thisResourceT
type are defined in the companion object ResourceT