Skip to content

Commit

Permalink
Add the wait-on-locks option for acquiring lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jfan666 committed Jun 5, 2018
1 parent b411bd2 commit 0559736
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions pytest_lab/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,31 @@ def __init__(self, config, backend, ttl=30):
self._thread = None
self._stop = threading.Event()

def aquire(self, key, user=None):
def aquire(self, key, wait_on_locks, user=None):
record = self.backend.read(key)

if record and record.ttl:
logger.error('{} is locked by {}, waiting {} seconds for lock '
'to expire...'.format(key, record.value, record.ttl + 1))
start = time.time()
while time.time() - start < record.ttl + 1:
record = self.backend.read(key)
if not record:
break
time.sleep(0.5)

if record:
raise ResourceLocked(
'{} is currently locked by {}'.format(key, record.value))
def check_record(record):
if record and record.ttl:
logger.error(
'{} is locked by {}, waiting {} seconds for re-checking '
'lock to expire...'.format(key, record.value, record.ttl + 1))
start = time.time()
while time.time() - start < record.ttl + 1:
record = self.backend.read(key)
if not record:
return True
time.sleep(0.5)
else:
return True

if not check_record(record):
if wait_on_locks:
while True:
if check_record(record):
break
else:
raise ResourceLocked(
'{} is currently locked by {}'.format(key, record.value))

# acquire
lockid = get_lock_id(user)
Expand Down Expand Up @@ -173,7 +182,7 @@ def pytest_unconfigure(self, config):

@pytest.hookimpl
def pytest_lab_aquire_lock(self, config, identifier):
self.aquire(identifier)
self.aquire(identifier, config.getoption('--wait-on-locks'))
return True

@pytest.hookimpl
Expand Down

0 comments on commit 0559736

Please sign in to comment.