Skip to content

Commit f32f4e9

Browse files
authored
Merge pull request #202 from tanzoniteblack/let-flow-bound-fn
Use `bound-fn` in `let-flow`
2 parents dbdd600 + a21a832 commit f32f4e9

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

CHANGES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
### 0.2.0-SNAPSHOT
22

3-
Contributions by Matthew Davidson
3+
Contributions by Matthew Davidson, Ryan Smith
44

5-
* Modernized indentation to match current Clojure styles and fixed misalignments
5+
* Switch to `bound-fn` in `let-flow` to fix bug where dynamic vars were incorrect for other threads
6+
* Modernized indentation to match current Clojure styles and fix misalignments
67

78
### 0.1.9
89

src/manifold/deferred.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,14 +1330,14 @@
13301330
[gensym val])
13311331
[gensym
13321332
`(~chain-fn (~zip-fn ~@deps)
1333-
(fn [[~@(map gensym->var deps)]]
1333+
(bound-fn [[~@(map gensym->var deps)]]
13341334
~val))])))
13351335
(range)
13361336
vars'
13371337
vals'
13381338
gensyms)]
13391339
(~chain-fn (onto (~zip-fn ~@body-dep?) executor#)
1340-
(fn [[~@(map gensym->var body-dep?)]]
1340+
(bound-fn [[~@(map gensym->var body-dep?)]]
13411341
~@body)))))))
13421342

13431343
(defmacro let-flow

test/manifold/deferred_test.clj

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
[manifold.utils :as utils]
66
[clojure.test :refer :all]
77
[manifold.test-utils :refer :all]
8-
[manifold.deferred :as d]))
8+
[manifold.deferred :as d]
9+
[manifold.executor :as ex]))
910

1011
(defmacro future' [& body]
1112
`(d/future
@@ -72,6 +73,8 @@
7273
(d/chain #(/ 1 %))
7374
(d/catch ArithmeticException (constantly :foo))))))
7475

76+
(def ^:dynamic *test-dynamic-var*)
77+
7578
(deftest test-let-flow
7679

7780
(let [flag (atom false)]
@@ -93,7 +96,18 @@
9396
(d/let-flow [[x] (future' [1])]
9497
(d/let-flow [[x'] (future' [(inc x)])
9598
y (future' true)]
96-
(when y x')))))))
99+
(when y x'))))))
100+
101+
(testing "let-flow callbacks happen on different executor retain thread bindings"
102+
(let [d (d/deferred (ex/fixed-thread-executor 1))
103+
test-internal-fn (fn [] (let [x *test-dynamic-var*]
104+
(d/future (Thread/sleep 100) (d/success! d x))))]
105+
(binding [*test-dynamic-var* "cat"]
106+
(test-internal-fn)
107+
(is (= ["cat" "cat" "cat"]
108+
@(d/let-flow [a d
109+
b (do a *test-dynamic-var*)]
110+
[a b *test-dynamic-var*])))))))
97111

98112
(deftest test-chain-errors
99113
(let [boom (fn [n] (throw (ex-info "" {:n n})))]
@@ -388,3 +402,4 @@
388402
(Thread/sleep (rand-int 10))
389403
(d/success! d 1)
390404
(is (= 1 @@result)))))
405+

0 commit comments

Comments
 (0)