From b5337c1326d385289d8bf458e0573b81784291bc Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 31 Oct 2024 17:07:34 +0400 Subject: [PATCH 1/2] fix add-side-dependency Co-authored-by: Gleb Eliseev Co-authored-by: Nikita Domnitskii Co-authored-by: KGOH --- src/darkleaf/di/core.clj | 34 ++++++++----------- test/darkleaf/di/add_side_dependency_test.clj | 21 +++++++++--- test/darkleaf/di/dependencies_test.clj | 12 ++++--- test/darkleaf/di/tutorial/x_inspect_test.clj | 8 +++-- test/darkleaf/di/tutorial/x_log_test.clj | 2 ++ 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 771f8e7e..eb72a83b 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -209,8 +209,8 @@ (defn- key->key®istry [key] (cond (vector? key) [::implicit-root {::implicit-root (->> key (map ref) template)}] - (map? key) [::implicit-root {::implicit-root (-> key (update-vals ref) template)}] - true [key nil])) + (map? key) [::implicit-root {::implicit-root (-> key (update-vals ref) template)}] + :else [::implicit-root {::implicit-root (-> key ref)}])) (defn- ->next-id [] (let [id (atom -1)] @@ -560,26 +560,20 @@ ```" [dep-key] (fn [registry] - (let [*orig-key (volatile! nil) - *orig-factory (volatile! nil) - new-key (symbol (str "darkleaf.di.core/new-key#" (*next-id*))) - new-factory (reify p/Factory - (dependencies [_] - ;; array-map preserves order of keys - {new-key :required - dep-key :required}) - (build [_ deps] - (new-key deps)) - (demolish [_ _]))] + (let [new-key (symbol (str "darkleaf.di.core/new-key#" (*next-id*))) + new-factory (reify p/Factory + (dependencies [_] + ;; array-map preserves order of keys + {new-key :required + dep-key :required}) + (build [_ deps] + (new-key deps)) + (demolish [_ _]))] (fn [key] - (when (nil? @*orig-key) - (vreset! *orig-key key)) - (when (nil? @*orig-factory) - (vreset! *orig-factory (registry key))) (cond - (= @*orig-key key) new-factory - (= new-key key) @*orig-factory - :else (registry key)))))) + (= ::implicit-root key) new-factory + (= new-key key) (registry ::implicit-root) + :else (registry key)))))) (defn- arglists [variable] (-> variable meta :arglists)) diff --git a/test/darkleaf/di/add_side_dependency_test.clj b/test/darkleaf/di/add_side_dependency_test.clj index dbfeded6..ce99d06d 100644 --- a/test/darkleaf/di/add_side_dependency_test.clj +++ b/test/darkleaf/di/add_side_dependency_test.clj @@ -85,7 +85,8 @@ :h (di/ref `h)})} (di/add-side-dependency `side-dep) (di/log :after-build! after-build!))] - (t/is (= [`a `b `c `d `e `f `g `h `di/new-key#0 `side-dep ::root] @log)) + (t/is (= [`a `b `c `d `e `f `g `h + ::root `di/new-key#0 `side-dep ::di/implicit-root] @log)) (t/is (= {:a :a :b :b :c :c @@ -117,9 +118,12 @@ :h (di/ref `h)})} (di/add-side-dependency `side-dep2) (di/log :after-build! after-build!))] - (t/is (= [`di/new-key#0 `side-dep `a - `b `c `d `e `f `g `h - `di/new-key#1 `side-dep2 ::root] @log)) + (t/is (= [`a `b `c `d `e `f `g `h + ::root + `di/new-key#0 `side-dep + `di/new-key#1 `side-dep2 + ::di/implicit-root] + @log)) (t/is (= {:a :a :b :b :c :c @@ -128,3 +132,12 @@ :f :f :g :g :h :h} @root))))) + +(t/deftest bug-with-update-key + (let [info (di/inspect ::root + {::root 42 + ::unused 0 + ::side-dep :side-dep} + (di/add-side-dependency ::side-dep) + (di/update-key ::unused inc))] + (t/is (->> info (map :key) (filter #{::side-dep}) first some?)))) diff --git a/test/darkleaf/di/dependencies_test.clj b/test/darkleaf/di/dependencies_test.clj index 0c87e1bf..7b200518 100644 --- a/test/darkleaf/di/dependencies_test.clj +++ b/test/darkleaf/di/dependencies_test.clj @@ -42,7 +42,9 @@ [`a :built] [`b :built] [`root :built] + [::di/implicit-root :built] + [::di/implicit-root :stopped] [`root :stopped] [`b :stopped] [`a :stopped] @@ -76,7 +78,7 @@ (t/deftest error-path-test (t/is (= {:type ::di/missing-dependency - :stack [`missing-path `b-path `root-path]} + :stack [`missing-path `b-path `root-path ::di/implicit-root]} (try-ex-data (di/start `root-path))))) @@ -87,11 +89,11 @@ (t/deftest missing-dependency-test (t/is (= {:type ::di/missing-dependency - :stack [`missing-root]} + :stack [`missing-root ::di/implicit-root]} (try-ex-data (di/start `missing-root)))) (t/is (= {:type ::di/missing-dependency - :stack [`missing-key `parent]} + :stack [`missing-key `parent ::di/implicit-root]} (try-ex-data (di/start `parent))))) @@ -109,10 +111,10 @@ (t/deftest circular-dependency-test (t/is (= {:type ::di/circular-dependency - :stack [`recursion-a `recursion-b `recursion-a]} + :stack [`recursion-a `recursion-b `recursion-a ::di/implicit-root]} (try-ex-data (di/start `recursion-a)))) (t/is (= {:type ::di/circular-dependency - :stack [`recursion-c `recursion-c]} + :stack [`recursion-c `recursion-c ::di/implicit-root]} (try-ex-data (di/start `recursion-c))))) diff --git a/test/darkleaf/di/tutorial/x_inspect_test.clj b/test/darkleaf/di/tutorial/x_inspect_test.clj index f8b69fd1..5a0bbfdb 100644 --- a/test/darkleaf/di/tutorial/x_inspect_test.clj +++ b/test/darkleaf/di/tutorial/x_inspect_test.clj @@ -15,7 +15,11 @@ :ok) (t/deftest ok - (t/is (= [{:key `c :dependencies {`a :required `b :optional}} + (t/is (= [{:key ::di/implicit-root + :dependencies {`c :required}} + {:key `c + :dependencies {`a :required `b :optional}} {:key `a} - {:key `b :dependencies {`a :required}}] + {:key `b + :dependencies {`a :required}}] (di/inspect `c)))) diff --git a/test/darkleaf/di/tutorial/x_log_test.clj b/test/darkleaf/di/tutorial/x_log_test.clj index ae8fa1ae..122277b9 100644 --- a/test/darkleaf/di/tutorial/x_log_test.clj +++ b/test/darkleaf/di/tutorial/x_log_test.clj @@ -28,6 +28,8 @@ [:built `b "#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"] [:built `c ":c"] + [:built ::di/implicit-root ":c"] + [:demolished ::di/implicit-root ":c"] [:demolished `c ":c"] [:demolished `b "#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"] From a8e2580d1d3d2fc494aae0dc62b61c5ea9550b7d Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Thu, 31 Oct 2024 17:10:35 +0400 Subject: [PATCH 2/2] fix test --- test/darkleaf/di/add_side_dependency_test.clj | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/darkleaf/di/add_side_dependency_test.clj b/test/darkleaf/di/add_side_dependency_test.clj index ce99d06d..c31c697f 100644 --- a/test/darkleaf/di/add_side_dependency_test.clj +++ b/test/darkleaf/di/add_side_dependency_test.clj @@ -139,5 +139,8 @@ ::unused 0 ::side-dep :side-dep} (di/add-side-dependency ::side-dep) - (di/update-key ::unused inc))] - (t/is (->> info (map :key) (filter #{::side-dep}) first some?)))) + (di/update-key ::unused inc)) + keys (into #{} + (map :key) + info)] + (t/is (contains? keys ::side-dep))))