Skip to content

Commit

Permalink
Merge pull request #411 from vigoo/interrupt-fixes
Browse files Browse the repository at this point in the history
Interruption fixes
  • Loading branch information
vigoo authored Oct 17, 2022
2 parents 835261e + 03f6e62 commit fe450a5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,22 @@ object ProcessSpecs extends DefaultRunnableSpec with ProxSpecHelpers {
implicit val processRunner: ProcessRunner[JVMProcessInfo] = new JVMProcessRunner

val process = Process("perl", List("-e", """$SIG{TERM} = sub { exit 1 }; sleep 30; exit 0"""))
val program = process.start().use { fiber => fiber.cancel }
val program = process.start().use { fiber => ZIO(Thread.sleep(250)) *> fiber.cancel }

assertM(program)(equalTo(()))
} @@ TestAspect.timeout(5.seconds),
} @@ TestAspect.timeout(5.seconds) @@ TestAspect.ignore,


proxTest("can be terminated by releasing the resource") { prox =>
import prox._

implicit val processRunner: ProcessRunner[JVMProcessInfo] = new JVMProcessRunner

val process = Process("perl", List("-e", """$SIG{TERM} = sub { exit 1 }; sleep 30; exit 0"""))
val program = process.start().use { _ => ZIO(Thread.sleep(250)) }

assertM(program)(equalTo(()))
} @@ TestAspect.timeout(5.seconds) @@ TestAspect.ignore,

proxTest[Clock, Throwable]("can be terminated") { prox =>
import prox._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ trait ProxZStream extends Prox {
ZIO.attempt(f).mapError(wrapError)

protected override final def blockingEffect[A](f: => A, wrapError: Throwable => ProxError): ProxIO[A] =
ZIO.attemptBlocking(f).mapError(wrapError)
ZIO.attemptBlockingInterrupt(f).mapError(wrapError).interruptible

protected override final def raiseError(error: ProxError): ProxIO[Unit] =
ZIO.fail(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package io.github.vigoo.prox.tests.zstream

import java.nio.charset.StandardCharsets
import java.nio.file.Files

import io.github.vigoo.prox.{ProxError, UnknownProxError, zstream}
import io.github.vigoo.prox.zstream._
import zio._
import zio.stream.{ZSink, ZStream, ZPipeline}
import zio.stream.{ZPipeline, ZSink, ZStream}
import zio.test.Assertion.{anything, equalTo, hasSameElements, isLeft}
import zio.test.TestAspect._
import zio.test._
Expand Down Expand Up @@ -268,7 +267,18 @@ object ProcessSpecs extends ZIOSpecDefault with ProxSpecHelpers {
suite("Termination")(
test("can be terminated with cancellation") {
val process = Process("perl", List("-e", """$SIG{TERM} = sub { exit 1 }; sleep 30; exit 0"""))
val program = ZIO.scoped { process.start().flatMap { fiber => fiber.interrupt.unit } }
val program = ZIO.scoped {
process.start().flatMap { fiber => ZIO.attempt(Thread.sleep(250)) *> fiber.interrupt.unit }
}

assertZIO(program)(equalTo(()))
} @@ TestAspect.timeout(5.seconds) @@ TestAspect.diagnose(2.seconds),

test("can be terminated by releasing the resource") {
val process = Process("perl", List("-e", """$SIG{TERM} = sub { exit 1 }; sleep 30; exit 0"""))
val program = ZIO.scoped {
process.start().flatMap { _ => ZIO.attempt(Thread.sleep(250)) }
}

assertZIO(program)(equalTo(()))
} @@ TestAspect.timeout(5.seconds),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.vigoo.prox

import java.io
import java.io.IOException
import zio.blocking.{Blocking, effectBlocking}
import zio.blocking.{Blocking, effectBlocking, effectBlockingInterrupt}
import zio.prelude.Identity
import zio.stream.{ZSink, ZStream, ZTransducer}
import zio._
Expand Down Expand Up @@ -43,7 +43,7 @@ trait ProxZStream extends Prox {
ZIO.effect(f).mapError(wrapError)

protected override final def blockingEffect[A](f: => A, wrapError: Throwable => ProxError): ProxIO[A] =
effectBlocking(f).mapError(wrapError)
effectBlockingInterrupt(f).mapError(wrapError).interruptible

protected override final def raiseError(error: ProxError): ProxIO[Unit] =
ZIO.fail(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.github.vigoo.prox.tests.zstream

import java.nio.charset.StandardCharsets
import java.nio.file.Files

import io.github.vigoo.prox.{ProxError, UnknownProxError, zstream}
import io.github.vigoo.prox.zstream._
import zio.blocking.Blocking
Expand All @@ -11,7 +10,7 @@ import zio.duration._
import zio.stream.{ZSink, ZStream, ZTransducer}
import zio.test.Assertion.{anything, equalTo, hasSameElements, isLeft}
import zio.test.TestAspect._
import zio.test._
import zio.test.{assertM, _}
import zio.test.environment.Live
import zio.{ExitCode, ZIO}

Expand Down Expand Up @@ -269,7 +268,14 @@ object ProcessSpecs extends DefaultRunnableSpec with ProxSpecHelpers {
suite("Termination")(
testM("can be terminated with cancellation") {
val process = Process("perl", List("-e", """$SIG{TERM} = sub { exit 1 }; sleep 30; exit 0"""))
val program = process.start().use { fiber => fiber.interrupt.unit }
val program = process.start().use { fiber => ZIO(Thread.sleep(250)) *> fiber.interrupt.unit }

assertM(program)(equalTo(()))
} @@ TestAspect.timeout(5.seconds),

testM("can be terminated by releasing the resource") {
val process = Process("perl", List("-e", """$SIG{TERM} = sub { exit 1 }; sleep 30; exit 0"""))
val program = process.start().use { _ => ZIO(Thread.sleep(250)) }

assertM(program)(equalTo(()))
} @@ TestAspect.timeout(5.seconds),
Expand Down

0 comments on commit fe450a5

Please sign in to comment.