diff --git a/streams-tests/shared/src/test/scala/zio/stream/ZChannelSpec.scala b/streams-tests/shared/src/test/scala/zio/stream/ZChannelSpec.scala index ffef59f18e4e..f3e0427f2ce2 100644 --- a/streams-tests/shared/src/test/scala/zio/stream/ZChannelSpec.scala +++ b/streams-tests/shared/src/test/scala/zio/stream/ZChannelSpec.scala @@ -4,7 +4,7 @@ import zio._ import zio.stream.ZChannel.{ChildExecutorDecision, UpstreamPullRequest, UpstreamPullStrategy} import zio.test._ import zio.test.Assertion._ -import zio.test.TestAspect.timeout +import zio.test.TestAspect.{jvmOnly, timeout} object ZChannelSpec extends ZIOBaseSpec { import ZIOTag._ @@ -802,7 +802,7 @@ object ZChannelSpec extends ZIOBaseSpec { .repeatN(100000) value <- ref.get } yield assertTrue(value == 0) - }, + } @@ jvmOnly, test("scoped closes the scope") { for { ref <- Ref.make(0) diff --git a/test-tests/shared/src/test/scala/zio/test/SpecSpec.scala b/test-tests/shared/src/test/scala/zio/test/SpecSpec.scala index a0678e6e2759..9945b9c561cf 100644 --- a/test-tests/shared/src/test/scala/zio/test/SpecSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/SpecSpec.scala @@ -168,6 +168,34 @@ object SpecSpec extends ZIOBaseSpec { ) ).provideLayerShared(ZLayer.scoped[Any](ZIO.acquireRelease(Ref.make(0))(_.set(-1)))) assertZIO(succeeded(spec))(isTrue) + }, + test("dependencies of shared service are scoped to lifetime of suite") { + trait Service { + def open: UIO[Boolean] + } + object Service { + val open: ZIO[Service, Nothing, Boolean] = + ZIO.serviceWithZIO(_.open) + } + val layer = + ZLayer { + for { + ref <- Ref.make(true) + _ <- ZIO.addFinalizer(ref.set(false)) + } yield new Service { + def open: UIO[Boolean] = + ref.get + } + } + val spec = suite("suite")( + test("test1") { + assertZIO(Service.open)(isTrue) + }, + test("test2") { + assertZIO(Service.open)(isTrue) + } + ).provideSomeLayerShared[Scope](layer).provideLayer(Scope.default) @@ sequential + assertZIO(succeeded(spec))(isTrue) } ), suite("iterable constructor") { diff --git a/test/shared/src/main/scala/zio/test/Spec.scala b/test/shared/src/main/scala/zio/test/Spec.scala index 3abcaabd8feb..341ef75ec7ca 100644 --- a/test/shared/src/main/scala/zio/test/Spec.scala +++ b/test/shared/src/main/scala/zio/test/Spec.scala @@ -335,16 +335,14 @@ final case class Spec[-R, +E](caseValue: SpecCase[R, E, Spec[R, E]]) extends Spe case LabeledCase(label, spec) => Spec.labeled(label, spec.provideLayerShared(layer)) case ScopedCase(scoped) => Spec.scoped[R0]( - layer.memoize - .flatMap(layer => - scoped - .map(_.provideLayer(layer)) - .provideLayer(layer.mapError(TestFailure.fail) ++ ZLayer.environment[Scope]) - ) + layer + .mapError(TestFailure.fail) + .build + .flatMap(r => scoped.map(_.provideEnvironment(r)).provideSomeEnvironment[Scope](r.union[Scope])) ) case MultipleCase(specs) => Spec.scoped[R0]( - layer.memoize.map(layer => Spec.multiple(specs.map(_.provideLayer(layer)))) + layer.mapError(TestFailure.fail).build.map(r => Spec.multiple(specs.map(_.provideEnvironment(r)))) ) case TestCase(test, annotations) => Spec.test(test.provideLayer(layer.mapError(TestFailure.fail)), annotations) } @@ -525,17 +523,18 @@ object Spec { case LabeledCase(label, spec) => Spec.labeled(label, spec.provideSomeLayerShared(layer)) case ScopedCase(scoped) => Spec.scoped[R0]( - layer.memoize.flatMap { layer => + layer.mapError(TestFailure.fail).build.flatMap { r => scoped - .map(_.provideSomeLayer[R0](layer)) - .asInstanceOf[ZIO[R0 with R1 with Scope, TestFailure[E], Spec[R0, E1]]] - .provideLayer(ZLayer.environment[R0 with Scope] ++ layer.mapError(TestFailure.fail)) + .map(_.provideSomeLayer[R0](ZLayer.succeedEnvironment(r))) + .provideSomeEnvironment[R0 with Scope](in => in.union[R1](r).asInstanceOf[ZEnvironment[R with Scope]]) } ) case MultipleCase(specs) => Spec.scoped[R0]( - layer.memoize - .map(layer => Spec.multiple(specs.map(_.provideSomeLayer(layer)))) + layer + .mapError(TestFailure.fail) + .build + .map(r => Spec.multiple(specs.map(_.provideSomeLayer[R0](ZLayer.succeedEnvironment(r))))) ) case TestCase(test, annotations) => Spec.test(test.provideSomeLayer(layer.mapError(TestFailure.fail)), annotations)