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
38 changes: 38 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 @@ -539,6 +539,44 @@ object ZStreamSpec extends ZIOBaseSpec {
}
}
),
suite("broadcastDynamic")(
test("Values") {
val stream = ZStream.range(0, 5)
for {
broadcasted <- stream.broadcastDynamic(12)
out1 <- stream.runCollect
out2 <- broadcasted.runCollect
expected = Chunk.fromIterable(Range(0, 5))
} yield assert(out1)(equalTo(expected)) && assert(out2)(equalTo(expected))
},
test("Errors") {
val stream = ZStream.range(0, 1) ++ ZStream.fail("Boom")
for {
broadcasted <- stream.broadcastDynamic(12)
out1 <- stream.runCollect.either
out2 <- broadcasted.runCollect.either
expected = Left("Boom")
} yield assert(out1)(equalTo(expected)) && assert(out2)(equalTo(expected))
},
test("Unsubscribe") {
val stream = ZStream.range(0, 5)
for {
broadcasted <- stream.broadcastDynamic(12)
_ <- stream.toPull.ignore
out2 <- broadcasted.runCollect
} yield assert(out2)(equalTo(Chunk.fromIterable(Range(0, 5))))
},
test("Cleanup Resources") {
for {
finalized <- Ref.make(false)
stream = ZStream.range(0, 5).ensuring(finalized.set(true))
broadcasted <- stream.broadcastDynamic(2)
_ <- stream.runCollect
_ <- broadcasted.runCollect
cleaned <- finalized.get
} yield assertTrue(cleaned)
}
) @@ TestAspect.timeout(5.seconds),
suite("buffer")(
test("maintains elements and ordering")(check(tinyChunkOf(tinyChunkOf(Gen.int))) { chunk =>
assertZIO(
Expand Down
2 changes: 1 addition & 1 deletion streams/shared/src/main/scala/zio/stream/ZStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ final class ZStream[-R, +E, +A] private (val channel: ZChannel[R, Any, Any, Any,
)(implicit trace: Trace): ZIO[R with Scope, Nothing, ZStream[Any, E, A]] =
self
.broadcastedQueuesDynamic(maximumLag)
.map(ZStream.scoped(_).flatMap(ZStream.fromQueue(_)).flattenTake)
.flatMap(_.map(ZStream.fromQueueWithShutdown(_).flattenTake))

/**
* Converts the stream to a scoped list of queues. Every value will be
Expand Down
Loading