Skip to content

Commit

Permalink
fix: add uninterruptible in termination of channel
Browse files Browse the repository at this point in the history
  • Loading branch information
tassiluca committed Mar 1, 2024
1 parent e1c027d commit fb27a2b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
2 changes: 0 additions & 2 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package io.github.tassiLuca.pimping

import gears.async.Channel.Closed
import gears.async.{Async, BufferedChannel, Channel, Future, Listener, SyncChannel, UnboundedChannel}
import gears.async.{Async, BufferedChannel, Channel, SyncChannel, UnboundedChannel, uninterruptible}

import scala.annotation.tailrec
import scala.language.postfixOps
import scala.reflect.ClassTag

/** A token to be sent to a channel to signal that it has been terminated. */
case object Terminated

type Terminated = Terminated.type

/** A union type of [[T]] and [[Terminated]]. */
type Terminable[T] = T | Terminated

/** A [[Channel]] that can be terminated, signalling no more items will be sent,
* still allowing to consumer to read pending values.
*/
trait TerminableChannel[T] extends Channel[Terminable[T]]:
def terminate()(using Async): Unit

Expand Down Expand Up @@ -47,7 +52,8 @@ object TerminableChannel:

override def close(): Unit = c.close()

override def terminate()(using Async): Unit = c.send(Terminated)
override def terminate()(using Async): Unit = uninterruptible:
c.send(Terminated)

object TerminableChannelOps:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import gears.async.TaskSchedule.{Every, RepeatUntilFailure}
import gears.async.default.given
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers
import io.github.tassiLuca.pimping.ChannelsPimping.toTry

import scala.util.Random
import scala.util.{Failure, Random}

class ChannelsContextTest extends AnyFunSpec with Matchers {

Expand All @@ -17,7 +18,7 @@ class ChannelsContextTest extends AnyFunSpec with Matchers {
describe("Consumer") {
it("read no item if the producer is run in another context") {
var i = 0
val channel = BufferedChannel[Int](itemsProduced)
val channel = BufferedChannel[Item](itemsProduced)
Async.blocking:
channel.consume {
case Left(_) => ()
Expand All @@ -27,6 +28,17 @@ class ChannelsContextTest extends AnyFunSpec with Matchers {
produceOn(channel).run.await
i shouldBe 0
}

it("receive a Cancellation exception if a channel is used as a container of futures produced in other process") {
Async.blocking:
val channel = UnboundedChannel[Future[Item]]()
Future:
for _ <- 0 to itemsProduced do channel.send(Future { AsyncOperations.sleep(5_000); 0 })
for _ <- 0 to itemsProduced do
val result = channel.read().toTry().flatMap(_.awaitResult)
result.isFailure shouldBe true
intercept[CancellationException](result.get)
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ChannelsPimpingTest extends AnyFunSpec with Matchers {
Async.blocking:
var collectedItems = Seq[Item]()
val channel = TerminableChannel.ofUnbounded[Item]
produceOn(channel).run.onComplete(Listener { (_, _) => channel.terminate() })
produceOn(channel).run.onComplete(Listener((_, _) => channel.send(Terminated)))
channel.foreach(res => collectedItems = collectedItems :+ res)
collectedItems shouldBe Seq.range(0, itemsProduced)
}
Expand Down
12 changes: 6 additions & 6 deletions docs/content/res/schemas/.$diagrams.drawio.bkp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<mxfile host="Electron" modified="2024-02-29T08:20:18.152Z" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/23.1.5 Chrome/120.0.6099.109 Electron/28.1.0 Safari/537.36" etag="JfXl1pFTbFHOHVBNmm9H" version="23.1.5" type="device" pages="2">
<mxfile host="Electron" modified="2024-03-01T08:15:59.350Z" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/23.1.5 Chrome/120.0.6099.109 Electron/28.1.0 Safari/537.36" etag="4oupKd-XbEzcRU_GyDoc" version="23.1.5" type="device" pages="2">
<diagram id="aMwOTs6f9R6VMjx3bppE" name="Page-1">
<mxGraphModel dx="500" dy="342" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1169" pageHeight="827" math="0" shadow="0">
<mxGraphModel dx="437" dy="299" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1169" pageHeight="827" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
Expand Down Expand Up @@ -101,7 +101,7 @@
<mxCell id="PeGZlQfUJxuQuzUFIjWj-4" value="&lt;font style=&quot;&quot; color=&quot;#000000&quot;&gt;&lt;span style=&quot;font-size: 7px;&quot;&gt;FUTURE/COROUTINE&lt;/span&gt;&lt;br style=&quot;border-color: var(--border-color); font-size: 7px;&quot;&gt;&lt;font style=&quot;font-size: 12px;&quot;&gt;Consumer #1&lt;/font&gt;&lt;/font&gt;" style="shape=cube;whiteSpace=wrap;html=1;boundedLbl=1;backgroundOutline=1;darkOpacity=0.05;darkOpacity2=0.1;size=10;fillColor=#FFCCE6;strokeColor=#A50040;fontColor=#ffffff;" vertex="1" parent="1">
<mxGeometry x="476" y="182" width="90" height="38" as="geometry" />
</mxCell>
<mxCell id="PeGZlQfUJxuQuzUFIjWj-5" value="&lt;font color=&quot;#000000&quot; style=&quot;font-size: 7px;&quot;&gt;FUTURE/COROUTINE&lt;br style=&quot;border-color: var(--border-color);&quot;&gt;&lt;/font&gt;&lt;font color=&quot;#000000&quot;&gt;Consumer #2&lt;/font&gt;" style="shape=cube;whiteSpace=wrap;html=1;boundedLbl=1;backgroundOutline=1;darkOpacity=0.05;darkOpacity2=0.1;size=10;fillColor=#FFCCE6;strokeColor=#A50040;fontColor=#ffffff;" vertex="1" parent="1">
<mxCell id="PeGZlQfUJxuQuzUFIjWj-5" value="&lt;font color=&quot;#000000&quot; style=&quot;font-size: 7px;&quot;&gt;FUTURE/COROUTINE&lt;br style=&quot;border-color: var(--border-color);&quot;&gt;&lt;/font&gt;&lt;font color=&quot;#000000&quot;&gt;Consumer #1&lt;/font&gt;" style="shape=cube;whiteSpace=wrap;html=1;boundedLbl=1;backgroundOutline=1;darkOpacity=0.05;darkOpacity2=0.1;size=10;fillColor=#FFCCE6;strokeColor=#A50040;fontColor=#ffffff;" vertex="1" parent="1">
<mxGeometry x="476" y="240" width="90" height="38" as="geometry" />
</mxCell>
<mxCell id="PeGZlQfUJxuQuzUFIjWj-15" value="" style="endArrow=none;dashed=1;html=1;dashPattern=1 3;strokeWidth=2;rounded=0;strokeColor=#FFFF66;" edge="1" parent="1">
Expand All @@ -122,14 +122,14 @@
<mxCell id="PeGZlQfUJxuQuzUFIjWj-21" value="" style="ellipse;whiteSpace=wrap;html=1;aspect=fixed;fillColor=#CCFF99;strokeColor=#00CC66;fontColor=#ffffff;" vertex="1" parent="1">
<mxGeometry x="372" y="222.01" width="20" height="20" as="geometry" />
</mxCell>
<mxCell id="PeGZlQfUJxuQuzUFIjWj-22" value="" style="endArrow=none;html=1;rounded=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;dashed=1;endFill=0;" edge="1" parent="1" target="PeGZlQfUJxuQuzUFIjWj-21">
<mxCell id="PeGZlQfUJxuQuzUFIjWj-22" value="" style="endArrow=none;html=1;rounded=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;dashed=1;endFill=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;dashPattern=1 1;" edge="1" parent="1" source="PeGZlQfUJxuQuzUFIjWj-23" target="PeGZlQfUJxuQuzUFIjWj-21">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="380" y="270" as="sourcePoint" />
<mxPoint x="400" y="220" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="PeGZlQfUJxuQuzUFIjWj-23" value="&lt;font style=&quot;font-size: 10px;&quot;&gt;data&lt;/font&gt;" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="362.5" y="269.88" width="35" height="16.25" as="geometry" />
<mxCell id="PeGZlQfUJxuQuzUFIjWj-23" value="&lt;font face=&quot;Noteworthy&quot; style=&quot;font-size: 10px;&quot;&gt;data&lt;/font&gt;" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="364.5" y="260.88" width="35.5" height="10.12" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
Expand Down
8 changes: 4 additions & 4 deletions docs/content/res/schemas/diagrams.drawio
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<mxfile host="Electron" modified="2024-02-29T08:22:24.652Z" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/23.1.5 Chrome/120.0.6099.109 Electron/28.1.0 Safari/537.36" etag="XYYLldC3B-y1gJl2acXH" version="23.1.5" type="device" pages="2">
<mxfile host="Electron" modified="2024-03-01T08:16:21.186Z" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/23.1.5 Chrome/120.0.6099.109 Electron/28.1.0 Safari/537.36" etag="gYcVk6lTDHdMDjumfbOW" version="23.1.5" type="device" pages="2">
<diagram id="aMwOTs6f9R6VMjx3bppE" name="Page-1">
<mxGraphModel dx="500" dy="342" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1169" pageHeight="827" math="0" shadow="0">
<root>
Expand Down Expand Up @@ -122,14 +122,14 @@
<mxCell id="PeGZlQfUJxuQuzUFIjWj-21" value="" style="ellipse;whiteSpace=wrap;html=1;aspect=fixed;fillColor=#CCFF99;strokeColor=#00CC66;fontColor=#ffffff;" vertex="1" parent="1">
<mxGeometry x="372" y="222.01" width="20" height="20" as="geometry" />
</mxCell>
<mxCell id="PeGZlQfUJxuQuzUFIjWj-22" value="" style="endArrow=none;html=1;rounded=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;dashed=1;endFill=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" edge="1" parent="1" source="PeGZlQfUJxuQuzUFIjWj-23" target="PeGZlQfUJxuQuzUFIjWj-21">
<mxCell id="PeGZlQfUJxuQuzUFIjWj-22" value="" style="endArrow=none;html=1;rounded=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;dashed=1;endFill=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;dashPattern=1 1;" edge="1" parent="1" source="PeGZlQfUJxuQuzUFIjWj-23" target="PeGZlQfUJxuQuzUFIjWj-21">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="380" y="270" as="sourcePoint" />
<mxPoint x="400" y="220" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="PeGZlQfUJxuQuzUFIjWj-23" value="&lt;font style=&quot;font-size: 8px;&quot;&gt;data&lt;/font&gt;" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="364.5" y="259.88" width="35.5" height="10.12" as="geometry" />
<mxCell id="PeGZlQfUJxuQuzUFIjWj-23" value="&lt;font face=&quot;Noteworthy&quot; style=&quot;font-size: 9px;&quot;&gt;data to be consumed&lt;/font&gt;" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="359.25" y="263.75" width="45.5" height="30.51" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
Expand Down

0 comments on commit fb27a2b

Please sign in to comment.