Skip to content

Commit

Permalink
sync with latest pytest-lockable core functionality (#2)
Browse files Browse the repository at this point in the history
* update lockable implementation and rename file
* add missing import
* update tests
* add missing docstring
  • Loading branch information
jupe authored Oct 2, 2020
1 parent cf2b3e2 commit 76551a8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
3 changes: 3 additions & 0 deletions lockable/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
""" Lockable module """
from lockable.lockable import read_resources_list, parse_requirements, lock, \
validate_json, ResourceNotFound, get_requirements
19 changes: 15 additions & 4 deletions lockable/plugin.py → lockable/lockable.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from filelock import Timeout, FileLock


class ResourceNotFound(Exception):
""" Exception raised when resource not found """


def read_resources_list(filename):
""" Read resources json file """
with open(filename) as json_file:
Expand Down Expand Up @@ -49,6 +53,10 @@ def parse_requirements(requirements_str):
except ValueError:
continue
key, value = part.split('=')
if value.lower() == "true":
value = True
elif value.lower() == "false":
value = False
requirements[key] = value
return requirements

Expand All @@ -58,13 +66,14 @@ def _try_lock(candidate, lock_folder):
resource_id = candidate.get("id")
try:
lock_file = os.path.join(lock_folder, f"{resource_id}.lock")
lockable = FileLock(lock_file)
lockable.acquire(timeout=0)
_lockable = FileLock(lock_file)
_lockable.acquire(timeout=0)
print(f'Allocated resource: {resource_id}')

def release():
nonlocal _lockable
print(f'Release resource: {resource_id}')
lockable.release()
_lockable.release()
try:
os.remove(lock_file)
except OSError as error:
Expand Down Expand Up @@ -106,11 +115,13 @@ def lock(requirements: dict,
""" Lock resource context """
local_resources = filter_(resource_list, requirements)
random.shuffle(local_resources)
if not local_resources:
raise ResourceNotFound("Suitable resource not available")
with _lock_some(local_resources, timeout_s, lock_folder, retry_interval) as resource:
yield resource


def _get_requirements(requirements, hostname):
def get_requirements(requirements, hostname):
""" Generate requirements"""
print(f'hostname: {hostname}')
return merge(dict(hostname=hostname, online=True), requirements)
11 changes: 6 additions & 5 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import multiprocessing as mp
from tempfile import mktemp
from contextlib import contextmanager
from lockable.plugin import read_resources_list, parse_requirements, lock, validate_json
from lockable.lockable import read_resources_list, parse_requirements, lock, validate_json, ResourceNotFound


HOSTNAME = socket.gethostname()
Expand Down Expand Up @@ -50,6 +50,7 @@ def test_parse_requirements(self):
self.assertEqual(parse_requirements("a=b&b=2&"), dict(a='b', b="2"))
self.assertEqual(parse_requirements('{"a":"c"}'), dict(a='c'))
self.assertEqual(parse_requirements('{"a":"c", "d":2}'), dict(a='c', d=2))
self.assertEqual(parse_requirements('a=true&d=False'), dict(a=True, d=False))

def test_lock_success(self):
requirements = dict(id=1)
Expand All @@ -66,18 +67,18 @@ def test_not_available(self):
with lock(requirements=requirements, resource_list=resource_list,
lock_folder='.', timeout_s=0.1):
raise AssertionError('did not raise TimeoutError')
except TimeoutError:
except ResourceNotFound:
pass
self.assertFalse(os.path.exists('1.lock'))

def test_not_online(self):
requirements = dict(id=1)
requirements = dict(id='1', online=True)
resource_list = [dict(hostame=HOSTNAME, id='1', online=False)]
try:
with lock(requirements=requirements, resource_list=resource_list,
lock_folder='.', timeout_s=0.1):
raise AssertionError('did not raise TimeoutError')
except TimeoutError:
raise AssertionError('did not raise ResourceNotFound')
except ResourceNotFound:
pass
self.assertFalse(os.path.exists('1.lock'))

Expand Down

0 comments on commit 76551a8

Please sign in to comment.