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