From dd2c059149844e9d5d1ba1c63bb0cb67ac7dbe65 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Thu, 5 Sep 2013 12:56:17 -0700 Subject: [PATCH] Warn if multiple providers found If many providers produce a given name as an output it is nice to be able to warn the user when this is happening as early as possible. Change-Id: Ia5f963120827c0228d318a799c8bc3c21b9687ac --- taskflow/storage.py | 14 +++++++------- taskflow/tests/unit/test_storage.py | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/taskflow/storage.py b/taskflow/storage.py index b87f860b..f0254c81 100644 --- a/taskflow/storage.py +++ b/taskflow/storage.py @@ -26,10 +26,7 @@ from taskflow import states from taskflow.utils import misc from taskflow.utils import threading_utils - LOG = logging.getLogger(__name__) - - STATES_WITH_RESULTS = (states.SUCCESS, states.REVERTING, states.FAILURE) @@ -214,9 +211,9 @@ class Storage(object): injector_uuid = uuidutils.generate_uuid() self.add_task(injector_uuid, self.injector_name) self.save(injector_uuid, pairs) - for name in pairs.iterkeys(): - entries = self._reverse_mapping.setdefault(name, []) - entries.append((injector_uuid, name)) + self.set_result_mapping(injector_uuid, + dict((name, name) + for name in pairs.iterkeys())) def set_result_mapping(self, uuid, mapping): """Set mapping for naming task results @@ -232,6 +229,9 @@ class Storage(object): for name, index in mapping.iteritems(): entries = self._reverse_mapping.setdefault(name, []) entries.append((uuid, index)) + if len(entries) > 1: + LOG.warning("Multiple provider mappings being created for %r", + name) def fetch(self, name): """Fetch named task result""" @@ -240,7 +240,7 @@ class Storage(object): except KeyError: raise exceptions.NotFound("Name %r is not mapped" % name) # Return the first one that is found. - for uuid, index in indexes: + for uuid, index in reversed(indexes): try: result = self.get(uuid) return _item_from_result(result, index, name) diff --git a/taskflow/tests/unit/test_storage.py b/taskflow/tests/unit/test_storage.py index 9fe6040f..36888da0 100644 --- a/taskflow/tests/unit/test_storage.py +++ b/taskflow/tests/unit/test_storage.py @@ -223,3 +223,24 @@ class StorageTest(test.TestCase): with self.assertRaisesRegexp(exceptions.NotFound, '^Unable to find result'): s.fetch('b') + + @mock.patch.object(storage.LOG, 'warning') + def test_multiple_providers_are_checked(self, mocked_warning): + s = self._get_storage() + s.add_task('42', 'my task') + s.set_result_mapping('42', {'result': 'key'}) + self.assertEquals(mocked_warning.mock_calls, []) + s.add_task('43', 'my other task') + s.set_result_mapping('43', {'result': 'key'}) + mocked_warning.assert_called_once_with( + mock.ANY, 'result') + + @mock.patch.object(storage.LOG, 'warning') + def test_multiple_providers_with_inject_are_checked(self, mocked_warning): + s = self._get_storage() + s.inject({'result': 'DONE'}) + self.assertEquals(mocked_warning.mock_calls, []) + s.add_task('43', 'my other task') + s.set_result_mapping('43', {'result': 'key'}) + mocked_warning.assert_called_once_with( + mock.ANY, 'result')