From 39307f5a738e2b8d172e239f162f5d1ec048842f Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Thu, 25 Sep 2014 21:58:18 +0300 Subject: [PATCH] Test at "server agent" level --- src/clj/grub/state.clj | 58 +++++----- src/cljx/grub/sync.cljx | 14 +-- src/test/grub/test/unit/state.clj | 179 ++++++++++++++++++++++++++---- src/test/grub/test/unit/sync.clj | 51 --------- 4 files changed, 189 insertions(+), 113 deletions(-) diff --git a/src/clj/grub/state.clj b/src/clj/grub/state.clj index 7a6bc3f..6fb1348 100644 --- a/src/clj/grub/state.clj +++ b/src/clj/grub/state.clj @@ -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 (! >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 (! 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 !! 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 (!! in msg) + (let [diff-response (!! in msg) + (let [diff-response (!! in msg) + (let [diff-response (!! in msg) + (let [diff-response (