diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 6d3b2eb4..230b2cf3 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -805,6 +805,7 @@ cached-obj (p/build factory deps))) (demolish [_ obj] - (if (identical? cached-obj obj) - nil - (p/demolish factory obj)))))))) + ;;todo: add test + #_(if (identical? cached-obj obj) + nil + (p/demolish factory obj)))))))) diff --git a/test/darkleaf/di/cache_test.clj b/test/darkleaf/di/cache_test.clj new file mode 100644 index 00000000..2b8af4a7 --- /dev/null +++ b/test/darkleaf/di/cache_test.clj @@ -0,0 +1,101 @@ +(ns darkleaf.di.cache-test + (:require + [clojure.test :as t] + [darkleaf.di.core :as di])) + +(set! *warn-on-reflection* true) + +(defn a + {::di/kind :component} + [] + {:name :a + :obj (Object.)}) + +(t/deftest a-test + (with-open [main (di/start {:root `a :cache ::di/cache} + (di/collect-cache)) + secondary (di/start {:root `a} + (di/use-cache (:cache main)))] + (t/is (identical? (-> main :root) + (-> secondary :root))))) + +(defn b + {::di/kind :component} + [{conf "B_CONF"}] + {:name :b + :conf conf + :obj (Object.)}) + +(t/deftest b-test + (with-open [main (di/start {:root `b :cache ::di/cache} + {"B_CONF" "conf"} + ;; должен быть последним в цепочке, чтобы закешировать все + (di/collect-cache)) + secondary (di/start {:root `b} + (di/use-cache (:cache main)))] + (t/is (identical? (-> main :root) + (-> secondary :root))))) + +(t/deftest b-changed-test + (with-open [main (di/start {:root `b :cache ::di/cache} + {"B_CONF" "conf"} + (di/collect-cache)) + secondary (di/start {:root `b} + ;; должен быть первым, чтобы его можно было переопределять + (di/use-cache (:cache main)) + {"B_CONF" "changed"})] + (t/is (not (identical? (-> main :root) + (-> secondary :root)))))) + +(defn c + {::di/kind :component} + [{a `a, b `b}] + {:name :c + :a a + :b b + :obj (Object.)}) + +(t/deftest c-test + (with-open [main (di/start {:root `c :cache ::di/cache} + {"B_CONF" "conf"} + (di/collect-cache)) + secondary (di/start {:root `c} + (di/use-cache (:cache main)))] + (t/is (identical? (-> main :root) + (-> secondary :root))))) + +(t/deftest c-changed-test + (with-open [main (di/start {:root `c :cache ::di/cache} + {"B_CONF" "conf"} + (di/collect-cache)) + secondary (di/start {:root `c} + (di/use-cache (:cache main)) + {"B_CONF" "changed"})] + (t/is (not (identical? (-> main :root) + (-> secondary :root)))) + (t/is (= :c + (-> main :root :name) + (-> secondary :root :name))) + (t/is (identical? (-> main :root :a) + (-> secondary :root :a))) + (t/is (not (identical? (-> main :root :b) + (-> secondary :root :b)))) + ;; надо ли проверять? + (t/is (not (identical? (-> main :root :obj) + (-> secondary :root :obj)))))) + +(t/deftest invalid-cache-test + (let [main (di/start {:root `c :cache ::di/cache} + {"B_CONF" "conf"} + (di/collect-cache)) + _ (di/stop main)] + (t/is (thrown? IllegalStateException + (di/start `c + (di/use-cache (:cache main))))))) + +(t/deftest not-recursive-test + (with-open [main (di/start {:root `c :cache ::di/cache} + {"B_CONF" "conf"} + (di/collect-cache))] + (t/try-expr "must not be recrusive" + (prn-str (:cache main)))))