This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-04-23
Channels
- # babashka (18)
- # babashka-sci-dev (42)
- # beginners (84)
- # calva (11)
- # cider (5)
- # clj-kondo (11)
- # cljdoc (70)
- # cljs-dev (34)
- # clojure-europe (1)
- # clojurescript (3)
- # conjure (1)
- # core-async (29)
- # data-oriented-programming (10)
- # emacs (13)
- # fulcro (8)
- # gratitude (2)
- # honeysql (1)
- # introduce-yourself (4)
- # kaocha (10)
- # missionary (8)
- # nrepl (4)
- # off-topic (27)
- # portal (32)
- # releases (11)
- # tools-deps (11)
- # xtdb (19)
the code being tested will publish a known number of messages to the channel but trying to avoid the looping and simplify the code in the test
I think it's possibly not perfect, because as you poll! it is technically possible that you polled the last thing pending in the channel, but by the time the next iteration calls poll again, something was delivered to the channel.
So there's a small time window where poll! could have failed, but because something got concurrently delivered it'll succeed another round.
Like if I run this:
(def chan (a/chan 100))
(def ccount (atom 0))
(a/go
(dotimes [i 300]
(a/>! chan i)))
(while (a/poll! chan)
(swap! ccount inc))
(println @ccount)
It doesn't always print 100 perfectly. Sometimes it is a bit more. Probably because the go block added more things when poll! polled the 100ed item, and by the time poll! was call again there was now more things to poll.The thing is, you can't lock the channel 😛 that's kind of the whole point of core.async. So I'm not sure if there's a better way.
(.full? (.-buf chan))
This tells you if it is full.
So in your test, you could check until it is full?, and then get the count to see if it is the count you expect.(do
(def chan (a/chan 100))
(a/go
(dotimes [i 300]
(a/>! chan i)))
(while (not (.full? (.-buf chan))))
(println (count (.-buf chan))))
Always gives me 100 exactly@U4ZJ5UHQD don't use poll in this case, use reduce. Reduce is always the way to drain/consume a source. It respects channel semantics, blocking and time. Poll doesn't