Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guards synchronize operation inside schema.core/converge against the case where c1 is from a remote node. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/schism/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@
[& args]
(apply nvector/new-vector args))

(defn- is-local-node?
[n]
"Checks the most recent update in this entity's vector clock. Returns true if that most recent update is not from most-recent schism.node/*current-node*"
(let [most-recent (->> (into [] (sp/get-clock n))
(sort-by second)
last
first)]
(= most-recent schism.node/*current-node*)))

(defn converge
"Return a converged copy of `c1` containing the modifications of
`c2`. Convergence is defined on a per-type basis. If `c1` has
Expand All @@ -71,7 +80,9 @@
- The current value of `schism.node/*current-node*` is shared with
another node making modifications to the same logical collection."
[c1 c2]
(sp/synchronize c1 c2))
(if (is-local-node? c1)
(sp/synchronize c1 c2)
(throw (IllegalArgumentException. "The most recent update in c1's vector clock is not from the local node. schism.core/converge expects the c1 param to be the local node and the c2 param to be the remote node. Please check whether you have reversed that order."))))

(def initialize-node!
"Initialize the current node to an edn serializable value if
Expand Down
16 changes: 15 additions & 1 deletion test/schism/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@
#?(:cljs (gen/such-that #(not (js/Number.isNaN %)) gen/any)
:clj gen/any))


(deftest handles-non-local-copy-as-first-param
(let [m1 (schism/with-node "LOCAL" (schism/convergent-map :foo "bar", :a "b"))
_ (Thread/sleep 1)
m2 (schism/with-node "NOT LOCAL" (schism/convergent-map :y 2, :z 3, :x 1))]
(testing "FIXME, I fail."
(is
(thrown-with-msg? IllegalArgumentException
#"The most recent update in c1's vector clock is not from the local node. schism.core/converge expects the c1 param to be the local node and the c2 param to be the remote node. Please check whether you have reversed that order."
(schism/with-node "LOCAL" (schism/converge m2 m1))))
;; We haven't broken existing code in the case where the order is correct (c1 is the local node)
(is (= {:y 2, :z 3, :x 1}
(->>
(schism/with-node "LOCAL" (schism/converge m1 m2))
seq
(into {})))))))

(defspec convergent-set-is-equivalent-to-hash-set
50
Expand Down