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: Warn If Scope Cannot Be Closed #8288

Merged
merged 3 commits into from
Jul 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion test-tests/shared/src/test/scala/zio/test/SpecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ object SpecSpec extends ZIOBaseSpec {
val specLayer: ZLayer[Any, Nothing, Unit] =
ZLayer.succeed(())

val neverFinalizerLayer =
ZLayer.scoped(ZIO.acquireRelease(ZIO.unit)(_ => ZIO.never))

def spec: Spec[TestEnvironment, TestFailure[Nothing]] = suite("SpecSpec")(
suite("provideLayer")(
test("does not have early initialization issues") {
Expand Down Expand Up @@ -207,6 +210,14 @@ object SpecSpec extends ZIOBaseSpec {
assert(false)(isFalse)
}
)
}
},
suite("suite does not wait for finalizer to complete")(
test("some test") {
assertCompletes
},
test("some other test") {
assertCompletes
}
).provideLayerShared(neverFinalizerLayer)
)
}
32 changes: 22 additions & 10 deletions test/shared/src/main/scala/zio/test/TestExecutor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,29 @@ object TestExecutor {
loop(label :: labels, spec, exec, ancestors, sectionId)

case Spec.ScopedCase(managed) =>
ZIO
.scoped(
managed
.flatMap(loop(labels, _, exec, ancestors, sectionId))
)
.catchAllCause { e =>
val event =
ExecutionEvent.RuntimeFailure(sectionId, labels, TestFailure.Runtime(e), ancestors)
Scope.make.flatMap { scope =>
scope
.extend(managed.flatMap(loop(labels, _, exec, ancestors, sectionId)))
.onExit { exit =>
val warning =
"Warning: ZIO Test is attempting to close the scope of suite " +
s"${labels.reverse.mkString(" - ")} in $fullyQualifiedName, " +
"but closing the scope has taken more than 60 seconds to " +
"complete. This may indicate a resource leak."
for {
warning <-
ZIO.logWarning(warning).delay(60.seconds).withClock(ClockLive).interruptible.forkDaemon
finalizer <- scope.close(exit).ensuring(warning.interrupt).forkDaemon
exit <- warning.await
_ <- finalizer.join.when(exit.isInterrupted)
} yield ()
}
}.catchAllCause { e =>
val event =
ExecutionEvent.RuntimeFailure(sectionId, labels, TestFailure.Runtime(e), ancestors)

processEvent(event)
}
processEvent(event)
}

case Spec.MultipleCase(specs) =>
ZIO.uninterruptibleMask(restore =>
Expand Down