Test at "server agent" level

This commit is contained in:
Nicholas Kariniemi 2014-09-25 21:58:18 +03:00
parent edab2ad684
commit 39307f5a73
4 changed files with 189 additions and 113 deletions

View file

@ -7,34 +7,36 @@
;; Server state
(def states (atom []))
(defn make-server-agent [in >client states]
(a/go-loop [client-state sync/empty-state]
(when-let [msg (<! in)]
(condp = (:type msg)
:diff
(let [{:keys [new-states new-shadow full-sync?]} (sync/apply-diff @states (:diff msg) (:hash msg))]
(println "diff!")
(if full-sync?
(let [state (sync/get-current-state @states)]
(println "state not found, full sync!")
(>! >client (message/full-sync state))
(recur state))
(do (println "state found, just send changes")
(let [{:keys [diff hash]} (sync/diff-states new-states new-shadow)]
(reset! states new-states)
(>! >client (message/diff-msg diff hash))
(recur new-shadow)))))
:full-sync
(let [state (sync/get-current-state @states)]
(println "got full sync, send full sync")
(>! >client (message/full-sync state))
(recur state))
:new-state
(let [{:keys [diff shadow-hash]} (sync/diff-states (:new-states msg) client-state)]
(println "new-state!")
(>! >client (message/diff-msg diff shadow-hash)))
(do (println "Unknown message:" msg)
(recur client-state))))))
(defn make-server-agent
([in out states] (make-server-agent in out states sync/empty-state))
([in out states initial-client-state]
(a/go-loop [client-state initial-client-state]
(when-let [msg (<! in)]
(condp = (:type msg)
:diff
(let [states* @states
shadow (sync/get-history-state states* (:hash msg))]
(if shadow
(let [new-states (sync/apply-diff states* (:diff msg))
new-shadow (diff/patch-state shadow (:diff msg))
{:keys [diff hash]} (sync/diff-states new-states new-shadow)]
(reset! states new-states)
(>! out (message/diff-msg diff hash))
(recur new-shadow))
(let [state (sync/get-current-state @states)]
(>! out (message/full-sync state))
(recur state))))
:full-sync
(let [state (sync/get-current-state @states)]
(>! out (message/full-sync state))
(recur state))
:new-state
(let [{:keys [diff hash]} (sync/diff-states (:new-states msg) client-state)]
(>! out (message/diff-msg diff hash)))
(do (println "Unknown message:" msg)
(recur client-state)))))))
;; TODO: Remove watch, close up channels properly
(defn sync-new-client! [>client <client]

View file

@ -28,13 +28,7 @@
{:hash (hasch/uuid shadow)
:diff (diff/diff-states shadow state)}))
(defn apply-diff [states diff shadow-hash]
(let [state (:state (first states))
shadow (get-history-state states shadow-hash)]
(if shadow
{:new-states (add-history-state states (diff/patch-state state diff))
:new-shadow (diff/patch-state shadow diff)
:full-sync? false}
{:new-states states
:new-shadow state
:full-sync? true})))
(defn apply-diff [states diff]
(let [new-state (diff/patch-state (get-current-state states) diff)]
(add-history-state states new-state)))

View file

@ -4,29 +4,160 @@
[hasch.core :as hasch]
[clojure.core.async :as a :refer [<!! >!! chan go]]))
(deftest single-diff
;; Returns empty ACK diff
(let [in (chan 1)
>client (chan 1)
state {:grubs {"1" {:text "2 apples" :completed false}}
:recipes {}}
hash (hasch/uuid state)
states* [{:hash hash :state state}]
states (atom states*)
diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
diff-msg {:type :diff
:diff diff
:hash hash}
server-agent (state/make-server-agent in >client states)]
(>!! in diff-msg)
(let [diff-response (<!! >client)]
(is (= @states
[{:hash #uuid "0cb7ae13-2523-52fa-aa79-4a6f2489cafd"
:state {:grubs {"1" {:completed false, :text "2 apples"}}, :recipes {}}}
{:hash #uuid "166d7e23-5a7b-5101-8364-0d2c06b8d554"
:state {:grubs {"1" {:completed true, :text "2 apples"}}, :recipes {}}}]))
(is (= diff-response
{:type :diff
(defn hashed-states [& states]
(->> states
(map (fn [s] {:hash (hasch/uuid s)
:state s}))
(into [])))
(deftest diff-no-server-changes
;; Returns empty ACK diff with hash of current state
;; when no server changes
(let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}})
states* (atom states)
msg {:type :diff
:diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
:hash (:hash (first states))}
in (chan 1)
out (chan 1)]
(state/make-server-agent in out states*)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (hashed-states
{:grubs {"1" {:completed false, :text "2 apples"}}, :recipes {}}
{:grubs {"1" {:completed true, :text "2 apples"}}, :recipes {}})
@states*))
(is (= {:type :diff
:diff {:grubs {:deleted #{}, :updated nil}
:recipes {:deleted #{}, :updated nil}}
:hash #uuid "166d7e23-5a7b-5101-8364-0d2c06b8d554"})))))
:hash (:hash (last @states*))}
diff-response)))))
(deftest diff-server-changes
;; Returns diff with changes when server has changed
;; Client state fetched from history
(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 {}})
states* (atom states)
msg {:type :diff
:diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
:hash (:hash (first states))}
in (chan 1)
out (chan 1)]
(state/make-server-agent in out states*)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (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 true}
"2" {:text "3 onions" :completed false}}
:recipes {}})
@states*))
(is (= {:type :diff
:diff {:grubs {:deleted #{}
:updated {"2" {:completed false, :text "3 onions"}}}
:recipes {:deleted #{}, :updated nil}}
:hash (hasch/uuid {:grubs {"1" {:text "2 apples" :completed true}}
:recipes {}})}
diff-response)))))
(deftest diff-client-out-of-sync
;; Returns full sync if client state not found
;; in history
(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 {}})
states* (atom states)
msg {:type :diff
:diff {:grubs {:updated {"0" {:completed true}} :deleted #{}}}
:hash (:hash {:grubs {"0" {:text "milk" :completed false}}
:recipes {}})}
in (chan 1)
out (chan 1)]
(state/make-server-agent in out states*)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}})
@states*))
(is (= {:type :full-sync
:state {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}}
diff-response)))))
(deftest full-sync-request
;; Returns full sync if client requests it
(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 {}})
states* (atom states)
msg {:type :full-sync}
in (chan 1)
out (chan 1)]
(state/make-server-agent in out states*)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}})
@states*))
(is (= {:type :full-sync
:state {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}}
diff-response)))))
(deftest new-state
;; Passes diff with new state to client
(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 {}})
states* (atom states)
client-state {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
msg {:type :new-state
:new-states states}
in (chan 1)
out (chan 1)]
(state/make-server-agent in out states* client-state)
(>!! in msg)
(let [diff-response (<!! out)]
(is (= (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 {}})
@states*))
(is (= {:type :diff
:diff {:grubs {:deleted #{}
:updated {"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}}
:recipes {:deleted #{}, :updated nil}}
:hash (hasch/uuid client-state)}
diff-response)))))

View file

@ -25,54 +25,3 @@
(is (= {:a :b} (s/get-history-state states "hash1")))
(is (= {:c :d} (s/get-history-state states "hash2")))
(is (= {:e :f} (s/get-history-state states "hash3")))))
(deftest apply-diff-no-changes
;; Apply changes and return ACK for in sync client/server
(let [state {:grubs {"1" {:text "2 apples" :completed false}}
:recipes {}}
hash (hasch/uuid state)
states [{:hash hash :state state}]
diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
shadow-hash hash
{:keys [new-states new-shadow full-sync?]} (s/apply-diff states diff shadow-hash)]
(do
(is (= {:grubs {"1" {:text "2 apples" :completed true}}
:recipes {}}
(:state (last new-states))))
(is (= {:grubs {"1" {:text "2 apples" :completed true}}
:recipes {}}
new-shadow))
(is (not full-sync?)))))
(deftest apply-diff-server-state-changed
;; Send differences back if server state changed
(let [state {:grubs {"1" {:text "3 apples" :completed false}} :recipes {}}
prev {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
states [{:hash (hasch/uuid state) :state state}
{:hash (hasch/uuid prev) :state prev}]
diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
shadow-hash (hasch/uuid prev)
{:keys [new-states new-shadow full-sync?]} (s/apply-diff states diff shadow-hash)]
(do
(is (= {:grubs {"1" {:text "3 apples" :completed true}}
:recipes {}}
(:state (last new-states))))
(is (= {:grubs {"1" {:text "2 apples" :completed true}}
:recipes {}}
new-shadow))
(is (not full-sync?)))))
(deftest apply-diff-client-out-of-sync
;; Shadow hash not in history means client has fallen too far
;; out of sync. Send a full sync
(let [state {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
prev {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
states [{:hash (hasch/uuid state) :state state}
{:hash (hasch/uuid prev) :state prev}]
shadow-hash (hasch/uuid {:grubs {} :recipes {}})
diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
{:keys [new-states new-shadow full-sync?]} (s/apply-diff states diff shadow-hash)]
(do
(is (= state (:state (last new-states))))
(is (= state new-shadow))
(is full-sync?))))