[MERGED] osmo-gsm-tester[master]: Resource.find: allow returning empty instead of raising

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Mon May 8 10:11:53 UTC 2017


Neels Hofmeyr has submitted this change and it was merged.

Change subject: Resource.find: allow returning empty instead of raising
......................................................................


Resource.find: allow returning empty instead of raising

Add flag raise_if_missing, and if False, instead of raising an exception,
return an empty list for that kind of resource. This makes sense for a caller
that requests a single resource.

When finding a single resource to use in ReservedResources.get(), use this to
raise a more adequate exception message if none was found.

Change-Id: Ia296ea68a787bede037a6cea38563b570fb0766e
---
M src/osmo_gsm_tester/resource.py
1 file changed, 15 insertions(+), 6 deletions(-)

Approvals:
  Pau Espin Pedrol: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/src/osmo_gsm_tester/resource.py b/src/osmo_gsm_tester/resource.py
index 73bb184..699b465 100644
--- a/src/osmo_gsm_tester/resource.py
+++ b/src/osmo_gsm_tester/resource.py
@@ -270,7 +270,7 @@
     def without(self, reserved):
         return Resources(self).drop(reserved)
 
-    def find(self, want, skip_if_marked=None, do_copy=True):
+    def find(self, want, skip_if_marked=None, do_copy=True, raise_if_missing=True):
         '''
         Pass a dict of resource requirements, e.g.:
           want = {
@@ -321,13 +321,22 @@
                     if item_matches(my_item, want_item, ignore_keys=('times',)):
                         item_match_list.append(i)
                 if not item_match_list:
-                    raise NoResourceExn('No matching resource available for %s = %r'
-                                        % (key, want_item))
+                    if raise_if_missing:
+                        raise NoResourceExn('No matching resource available for %s = %r'
+                                            % (key, want_item))
+                    else:
+                        # this one failed... see below
+                        all_matches = []
+                        break
+
                 all_matches.append( item_match_list )
 
             if not all_matches:
-                raise NoResourceExn('No matching resource available for %s = %r'
-                                    % (key, want_list))
+                # ...this one failed. Makes no sense to solve resource
+                # allocations, return an empty list for this key to mark
+                # failure.
+                matches[key] = []
+                continue
 
             # figure out who gets what
             solution = solve(all_matches)
@@ -450,7 +459,7 @@
             specifics = {}
         self.dbg('requesting use of', kind, specifics=specifics)
         want = { kind: [specifics] }
-        available_dict = self.reserved.find(want, skip_if_marked=USED_KEY, do_copy=False)
+        available_dict = self.reserved.find(want, skip_if_marked=USED_KEY, do_copy=False, raise_if_missing=False)
         available = available_dict.get(kind)
         self.dbg(available=len(available))
         if not available:

-- 
To view, visit https://gerrit.osmocom.org/2502
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia296ea68a787bede037a6cea38563b570fb0766e
Gerrit-PatchSet: 3
Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list