Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
42 changes: 25 additions & 17 deletions src/manifold/stream.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1154,23 +1154,31 @@
out (sliding-stream n in)]
(splice in out)))
([n source]
(let [sink (stream n)]
(connect-via
source
(fn [val]
(d/loop []
(d/chain
(try-put! sink val 0 ::timeout)
(fn [put-result]
(case put-result
true true
false false
::timeout (d/chain (take! sink)
(fn [_] (d/recur))))))))
sink
{:upstream? true
:downstream? true})
sink)))
(let [buf (stream)
sink (stream n)]
(connect-via-proxy source buf sink {:description {:op "sliding_stream"}})
(d/loop []
(d/chain'
(take! buf ::none)
(fn [val]
(if (identical? val ::none)
(close! sink)
(d/chain'
(d/loop []
(d/chain'
(try-put! sink val 0 ::timeout)
(fn [put-result]
(case put-result
(true false) put-result
::timeout
(d/chain'
(take! sink ::empty)
(fn [x]
(when-not (identical? x ::empty)
(d/recur))))))))
(fn [_]
(d/recur)))))))
(->sink sink))))

;;;

Expand Down
20 changes: 14 additions & 6 deletions test/manifold/stream_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -467,17 +467,25 @@

(deftest test-window-streams
(testing "dropping-stream"
(let [s (s/->source (range 11))
(let [s (s/->source (range 11))
dropping-s (s/dropping-stream 10 s)]
(is (= (range 10)
(s/stream->seq dropping-s)))))

(testing "sliding-stream"
(let [s (s/->source (range 11))
sliding-s (s/sliding-stream 10 s)]
(is (= (range 1 11)
(s/stream->seq sliding-s))))))

(let [in (s/stream)
sliding-s (s/sliding-stream 3 in)]
(testing "passthrough within buffer size"
@(s/put! in 1)
(is (= 1 @(s/try-take! sliding-s 0))))
(testing "discards oldest elements when blocked"
@(s/put-all! in [1 2 3 4])
(is (= 2 @(s/try-take! sliding-s 0)))
(is (= 3 @(s/try-take! sliding-s 0)))
(is (= 4 @(s/try-take! sliding-s 0))))
(testing "propagates closes"
(s/close! in)
(is (= ::closed @(s/take! sliding-s ::closed)))))))
;;;

(deftest ^:stress stress-buffered-stream
Expand Down