Skip to content

Commit

Permalink
Merge branch 'master' into nil-component
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Dec 30, 2024
2 parents b861d80 + 5f2d9d2 commit 306587a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
29 changes: 18 additions & 11 deletions test/darkleaf/di/dependencies_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require
[clojure.test :as t]
[darkleaf.di.core :as di]
[darkleaf.di.protocols :as dip]))
[darkleaf.di.protocols :as dip]
[darkleaf.di.test-utils :refer [catch-some]]))

;; root
;; / \
Expand Down Expand Up @@ -51,11 +52,6 @@
[`c :stopped]]
@log))))

(defmacro try-ex-data [& body]
`(try ~@body
(catch clojure.lang.ExceptionInfo e#
(ex-data e#))))

(defn root-path
[{::syms [a-path b-path c-path]}]
:done)
Expand All @@ -79,7 +75,9 @@
(t/deftest error-path-test
(t/is (= {:type ::di/missing-dependency
:stack [`missing-path `b-path `root-path ::di/implicit-root]}
(try-ex-data (di/start `root-path)))))
(-> (di/start `root-path)
catch-some
ex-data))))


(defn parent
Expand All @@ -90,11 +88,15 @@
(t/deftest missing-dependency-test
(t/is (= {:type ::di/missing-dependency
:stack [`missing-root ::di/implicit-root]}
(try-ex-data (di/start `missing-root))))
(-> (di/start `missing-root)
catch-some
ex-data)))

(t/is (= {:type ::di/missing-dependency
:stack [`missing-key `parent ::di/implicit-root]}
(try-ex-data (di/start `parent)))))
(-> (di/start `parent)
catch-some
ex-data))))


(defn recursion-a
Expand All @@ -112,9 +114,14 @@
(t/deftest circular-dependency-test
(t/is (= {:type ::di/circular-dependency
:stack [`recursion-a `recursion-b `recursion-a ::di/implicit-root]}
(try-ex-data (di/start `recursion-a))))
(-> (di/start `recursion-a)
catch-some
ex-data)))



(t/is (= {:type ::di/circular-dependency
:stack [`recursion-c `recursion-c ::di/implicit-root]}
(try-ex-data (di/start `recursion-c)))))
(-> (di/start `recursion-c)
catch-some
ex-data))))
8 changes: 8 additions & 0 deletions test/darkleaf/di/test_utils.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns darkleaf.di.test-utils)

(defmacro catch-some [& body]
`(try
~@body
nil
(catch Exception ex#
ex#)))
9 changes: 4 additions & 5 deletions test/darkleaf/di/tutorial/y_graceful_stop_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
{:nextjournal.clerk/visibility {:result :hide}}
(:require
[clojure.test :as t]
[darkleaf.di.core :as di]))
[darkleaf.di.core :as di]
[darkleaf.di.test-utils :refer [catch-some]]))

;; The DI tries to stop components that are already started
;; if another component fails while it is starting.
Expand All @@ -30,9 +31,7 @@
on-stop-dep-ex (ex-info "on stop dep" {})
registry {::on-start-root-ex on-start-root-ex
::on-stop-dep-ex on-stop-dep-ex}
ex (try
(di/start `root registry)
(catch Exception ex
ex))]
ex (-> (di/start `root registry)
catch-some)]
(t/is (= on-start-root-ex (ex-cause ex)))
(t/is (= [on-stop-dep-ex] (vec (.getSuppressed ex))))))

0 comments on commit 306587a

Please sign in to comment.