tests: fix etcd and consul test run

There was a typo in pifpaf call where the prefix for the variable name should
be prefixed by using the -e option and not -g.

Change-Id: I5a97e0a0ffc5ba675914bb0881520ddc9693fa1b
This commit is contained in:
Julien Danjou 2017-03-22 14:16:15 +01:00
parent 19aa9380d7
commit 85fbf6ca3c
3 changed files with 73 additions and 37 deletions

@ -162,3 +162,15 @@ class ConsulDriver(coordination.CoordinationDriver):
for arg in args: for arg in args:
pieces.append(encodeutils.safe_decode(arg)) pieces.append(encodeutils.safe_decode(arg))
return u"/".join(pieces) return u"/".join(pieces)
def watch_join_group(self, group_id, callback):
raise tooz.NotImplemented
def unwatch_join_group(self, group_id, callback):
raise tooz.NotImplemented
def watch_leave_group(self, group_id, callback):
raise tooz.NotImplemented
def unwatch_leave_group(self, group_id, callback):
raise tooz.NotImplemented

@ -91,7 +91,6 @@ class EtcdLock(locking.Lock):
super(EtcdLock, self).__init__(name) super(EtcdLock, self).__init__(name)
self.client = client self.client = client
self.coord = coord self.coord = coord
self.lock = None
self.ttl = ttl self.ttl = ttl
self._lock_url = lock_url self._lock_url = lock_url
self._node = None self._node = None
@ -107,7 +106,6 @@ class EtcdLock(locking.Lock):
reply = self.client.delete(self._lock_url, make_url=False) reply = self.client.delete(self._lock_url, make_url=False)
return reply.get('errorCode') is None return reply.get('errorCode') is None
@fasteners.locked
def acquire(self, blocking=True, shared=False): def acquire(self, blocking=True, shared=False):
if shared: if shared:
raise tooz.NotImplemented raise tooz.NotImplemented
@ -120,6 +118,11 @@ class EtcdLock(locking.Lock):
watch = None watch = None
while True: while True:
if self.acquired:
# We already acquired the lock. Just go ahead and wait for ever
# if blocking != False using the last index.
lastindex = self._node['modifiedIndex']
else:
try: try:
reply = self.client.put( reply = self.client.put(
self._lock_url, self._lock_url,
@ -133,10 +136,14 @@ class EtcdLock(locking.Lock):
# We got the lock! # We got the lock!
if reply.get("errorCode") is None: if reply.get("errorCode") is None:
with self._lock:
self._node = reply['node'] self._node = reply['node']
self.coord._acquired_locks.append(self) self.coord._acquired_locks.add(self)
return True return True
# No lock, somebody got it, wait for it to be released
lastindex = reply['index'] + 1
# We didn't get the lock and we don't want to wait # We didn't get the lock and we don't want to wait
if not blocking: if not blocking:
return False return False
@ -145,7 +152,7 @@ class EtcdLock(locking.Lock):
try: try:
reply = self.client.get( reply = self.client.get(
self._lock_url + self._lock_url +
"?wait=true&waitIndex=%d" % (reply['index'] + 1), "?wait=true&waitIndex=%d" % lastindex,
make_url=False, make_url=False,
timeout=watch.leftover() if watch else None) timeout=watch.leftover() if watch else None)
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
@ -155,13 +162,13 @@ class EtcdLock(locking.Lock):
@_translate_failures @_translate_failures
@fasteners.locked @fasteners.locked
def release(self): def release(self):
if self in self.coord._acquired_locks: if self.acquired:
lock_url = self._lock_url lock_url = self._lock_url
lock_url += "?prevIndex=%s" % self._node['modifiedIndex'] lock_url += "?prevIndex=%s" % self._node['modifiedIndex']
reply = self.client.delete(lock_url, make_url=False) reply = self.client.delete(lock_url, make_url=False)
errorcode = reply.get("errorCode") errorcode = reply.get("errorCode")
if errorcode is None: if errorcode is None:
self.coord._acquired_locks.remove(self) self.coord._acquired_locks.discard(self)
self._node = None self._node = None
return True return True
else: else:
@ -169,10 +176,15 @@ class EtcdLock(locking.Lock):
self.name, errorcode, reply.get('message')) self.name, errorcode, reply.get('message'))
return False return False
@property
def acquired(self):
return self in self.coord._acquired_locks
@_translate_failures @_translate_failures
@fasteners.locked @fasteners.locked
def heartbeat(self): def heartbeat(self):
"""Keep the lock alive.""" """Keep the lock alive."""
if self.acquired:
poked = self.client.put(self._lock_url, poked = self.client.put(self._lock_url,
data={"ttl": self.ttl, data={"ttl": self.ttl,
"prevExist": "true"}, make_url=False) "prevExist": "true"}, make_url=False)
@ -213,7 +225,7 @@ class EtcdDriver(coordination.CoordinationDriver):
default_timeout = options.get('timeout', self.DEFAULT_TIMEOUT) default_timeout = options.get('timeout', self.DEFAULT_TIMEOUT)
self.lock_encoder = self.lock_encoder_cls(self.client.get_url("keys")) self.lock_encoder = self.lock_encoder_cls(self.client.get_url("keys"))
self.lock_timeout = int(options.get('lock_timeout', default_timeout)) self.lock_timeout = int(options.get('lock_timeout', default_timeout))
self._acquired_locks = [] self._acquired_locks = set()
def _start(self): def _start(self):
try: try:
@ -227,6 +239,18 @@ class EtcdDriver(coordination.CoordinationDriver):
self, self.client, self.lock_timeout) self, self.client, self.lock_timeout)
def heartbeat(self): def heartbeat(self):
for lock in self._acquired_locks: for lock in self._acquired_locks.copy():
lock.heartbeat() lock.heartbeat()
return self.lock_timeout return self.lock_timeout
def watch_join_group(self, group_id, callback):
raise tooz.NotImplemented
def unwatch_join_group(self, group_id, callback):
raise tooz.NotImplemented
def watch_leave_group(self, group_id, callback):
raise tooz.NotImplemented
def unwatch_leave_group(self, group_id, callback):
raise tooz.NotImplemented

12
tox.ini

@ -63,18 +63,18 @@ commands = pifpaf -e TOOZ_TEST run mysql -- {toxinidir}/tools/pretty_tox.sh "{po
commands = pifpaf -e TOOZ_TEST run mysql -- {toxinidir}/tools/pretty_tox.sh "{posargs}" commands = pifpaf -e TOOZ_TEST run mysql -- {toxinidir}/tools/pretty_tox.sh "{posargs}"
[testenv:py27-etcd] [testenv:py27-etcd]
commands = {toxinidir}/setup-etcd-env.sh pifpaf -g TOOZ_TEST run etcd -- {toxinidir}/tools/pretty_tox.sh "{posargs}" commands = {toxinidir}/setup-etcd-env.sh pifpaf -e TOOZ_TEST run etcd -- {toxinidir}/tools/pretty_tox.sh "{posargs}"
{toxinidir}/setup-etcd-env.sh pifpaf -g TOOZ_TEST run etcd --cluster -- {toxinidir}/tools/pretty_tox.sh "{posargs}" {toxinidir}/setup-etcd-env.sh pifpaf -e TOOZ_TEST run etcd --cluster -- {toxinidir}/tools/pretty_tox.sh "{posargs}"
[testenv:py35-etcd] [testenv:py35-etcd]
commands = {toxinidir}/setup-etcd-env.sh pifpaf -g TOOZ_TEST run etcd -- {toxinidir}/tools/pretty_tox.sh "{posargs}" commands = {toxinidir}/setup-etcd-env.sh pifpaf -e TOOZ_TEST run etcd -- {toxinidir}/tools/pretty_tox.sh "{posargs}"
{toxinidir}/setup-etcd-env.sh pifpaf -g TOOZ_TEST run etcd --cluster -- {toxinidir}/tools/pretty_tox.sh "{posargs}" {toxinidir}/setup-etcd-env.sh pifpaf -e TOOZ_TEST run etcd --cluster -- {toxinidir}/tools/pretty_tox.sh "{posargs}"
[testenv:py27-consul] [testenv:py27-consul]
commands = {toxinidir}/setup-consul-env.sh pifpaf -g TOOZ_TEST run consul -- {toxinidir}/tools/pretty_tox.sh "{posargs}" commands = {toxinidir}/setup-consul-env.sh pifpaf -e TOOZ_TEST run consul -- {toxinidir}/tools/pretty_tox.sh "{posargs}"
[testenv:py35-consul] [testenv:py35-consul]
commands = {toxinidir}/setup-consul-env.sh pifpaf -g TOOZ_TEST run consul -- {toxinidir}/tools/pretty_tox.sh "{posargs}" commands = {toxinidir}/setup-consul-env.sh pifpaf -e TOOZ_TEST run consul -- {toxinidir}/tools/pretty_tox.sh "{posargs}"
[testenv:cover] [testenv:cover]
commands = python setup.py testr --slowest --coverage --testr-args="{posargs}" commands = python setup.py testr --slowest --coverage --testr-args="{posargs}"