Created
July 19, 2014 19:03
-
-
Save pabzdzdzwiagief/ab9be00eb98379e0dbf5 to your computer and use it in GitHub Desktop.
Examples of zookeeper-clj 0.9.1 library usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns zookeeper-client | |
| "Examples of zookeeper-clj 0.9.1 library usage" | |
| (:require [zookeeper :as zk] | |
| [zookeeper.data :as data] | |
| [clojure.test :refer [deftest is run-tests]] | |
| [clojure.string :refer [join]] | |
| [clojure.edn :as edn])) | |
| (def zk-server-address "localhost:2181") | |
| (defn path [& names] (str "/" (join "/" names))) | |
| (deftest node-creation-and-deletion | |
| (let [dir-name "nested" | |
| node-name "test-node"] | |
| (with-open [client (zk/connect zk-server-address)] | |
| (zk/create client (path node-name)) | |
| (is (some #{node-name} (zk/children client (path)))) | |
| (is (zk/exists client (path node-name))) | |
| (zk/delete client (path node-name)) | |
| (is ((complement zk/exists) client (path node-name))) | |
| (is (thrown? org.apache.zookeeper.KeeperException$NoNodeException | |
| (zk/create client (path dir-name node-name)))) | |
| (zk/create-all client (path dir-name node-name)) | |
| (is (zk/exists client (path dir-name node-name))) | |
| (is (thrown? org.apache.zookeeper.KeeperException$NotEmptyException | |
| (zk/delete client (path dir-name)))) | |
| (zk/delete-all client (path dir-name)) | |
| (is ((complement zk/exists) client (path dir-name)))))) | |
| (deftest node-data | |
| (let [node-name "test-node" | |
| node-path (path node-name) | |
| stored {:some "data"} | |
| store-edn #(-> % pr-str data/to-bytes) | |
| load-edn #(-> % data/to-string edn/read-string) | |
| version 0] | |
| (with-open [client (zk/connect zk-server-address)] | |
| (is (= (-> (doto client | |
| (zk/create node-path) | |
| (zk/set-data node-path (store-edn stored) version)) | |
| (zk/data node-path) | |
| :data | |
| load-edn) | |
| stored)) | |
| (is (thrown? org.apache.zookeeper.KeeperException$BadVersionException | |
| (zk/set-data client node-path (store-edn stored) | |
| (+ version 2))))))) | |
| (run-tests *ns*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment