Skip to content

Commit a731243

Browse files
foguspuredanger
authored andcommitted
ASYNC-274: Modified the operation of futurize to account for the change to Executor interface in ASYNC-272. The futurize function now creates and returns future.
1 parent c2f4536 commit a731243

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/main/clojure/clojure/core/async/flow/impl.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[clojure.core.async.impl.dispatch :as disp]
1515
[clojure.walk :as walk]
1616
[clojure.datafy :as datafy])
17-
(:import [java.util.concurrent Future Executors Executor TimeUnit]
17+
(:import [java.util.concurrent Future Executor TimeUnit CompletableFuture]
1818
[java.util.concurrent.locks ReentrantLock]))
1919

2020
(set! *warn-on-reflection* true)
@@ -30,8 +30,10 @@
3030
(fn [& args]
3131
(let [^Executor e (if (instance? Executor exec)
3232
exec
33-
(disp/executor-for exec))]
34-
(.execute e ^Runnable #(apply f args)))))
33+
(disp/executor-for exec))
34+
fut (CompletableFuture.)]
35+
(.execute e #(.complete fut (apply f args)))
36+
fut)))
3537

3638
(defn prep-proc [ret pid {:keys [proc, args, chan-opts] :or {chan-opts {}}}]
3739
(let [{:keys [ins outs signal-select]} (spi/describe proc)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
;; Copyright (c) Rich Hickey and contributors. All rights reserved.
2+
;; The use and distribution terms for this software are covered by the
3+
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
;; which can be found in the file epl-v10.html at the root of this distribution.
5+
;; By using this software in any fashion, you are agreeing to be bound by
6+
;; the terms of this license.
7+
;; You must not remove this notice, or any other, from this software.
8+
9+
(ns clojure.core.async.flow-test
10+
(:require [clojure.test :refer :all]
11+
[clojure.core.async.flow :as flow]))
12+
13+
(deftest test-futurize
14+
(testing ""
15+
(let [in-es? (atom false)
16+
es (reify java.util.concurrent.Executor
17+
(^void execute [_ ^Runnable f]
18+
(reset! in-es? true)
19+
(future-call f)))]
20+
(is (= 16 @((flow/futurize #(* % %) {:exec :mixed}) 4)))
21+
(is (= 16 @((flow/futurize #(* % %)) 4)))
22+
(is (= 16 @((flow/futurize #(* % %) {:exec es}) 4)))
23+
(is @in-es?))))
24+

0 commit comments

Comments
 (0)