Skip to content

Commit

Permalink
Change Lockable._resource_list to public (#29)
Browse files Browse the repository at this point in the history
* Change Lockable._resource_list to public
* Add docstring
* Add test
  • Loading branch information
juhhov authored Nov 9, 2021
1 parent 863b691 commit b9fc6ec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lockable/lockable.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def __init__(self, hostname=socket.gethostname(),
self._provider = create_provider(resource_list_file or resource_list)

@property
def _resource_list(self):
def resource_list(self) -> list:
""" Return current resources list"""
return self._provider.data

@staticmethod
Expand Down Expand Up @@ -134,7 +135,7 @@ def _lock_some(self, requirements, candidates, timeout_s, retry_interval):

def _lock(self, requirements, timeout_s, retry_interval=1) -> Allocation:
""" Lock resource """
local_resources = filter_(self._resource_list, requirements)
local_resources = filter_(self.resource_list, requirements)
random.shuffle(local_resources)
ResourceNotFound.invariant(local_resources, "Suitable resource not available")
return self._lock_some(requirements, local_resources, timeout_s, retry_interval)
Expand All @@ -152,15 +153,15 @@ def lock(self, requirements: (str or dict), timeout_s: int = 1000) -> Allocation
:param timeout_s: timeout while trying to lock
:return: Allocation context
"""
assert isinstance(self._resource_list, list), 'resources list is not loaded'
assert isinstance(self.resource_list, list), 'resources list is not loaded'
requirements = self.parse_requirements(requirements)
predicate = self._get_requirements(requirements, self._hostname)
# Refresh resources data
self._provider.reload()
begin = datetime.now()
MODULE_LOGGER.debug("Use lock folder: %s", self._lock_folder)
MODULE_LOGGER.debug("Requirements: %s", json.dumps(predicate))
MODULE_LOGGER.debug("Resource list: %s", json.dumps(self._resource_list))
MODULE_LOGGER.debug("Resource list: %s", json.dumps(self.resource_list))
allocation = self._lock(predicate, timeout_s)
self._allocations[allocation.resource_id] = allocation
allocation.allocation_queue_time = datetime.now() - begin
Expand Down
8 changes: 8 additions & 0 deletions tests/test_Lockable.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def test_constructor(self):
lockable = Lockable(hostname='myhost', resource_list_file=list_file, lock_folder=tmpdirname)
self.assertFalse(lockable._provider._resource_list_file_mtime is None)

def test_resource_list(self):
with TemporaryDirectory() as tmpdirname:
list_file = os.path.join(tmpdirname, 'test.json')
with open(list_file, 'w') as fp:
fp.write('[{"id": "123"}]')
lockable = Lockable(hostname='myhost', resource_list_file=list_file, lock_folder=tmpdirname)
self.assertEqual(lockable.resource_list, [{"id": "123"}])

def test_reload_resource_list_file(self):
with TemporaryDirectory() as tmpdirname:
list_file = os.path.join(tmpdirname, 'test.json')
Expand Down

0 comments on commit b9fc6ec

Please sign in to comment.