Skip to content

Commit

Permalink
wip8
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Oct 15, 2024
1 parent 7c8ad87 commit 2e81c98
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))))))
101 changes: 101 additions & 0 deletions test/darkleaf/di/cache_test.clj
Original file line number Diff line number Diff line change
@@ -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)))))

0 comments on commit 2e81c98

Please sign in to comment.