Skip to content

Commit

Permalink
add better tests
Browse files Browse the repository at this point in the history
Signed-off-by: Bailey Kocin <bkocin@crossbeam.com>
  • Loading branch information
Bailey Kocin authored and ghaskins committed May 16, 2024
1 parent 2693986 commit 9e3f242
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
15 changes: 8 additions & 7 deletions src/temporal/promise.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ For more Java SDK samples example look here:
```
"
[coll]
(letfn [(wait! [^Promise p] (try (.get p) (catch Exception _)))]
(-> (into-array Promise (mapv wait! (->array coll)))
(Promise/allOf)
(pt/->PromiseAdapter)
;; The promises are all completed at this point,
;; this is just to use the promesa library
(p/then (fn [_] (mapv deref coll))))))
(letfn [(wait! [^Promise p] (try (.get p) p (catch Exception _ p)))]
(->
(into-array Promise (mapv wait! (->array coll)))
(Promise/allOf)
(pt/->PromiseAdapter)
;; The promises are all completed at this point,
;; this is just to use the promesa library
(p/then (fn [_] (mapv deref coll))))))

(defn race
"Returns Promise that becomes completed when any of the arguments are completed.
Expand Down
19 changes: 15 additions & 4 deletions test/temporal/test/concurrency.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,34 @@
(is (-> workflow c/get-result deref count (= 10))))))

(defactivity all-settled-activity
[ctx args] args)
[ctx args]
(log/tracef "calling all-settled-activity %d" args)
args)

(defn invoke-settled [x]
(a/invoke all-settled-activity x))

(defworkflow all-settled-workflow
[args]
@(-> (pt/all (map #(a/invoke all-settled-activity %) (range 10)))
(log/trace "calling all-settled-workflow")
@(-> (pt/all-settled (map invoke-settled (range 10)))
(p/then (fn [r] r))
(p/catch (fn [e] (:args (ex-data e))))))

(defactivity error-prone-activity
[ctx args]
(log/tracef "calling error-prone-activity %d" args)
(when (= args 5)
(throw (ex-info "error on 5" {:args args})))
args)

(defn invoke-error [x]
(a/invoke error-prone-activity x))

(defworkflow error-prone-workflow
[args]
@(-> (pt/all (map #(a/invoke error-prone-activity %) (range 10)))
(log/trace "calling error-prone-workflow")
@(-> (pt/all-settled (map invoke-error (range 10)))
(p/then (fn [r] r))
(p/catch (fn [e] (:args (ex-data e))))))

Expand All @@ -62,7 +73,7 @@
(c/start workflow {})
(is (-> workflow c/get-result deref count (= 10)))))
(testing "Testing that all-settled waits for all the activities to complete
despite error and can return the errors"
just like `p/all` and can still propogate errors"
(let [workflow (t/create-workflow error-prone-workflow)]
(c/start workflow {})
(is (-> workflow c/get-result deref (= 5))))))

0 comments on commit 9e3f242

Please sign in to comment.