Skip to content

Commit

Permalink
di/with-open
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Oct 15, 2024
1 parent ad26619 commit 3b99cd3
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions test/darkleaf/di/cache_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
: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)))))
(di/with-open [[main cache] (di/start [`a ::di/cache]
(di/collect-cache))
[secondary] (di/start [`a]
(di/use-cache cache))]
(t/is (not= nil main secondary))
(t/is (identical? main secondary))))

(defn b
{::di/kind :component}
Expand All @@ -27,25 +27,25 @@
: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)))))
(di/with-open [[main cache] (di/start [`b ::di/cache]
{"B_CONF" "conf"}
;; должен быть последним в цепочке, чтобы закешировать все
(di/collect-cache))
[secondary] (di/start [`b]
(di/use-cache cache))]
(t/is (not= nil main secondary))
(t/is (identical? main secondary))))

(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))))))
(di/with-open [[main cache] (di/start [`b ::di/cache]
{"B_CONF" "conf"}
(di/collect-cache))
[secondary] (di/start [`b]
;; должен быть первым, чтобы его можно было переопределять
(di/use-cache cache)
{"B_CONF" "changed"})]
(t/is (not= nil main secondary))
(t/is (not (identical? main secondary)))))

(defn c
{::di/kind :component}
Expand All @@ -56,46 +56,46 @@
: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)))))
(di/with-open [[main cache] (di/start [`c ::di/cache]
{"B_CONF" "conf"}
(di/collect-cache))
[secondary] (di/start [`c]
(di/use-cache cache))]
(t/is (not= nil main secondary))
(t/is (identical? main secondary))))

(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))))
(di/with-open [[main cache] (di/start [`c ::di/cache]
{"B_CONF" "conf"}
(di/collect-cache))
[secondary] (di/start [`c]
(di/use-cache cache)
{"B_CONF" "changed"})]
(t/is (not= nil main secondary))
(t/is (not (identical? main secondary)))
(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))))
(:name main)
(:name secondary)))
(t/is (identical? (:a main)
(:a secondary)))
(t/is (not (identical? (:b main)
(:b secondary))))
;; надо ли проверять?
(t/is (not (identical? (-> main :root :obj)
(-> secondary :root :obj))))))
(t/is (not (identical? (:obj main)
(:obj secondary))))))

(t/deftest invalid-cache-test
(let [main (di/start {:root `c :cache ::di/cache}
{"B_CONF" "conf"}
(di/collect-cache))
_ (di/stop main)]
(let [[main cache :as system] (di/start [`c ::di/cache]
{"B_CONF" "conf"}
(di/collect-cache))
_ (di/stop system)]
(t/is (thrown? IllegalStateException
(di/start `c
(di/use-cache (:cache main)))))))
(di/use-cache cache))))))

(t/deftest not-recursive-test
(with-open [main (di/start {:root `c :cache ::di/cache}
{"B_CONF" "conf"}
(di/collect-cache))]
(di/with-open [[main cache] (di/start [`c ::di/cache]
{"B_CONF" "conf"}
(di/collect-cache))]
(t/try-expr "must not be recrusive"
(prn-str (:cache main)))))
(prn-str cache))))

0 comments on commit 3b99cd3

Please sign in to comment.