Complete async testing

This commit is contained in:
Dan Holmsand 2015-09-12 19:13:31 +02:00
parent 544a3339d0
commit b036cf938e
1 changed files with 49 additions and 25 deletions

View File

@ -75,29 +75,36 @@
(is (= (running) runs)))) (is (= (running) runs))))
(deftest test-from-reflex (deftest test-from-reflex
(sync)
(let [runs (running)] (let [runs (running)]
(let [!counter (rv/atom 0) (let [!counter (rv/atom 0)
!signal (rv/atom "All I do is change") !signal (rv/atom "All I do is change")
co (run! co (ar (fn []
;;when I change... ;;when I change...
@!signal @!signal
;;update the counter ;;update the counter
(swap! !counter inc))] (swap! !counter inc)))]
(is (= 0 @!counter))
(sync)
(is (= 1 @!counter) "Constraint run on init") (is (= 1 @!counter) "Constraint run on init")
(reset! !signal "foo") (reset! !signal "foo")
(sync)
(is (= 2 @!counter) (is (= 2 @!counter)
"Counter auto updated") "Counter auto updated")
(dispose co)) (dispose co))
(let [!x (rv/atom 0) (let [!x (rv/atom 0)
!co (rv/make-reaction #(inc @!x) :auto-run true)] !co (rv/make-reaction #(inc @!x) :auto-run :async)]
(sync)
(is (= 1 @!co) "CO has correct value on first deref") (is (= 1 @!co) "CO has correct value on first deref")
(swap! !x inc) (swap! !x inc)
(sync)
(is (= 2 @!co) "CO auto-updates") (is (= 2 @!co) "CO auto-updates")
(dispose !co)) (dispose !co))
(is (= runs (running))))) (is (= runs (running)))))
(deftest test-unsubscribe (deftest test-unsubscribe
(sync)
(dotimes [x testite] (dotimes [x testite]
(let [runs (running) (let [runs (running)
a (rv/atom 0) a (rv/atom 0)
@ -111,31 +118,37 @@
c (reaction c (reaction
(swap! c-changed inc) (swap! c-changed inc)
(+ 10 @a2)) (+ 10 @a2))
res (run! res (ar (fn []
(if (< @a2 1) @b @c))] (if (< @a2 1) @b @c)))]
(is (= @res (+ 2 @a))) (is (= @res (+ 2 @a)))
(is (= @b-changed 1)) (is (= @b-changed 1))
(is (= @c-changed 0)) (is (= @c-changed 0))
(reset! a -1) (reset! a -1)
(is (= @res (+ 2 @a))) (is (= @b-changed 1))
(sync)
(is (= @b-changed 2)) (is (= @b-changed 2))
(is (= @c-changed 0)) (is (= @c-changed 0))
(is (= @res (+ 2 @a)))
(reset! a 2) (reset! a 2)
(is (= @res (+ 10 @a))) (sync)
(is (<= 2 @b-changed 3)) (is (= @b-changed 3))
(is (= @c-changed 1)) (is (= @c-changed 1))
(is (= @res (+ 10 @a)))
(reset! a 3) (reset! a 3)
(is (= @res (+ 10 @a))) (sync)
(is (<= 2 @b-changed 3)) (is (= @b-changed 3))
(is (= @c-changed 2)) (is (= @c-changed 2))
(is (= @res (+ 10 @a)))
(reset! a 3) (reset! a 3)
(is (= @res (+ 10 @a))) (sync)
(is (<= 2 @b-changed 3)) (is (= @b-changed 3))
(is (= @c-changed 2)) (is (= @c-changed 2))
(is (= @res (+ 10 @a)))
(reset! a -1) (reset! a -1)
(is (= @res (+ 2 @a))) (is (= @res (+ 2 @a)))
@ -143,6 +156,7 @@
(is (= runs (running)))))) (is (= runs (running))))))
(deftest maybe-broken (deftest maybe-broken
(sync)
(let [runs (running)] (let [runs (running)]
(let [runs (running) (let [runs (running)
a (rv/atom 0) a (rv/atom 0)
@ -150,8 +164,9 @@
c (reaction (dec @a)) c (reaction (dec @a))
d (reaction (str @b)) d (reaction (str @b))
res (rv/atom 0) res (rv/atom 0)
cs (run! cs (ar
(reset! res @d))] #(reset! res @d))]
(sync)
(is (= @res "1")) (is (= @res "1"))
(dispose cs)) (dispose cs))
;; should be broken according to https://github.com/lynaghk/reflex/issues/1 ;; should be broken according to https://github.com/lynaghk/reflex/issues/1
@ -159,16 +174,17 @@
(let [a (rv/atom 0) (let [a (rv/atom 0)
b (reaction (inc @a)) b (reaction (inc @a))
c (reaction (dec @a)) c (reaction (dec @a))
d (run! [@b @c])] d (ar (fn [] [@b @c]))]
(is (= @d [1 -1])) (is (= @d [1 -1]))
(dispose d)) (dispose d))
(let [a (rv/atom 0) (let [a (rv/atom 0)
b (reaction (inc @a)) b (reaction (inc @a))
c (reaction (dec @a)) c (reaction (dec @a))
d (run! [@b @c]) d (ar (fn [] [@b @c]))
res (rv/atom 0)] res (rv/atom 0)]
(is (= @d [1 -1])) (is (= @d [1 -1]))
(let [e (run! (reset! res @d))] (let [e (ar #(reset! res @d))]
(sync)
(is (= @res [1 -1])) (is (= @res [1 -1]))
(dispose e)) (dispose e))
(dispose d)) (dispose d))
@ -190,28 +206,33 @@
:on-dispose #(reset! disposed-c true)) :on-dispose #(reset! disposed-c true))
res (rv/atom nil) res (rv/atom nil)
cns (rv/make-reaction #(reset! res @c) cns (rv/make-reaction #(reset! res @c)
:auto-run true :auto-run :async
:on-dispose #(reset! disposed-cns true))] :on-dispose #(reset! disposed-cns true))]
@cns (sync)
(is (= @res 2)) (is (= @res 2))
(is (= (+ 4 runs) (running))) (is (= (+ 4 runs) (running)))
(is (= @count-b 1)) (is (= @count-b 1))
(reset! a -1) (reset! a -1)
(is (= @res 2))
(sync)
(is (= @res 1)) (is (= @res 1))
(is (= @disposed nil)) (is (= @disposed nil))
(is (= @count-b 2)) (is (= @count-b 2))
(is (= (+ 4 runs) (running)) "still running") (is (= (+ 4 runs) (running)) "still running")
(reset! a 2) (reset! a 2)
(sync)
(is (= @res 1)) (is (= @res 1))
(is (= @disposed true)) (is (= @disposed true))
(is (= (+ 2 runs) (running)) "less running count") (is (= (+ 2 runs) (running)) "less running count")
(reset! disposed nil) (reset! disposed nil)
(reset! a -1) (reset! a -1)
(sync)
;; This fails sometimes on node. I have no idea why. ;; This fails sometimes on node. I have no idea why.
(is (= 1 @res) "should be one again") (is (= 1 @res) "should be one again")
(is (= @disposed nil)) (is (= @disposed nil))
(reset! a 2) (reset! a 2)
(sync)
(is (= @res 1)) (is (= @res 1))
(is (= @disposed true)) (is (= @disposed true))
(dispose cns) (dispose cns)
@ -220,17 +241,20 @@
(is (= runs (running)))))) (is (= runs (running))))))
(deftest test-on-set (deftest test-on-set
(sync)
(let [runs (running) (let [runs (running)
a (rv/atom 0) a (rv/atom 0)
b (rv/make-reaction #(+ 5 @a) b (rv/make-reaction #(+ 5 @a)
:auto-run true :auto-run :async
:on-set (fn [oldv newv] :on-set (fn [oldv newv]
(reset! a (+ 10 newv))))] (reset! a (+ 10 newv))))]
@b (sync)
(is (= 5 @b)) (is (= 5 @b))
(reset! a 1) (reset! a 1)
(sync)
(is (= 6 @b)) (is (= 6 @b))
(reset! b 1) (reset! b 1)
(sync)
(is (= 11 @a)) (is (= 11 @a))
(is (= 16 @b)) (is (= 16 @b))
(dispose b) (dispose b)