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 3 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
18 changes: 15 additions & 3 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,12 @@ 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
*
* 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 +157,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 @@ -162,17 +168,23 @@ object Channel {
if (size < capacity)
(
State(a :: values, size + 1, None, producers, false),
notifyStream(waiting).as(rightUnit)
signalClosure.whenA(close) *> notifyStream(waiting).as(rightUnit)
)
else
(
State(values, size, None, (a, producer) :: producers, false),
notifyStream(waiting).as(rightUnit) <* waitOnBound(producer, poll)
signalClosure.whenA(close) *>
notifyStream(waiting).as(rightUnit) <*
waitOnBound(producer, poll)
)
}
}
}

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
24 changes: 24 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,28 @@ class ChannelSuite extends Fs2Suite {

TestControl.executeEmbed(test)
}

test("sendAndClose closes right after sending the last element") {
tothpeti marked this conversation as resolved.
Show resolved Hide resolved
val result = Channel.bounded[IO, Int](1).flatMap { ch =>
ch.sendAndClose(0) *> ch.isClosed
tothpeti marked this conversation as resolved.
Show resolved Hide resolved
}

result.assertEquals(true)
}

test("racing send and sendAndClose should work in bounded case") {
val test = Channel.bounded[IO, Int](2).flatMap { ch =>
ch.send(0).both(ch.sendAndClose(1))
}
test.assertEquals((Right(()), Right(())))
tothpeti marked this conversation as resolved.
Show resolved Hide resolved
}

test("racing send and sendAndClose should work in unbounded case") {
val test = Channel.unbounded[IO, Int].flatMap { ch =>
ch.send(0).both(ch.sendAndClose(1))
}

test.assertEquals((Right(()), Right(())))
}

}