Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't remove lazy component from deferred until success #651

Merged
merged 1 commit into from
Sep 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion botocore/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,12 @@ def __init__(self):

def get_component(self, name):
if name in self._deferred:
factory = self._deferred.pop(name)
factory = self._deferred[name]
self._components[name] = factory()
# Only delete the component from the deferred dict after
# successfully creating the object from the factory as well as
# injecting the instantiated value into the _components dict.
del self._deferred[name]
try:
return self._components[name]
except KeyError:
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,65 @@ def test_create_client_no_region_and_no_client_config(self):
self.assertEqual(ec2_client.meta.region_name, 'moon-west-1')


class TestComponentLocator(unittest.TestCase):
def setUp(self):
self.components = botocore.session.ComponentLocator()

def test_unknown_component_raises_exception(self):
with self.assertRaises(ValueError):
self.components.get_component('unknown-component')

def test_can_register_and_retrieve_component(self):
component = object()
self.components.register_component('foo', component)
self.assertIs(self.components.get_component('foo'), component)

def test_last_registration_wins(self):
first = object()
second = object()
self.components.register_component('foo', first)
self.components.register_component('foo', second)
self.assertIs(self.components.get_component('foo'), second)

def test_can_lazy_register_a_component(self):
component = object()
lazy = lambda: component
self.components.lazy_register_component('foo', lazy)
self.assertIs(self.components.get_component('foo'), component)

def test_latest_registration_wins_even_if_lazy(self):
first = object()
second = object()
lazy_second = lambda: second
self.components.register_component('foo', first)
self.components.lazy_register_component('foo', lazy_second)
self.assertIs(self.components.get_component('foo'), second)

def test_latest_registration_overrides_lazy(self):
first = object()
second = object()
lazy_first = lambda: first
self.components.lazy_register_component('foo', lazy_first)
self.components.register_component('foo', second)
self.assertIs(self.components.get_component('foo'), second)

def test_lazy_registration_factory_does_not_remove_from_list_on_error(self):
class ArbitraryError(Exception):
pass

def bad_factory():
raise ArbitraryError("Factory raises an exception.")

self.components.lazy_register_component('foo', bad_factory)

with self.assertRaises(ArbitraryError):
self.components.get_component('foo')

# Trying again should raise the same exception,
# not an ValueError("Unknown component")
with self.assertRaises(ArbitraryError):
self.components.get_component('foo')


if __name__ == "__main__":
unittest.main()