Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adamgfraser
Copy link
Contributor

Right now we are keeping resources open too long with provideLayerShared. Basically we are building one giant ZManaged for all the suites and specs in a test and then running them so the result is we are closing all the shared resources at the end of the spec instead of at the end of the suite they are provided to. To address this I added a release combinator on ZManaged that conceptually "closes" the scope.

This works but looking at this the only combinator is quite problematic. It requires us to traverse the entire spec, which means acquiring all the resources in any nested suites, to determine whether there is any test annotated with only. For example if a spec is to be run sequentially we need to acquire and release the resources for suites one by one but we also need to potentially examine all of the suites and acquire their resources to determine which ones we should run. I have removed it for now though it is a very nice combinator and I would like to figure out a way to support it.

@adamgfraser adamgfraser requested review from iravid and jdegoes May 24, 2020 19:38
* Note that this is only safe if the result of this managed effect is valid
* outside its scope.
*/
def release: ZManaged[R, E, A] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's an issue here in that a *> b.release *> c won't work, as after b.release, the ReleaseMap is already closed.

The workaround may be for this combinator to run self on a nested ReleaseMap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test for this. Seems to be working as expected:

testM("closes the scope") {
  val expected = Chunk("acquiring a", "acquiring b", "releasing b", "acquiring c", "releasing c", "releasing a")
  for {
    ref     <- Ref.make[Chunk[String]](Chunk.empty)
    a       = Managed.make(ref.update(_ + "acquiring a"))(_ => ref.update(_ + "releasing a"))
    b       = Managed.make(ref.update(_ + "acquiring b"))(_ => ref.update(_ + "releasing b"))
    c       = Managed.make(ref.update(_ + "acquiring c"))(_ => ref.update(_ + "releasing c"))
    managed = a *> b.release *> c
    _       <- managed.useNow
    log     <- ref.get
  } yield assert(log)(equalTo(expected))
}

@iravid iravid merged commit 03d898e into zio:master May 25, 2020
@adamgfraser adamgfraser deleted the spec branch August 20, 2020 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants