Skip to content
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

fix: Fix occasional ordering issue in FlowWithContext#unsafeOptionalDataVia #1681

Merged
merged 2 commits into from
Jan 5, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,19 @@ class FlowWithContextSpec extends StreamSpec {
.asFlowWithContext[Option[String], Int, Int](collapseContext = Tuple2.apply)(extractContext = _._2)
.map(_._1)

SourceWithContext
.fromTuples(Source(data)).via(
FlowWithContext.unsafeOptionalDataVia(
flow,
Flow.fromFunction { (string: String) => string.toInt }
)(Keep.none)
)
.runWith(TestSink.probe[(Option[Int], Int)])
.request(4)
.expectNext((Some(1), 1), (None, 2), (None, 3), (Some(4), 4))
.expectComplete()
for (_ <- 0 until 64) {
SourceWithContext
.fromTuples(Source(data)).via(
FlowWithContext.unsafeOptionalDataVia(
flow,
Flow.fromFunction { (string: String) => string.toInt }
)(Keep.none)
)
.runWith(TestSink.probe[(Option[Int], Int)])
.request(4)
.expectNext((Some(1), 1), (None, 2), (None, 3), (Some(4), 4))
.expectComplete()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,16 @@ class SourceWithContextSpec extends StreamSpec {

val source = SourceWithContext.fromTuples(Source(data))

SourceWithContext.unsafeOptionalDataVia(
source,
Flow.fromFunction { (string: String) => string.toInt }
)(Keep.none)
.runWith(TestSink.probe[(Option[Int], Int)])
.request(4)
.expectNext((Some(1), 1), (None, 2), (None, 3), (Some(4), 4))
.expectComplete()
for (_ <- 0 until 64) {
SourceWithContext.unsafeOptionalDataVia(
source,
Flow.fromFunction { (string: String) => string.toInt }
)(Keep.none)
.runWith(TestSink.probe[(Option[Int], Int)])
.request(4)
.expectNext((Some(1), 1), (None, 2), (None, 3), (Some(4), 4))
.expectComplete()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,44 @@ object FlowWithContext {
FlowWithContext.fromTuples(Flow.fromGraph(GraphDSL.createGraph(flow, viaFlow)(combine) {
implicit b => (f, viaF) =>
import GraphDSL.Implicits._
val broadcast = b.add(Broadcast[(Option[FOut], Ctx)](2))
val merge = b.add(Merge[(Option[FViaOut], Ctx)](2))

val unzip = b.add(Unzip[FOut, Ctx]())
val zipper = b.add(Zip[FViaOut, Ctx]())

val filterAvailable = Flow[(Option[FOut], Ctx)].collect {
case (Some(f), ctx) => (f, ctx)
}

val filterUnavailable = Flow[(Option[FOut], Ctx)].collect {
case (None, ctx) => (Option.empty[FViaOut], ctx)
case class IndexedCtx(idx: Long, ctx: Ctx)
val partition = b.add(Partition[(Option[FOut], IndexedCtx)](2,
{
case (None, _) => 0
case (Some(_), _) => 1
}))

val sequence = Flow[(Option[FOut], Ctx)].zipWithIndex
.map {
case ((opt, ctx), idx) => (opt, IndexedCtx(idx, ctx))
}

val unzip = b.add(Unzip[Option[FOut], IndexedCtx]())
val zipper = b.add(Zip[FViaOut, IndexedCtx]())
val mergeSequence = b.add(MergeSequence[(Option[FViaOut], IndexedCtx)](2)(_._2.idx))
val unwrapSome = b.add(Flow[Option[FOut]].map {
case Some(elem) => elem
case _ => throw new IllegalStateException("Only expects Some")
})
val unwrap = Flow[(Option[FViaOut], IndexedCtx)].map {
case (opt, indexedCtx) => (opt, indexedCtx.ctx)
}

val mapIntoOption = Flow[(FViaOut, Ctx)].map {
case (f, ctx) => (Some(f), ctx)
val mapIntoOption = Flow[(FViaOut, IndexedCtx)].map {
case (elem, indexedCtx) => (Some(elem), indexedCtx)
}

f ~> broadcast.in

broadcast.out(0) ~> filterAvailable ~> unzip.in

unzip.out0 ~> viaF ~> zipper.in0
unzip.out1 ~> zipper.in1

zipper.out ~> mapIntoOption ~> merge.in(0)

broadcast.out(1) ~> filterUnavailable ~> merge.in(1)
//format: off
f ~> sequence ~> partition.in
partition.out(0).asInstanceOf[Outlet[(Option[FViaOut], IndexedCtx)]] ~> mergeSequence.in(0)
partition.out(1) ~> unzip.in
unzip.out0 ~> unwrapSome ~> viaF ~> zipper.in0
unzip.out1 ~> zipper.in1
zipper.out ~> mapIntoOption ~> mergeSequence.in(1)

FlowShape(f.in, merge.out)
//format: on
FlowShape(f.in, (mergeSequence.out ~> unwrap).outlet)
}))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ object SourceWithContext {

//format: off
s ~> sequence ~> partition.in
partition.out(0).asInstanceOf[Outlet[(Option[FOut], IndexedCtx)]] ~> mergeSequence.in(0)
partition.out(0).asInstanceOf[Outlet[(Option[FOut], IndexedCtx)]] ~> mergeSequence.in(0)
partition.out(1) ~> unzip.in
unzip.out0 ~> unwrapSome ~> viaF ~> zipper.in0
unzip.out1 ~> zipper.in1
Expand Down
Loading