Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZIO Test: Scope Dependencies of Shared Service to Lifetime of Suite #7533

Merged
merged 3 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions test-tests/shared/src/test/scala/zio/test/SpecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
25 changes: 12 additions & 13 deletions test/shared/src/main/scala/zio/test/Spec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down