File tree Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Original file line number Diff line number Diff line change 40914091
40924092(defmacro afor
40934093 "Repeatedly execute ``body`` while the binding name is repeatedly rebound to
4094- successive values from the asynchronous iterable."
4094+ successive values from the asynchronous iterable.
4095+
4096+ .. warning::
4097+
4098+ The ``afor`` macro may only be used in an asynchronous function context."
40954099 [binding & body]
40964100 (if (operator/ne 2 (count binding))
40974101 (throw
41124116(defmacro awith
41134117 "Evaluate ``body`` within a ``try`` / ``except`` expression, binding the named
41144118 expressions as per Python's async context manager protocol spec (Python's
4115- ``async with`` blocks)."
4119+ ``async with`` blocks).
4120+
4121+ .. warning::
4122+
4123+ The ``awith`` macro may only be used in an asynchronous function context."
41164124 [bindings & body]
41174125 (let [binding (first bindings)
41184126 expr (second bindings)]
Original file line number Diff line number Diff line change 1+ (ns tests.basilisp.test-core-async-macros
2+ (:import asyncio contextlib)
3+ (:require
4+ [basilisp.test :refer [deftest is are testing]]))
5+
6+ (defn async-to-sync
7+ [f & args]
8+ (let [loop (asyncio/new-event-loop)]
9+ (asyncio/set-event-loop loop)
10+ (.run-until-complete loop (apply f args))))
11+
12+ (deftest awith-test
13+ (testing "base case"
14+ (let [get-val (contextlib/asynccontextmanager
15+ (fn ^:async get-val
16+ []
17+ (yield :async-val)))
18+ val-ctxmgr (fn ^:async yield-val
19+ []
20+ (awith [v (get-val)]
21+ v))]
22+ (is (= :async-val (async-to-sync val-ctxmgr))))))
23+
24+ (deftest afor-test
25+ (testing "base case"
26+ (let [get-vals (fn ^:async get-vals
27+ []
28+ (dotimes [n 5]
29+ (yield n)))
30+ val-loop (fn ^:async val-loop
31+ []
32+ (let [a (atom [])
33+ res (afor [v (get-vals)]
34+ (swap! a conj v)
35+ v)]
36+ [@a res]))]
37+ (is (= [[0 1 2 3 4] 4] (async-to-sync val-loop))))))
You can’t perform that action at this time.
0 commit comments