Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
15 changes: 14 additions & 1 deletion streams-tests/shared/src/test/scala/zio/stream/ZStreamSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,20 @@ object ZStreamSpec extends ZIOBaseSpec {
.run
.map(_.interrupted)
)(equalTo(false))
} @@ nonFlaky(10)
} @@ nonFlaky(10),
testM("interrupts pending tasks when one of the tasks fails") {
for {
interrupted <- Ref.make(0)
latch1 <- Promise.make[Nothing, Unit]
latch2 <- Promise.make[Nothing, Unit]
_ <- ZStream(
latch1.succeed(()) *> ZIO.never.onInterrupt(interrupted.update(_ + 1)),
latch2.succeed(()) *> ZIO.never.onInterrupt(interrupted.update(_ + 1)),
latch1.await *> latch2.await *> ZIO.fail("Boom")
).mapMPar(3)(identity).runDrain.run
count <- interrupted.get
} yield assert(count)(equalTo(2))
}
),
suite("mergeTerminateLeft")(
testM("terminates as soon as the first stream terminates") {
Expand Down
17 changes: 12 additions & 5 deletions streams/shared/src/main/scala/zio/stream/ZStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1919,18 +1919,25 @@ abstract class ZStream[-R, +E, +O](val process: ZManaged[R, Nothing, ZIO[R, Opti
final def mapMPar[R1 <: R, E1 >: E, O2](n: Int)(f: O => ZIO[R1, E1, O2]): ZStream[R1, E1, O2] =
ZStream[R1, E1, O2] {
for {
out <- Queue.bounded[ZIO[R1, Option[E1], O2]](n).toManaged(_.shutdown)
permits <- Semaphore.make(n.toLong).toManaged_
out <- Queue.bounded[ZIO[R1, Option[E1], O2]](n).toManaged(_.shutdown)
errorSignal <- Promise.make[E1, Nothing].toManaged_
permits <- Semaphore.make(n.toLong).toManaged_
_ <- self.foreachManaged { a =>
for {
p <- Promise.make[E1, O2]
latch <- Promise.make[Nothing, Unit]
_ <- out.offer(p.await.mapError(Some(_)))
_ <- permits.withPermit(latch.succeed(()) *> f(a).to(p)).fork
_ <- latch.await
_ <- permits.withPermit {
latch.succeed(()) *> // Make sure we start evaluation before moving on to the next element
(errorSignal.await // Interrupt evaluation if another task fails
raceFirst f(a))
.tapCause(errorSignal.halt) // Notify other tasks of a failure
.to(p) // Transfer the result to the consuming stream
}.fork
_ <- latch.await
} yield ()
}.foldCauseM(
c => out.offer(Pull.halt(c)).unit.toManaged_,
c => out.offer(Pull.halt(c)).toManaged_,
_ => (permits.withPermits(n.toLong)(ZIO.unit).interruptible *> out.offer(Pull.end)).toManaged_
)
.fork
Expand Down