         (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")

    x))

(defn- find-leader [c]
  (first (filter (comp = :leader :type unatom second) (:servers c))))

(deftest simple-test
  (testing "start elects a leader"
    (with-cluster [c (cluster/start-cluster (test-cluster))]
      (Thread/sleep 500)
      (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))]
      (Thread/sleep 300)
      (with-client [klient (create-client)
                    [k v] (client/set-or-create klient :a "a")
                    [k' v'] (client/get-value k :a)]


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


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