/usr/share/pyshared/txzookeeper/tests/test_managed.py is in python-txzookeeper 0.9.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | #
# Copyright (C) 2010-2012 Canonical Ltd. All Rights Reserved
#
# This file is part of txzookeeper.
#
# Authors:
# Kapil Thangavelu
#
# txzookeeper is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# txzookeeper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with txzookeeper. If not, see <http://www.gnu.org/licenses/>.
#
import zookeeper
from twisted.internet.defer import inlineCallbacks, Deferred, DeferredList
from txzookeeper.client import ZookeeperClient, ClientEvent
from txzookeeper import managed
from txzookeeper.tests import ZookeeperTestCase, utils
from txzookeeper.tests import test_client
class WatchTest(ZookeeperTestCase):
"""Watch manager and watch tests"""
def setUp(self):
self.watches = managed.WatchManager()
def tearDown(self):
self.watches.clear()
del self.watches
def test_add_remove(self):
w = self.watches.add("/foobar", "child", lambda x: 1)
self.assertIn(
"<Watcher child /foobar",
str(w))
self.assertIn(w, self.watches._watches)
self.watches.remove(w)
self.assertNotIn(w, self.watches._watches)
self.watches.remove(w)
@inlineCallbacks
def test_watch_fire_removes(self):
"""Firing the watch removes it from the manager.
"""
w = self.watches.add("/foobar", "child", lambda x: 1)
yield w("a")
self.assertNotIn(w, self.watches._watches)
@inlineCallbacks
def test_watch_fire_with_error_removes(self):
"""Firing the watch removes it from the manager.
"""
d = Deferred()
@inlineCallbacks
def cb_error(e):
yield d
raise ValueError("a")
w = self.watches.add("/foobar", "child", lambda x: 1)
try:
wd = w("a")
d.callback(True)
yield wd
except ValueError:
pass
self.assertNotIn(w, self.watches._watches)
@inlineCallbacks
def test_reset_with_error(self):
"""A callback firing an error on reset is ignored.
"""
output = self.capture_log("txzk.managed")
d = Deferred()
results = []
@inlineCallbacks
def callback(*args, **kw):
results.append((args, kw))
yield d
raise ValueError("a")
w = self.watches.add("/foobar", "child", callback)
reset_done = self.watches.reset()
e, _ = results.pop()
self.assertEqual(
str(ClientEvent(*e)),
"<ClientEvent session at '/foobar' state: connected>")
d.callback(True)
yield reset_done
self.assertNotIn(w, self.watches._watches)
self.assertIn("Error reseting watch", output.getvalue())
@inlineCallbacks
def test_reset(self):
"""Reset fires a synthentic client event, and clears watches.
"""
d = Deferred()
results = []
def callback(*args, **kw):
results.append((args, kw))
return d
w = self.watches.add("/foobar", "child", callback)
reset_done = self.watches.reset()
e, _ = results.pop()
self.assertEqual(
str(ClientEvent(*e)),
"<ClientEvent session at '/foobar' state: connected>")
d.callback(True)
yield reset_done
self.assertNotIn(w, self.watches._watches)
class SessionClientTests(test_client.ClientTests):
"""Run through basic operations with SessionClient."""
timeout = 5
def setUp(self):
super(SessionClientTests, self).setUp()
self.client = managed.SessionClient("127.0.0.1:2181")
def test_client_use_while_disconnected_returns_failure(self):
# managed session client reconnects here.
return True
class SessionClientExpireTests(ZookeeperTestCase):
"""Verify expiration behavior."""
def setUp(self):
super(SessionClientExpireTests, self).setUp()
self.client = managed.ManagedClient("127.0.0.1:2181", 3000)
self.client2 = None
return self.client.connect()
@inlineCallbacks
def tearDown(self):
self.client.close()
self.client2 = ZookeeperClient("127.0.0.1:2181")
yield self.client2.connect()
utils.deleteTree(handle=self.client2.handle)
yield self.client2.close()
super(SessionClientExpireTests, self).tearDown()
@inlineCallbacks
def expire_session(self):
assert self.client.connected
self.client2 = ZookeeperClient(self.client.servers)
yield self.client2.connect(client_id=self.client.client_id)
yield self.client2.close()
# It takes some time to propagate (1/3 session time as ping)
yield self.sleep(2)
@inlineCallbacks
def test_session_expiration_conn(self):
session_id = self.client.client_id[0]
yield self.client.create("/fo-1", "abc")
yield self.expire_session()
yield self.client.exists("/")
self.assertNotEqual(session_id, self.client.client_id[0])
@inlineCallbacks
def test_session_expiration_notification(self):
session_id = self.client.client_id[0]
c_d, w_d = self.client.get_and_watch("/")
yield c_d
d = self.client.subscribe_new_session()
self.assertFalse(d.called)
yield self.expire_session()
yield d
yield w_d
self.assertNotEqual(session_id, self.client.client_id[0])
@inlineCallbacks
def test_session_expiration_conn_watch(self):
session_id = self.client.client_id[0]
yield self.client.create("/fo-1", "abc")
yield self.expire_session()
yield self.client.exists("/")
self.assertNotEqual(session_id, self.client.client_id[0])
@inlineCallbacks
def test_invoked_watch_gc(self):
c_d, w_d = yield self.client.get_children_and_watch("/")
yield c_d
yield self.client.create("/foo")
yield w_d
yield self.expire_session()
yield self.client.create("/foo2")
# Nothing should blow up
yield self.sleep(0.2)
@inlineCallbacks
def test_ephemeral_and_watch_recreate(self):
# Create some ephemeral nodes
yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
yield self.client.create("/fo-2", "def", flags=zookeeper.EPHEMERAL)
# Create some watches
g_d, g_w_d = self.client.get_and_watch("/fo-1")
yield g_d
c_d, c_w_d = self.client.get_children_and_watch("/")
yield g_d
e_d, e_w_d = self.client.get_children_and_watch("/fo-2")
yield e_d
# Expire the session
yield self.expire_session()
# Poof
# Ephemerals back
c, s = yield self.client.get("/fo-1")
self.assertEqual(c, "abc")
c, s = yield self.client.get("/fo-2")
self.assertEqual(c, "def")
# Watches triggered
yield DeferredList(
[g_w_d, c_w_d, e_w_d],
fireOnOneErrback=True, consumeErrors=True)
self.assertEqual(
[str(d.result) for d in (g_w_d, c_w_d, e_w_d)],
["<ClientEvent session at '/fo-1' state: connected>",
"<ClientEvent session at '/' state: connected>",
"<ClientEvent session at '/fo-2' state: connected>"])
@inlineCallbacks
def test_ephemeral_no_track_sequence_nodes(self):
""" Ephemeral tracking ignores sequence nodes.
"""
yield self.client.create("/music", "abc")
yield self.client.create(
"/music/u2-", "abc",
flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE)
yield self.expire_session()
children = yield self.client.get_children("/music")
self.assertEqual(children, [])
@inlineCallbacks
def test_ephemeral_content_modification(self):
yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
yield self.client.set("/fo-1", "def")
yield self.expire_session()
c, s = yield self.client.get("/fo-1")
self.assertEqual(c, "def")
@inlineCallbacks
def test_ephemeral_acl_modification(self):
yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
acl = [test_client.PUBLIC_ACL,
dict(scheme="digest",
id="zebra:moon",
perms=zookeeper.PERM_ALL)]
yield self.client.set_acl("/fo-1", acl)
yield self.expire_session()
n_acl, stat = yield self.client.get_acl("/fo-1")
self.assertEqual(acl, n_acl)
@inlineCallbacks
def test_ephemeral_deletion(self):
yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
yield self.client.delete("/fo-1")
yield self.expire_session()
self.assertFalse((yield self.client.exists("/fo-1")))
|