-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateTests.py
83 lines (61 loc) · 2.09 KB
/
createTests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from kazoo.client import KazooClient
import time
#192.168.239.141 2181
from kazoo.client import KazooState
def my_listener(state):
if state == KazooState.LOST:
print "session lost"
elif state == KazooState.SUSPENDED:
print "session suspended"
else:
print "ok here i am"
def node_watcher(event):
print "got event from node_watcher"
#zk = KazooClient(hosts='192.168.239.141:2181')
zk = KazooClient(hosts='127.0.0.1:2181', timeout=2.0)
zk.add_listener(my_listener)
zk.start()
if zk.exists("/my/favorite") == None:
zk.ensure_path("/my/favorite")
print "node created /my/favorite"
else:
print "/my/favorite was there"
data, stat = zk.get("/my/favorite")
print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))
if zk.exists("/my/favorite/node") == None:
zk.create("/my/favorite/node", b"a value")
print "node created"
else:
print "/my/favorite/node was there??"
@zk.ChildrenWatch("/my/favorite/node")
def watch_children(children):
print ">>>>>>>>ChildrenWatch got event"
print("Children are now: %s" % children)
@zk.DataWatch("/my/favorite/node")
def watch_node(data, stat):
print ">>>>>>>>>.DataWatch got event"
print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))
data, stat = zk.get("/my/favorite/node")
print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))
#create a ephemeral node
if zk.exists("/my/favorite/emphnode") == None:
zk.create("/my/favorite/emphnode", b"a value", ephemeral=True)
print "ok created ephemeral node"
else:
print "shit this node should not exist at all"
#children = zk.get_children("/my/favorite/node", watch=node_watcher)
print "setting some data"
zk.set("/my/favorite/node", b"some data")
#children = zk.get_children("/my/favorite/node", watch=node_watcher)
#print "deleting node"
#zk.delete("/my/favorite/node", recursive=True)
if True:
transaction = zk.transaction()
transaction.check('/my/favorite/node', version=10)
transaction.set_data('/my/favorite/node', b" new value so new so new")
results = transaction.commit()
print results
data, stat = zk.get("/my/favorite/node")
print data,stat
zk.stop()
print "bye"