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

Add Channel#closeWithElement #3280

Merged
merged 19 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ ThisBuild / mimaBinaryIssueFilters ++= Seq(
// Private internal method: #3274
ProblemFilters.exclude[DirectMissingMethodProblem](
"fs2.Chunk.platformIterable"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"fs2.concurrent.Channel.sendAndClose"
)
)

Expand Down
23 changes: 18 additions & 5 deletions core/shared/src/main/scala/fs2/concurrent/Channel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ sealed trait Channel[F[_], A] {
*/
def close: F[Either[Channel.Closed, Unit]]

/** Sends an element through this channel, and closes it right after.
tothpeti marked this conversation as resolved.
Show resolved Hide resolved
* This operation is atomic so it is guaranteed that this will be the last element sent to the channel.
*
* No-op if the channel is closed, see [[close]] for further info.
*/
def sendAndClose(a: A): F[Either[Channel.Closed, Unit]]

/** Returns true if this channel is closed */
def isClosed: F[Boolean]

Expand Down Expand Up @@ -151,7 +158,7 @@ object Channel {
.drain
}

def send(a: A) =
def sendImpl(a: A, close: Boolean) =
F.deferred[Unit].flatMap { producer =>
state.flatModifyFull { case (poll, state) =>
state match {
Expand All @@ -161,18 +168,24 @@ object Channel {
case State(values, size, waiting, producers, closed @ false) =>
if (size < capacity)
(
State(a :: values, size + 1, None, producers, false),
notifyStream(waiting).as(rightUnit)
State(a :: values, size + 1, None, producers, close),
signalClosure.whenA(close) *> notifyStream(waiting).as(rightUnit)
)
else
(
State(values, size, None, (a, producer) :: producers, false),
notifyStream(waiting).as(rightUnit) <* waitOnBound(producer, poll)
State(values, size, None, (a, producer) :: producers, close),
signalClosure.whenA(close) *>
notifyStream(waiting).as(rightUnit) <*
waitOnBound(producer, poll).whenA(!close)
tothpeti marked this conversation as resolved.
Show resolved Hide resolved
tothpeti marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
}

def send(a: A) = sendImpl(a, false)

def sendAndClose(a: A) = sendImpl(a, true)

def trySend(a: A) =
state.flatModify {
case s @ State(_, _, _, _, closed @ true) =>
Expand Down
76 changes: 76 additions & 0 deletions core/shared/src/test/scala/fs2/concurrent/ChannelSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,80 @@ class ChannelSuite extends Fs2Suite {

TestControl.executeEmbed(test)
}

def checkIfSendAndCloseClosing(channel: IO[Channel[IO, Int]]) =
channel
.flatMap { ch =>
ch.sendAndClose(0) *> ch.isClosed
}
.assertEquals(true)

test("sendAndClose closes right after sending the last element in bounded(1) case") {
val channel = Channel.bounded[IO, Int](1)

checkIfSendAndCloseClosing(channel).parReplicateA_(if (isJVM) 10000 else 1)
}

test("sendAndClose closes right after sending the last element in bounded(2) case") {
val channel = Channel.bounded[IO, Int](2)

checkIfSendAndCloseClosing(channel).parReplicateA_(if (isJVM) 10000 else 1)
}

test("sendAndClose closes right after sending the last element in unbounded case") {
val channel = Channel.unbounded[IO, Int]

checkIfSendAndCloseClosing(channel).parReplicateA_(if (isJVM) 10000 else 1)
}

test("sendAndClose closes right after sending the last element in synchronous case") {
val channel = Channel.synchronous[IO, Int]

checkIfSendAndCloseClosing(channel).parReplicateA_(if (isJVM) 10000 else 1)
}

def racingSendOperations(channel: IO[Channel[IO, Int]]) = {
val expectedFirstCase = ((Right(()), Right(())), List(0, 1))
val expectedSecondCase = ((Right(()), Left(Channel.Closed)), List(1))
val expectedThirdCase = ((Left(Channel.Closed), Right(())), List(1))

channel
.flatMap { ch =>
ch.send(0).both(ch.sendAndClose(1)).parProduct(ch.stream.compile.toList)
}
.map {
case obtained @ ((Right(()), Right(())), List(0, 1)) =>
assertEquals(obtained, expectedFirstCase)
case obtained @ ((Right(()), Left(Channel.Closed)), List(1)) =>
assertEquals(obtained, expectedSecondCase)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't think this case should ever happen? Is it happening?

Copy link
Contributor Author

@tothpeti tothpeti Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is happening once or twice during 10000 runs, but it is pretty rare.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how that can happen if everything is working correctly. If the closeWithElement returns that the Channel is already closed, then who is closing it? Seems like a bug 😕

Copy link
Contributor Author

@tothpeti tothpeti Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I left this match case there, when we iterated over multiple possible solutions. So, probably this will not happen. I will try it without this match case soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I tried with 100000 runs, and that match case never occurred. So, it can be safely removed. 👍

case obtained @ ((Left(Channel.Closed), Right(())), List(1)) =>
assertEquals(obtained, expectedThirdCase)
case e => fail(s"An unknown test result: $e")
}
}

test("racing send and sendAndClose should work in bounded(1) case") {
val channel = Channel.bounded[IO, Int](1)

racingSendOperations(channel).parReplicateA_(if (isJVM) 10000 else 1)
tothpeti marked this conversation as resolved.
Show resolved Hide resolved
}

test("racing send and sendAndClose should work in bounded(2) case") {
val channel = Channel.bounded[IO, Int](2)

racingSendOperations(channel).parReplicateA_(if (isJVM) 10000 else 1)
}

test("racing send and sendAndClose should work in unbounded case") {
val channel = Channel.unbounded[IO, Int]

racingSendOperations(channel).parReplicateA_(if (isJVM) 10000 else 1)
}

test("racing send and sendAndClose should work in synchronous case") {
val channel = Channel.synchronous[IO, Int]

racingSendOperations(channel).parReplicateA_(if (isJVM) 10000 else 1)
}

}