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
16 changes: 11 additions & 5 deletions src/manifold/deferred.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1293,9 +1293,12 @@
(let [timeout-d (time/in interval
#(error! d
(TimeoutException.
(str "timed out after " interval " milliseconds"))))]
(chain d (fn [_]
(success! timeout-d true)))))
(str "timed out after " interval " milliseconds"))))]
(on-realized d
(fn [_]
(success! timeout-d true))
(fn [_]
(success! timeout-d false)))))
d)
([d interval timeout-value]
(cond
Expand All @@ -1308,8 +1311,11 @@
:else
(let [timeout-d (time/in interval
#(success! d timeout-value))]
(chain d (fn [_]
(success! timeout-d true)))))
(on-realized d
(fn [_]
(success! timeout-d true))
(fn [_]
(success! timeout-d false)))))
d))

(deftype+ Recur [s]
Expand Down
22 changes: 22 additions & 0 deletions test/manifold/deferred_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@
(is (= 1 @d))
(is (= 1 (deref d 10 :foo)))))

(deftest test-timeout
(testing "exception by default"
(let [d (d/deferred)
t (d/timeout! d 1)]
(is (identical? d t))
(is (thrown-with-msg? TimeoutException
#"^timed out after 1 milliseconds$"
@d))))

(testing "custom default value"
(let [d (d/deferred)
t (d/timeout! d 1 ::timeout)]
(is (identical? d t))
(is @(capture-success d ::timeout))))

(testing "error before timeout"
(let [ex (Exception.)
d (d/deferred)
t (d/timeout! d 1000)]
(d/error! d ex)
(is (= ex (deref (capture-error t) 100 ::error))))))
Comment on lines +260 to +262
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these numbers need to be so big?

if smaller works too that'd be better for faster tests

Suggested change
t (d/timeout! d 1000)]
(d/error! d ex)
(is (= ex (deref (capture-error t) 100 ::error))))))
t (d/timeout! d 20)]
(d/error! d ex)
(is (= ex (deref (capture-error t) 10 ::error))))))


(deftest test-loop
;; body produces a non-deferred value
(is @(capture-success
Expand Down