|
1 | 1 | (ns cljs.async-await-test |
2 | 2 | (:refer-global :only [Date Promise]) |
3 | | - (:require [clojure.test :refer [deftest is async]] |
| 3 | + (:require [clojure.test :refer [deftest is] :as t] |
4 | 4 | [cljs.core :as cc :refer [await] :rename {await aw}] |
5 | 5 | [goog.object :as gobj]) |
6 | 6 | (:require-macros [cljs.macro-test.macros :refer [await!] :as macros])) |
|
14 | 14 | (+ n x y (f)))) |
15 | 15 |
|
16 | 16 | (deftest ^:async defn-test |
17 | | - (try |
18 | | - (let [v (await (foo 10))] |
19 | | - (is (= 61 v))) |
20 | | - (let [v (await (apply foo [10]))] |
21 | | - (is (= 61 v))) |
22 | | - (catch :default _ (is false)))) |
| 17 | + (let [v (await (foo 10))] |
| 18 | + (is (= 61 v))) |
| 19 | + (let [v (await (apply foo [10]))] |
| 20 | + (is (= 61 v)))) |
23 | 21 |
|
24 | 22 | (defn ^:async variadic-foo [n & ns] |
25 | 23 | (let [x (await (js/Promise.resolve n)) |
|
30 | 28 | (+ n x y (f)))) |
31 | 29 |
|
32 | 30 | (deftest ^:async variadic-defn-test |
33 | | - (try |
34 | | - (let [v (await (variadic-foo 10))] |
35 | | - (is (= 41 v))) |
36 | | - (let [v (await (variadic-foo 10 1 2 3))] |
37 | | - (is (= 47 v))) |
38 | | - (let [v (await (apply variadic-foo [10 1 2 3]))] |
39 | | - (is (= 47 v))) |
40 | | - (catch :default _ (is false)))) |
| 31 | + (let [v (await (variadic-foo 10))] |
| 32 | + (is (= 41 v))) |
| 33 | + (let [v (await (variadic-foo 10 1 2 3))] |
| 34 | + (is (= 47 v))) |
| 35 | + (let [v (await (apply variadic-foo [10 1 2 3]))] |
| 36 | + (is (= 47 v)))) |
41 | 37 |
|
42 | 38 | (defn ^:async multi-arity-foo |
43 | 39 | ([n] (await n)) |
44 | 40 | ([n x] (+ (await n) x))) |
45 | 41 |
|
46 | 42 | (deftest ^:async multi-arity-defn-test |
47 | | - (try |
48 | | - (let [v (await (multi-arity-foo 10))] |
49 | | - (is (= 10 v))) |
50 | | - (let [v (await (multi-arity-foo 10 20))] |
51 | | - (is (= 30 v))) |
52 | | - (let [v (await (apply multi-arity-foo [10]))] |
53 | | - (is (= 10 v))) |
54 | | - (let [v (await (apply multi-arity-foo [10 20]))] |
55 | | - (is (= 30 v))) |
56 | | - (catch :default _ (is false)))) |
| 43 | + (let [v (await (multi-arity-foo 10))] |
| 44 | + (is (= 10 v))) |
| 45 | + (let [v (await (multi-arity-foo 10 20))] |
| 46 | + (is (= 30 v))) |
| 47 | + (let [v (await (apply multi-arity-foo [10]))] |
| 48 | + (is (= 10 v))) |
| 49 | + (let [v (await (apply multi-arity-foo [10 20]))] |
| 50 | + (is (= 30 v)))) |
57 | 51 |
|
58 | 52 | (defn ^:async multi-arity-variadic-foo |
59 | 53 | ([n] (await n)) |
60 | 54 | ([n & xs] (apply + (await n) xs))) |
61 | 55 |
|
62 | 56 | (deftest ^:async multi-arity-variadic-test |
63 | | - (try |
64 | | - (let [v (await (multi-arity-variadic-foo 10))] |
65 | | - (is (= 10 v))) |
66 | | - (let [v (await (multi-arity-variadic-foo 10 20))] |
67 | | - (is (= 30 v))) |
68 | | - (let [v (await (apply multi-arity-variadic-foo [10]))] |
69 | | - (is (= 10 v))) |
70 | | - (let [v (await (apply multi-arity-variadic-foo [10 20]))] |
71 | | - (is (= 30 v))) |
72 | | - (catch :default _ (is false)))) |
| 57 | + (let [v (await (multi-arity-variadic-foo 10))] |
| 58 | + (is (= 10 v))) |
| 59 | + (let [v (await (multi-arity-variadic-foo 10 20))] |
| 60 | + (is (= 30 v))) |
| 61 | + (let [v (await (apply multi-arity-variadic-foo [10]))] |
| 62 | + (is (= 10 v))) |
| 63 | + (let [v (await (apply multi-arity-variadic-foo [10 20]))] |
| 64 | + (is (= 30 v)))) |
73 | 65 |
|
74 | 66 | (deftest ^:async fn-test |
75 | | - (try |
76 | | - (let [f (^:async fn [x] (+ x (await (js/Promise.resolve 20)))) |
77 | | - v (await (f 10)) |
78 | | - v2 (await (apply f [10]))] |
79 | | - (is (= 30 v v2))) |
80 | | - (catch :default _ (is false)))) |
| 67 | + (let [f (^:async fn [x] (+ x (await (js/Promise.resolve 20)))) |
| 68 | + v (await (f 10)) |
| 69 | + v2 (await (apply f [10]))] |
| 70 | + (is (= 30 v v2)))) |
81 | 71 |
|
82 | 72 | (deftest ^:async varargs-fn-test |
83 | | - (try |
84 | | - (let [f (^:async fn [x & xs] (apply + x (await (js/Promise.resolve 20)) xs)) |
85 | | - v (await (f 10)) |
86 | | - v2 (await (apply f [10])) |
87 | | - v3 (await (f 5 5)) |
88 | | - v4 (await (apply f [5 5]))] |
89 | | - (is (= 30 v v2 v3 v4))) |
90 | | - (catch :default _ (is false)))) |
| 73 | + (let [f (^:async fn [x & xs] (apply + x (await (js/Promise.resolve 20)) xs)) |
| 74 | + v (await (f 10)) |
| 75 | + v2 (await (apply f [10])) |
| 76 | + v3 (await (f 5 5)) |
| 77 | + v4 (await (apply f [5 5]))] |
| 78 | + (is (= 30 v v2 v3 v4)))) |
91 | 79 |
|
92 | 80 | (deftest ^:async variadic-fn-test |
93 | | - (try (let [f (^:async fn |
94 | | - ([x] (await (js/Promise.resolve x))) |
95 | | - ([x y] (cons (await (js/Promise.resolve x)) [y])))] |
96 | | - (is (= [1 1 [1 2] [1 2]] |
97 | | - [(await (f 1)) |
98 | | - (await (apply f [1])) |
99 | | - (await (f 1 2)) |
100 | | - (await (apply f [1 2]))]))) |
101 | | - (catch :default _ (is false)))) |
| 81 | + (let [f (^:async fn |
| 82 | + ([x] (await (js/Promise.resolve x))) |
| 83 | + ([x y] (cons (await (js/Promise.resolve x)) [y])))] |
| 84 | + (is (= [1 1 [1 2] [1 2]] |
| 85 | + [(await (f 1)) |
| 86 | + (await (apply f [1])) |
| 87 | + (await (f 1 2)) |
| 88 | + (await (apply f [1 2]))])))) |
102 | 89 |
|
103 | 90 | (deftest ^:async variadic-varargs-fn-test |
104 | | - (try (let [f (^:async fn |
105 | | - ([x] (await (js/Promise.resolve x))) |
106 | | - ([x & xs] (cons (await (js/Promise.resolve x)) xs)))] |
107 | | - (is (= [1 1 [1 2 3] [1 2 3]] |
108 | | - [(await (f 1)) |
109 | | - (await (apply f [1])) |
110 | | - (await (f 1 2 3)) |
111 | | - (await (apply f [1 2 3]))]))) |
112 | | - (catch :default _ (is false)))) |
| 91 | + (let [f (^:async fn |
| 92 | + ([x] (await (js/Promise.resolve x))) |
| 93 | + ([x & xs] (cons (await (js/Promise.resolve x)) xs)))] |
| 94 | + (is (= [1 1 [1 2 3] [1 2 3]] |
| 95 | + [(await (f 1)) |
| 96 | + (await (apply f [1])) |
| 97 | + (await (f 1 2 3)) |
| 98 | + (await (apply f [1 2 3]))])))) |
113 | 99 |
|
114 | 100 | (deftest ^:async await-in-throw-test |
115 | 101 | (let [f (^:async fn [x] (inc (if (odd? x) (throw (await (js/Promise.resolve "dude"))) x)))] |
116 | | - (try |
117 | | - (let [x (await (f 2))] |
118 | | - (is (= 3 x))) |
119 | | - (let [x (try (await (f 1)) |
120 | | - (catch :default e e))] |
121 | | - (is (= "dude" x))) |
122 | | - (catch :default _ (is false))))) |
| 102 | + (let [x (await (f 2))] |
| 103 | + (is (= 3 x))) |
| 104 | + (let [x (try (await (f 1)) |
| 105 | + (catch :default e e))] |
| 106 | + (is (= "dude" x))))) |
123 | 107 |
|
124 | 108 | (deftest ^:async await-in-do-test |
125 | | - (try |
126 | | - (let [a (atom 0) |
127 | | - f (^:async fn [] (let [_ (do (swap! a inc) |
128 | | - (swap! a + (await (js/Promise.resolve 2))))] |
129 | | - @a)) |
130 | | - v (await (f))] |
131 | | - (is (= 3 v))) |
132 | | - (catch :default _ (is false)))) |
| 109 | + (let [a (atom 0) |
| 110 | + f (^:async fn [] (let [_ (do (swap! a inc) |
| 111 | + (swap! a + (await (js/Promise.resolve 2))))] |
| 112 | + @a)) |
| 113 | + v (await (f))] |
| 114 | + (is (= 3 v)))) |
133 | 115 |
|
134 | 116 | (deftest ^:async await-let-fn-test |
135 | | - (try |
136 | | - (let [f (^:async fn [] (let [v |
137 | | - ;; force letfn in expr position |
138 | | - (letfn [(^:async f [] (inc (await (js/Promise.resolve 10))))] |
139 | | - (inc (await (f))))] |
140 | | - (identity v))) |
141 | | - v (await (f))] |
142 | | - (is (= 12 v))) |
143 | | - (catch :default _ (is false)))) |
| 117 | + (let [f (^:async fn [] (let [v |
| 118 | + ;; force letfn in expr position |
| 119 | + (letfn [(^:async f [] (inc (await (js/Promise.resolve 10))))] |
| 120 | + (inc (await (f))))] |
| 121 | + (identity v))) |
| 122 | + v (await (f))] |
| 123 | + (is (= 12 v)))) |
144 | 124 |
|
145 | 125 | (deftest ^:async await-in-loop-test |
146 | | - (try |
147 | | - (let [f (^:async fn [] (let [x |
148 | | - ;; force loop in expr position |
149 | | - (loop [xs (map #(js/Promise.resolve %) [1 2 3]) |
150 | | - ys []] |
151 | | - (if (seq xs) |
152 | | - (let [x (first xs) |
153 | | - v (await x)] |
154 | | - (recur (rest xs) (conj ys v))) |
155 | | - ys))] |
156 | | - (identity x))) |
157 | | - v (await (f))] |
158 | | - (is (= [1 2 3] v))) |
159 | | - (catch :default _ (is false)))) |
| 126 | + (let [f (^:async fn [] (let [x |
| 127 | + ;; force loop in expr position |
| 128 | + (loop [xs (map #(js/Promise.resolve %) [1 2 3]) |
| 129 | + ys []] |
| 130 | + (if (seq xs) |
| 131 | + (let [x (first xs) |
| 132 | + v (await x)] |
| 133 | + (recur (rest xs) (conj ys v))) |
| 134 | + ys))] |
| 135 | + (identity x))) |
| 136 | + v (await (f))] |
| 137 | + (is (= [1 2 3] v)))) |
160 | 138 |
|
161 | 139 | (deftest ^:async await-in-nested |
162 | | - (try |
163 | | - (let [f (^:async fn [] |
164 | | - (let [b1 1 |
165 | | - b2 (let [x 2] |
166 | | - (+ x |
167 | | - ;; outer let doesn't have awaits |
168 | | - ;; but inner let does, so outer let should become async |
169 | | - (let [x (await (js/Promise.resolve 1))] x))) |
170 | | - b3 (case :foo :foo (case :foo :foo (await (js/Promise.resolve 1)))) |
171 | | - b4 (int ;; wrapped in int to avoid false positive warning: |
172 | | - ;; all arguments must be numbers, got [number |
173 | | - ;; ignore] instead |
174 | | - (try (throw (throw (await (js/Promise.resolve 1)))) (catch :default _ 1 ))) |
175 | | - a (atom 0) |
176 | | - b5 (do (swap! a inc) (swap! a inc) |
177 | | - ;; do with single expr, wrapped in identity to avoid merging with upper do |
178 | | - (identity (do (swap! a (await (js/Promise.resolve inc))))) |
179 | | - ;; do with multiple exprs, wrapped identity to avoid merging with upper do |
180 | | - (identity (do (swap! a inc) (swap! a (await (js/Promise.resolve inc))))) |
181 | | - @a) |
182 | | - b6 (try (identity (try 1 (finally (await nil)))) |
183 | | - (finally nil)) |
184 | | - b7 (letfn [(f [x] x)] |
185 | | - (f (letfn [(f [x] x)] |
186 | | - (f (await 1)))))] |
187 | | - (await (+ b1 b2 b3 b4 b5 b6 b7))))] |
188 | | - (is (= 13 (await (f))))) |
189 | | - (catch :default _ (is false)))) |
| 140 | + (let [f (^:async fn [] |
| 141 | + (let [b1 1 |
| 142 | + b2 (let [x 2] |
| 143 | + (+ x |
| 144 | + ;; outer let doesn't have awaits |
| 145 | + ;; but inner let does, so outer let should become async |
| 146 | + (let [x (await (js/Promise.resolve 1))] x))) |
| 147 | + b3 (case :foo :foo (case :foo :foo (await (js/Promise.resolve 1)))) |
| 148 | + b4 (int ;; wrapped in int to avoid false positive warning: |
| 149 | + ;; all arguments must be numbers, got [number |
| 150 | + ;; ignore] instead |
| 151 | + (try (throw (throw (await (js/Promise.resolve 1)))) (catch :default _ 1 ))) |
| 152 | + a (atom 0) |
| 153 | + b5 (do (swap! a inc) (swap! a inc) |
| 154 | + ;; do with single expr, wrapped in identity to avoid merging with upper do |
| 155 | + (identity (do (swap! a (await (js/Promise.resolve inc))))) |
| 156 | + ;; do with multiple exprs, wrapped identity to avoid merging with upper do |
| 157 | + (identity (do (swap! a inc) (swap! a (await (js/Promise.resolve inc))))) |
| 158 | + @a) |
| 159 | + b6 (try (identity (try 1 (finally (await nil)))) |
| 160 | + (finally nil)) |
| 161 | + b7 (letfn [(f [x] x)] |
| 162 | + (f (letfn [(f [x] x)] |
| 163 | + (f (await 1)))))] |
| 164 | + (await (+ b1 b2 b3 b4 b5 b6 b7))))] |
| 165 | + (is (= 13 (await (f)))))) |
190 | 166 |
|
191 | 167 | (deftest ^:async await-with-aliases-or-renamed-and-via-macros-test |
192 | | - (try |
193 | | - (let [a (await! (js/Promise.resolve 1)) |
194 | | - b (macros/await! (js/Promise.resolve 1)) |
195 | | - c (cc/await (js/Promise.resolve 1)) |
196 | | - d (aw (js/Promise.resolve 1)) |
197 | | - e (cljs.core/await (js/Promise.resolve 1)) |
198 | | - f (clojure.core/await (js/Promise.resolve 1))] |
199 | | - (is (= 1 a)) |
200 | | - (is (= 1 b)) |
201 | | - (is (= 1 c)) |
202 | | - (is (= 1 d)) |
203 | | - (is (= 1 e)) |
204 | | - (is (= 1 f))) |
205 | | - (catch :default _ (is false)))) |
| 168 | + (let [a (await! (js/Promise.resolve 1)) |
| 169 | + b (macros/await! (js/Promise.resolve 1)) |
| 170 | + c (cc/await (js/Promise.resolve 1)) |
| 171 | + d (aw (js/Promise.resolve 1)) |
| 172 | + e (cljs.core/await (js/Promise.resolve 1)) |
| 173 | + f (clojure.core/await (js/Promise.resolve 1))] |
| 174 | + (is (= 1 a)) |
| 175 | + (is (= 1 b)) |
| 176 | + (is (= 1 c)) |
| 177 | + (is (= 1 d)) |
| 178 | + (is (= 1 e)) |
| 179 | + (is (= 1 f)))) |
206 | 180 |
|
207 | 181 | (deftest ^:async await-with-ctor |
208 | 182 | (let [f (^:async fn [] (Date. (await (Promise/resolve 0))))] |
|
235 | 209 | (let [res (await (async-destructure {:foo 1}))] |
236 | 210 | (is (= [1 "hello!"] res)))) |
237 | 211 |
|
| 212 | +(def throw-in-async-error-test (volatile! false)) |
| 213 | + |
| 214 | +(deftest ^:async simulated-async-error-test |
| 215 | + (when @throw-in-async-error-test |
| 216 | + (throw (js/Error. "simulated"))) |
| 217 | + (is (not @throw-in-async-error-test))) |
| 218 | + |
| 219 | +(def async-error-reports (atom [])) |
| 220 | + |
| 221 | +(defmethod t/report [::error-capture :error] [m] |
| 222 | + (swap! async-error-reports conj m)) |
| 223 | + |
| 224 | +(deftest ^:async async-test-error-reporting-test |
| 225 | + (reset! async-error-reports []) |
| 226 | + (let [outer-env (t/get-current-env) |
| 227 | + reports (try |
| 228 | + (t/set-env! (t/empty-env ::error-capture)) |
| 229 | + (vreset! throw-in-async-error-test true) |
| 230 | + (await (js/Promise. |
| 231 | + (fn [resolve _] |
| 232 | + (t/run-block |
| 233 | + (concat (t/test-var-block (var simulated-async-error-test)) |
| 234 | + [(fn [] (resolve @async-error-reports))]))))) |
| 235 | + (finally |
| 236 | + (vreset! throw-in-async-error-test false) |
| 237 | + (t/set-env! outer-env)))] |
| 238 | + (is (= 1 (count reports))) |
| 239 | + (let [{:keys [message actual]} (first reports)] |
| 240 | + (is (= "Uncaught exception, not in assertion." message)) |
| 241 | + (is (= "simulated" (ex-message actual)))))) |
| 242 | + |
238 | 243 | (comment |
239 | 244 | (clojure.test/run-tests) |
240 | 245 | ) |
0 commit comments