diff options
author | Sam Thursfield <sam@afuera.me.uk> | 2014-07-20 14:55:37 +0100 |
---|---|---|
committer | Sam Thursfield <sam@afuera.me.uk> | 2014-10-14 14:08:19 +0100 |
commit | 6d9481fc17e2e517c335ff1ee05848459e382a3b (patch) | |
tree | 260742c5d4e368386734ba2e9baa13af402990ef | |
parent | b93b6ec21e095c2628fadf147d692b42f04ea130 (diff) | |
download | tracker-6d9481fc17e2e517c335ff1ee05848459e382a3b.tar.gz |
functional-tests: Improve the wait-for-change code in helpers.StoreHelper
Fixes a bug in await_resource_deleted() where it would stop waiting as soon
as any resource was deleted, even if it wasn't the correct one.
New get_resource_id() function which is useful when calling
await_resource_deleted().
Code is hopefully clearer too.
-rw-r--r-- | tests/functional-tests/common/utils/helpers.py | 83 |
1 files changed, 49 insertions, 34 deletions
diff --git a/tests/functional-tests/common/utils/helpers.py b/tests/functional-tests/common/utils/helpers.py index ab91c0ae4..d957381b5 100644 --- a/tests/functional-tests/common/utils/helpers.py +++ b/tests/functional-tests/common/utils/helpers.py @@ -282,27 +282,37 @@ class StoreHelper (Helper): """ Process notifications from tracker-store on resource changes. """ - matched = False + exit_loop = False + if inserts_list is not None: if self.inserts_match_function is not None: # The match function will remove matched entries from the list - (matched, inserts_list) = self.inserts_match_function (inserts_list) + (exit_loop, inserts_list) = self.inserts_match_function (inserts_list) self.inserts_list += inserts_list if deletes_list is not None: if self.deletes_match_function is not None: - (matched, deletes_list) = self.deletes_match_function (deletes_list) + (exit_loop, deletes_list) = self.deletes_match_function (deletes_list) self.deletes_list += deletes_list + if exit_loop: + GLib.source_remove(self.graph_updated_timeout_id) + self.graph_updated_timeout_id = 0 + self.loop.quit () + + def _enable_await_timeout (self): + self.graph_updated_timeout_id = GLib.timeout_add_seconds (REASONABLE_TIMEOUT, + self._graph_updated_timeout_cb) + def await_resource_inserted (self, rdf_class, url = None, title = None): """ Block until a resource matching the parameters becomes available """ assert (self.inserts_match_function == None) - def match_cb (inserts_list, in_main_loop = True): + def find_resource_insertion (inserts_list): matched = False - filtered_list = [] + remaining_events = [] known_subjects = set () #print "Got inserts: ", inserts_list, "\n" @@ -335,16 +345,14 @@ class StoreHelper (Helper): self.matched_resource_id = insert[1] if not matched or id != self.matched_resource_id: - filtered_list += [insert] + remaining_events += [insert] - if matched and in_main_loop: - GLib.source_remove (self.graph_updated_timeout_id) - self.graph_updated_timeout_id = 0 - self.inserts_match_function = None - self.loop.quit () - - return (matched, filtered_list) + return matched, remaining_events + def match_cb (inserts_list): + matched, remaining_events = find_resource_insertion (inserts_list) + exit_loop = matched + return exit_loop, remaining_events self.matched_resource_urn = None self.matched_resource_id = None @@ -352,60 +360,55 @@ class StoreHelper (Helper): log ("Await new %s (%i existing inserts)" % (rdf_class, len (self.inserts_list))) # Check the list of previously received events for matches - (existing_match, self.inserts_list) = match_cb (self.inserts_list, False) + (existing_match, self.inserts_list) = find_resource_insertion (self.inserts_list) if not existing_match: - self.graph_updated_timeout_id = GLib.timeout_add_seconds (REASONABLE_TIMEOUT, - self._graph_updated_timeout_cb) + self._enable_await_timeout () self.inserts_match_function = match_cb - # Run the event loop until the correct notification arrives self.loop.run () + self.inserts_match_function = None if self.graph_updated_timed_out: raise Exception ("Timeout waiting for resource: class %s, URL %s, title %s" % (rdf_class, url, title)) return (self.matched_resource_id, self.matched_resource_urn) - def await_resource_deleted (self, id, fail_message = None): """ Block until we are notified of a resources deletion """ assert (self.deletes_match_function == None) - def match_cb (deletes_list, in_main_loop = True): - matched = False - filtered_list = [] + def find_resource_deletion (deletes_list): + log ("find_resource_deletion: looking for %i in %s" % (id, deletes_list)) - #print "Looking for %i in " % id, deletes_list, "\n" + matched = False + remaining_events = [] for delete in deletes_list: if delete[1] == id: matched = True else: - filtered_list += [delete] - - if matched and in_main_loop: - GLib.source_remove (self.graph_updated_timeout_id) - self.graph_updated_timeout_id = 0 - self.deletes_match_function = None + remaining_events += [delete] - self.loop.quit () + return matched, remaining_events - return (matched, filtered_list) + def match_cb (deletes_list): + matched, remaining_events = find_resource_deletion(deletes_list) + exit_loop = matched + return exit_loop, remaining_events log ("Await deletion of %i (%i existing)" % (id, len (self.deletes_list))) - (existing_match, self.deletes_list) = match_cb (self.deletes_list, False) + (existing_match, self.deletes_list) = find_resource_deletion (self.deletes_list) if not existing_match: - self.graph_updated_timeout_id = GLib.timeout_add_seconds (REASONABLE_TIMEOUT, - self._graph_updated_timeout_cb) + self._enable_await_timeout () self.deletes_match_function = match_cb - # Run the event loop until the correct notification arrives self.loop.run () + self.deletes_match_function = None if self.graph_updated_timed_out: if fail_message is not None: @@ -496,6 +499,18 @@ class StoreHelper (Helper): else: return -1 + def get_resource_id(self, url): + """ + Get the internal ID for a given resource, identified by URL. + """ + result = self.query( + 'SELECT tracker:id(?r) WHERE { ?r nie:url "%s" }' % url) + if len(result) == 1: + return int (result [0][0]) + elif len(result) == 0: + raise Exception ("No entry for resource %s" % url) + else: + raise Exception ("Multiple entries for resource %s" % url) def ask (self, ask_query): assert ask_query.strip ().startswith ("ASK") |