8000 ZIO Test: Don't Wait To Interrupt Fibers Due To Auto Supervision by adamgfraser · Pull Request #5578 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ZIO Test: Don't Wait To Interrupt Fibers Due To Auto Supervision #5578

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

Merged
merged 4 commits into from
Sep 11, 2021
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
21 changes: 17 additions & 4 deletions test-tests/shared/src/test/scala/zio/test/TestSpec.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package zio.test

import zio._
import zio.Clock._
import zio.test.environment._
import zio.test.Assertion._
import zio.test.TestAspect.failing
import zio.test.TestAspect.{failing, timeout}
import zio.test.TestUtils.execute
import zio.{Clock, Has, ZIO}

object TestSpec extends ZIOBaseSpec {

def spec: Spec[Has[Clock], TestFailure[Any], TestSuccess] = suite("TestSpec")(
override val runner: TestRunner[TestEnvironment, Any] =
defaultTestRunner.withRuntimeConfig { runtimeConfig =>
runtimeConfig.copy(logger = runtimeConfig.logger.filterLogLevel(_ >= LogLevel.Error))
}

def spec: Spec[Environment, TestFailure[Any], TestSuccess] = suite("TestSpec")(
test("assertM works correctly") {
assertM(nanoTime)(equalTo(0L))
},
Expand Down Expand Up @@ -39,6 +45,13 @@ object TestSpec extends ZIOBaseSpec {
for {
_ <- execute(spec)
} yield assert(n)(equalTo(1))
}
},
test("test does not wait to interrupt children") {
for {
promise <- Promise.make[Nothing, Unit]
_ <- (promise.succeed(()) *> Live.live(ZIO.sleep(20.seconds))).uninterruptible.fork
_ <- promise.await
} yield assertCompletes
} @@ timeout(10.seconds)
)
}
2 changes: 1 addition & 1 deletion test/shared/src/main/scala/zio/test/TestAspect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ object TestAspect extends TimeoutVariants {
def verify[R0, E0](condition: => ZIO[R0, E0, TestResult]): TestAspect[Nothing, R0, E0, Any] =
new TestAspect.PerTest[Nothing, R0, E0, Any] {
def perTest[R <: R0, E >: E0](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
test <* ZTest(condition)
test <* ZTest("verify", condition)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/shared/src/main/scala/zio/test/TestConstructor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait TestConstructorLowPriority1 extends TestConstructorLowPriority2 {
Spec.labeled(
label,
Spec
.test(ZTest(assertion), TestAnnotationMap.empty)
.test(ZTest(label, assertion), TestAnnotationMap.empty)
.annotate(TestAnnotation.location, location :: Nil)
)
}
Expand All @@ -52,7 +52,7 @@ trait TestConstructorLowPriority3 {
Spec.labeled(
label,
Spec
.test(ZTest(assertion), TestAnnotationMap.empty)
.test(ZTest(label, assertion), TestAnnotationMap.empty)
.annotate(TestAnnotation.location, location :: Nil)
)
}
Expand Down
18 changes: 17 additions & 1 deletion test/shared/src/main/scala/zio/test/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,25 @@ package object test extends CompileVariants {
/**
* Builds a test with an effectual assertion.
*/
def apply[R, E](assertion: => ZIO[R, E, TestResult]): ZIO[R, TestFailure[E], TestSuccess] =
def apply[R, E](label: String, assertion: => ZIO[R, E, TestResult]): ZIO[R, TestFailure[E], TestSuccess] =
ZIO
.suspendSucceed(assertion)
.overrideForkScope(ZScope.global)
.ensuringChildren { children =>
ZIO.foreach(children) { child =>
val warning =
s"Warning: ZIO Test is attempting to interrupt fiber " +
s"${child.id} forked in test $label due to automatic, " +
"supervision, but interruption has taken more than 10 " +
"seconds to complete. This may indicate a resource leak. " +
"Make sure you are not forking a fiber in an " +
"uninterruptible region."
for {
fiber <- ZIO.logWarning(warning).delay(10.seconds).provide(Has(Clock.ClockLive)).interruptible.forkDaemon
_ <- (child.interrupt *> fiber.interrupt).forkDaemon
} yield ()
}
}
.foldCauseZIO(
cause => ZIO.fail(TestFailure.Runtime(cause)),
_.failures match {
Expand Down
0