1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-21 10:47:40 +00:00

Add semantics for closing channels.

This makes certain algorithms simpler as channels
now have an explicit lifetime - multiple readers can coordinate
closing without needing to ensure the same number of reads as writes.
This commit is contained in:
Calvin Rose
2021-07-30 19:26:42 -05:00
parent 7e5f226480
commit e76b8da269
2 changed files with 94 additions and 15 deletions

View File

@@ -116,7 +116,6 @@
(assert (= "123\n456\n" (string (slurp "unique.txt"))) "File writing 4.2")
(os/rm "unique.txt"))
# ev/gather
(assert (deep= @[1 2 3] (ev/gather 1 2 3)) "ev/gather 1")
@@ -180,4 +179,46 @@
(assert (os/execute [janet "-e" `(+ 1 2 3)`] :xp) "os/execute self")
# Test some channel
(def c1 (ev/chan))
(def c2 (ev/chan))
(def arr @[])
(ev/spawn
(while (def x (ev/take c1))
(array/push arr x))
(ev/chan-close c2))
(for i 0 1000
(ev/give c1 i))
(ev/chan-close c1)
(ev/take c2)
(assert (= (slice arr) (slice (range 1000))) "ev/chan-close 1")
(def c1 (ev/chan))
(def c2 (ev/chan))
(def arr @[])
(ev/spawn
(while (def x (ev/take c1))
(array/push arr x))
(ev/sleep 0.1)
(ev/chan-close c2))
(for i 0 100
(ev/give c1 i))
(ev/chan-close c1)
(ev/select c2)
(assert (= (slice arr) (slice (range 100))) "ev/chan-close 2")
(def c1 (ev/chan))
(def c2 (ev/chan))
(def arr @[])
(ev/spawn
(while (def x (ev/take c1))
(array/push arr x))
(ev/chan-close c2))
(for i 0 100
(ev/give c1 i))
(ev/chan-close c1)
(ev/rselect c2)
(assert (= (slice arr) (slice (range 100))) "ev/chan-close 3")
(end-suite)