Skip to content

Commit

Permalink
fix: ensure we only call clojure.core/realized? on IPending
Browse files Browse the repository at this point in the history
  • Loading branch information
k13gomez committed Feb 11, 2024
1 parent 04e0737 commit 218f038
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
26 changes: 20 additions & 6 deletions src/futurama/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[futurama.state :as state]
[futurama.deferred]
[manifold.deferred :as d])
(:import [clojure.lang Var IDeref IFn]
(:import [clojure.lang Var IDeref IPending IFn]
[java.util.concurrent
CompletableFuture
CompletionException
Expand Down Expand Up @@ -122,6 +122,10 @@
res-fut#))))

(extend-type Future
proto/AsyncPending
(realized? [fut]
(.isDone ^Future fut))

proto/AsyncCancellable
(cancel [this]
(future-cancel this))
Expand All @@ -139,7 +143,7 @@
take-cb))]
(when-let [cb (commit-handler)]
(cond
(realized? fut)
(proto/realized? fut)
(let [val (try
(.get ^Future fut)
(catch Throwable e
Expand Down Expand Up @@ -167,12 +171,18 @@

impl/Channel
(close! [fut]
(when-not (realized? fut)
(when-not (proto/realized? fut)
(future-cancel ^Future fut)))
(closed? [fut]
(realized? ^Future fut)))
(proto/realized? ^Future fut)))

(extend-type IDeref
proto/AsyncPending
(realized? [ref]
(if (instance? IPending ref)
(realized? ref)
true))

impl/ReadPort
(take! [ref handler]
(let [^IDeref ref ref
Expand All @@ -184,7 +194,7 @@
take-cb))]
(when-let [cb (commit-handler)]
(cond
(realized? ref)
(proto/realized? ref)
(let [val (try
(deref ref)
(catch Throwable e
Expand Down Expand Up @@ -215,9 +225,13 @@
(when (instance? IFn ref)
(ref nil)))
(closed? [ref]
(realized? ref)))
(proto/realized? ref)))

(extend-type CompletableFuture
proto/AsyncPending
(realized? [fut]
(.isDone ^CompletableFuture fut))

proto/AsyncCancellable
(cancel [this]
(future-cancel this))
Expand Down
6 changes: 5 additions & 1 deletion src/futurama/protocols.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
(ns futurama.protocols)
(ns futurama.protocols
(:refer-clojure :exclude [realized?]))

(defprotocol AsyncCancellable
(cancel [this])
(cancelled? [this]))

(defprotocol AsyncPending
(realized? [this]))
7 changes: 4 additions & 3 deletions test/futurama/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,10 @@
(>! c {:foo "bar"})
(delay
(future
(let [p (promise)]
(deliver p c)
p)))))))))))))
(atom
(let [p (promise)]
(deliver p c)
p))))))))))))))
(testing "nested non-blocking take - !<!"
(<!!
(async
Expand Down

0 comments on commit 218f038

Please sign in to comment.