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
43 changes: 43 additions & 0 deletions streams-tests/shared/src/test/scala/zio/stream/ZStreamSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,49 @@ object ZStreamSpec extends ZIOBaseSpec {
).reduce(_ && _)
}
)
},
testM("Preserves errors") {
assertM(
ZStream
.fail(new Exception("boom"))
.toInputStream
.use(is =>
Task {
is.read
}
)
.run
)(
fails(hasMessage(equalTo("boom")))
)
},
testM("Preserves errors in the middle") {
val bytes: Seq[Byte] = (1 to 5).map(_.toByte)
val str: ZStream[Any, Throwable, Byte] = ZStream.fromIterable(bytes) ++ ZStream.fail(new Exception("boom"))
assertM(
str.toInputStream
.use(is =>
Task {
val buf = new Array[Byte](50)
is.read(buf)
"ok"
}
)
.run
)(fails(hasMessage(equalTo("boom"))))
},
testM("Allows reading something even in case of error") {
val bytes: Seq[Byte] = (1 to 5).map(_.toByte)
val str: ZStream[Any, Throwable, Byte] = ZStream.fromIterable(bytes) ++ ZStream.fail(new Exception("boom"))
assertM(
str.toInputStream.use(is =>
Task {
val buf = new Array[Byte](5)
is.read(buf)
buf.toList
}
)
)(equalTo(bytes))
}
),
testM("toIterator") {
Expand Down
3 changes: 2 additions & 1 deletion streams/shared/src/main/scala/zio/stream/ZStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2781,7 +2781,8 @@ abstract class ZStream[-R, +E, +O](val process: ZManaged[R, Nothing, ZIO[R, Opti
for {
runtime <- ZIO.runtime[R].toManaged_
pull <- process.asInstanceOf[ZManaged[R, Nothing, ZIO[R, Option[Throwable], Chunk[Byte]]]]
} yield ZInputStream.fromPull(runtime, pull)
is <- Task(ZInputStream.fromPull(runtime, pull)).asInstanceOf[ZIO[R, E, java.io.InputStream]].toManaged_
} yield is

/**
* Converts this stream into a `scala.collection.Iterator` wrapped in a [[ZManaged]].
Expand Down