         (is (= {:value "a"} v')))))
   (testing "compare and swap"
     (with-cluster [c (cluster/start-cluster (test-cluster))]
-       (Thread/sleep 500)
+      (wait-for-a-leader c)
        (with-client [klient (create-client)
                      [k v] (client/set-or-create klient :a "a")
                      [k' v'] (client/compare-and-swap k :a "b" "a")


(defn test-cluster []
  (cluster/init-cluster (create-network-cluster 10 9090) 3))

(defmacro with-cluster [bindings & body]
  `(let [~@bindings]

    x))

(defn- find-leader [c]
  (let [[k v] (first (filter
                      (fn [[k v]]
                        (= :leader (:type @v))) (:servers c)))]
    (if v @v v)))

(defn wait-for-a-leader [c]
  (loop [times 0]
    (cond
      (find-leader c) true
      (> times 1000) false
      :else (do
              (Thread/sleep 10)
              (recur (inc times))))))

(deftest simple-test
  (testing "start elects a leader"
    (with-cluster [c (cluster/start-cluster (test-cluster))]
      (is (wait-for-a-leader c))
      (is (= 1 (count (filter #(= :leader %)
                              (map (fn [[id s]] (:type @s)) (:servers c))))))
      (is (find-leader c))))
  (testing "start and write a value and get it back"
    (with-cluster [c (cluster/start-cluster (test-cluster))]
      (wait-for-a-leader c)
      (with-client [klient (create-client)
                    [k v] (client/set-or-create klient :a "a")
                    [k' v'] (client/get-value k :a)]
