Update unit tests to not use core async

This commit is contained in:
Nicholas Kariniemi 2014-10-10 22:02:51 +03:00
parent e581f34afc
commit 87b88f9340

View file

@ -1,8 +1,8 @@
(ns grub.test.unit.state (ns grub.test.unit.state
(:require [grub.state :as state] (:require [grub.state :as state]
[grub.sync :as sync]
[midje.sweet :refer :all] [midje.sweet :refer :all]
[hasch.core :as hasch] [hasch.core :as hasch]))
[clojure.core.async :as a :refer [<!! >!! chan go]]))
(defn hashed-states [& states] (defn hashed-states [& states]
(->> states (->> states
@ -10,110 +10,89 @@
:state s})) :state s}))
(into []))) (into [])))
(defn states-atom [& states]
(atom (apply hashed-states states)))
(defn <!!? [c]
(let [[v p] (a/alts!! [c (a/timeout 100)])]
v))
(fact "Applies diff and returns empty diff when no server changes" (fact "Applies diff and returns empty diff when no server changes"
(let [states (states-atom (let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}) {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}})
msg {:type :diff event {:type :diff
:diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}} :diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
:hash (:hash (first @states))} :hash (:hash (first states))
in (chan 1) :states states
out (chan 1)] :shadow (:state (last states))
(state/make-server-agent in out states) :client? false}
(>!! in msg) {:keys [new-states new-shadow out-event]} (state/handle-event event)]
(let [response (<!!? out)] new-states => (hashed-states
@states => (hashed-states {:grubs {"1" {:completed false, :text "2 apples"}}, :recipes {}}
{:grubs {"1" {:completed false, :text "2 apples"}}, :recipes {}} {:grubs {"1" {:completed true, :text "2 apples"}}, :recipes {}})
{:grubs {"1" {:completed true, :text "2 apples"}}, :recipes {}}) out-event => {:type :diff
response => {:type :diff :diff {:grubs {:deleted #{}, :updated nil}
:diff {:grubs {:deleted #{}, :updated nil} :recipes {:deleted #{}, :updated nil}}
:recipes {:deleted #{}, :updated nil}} :hash (:hash (last new-states))}))
:hash (:hash (last @states))})))
(fact "Applies diff and returns changes when server has changed" (fact "Applies diff and returns changes when server has changed"
(let [states (states-atom (let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}} {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false} {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}} "2" {:text "3 onions" :completed false}}
:recipes {}}) :recipes {}})
msg {:type :diff event {:type :diff
:diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}} :diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
:hash (:hash (first @states))} :hash (:hash (first states))
in (chan 1) :states states
out (chan 1)] :shadow sync/empty-state
(state/make-server-agent in out states) :client? false}
(>!! in msg) {:keys [new-states new-shadow out-event]} (state/handle-event event)]
(let [response (<!!? out)] new-states => (hashed-states
@states => (hashed-states {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}} {:grubs {"1" {:text "2 apples" :completed false}
{:grubs {"1" {:text "2 apples" :completed false} "2" {:text "3 onions" :completed false}}
"2" {:text "3 onions" :completed false}} :recipes {}}
:recipes {}} {:grubs {"1" {:text "2 apples" :completed true}
{:grubs {"1" {:text "2 apples" :completed true} "2" {:text "3 onions" :completed false}}
"2" {:text "3 onions" :completed false}} :recipes {}})
:recipes {}}) out-event => {:type :diff
response => {:type :diff :diff {:grubs {:deleted #{}
:diff {:grubs {:deleted #{} :updated {"2" {:completed false, :text "3 onions"}}}
:updated {"2" {:completed false, :text "3 onions"}}} :recipes {:deleted #{}, :updated nil}}
:recipes {:deleted #{}, :updated nil}} :hash (hasch/uuid {:grubs {"1" {:text "2 apples" :completed true}}
:hash (hasch/uuid {:grubs {"1" {:text "2 apples" :completed true}} :recipes {}})}))
:recipes {}})})))
(fact "Force full sync if client is out of sync" (fact "Force full sync if client is out of sync"
(let [states (states-atom (let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}} {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false} {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}} "2" {:text "3 onions" :completed false}}
:recipes {}}) :recipes {}})
msg {:type :diff event {:type :diff
:diff {:grubs {:updated {"0" {:completed true}} :deleted #{}}} :diff {:grubs {:updated {"0" {:completed true}} :deleted #{}}}
:hash (:hash {:grubs {"0" {:text "milk" :completed false}} :hash (:hash {:grubs {"0" {:text "milk" :completed false}}
:recipes {}})} :recipes {}})
in (chan 1) :states states
out (chan 1)] :shadow sync/empty-state
(state/make-server-agent in out states) :client? false}
(>!! in msg) {:keys [new-states new-shadow out-event]} (state/handle-event event)]
(let [response (<!!? out)] new-states => nil
@states => (hashed-states out-event => {:type :full-sync
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}} :state {:grubs {"1" {:text "2 apples" :completed false}
{:grubs {"1" {:text "2 apples" :completed false} "2" {:text "3 onions" :completed false}}
"2" {:text "3 onions" :completed false}} :recipes {}}}))
:recipes {}})
response => {:type :full-sync
:state {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}})))
(fact "Full sync if client requests it" (fact "Full sync if client requests it"
(let [states (states-atom (let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}} {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false} {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}} "2" {:text "3 onions" :completed false}}
:recipes {}}) :recipes {}})
msg {:type :full-sync-request} event {:type :full-sync-request
in (chan 1) :states states}
out (chan 1)] {:keys [new-states new-shadow out-event]} (state/handle-event event)]
(state/make-server-agent in out states) new-states => nil
(>!! in msg) out-event => {:type :full-sync
(let [response (<!!? out)] :state {:grubs {"1" {:text "2 apples" :completed false}
@states => (hashed-states "2" {:text "3 onions" :completed false}}
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}} :recipes {}}}))
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}})
response => {:type :full-sync
:state {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}})))
(fact "Passes diffs of new states to client" (fact "New state - server passes diff to client, does not update shadow"
(let [states (states-atom (let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}} {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false} {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}} "2" {:text "3 onions" :completed false}}
@ -123,26 +102,59 @@
"3" {:text "milk" :completed false}} "3" {:text "milk" :completed false}}
:recipes {}}) :recipes {}})
client-state {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}} client-state {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
msg {:type :new-state event {:type :new-state
:state (:state (last @states))} :state (:state (last states))
in (chan 1) :client? false
out (chan 1)] :states states
(state/make-server-agent in out states client-state) :shadow client-state}
(>!! in msg) {:keys [new-states new-shadow out-event]} (state/handle-event event)]
(let [response (<!!? out)] new-states => (hashed-states
@states => (hashed-states {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}} {:grubs {"1" {:text "2 apples" :completed false}
{:grubs {"1" {:text "2 apples" :completed false} "2" {:text "3 onions" :completed false}}
"2" {:text "3 onions" :completed false}} :recipes {}}
:recipes {}} {:grubs {"1" {:text "2 apples" :completed false}
{:grubs {"1" {:text "2 apples" :completed false} "2" {:text "3 onions" :completed false}
"2" {:text "3 onions" :completed false} "3" {:text "milk" :completed false}}
"3" {:text "milk" :completed false}} :recipes {}})
:recipes {}}) new-shadow => nil
response => {:type :diff out-event => {:type :diff
:diff {:grubs {:deleted #{} :diff {:grubs {:deleted #{}
:updated {"2" {:text "3 onions" :completed false} :updated {"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}} "3" {:text "milk" :completed false}}}
:recipes {:deleted #{}, :updated nil}} :recipes {:deleted #{}, :updated nil}}
:hash (hasch/uuid client-state)}))) :hash (hasch/uuid client-state)}))
(fact "New state - client passes diff to server, does not update shadow"
(let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}
:recipes {}})
shadow {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
event {:type :new-state
:state (:state (last states))
:client? true
:states states
:shadow shadow}
{:keys [new-states new-shadow out-event]} (state/handle-event event)]
new-states => (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}
:recipes {}})
new-shadow => nil
out-event => {:type :diff
:diff {:grubs {:deleted #{}
:updated {"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}}
:recipes {:deleted #{}, :updated nil}}
:hash (hasch/uuid shadow)}))