Skip to content

Commit fed74ef

Browse files
committed
Fix bad whenComplete
1 parent 710cdae commit fed74ef

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/manifold/deferred.clj

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,12 +1649,21 @@
16491649

16501650
result))
16511651

1652-
(defn- when-complete [this ^java.util.function.BiConsumer operator]
1653-
(assert-some operator)
1654-
(on-realized this
1655-
#(.accept operator % nil)
1656-
#(.accept operator nil %))
1657-
this)
1652+
(defn- when-complete [d ^BiConsumer f]
1653+
(assert-some f)
1654+
(let [d' (deferred)]
1655+
(on-realized d
1656+
(fn [val]
1657+
(try (.accept f val nil)
1658+
(success! d' val)
1659+
(catch Throwable err
1660+
(error! d' err))))
1661+
(fn [err]
1662+
(try (.accept f nil err)
1663+
(error! d' err)
1664+
(catch Throwable _
1665+
(error! d' err)))))
1666+
d'))
16581667

16591668
(def ^:private when-complete-async (async-for when-complete))
16601669

test/manifold/deferred_stage_test.clj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,25 @@
524524

525525
(is (thrown? RuntimeException @d1))
526526
(is (thrown? RuntimeException @d2))
527-
(is (= true @was-called))))
527+
(is (= true @was-called)))
528+
529+
(let [d1 (d/success-deferred 1)
530+
d2 (method
531+
d1
532+
(fn->BiConsumer (fn [_ _] (throw (RuntimeException.))))
533+
executor)]
534+
535+
(is (thrown? RuntimeException @d2)))
536+
537+
(let [error (RuntimeException. "d1 error")
538+
d1 (d/error-deferred error)
539+
d2 (method
540+
d1
541+
(fn->BiConsumer (fn [_ _]
542+
(throw (RuntimeException. "d2 error"))))
543+
executor)]
544+
545+
(is (thrown-with-msg? RuntimeException #"d1 error" @d2))))
528546

529547
(deftest test-when-complete
530548

0 commit comments

Comments
 (0)