Repository: boto/botocore
Pull request #651 (2015-09-18 22:37:28)

Don't remove lazy component from deferred until success
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.

The bug is that is the factory object raises an exception,
we've already removed it from the deferred list (via the
.pop() call), but we haven't added anything to the _components
dict.

Because of this, the next time you try to make a call you'll
get an error message about an unknown component.

It also looks like there were no unit tests for this class.
It was only tested transitively before.  I've added a set
of unit tests covering this behavior.

This was found while looking into https://github.com/aws/aws-cli/pull/1515

cc @kyleknap @mtdowling @rayluo 


Patch:
diff --git a/botocore/client.py b/botocore/client.py
index df65c3ae4..995fb776b 100644
--- a/botocore/client.py
+++ b/botocore/client.py
@@ -28,7 +28,6 @@ from botocore.signers import RequestSigner
 from botocore.utils import CachedProperty
 from botocore.utils import get_service_module_name
 from botocore.docs.docstring import ClientMethodDocstring
-from botocore.docs.docstring import PaginatorDocstring
 
 
 logger = logging.getLogger(__name__)
@@ -399,36 +398,9 @@ class BaseClient(object):
             raise OperationNotPageableError(operation_name=operation_name)
         else:
             actual_operation_name = self._PY_TO_OP_NAME[operation_name]
-
-            # Create a new paginate method that will serve as a proxy to
-            # the underlying Paginator.paginate method. This is needed to
-            # attach a docstring to the method.
-            def paginate(self, **kwargs):
-                return Paginator.paginate(self, **kwargs)
-
-            paginator_config = self._cache['page_config'][
-                actual_operation_name]
-            # Add the docstring for the paginate method.
-            paginate.__doc__ = PaginatorDocstring(
-                paginator_name=actual_operation_name,
-                event_emitter=self.meta.events,
-                service_model=self.meta.service_model,
-                paginator_config=paginator_config,
-                include_signature=False
-            )
-
-            # Rename the paginator class based on the type of paginator.
-            paginator_class_name = str('%s.Paginator.%s' % (
-                get_service_module_name(self.meta.service_model),
-                actual_operation_name))
-
-            # Create the new paginator class
-            documented_paginator_cls = type(
-                paginator_class_name, (Paginator,), {'paginate': paginate})
-
-            paginator = documented_paginator_cls(
+            paginator = Paginator(
                 getattr(self, operation_name),
-                paginator_config)
+                self._cache['page_config'][actual_operation_name])
             return paginator
 
     def can_paginate(self, operation_name):
diff --git a/botocore/docs/docstring.py b/botocore/docs/docstring.py
index 5146b79d6..de07e83e3 100644
--- a/botocore/docs/docstring.py
+++ b/botocore/docs/docstring.py
@@ -12,7 +12,6 @@
 # language governing permissions and limitations under the License.
 from botocore.docs.method import document_model_driven_method
 from botocore.docs.waiter import document_wait_method
-from botocore.docs.paginator import document_paginate_method
 from botocore.docs.bcdoc.restdoc import DocumentStructure
 
 
@@ -89,8 +88,3 @@ class ClientMethodDocstring(LazyLoadedDocstring):
 class WaiterDocstring(LazyLoadedDocstring):
     def _write_docstring(self, *args, **kwargs):
         document_wait_method(*args, **kwargs)
-
-
-class PaginatorDocstring(LazyLoadedDocstring):
-    def _write_docstring(self, *args, **kwargs):
-        document_paginate_method(*args, **kwargs)
diff --git a/botocore/docs/paginator.py b/botocore/docs/paginator.py
index 13e0bb226..08adb674c 100644
--- a/botocore/docs/paginator.py
+++ b/botocore/docs/paginator.py
@@ -13,7 +13,6 @@
 from botocore import xform_name
 from botocore.compat import OrderedDict
 from botocore.docs.utils import DocumentedShape
-from botocore.utils import get_service_module_name
 from botocore.docs.method import document_model_driven_method
 
 
@@ -39,7 +38,8 @@ class PaginatorDocumenter(object):
         for paginator_name in paginator_names:
             section.style.li(
                 ':py:class:`%s.Paginator.%s`' % (
-                    self._client.__class__.__name__, paginator_name))
+                    self._client.__class__.__name__,
+                    xform_name(paginator_name)))
             self._add_paginator(section, paginator_name)
 
     def _add_paginator(self, section, paginator_name):
@@ -48,7 +48,7 @@ class PaginatorDocumenter(object):
         # Docment the paginator class
         section.style.start_sphinx_py_class(
             class_name='%s.Paginator.%s' % (
-                self._client.__class__.__name__, paginator_name))
+                self._client.__class__.__name__, xform_name(paginator_name)))
         section.style.start_codeblock()
         section.style.new_line()
 
@@ -59,118 +59,93 @@ class PaginatorDocumenter(object):
         )
         section.style.end_codeblock()
         section.style.new_line()
+        self._add_paginate_method(section, paginator_name)
+
+    def _add_paginate_method(self, section, paginator_name):
         # Get the pagination model for the particular paginator.
         paginator_config = self._service_paginator_model.get_paginator(
             paginator_name)
-        document_paginate_method(
-            section=section,
-            paginator_name=paginator_name,
-            event_emitter=self._client.meta.events,
-            service_model=self._client.meta.service_model,
-            paginator_config=paginator_config
-        )
-
-
-def document_paginate_method(section, paginator_name, event_emitter,
-                             service_model, paginator_config,
-                             include_signature=True):
-    """Documents the paginate method of a paginator
-
-    :param section: The section to write to
-
-    :param paginator_name: The name of the paginator. It is snake cased.
-
-    :param event_emitter: The event emitter to use to emit events
-
-    :param service_model: The service model
 
-    :param paginator_config: The paginator config associated to a particular
-        paginator.
-
-    :param include_signature: Whether or not to include the signature.
-        It is useful for generating docstrings.
-    """
-    # Retrieve the operation model of the underlying operation.
-    operation_model = service_model.operation_model(
-        paginator_name)
-
-    # Add representations of the request and response parameters
-    # we want to include in the description of the paginate method.
-    # These are parameters we expose via the botocore interface.
-    pagination_config_members = OrderedDict()
-
-    pagination_config_members['MaxItems'] = DocumentedShape(
-        name='MaxItems', type_name='integer',
-        documentation=(
-            '<p>The total number of items to return. If the total '
-            'number of items available is more than the value '
-            'specified in max-items then a <code>NextToken</code> '
-            'will be provided in the output that you can use to '
-            'resume pagination.</p>'))
-
-    pagination_config_members['PageSize'] = DocumentedShape(
-        name='PageSize', type_name='integer',
-        documentation='<p>The size of each page.<p>')
+        # Retrieve the operation model of the underlying operation.
+        operation_model = self._client.meta.service_model.operation_model(
+            paginator_name)
 
-    pagination_config_members['StartingToken'] = DocumentedShape(
-        name='StartingToken', type_name='string',
-        documentation=(
-            '<p>A token to specify where to start paginating. '
-            'This is the <code>NextToken</code> from a previous '
-            'response.</p>'))
+        # Add representations of the request and response parameters
+        # we want to include in the description of the paginate method.
+        # These are parameters we expose via the botocore interface.
+        pagination_config_members = OrderedDict()
 
-    botocore_pagination_params = [
-        DocumentedShape(
-            name='PaginationConfig', type_name='structure',
+        pagination_config_members['MaxItems'] = DocumentedShape(
+            name='MaxItems', type_name='integer',
             documentation=(
-                '<p>A dictionary that provides parameters to control '
-                'pagination.</p>'),
-            members=pagination_config_members)
-    ]
-
-    botocore_pagination_response_params = [
-        DocumentedShape(
-            name='NextToken', type_name='string',
+                '<p>The total number of items to return. If the total '
+                'number of items available is more than the value '
+                'specified in max-items then a <code>NextToken</code> '
+                'will be provided in the output that you can use to '
+                'resume pagination.</p>'))
+
+        pagination_config_members['PageSize'] = DocumentedShape(
+            name='PageSize', type_name='integer',
+            documentation='<p>The size of each page.<p>')
+
+        pagination_config_members['StartingToken'] = DocumentedShape(
+            name='StartingToken', type_name='string',
             documentation=(
-                '<p>A token to resume pagination.</p>'))
-    ]
-
-    service_pagination_params = []
-
-    # Add the normal input token of the method to a list
-    # of input paramters that we wish to hide since we expose our own.
-    if isinstance(paginator_config['input_token'], list):
-        service_pagination_params += paginator_config['input_token']
-    else:
-        service_pagination_params.append(paginator_config['input_token'])
-
-    # Hide the limit key in the documentation.
-    if paginator_config.get('limit_key', None):
-        service_pagination_params.append(paginator_config['limit_key'])
-
-    # Hide the output tokens in the documentation.
-    service_pagination_response_params = []
-    if isinstance(paginator_config['output_token'], list):
-        service_pagination_response_params += paginator_config[
-            'output_token']
-    else:
-        service_pagination_response_params.append(paginator_config[
-            'output_token'])
-
-    paginate_description = (
-        'Creates an iterator that will paginate through responses '
-        'from :py:meth:`{0}.Client.{1}`.'.format(
-            get_service_module_name(service_model), xform_name(paginator_name))
-    )
-
-    document_model_driven_method(
-        section, 'paginate', operation_model,
-        event_emitter=event_emitter,
-        method_description=paginate_description,
-        example_prefix='response_iterator = paginator.paginate',
-        include_input=botocore_pagination_params,
-        include_output=botocore_pagination_response_params,
-        exclude_input=service_pagination_params,
-        exclude_output=service_pagination_response_params,
-        include_signature=include_signature
-    )
+                '<p>A token to specify where to start paginating. '
+                'This is the <code>NextToken</code> from a previous '
+                'response.</p>'))
+
+        botocore_pagination_params = [
+            DocumentedShape(
+                name='PaginationConfig', type_name='structure',
+                documentation=(
+                    '<p>A dictionary that provides parameters to control '
+                    'pagination.</p>'),
+                members=pagination_config_members)
+        ]
+
+        botocore_pagination_response_params = [
+            DocumentedShape(
+                name='NextToken', type_name='string',
+                documentation=(
+                    '<p>A token to resume pagination.</p>'))
+        ]
+
+        service_pagination_params = []
+
+        # Add the normal input token of the method to a list
+        # of input paramters that we wish to hide since we expose our own.
+        if isinstance(paginator_config['input_token'], list):
+            service_pagination_params += paginator_config['input_token']
+        else:
+            service_pagination_params.append(paginator_config['input_token'])
+
+        # Hide the limit key in the documentation.
+        if paginator_config.get('limit_key', None):
+            service_pagination_params.append(paginator_config['limit_key'])
+
+        # Hide the output tokens in the documentation.
+        service_pagination_response_params = []
+        if isinstance(paginator_config['output_token'], list):
+            service_pagination_response_params += paginator_config[
+                'output_token']
+        else:
+            service_pagination_response_params.append(paginator_config[
+                'output_token'])
+
+        paginate_description = (
+            'Creates an iterator that will paginate through responses '
+            'from :py:meth:`{0}.Client.{1}`.'.format(
+                self._client.__class__.__name__, xform_name(paginator_name))
+        )
+
+        document_model_driven_method(
+            section, 'paginate', operation_model,
+            event_emitter=self._client.meta.events,
+            method_description=paginate_description,
+            example_prefix='response_iterator = paginator.paginate',
+            include_input=botocore_pagination_params,
+            include_output=botocore_pagination_response_params,
+            exclude_input=service_pagination_params,
+            exclude_output=service_pagination_response_params
+        )
diff --git a/botocore/loaders.py b/botocore/loaders.py
index 3ec069f9f..5e0b63ad3 100644
--- a/botocore/loaders.py
+++ b/botocore/loaders.py
@@ -98,7 +98,7 @@ import os
 from botocore import BOTOCORE_ROOT
 from botocore.compat import json
 from botocore.compat import OrderedDict
-from botocore.exceptions import DataNotFoundError, ValidationError
+from botocore.exceptions import DataNotFoundError
 
 
 def instance_cache(func):
@@ -333,9 +333,6 @@ class Loader(object):
         """
         # Wrapper around the load_data.  This will calculate the path
         # to call load_data with.
-        if service_name not in self.list_available_services('service-2'):
-            raise ValidationError(value=service_name, param='service_name',
-                                  type_name='str')
         if api_version is None:
             api_version = self.determine_latest_version(
                 service_name, type_name)
diff --git a/botocore/session.py b/botocore/session.py
index 411bdf62a..ba88c040f 100644
--- a/botocore/session.py
+++ b/botocore/session.py
@@ -770,8 +770,12 @@ class ComponentLocator(object):
 
     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:


Test patch:
diff --git a/tests/cmd-runner b/tests/cmd-runner
index 7723952c7..3b1cb8869 100644
--- a/tests/cmd-runner
+++ b/tests/cmd-runner
@@ -78,7 +78,6 @@ class CommandRunner(object):
         self._session = botocore.session.get_session()
         self._clients = []
         self._waiters = []
-        self._paginators = []
 
     def run(self):
         while True:
@@ -135,27 +134,6 @@ class CommandRunner(object):
         # Frees the memory associated with all of the waiters.
         self._waiters = []
 
-    def _do_create_paginator(self, args):
-        # Args:
-        # [0] client name used in its instantiation
-        # [1] paginator name used in its instantiation
-        client = self._session.create_client(args[0])
-        self._paginators.append(client.get_paginator(args[1]))
-
-    def _do_create_multiple_paginators(self, args):
-        # Args:
-        # [0] num_paginators - Number of paginators to create in a loop
-        # [1] client name used in its instantiation
-        # [2] paginator name used in its instantiation
-        num_paginators = args[0]
-        client = self._session.create_client(args[1])
-        for _ in range(int(num_paginators)):
-            self._paginators.append(client.get_paginator(args[2]))
-
-    def _do_free_paginators(self, args):
-        # Frees the memory associated with all of the waiters.
-        self._paginators = []
-
     def _do_make_aws_request(self, args):
         # Create a client and make a number of AWS requests.
         # Args:
diff --git a/tests/functional/leak/test_resource_leaks.py b/tests/functional/leak/test_resource_leaks.py
index fad21c0ec..bc1a7b20b 100644
--- a/tests/functional/leak/test_resource_leaks.py
+++ b/tests/functional/leak/test_resource_leaks.py
@@ -82,29 +82,3 @@ class TestDoesNotLeakMemory(BaseClientDriverTest):
         self.record_memory()
         start, end = self.memory_samples
         self.assertTrue((end - start) < self.MAX_GROWTH_BYTES, (end - start))
-
-    def test_create_single_paginator_memory_constant(self):
-        self.cmd('create_paginator', 's3', 'list_objects')
-        self.cmd('free_paginators')
-        self.record_memory()
-        for _ in range(100):
-            self.cmd('create_paginator', 's3', 'list_objects')
-            self.cmd('free_paginators')
-        self.record_memory()
-        start, end = self.memory_samples
-        self.assertTrue((end - start) < self.MAX_GROWTH_BYTES, (end - start))
-
-    def test_create_memory_paginators_in_loop(self):
-        # See ``test_create_memory_clients_in_loop`` to understand why
-        # paginators are first initialized and then freed. Same reason applies.
-        self.cmd('create_multiple_paginators', '200', 's3', 'list_objects')
-        self.cmd('free_paginators')
-        self.record_memory()
-        # 500 waiters in batches of 50.
-        for _ in range(10):
-            self.cmd(
-                'create_multiple_paginators', '50', 's3', 'list_objects')
-            self.cmd('free_paginators')
-        self.record_memory()
-        start, end = self.memory_samples
-        self.assertTrue((end - start) < self.MAX_GROWTH_BYTES, (end - start))
diff --git a/tests/unit/docs/test_docstring.py b/tests/unit/docs/test_docstring.py
index 01adb4af3..0f3073c74 100644
--- a/tests/unit/docs/test_docstring.py
+++ b/tests/unit/docs/test_docstring.py
@@ -14,7 +14,6 @@ from tests import unittest, mock
 from botocore.docs.docstring import LazyLoadedDocstring
 from botocore.docs.docstring import ClientMethodDocstring
 from botocore.docs.docstring import WaiterDocstring
-from botocore.docs.docstring import PaginatorDocstring
 
 
 class MockedLazyLoadedDocstring(LazyLoadedDocstring):
@@ -84,13 +83,3 @@ class TestWaiterDocstring(unittest.TestCase):
             docstring = WaiterDocstring()
             str(docstring)
             self.assertTrue(mock_writer.called)
-
-
-class TestPaginatorDocstring(unittest.TestCase):
-    def test_use_correct_docstring_writer(self):
-        with mock.patch(
-                'botocore.docs.docstring'
-                '.document_paginate_method') as mock_writer:
-            docstring = PaginatorDocstring()
-            str(docstring)
-            self.assertTrue(mock_writer.called)
diff --git a/tests/unit/docs/test_paginator.py b/tests/unit/docs/test_paginator.py
index 76e116c20..f2673e0fc 100644
--- a/tests/unit/docs/test_paginator.py
+++ b/tests/unit/docs/test_paginator.py
@@ -35,8 +35,8 @@ class TestPaginatorDocumenter(BaseDocsTest):
             'Paginators',
             '==========',
             'The available paginators are:',
-            '* :py:class:`MyService.Paginator.SampleOperation`',
-            '.. py:class:: MyService.Paginator.SampleOperation',
+            '* :py:class:`MyService.Paginator.sample_operation`',
+            '.. py:class:: MyService.Paginator.sample_operation',
             '  ::',
             '    paginator = client.get_paginator(\'sample_operation\')',
             '  .. py:method:: paginate(**kwargs)',
diff --git a/tests/unit/docs/test_service.py b/tests/unit/docs/test_service.py
index 51451f601..99891bee5 100644
--- a/tests/unit/docs/test_service.py
+++ b/tests/unit/docs/test_service.py
@@ -49,7 +49,7 @@ class TestServiceDocumenter(BaseDocsTest):
             '==========',
             'Paginators',
             '==========',
-            '.. py:class:: MyService.Paginator.SampleOperation',
+            '.. py:class:: MyService.Paginator.sample_operation',
             '  .. py:method:: paginate(**kwargs)',
             '=======',
             'Waiters',
diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py
index 33c97790f..38b42bcfa 100644
--- a/tests/unit/test_client.py
+++ b/tests/unit/test_client.py
@@ -723,88 +723,6 @@ class TestAutoGeneratedClient(unittest.TestCase):
         # but we can at least make sure it looks like a paginator.
         self.assertTrue(hasattr(paginator, 'paginate'))
 
-    def test_paginator_class_name_from_client(self):
-        pagination_config = {
-            'pagination': {
-                'TestOperation': {
-                    "input_token": "Marker",
-                    "output_token": "Marker",
-                    "more_results": "IsTruncated",
-                    "limit_key": "MaxItems",
-                    "result_key": "Users"
-                }
-            }
-        }
-        self.loader.load_service_model.side_effect = [
-            self.service_description,
-            pagination_config
-        ]
-        creator = self.create_client_creator()
-        service_client = creator.create_client('myservice', 'us-west-2')
-        paginator = service_client.get_paginator('test_operation')
-        self.assertEqual(
-            paginator.__class__.__name__,
-            'MyService.Paginator.TestOperation')
-
-    def test_paginator_help_from_client(self):
-        pagination_config = {
-            'pagination': {
-                'TestOperation': {
-                    "input_token": "Marker",
-                    "output_token": "Marker",
-                    "more_results": "IsTruncated",
-                    "limit_key": "MaxItems",
-                    "result_key": "Users"
-                }
-            }
-        }
-        self.loader.load_service_model.side_effect = [
-            self.service_description,
-            pagination_config
-        ]
-        creator = self.create_client_creator()
-        service_client = creator.create_client('myservice', 'us-west-2')
-        paginator = service_client.get_paginator('test_operation')
-        with mock.patch('sys.stdout', six.StringIO()) as mock_stdout:
-            help(paginator.paginate)
-        contents = mock_stdout.getvalue()
-        lines = [
-            ('    Creates an iterator that will paginate through responses '
-             'from :py:meth:`MyService.Client.test_operation`.'),
-            '    **Request Syntax** ',
-            '    ::',
-            '      response_iterator = paginator.paginate(',
-            "          Foo='string',",
-            "          Bar='string',",
-            '          PaginationConfig={',
-            "              'MaxItems': 123,",
-            "              'PageSize': 123,",
-            "              'StartingToken': 'string'",
-            '          }',
-            '      )',
-            '    :type Foo: string',
-            '    :param Foo: **[REQUIRED]** Documents Foo',
-            '    :type Bar: string',
-            '    :param Bar: Documents Bar',
-            '    :type PaginationConfig: dict',
-            '    :param PaginationConfig: ',
-            ('      A dictionary that provides parameters to control '
-             'pagination.'),
-            '      - **MaxItems** *(integer) --* ',
-            ('        The total number of items to return. If the total '
-             'number of items available is more than the value specified '
-             'in max-items then a ``NextToken`` will be provided in the '
-             'output that you can use to resume pagination.'),
-            '      - **PageSize** *(integer) --* ',
-            '        The size of each page.',
-            '      - **StartingToken** *(string) --* ',
-            ('        A token to specify where to start paginating. This is '
-             'the ``NextToken`` from a previous response.'),
-            '    :returns: None',
-        ]
-        for line in lines:
-            self.assertIn(line, contents)
-
     def test_can_set_credentials_in_client_init(self):
         creator = self.create_client_creator()
         credentials = Credentials(
diff --git a/tests/unit/test_loaders.py b/tests/unit/test_loaders.py
index 6ec766bdb..deda4dd3b 100644
--- a/tests/unit/test_loaders.py
+++ b/tests/unit/test_loaders.py
@@ -24,7 +24,7 @@ import contextlib
 
 import mock
 
-from botocore.exceptions import DataNotFoundError, ValidationError
+from botocore.exceptions import DataNotFoundError
 from botocore.loaders import JSONFileLoader
 from botocore.loaders import Loader, create_loader
 
@@ -129,25 +129,9 @@ class TestLoader(BaseEnvVar):
                         file_loader=FakeLoader(),
                         include_default_search_paths=False)
         loader.determine_latest_version = mock.Mock(return_value='2015-03-01')
-        loader.list_available_services = mock.Mock(return_value=['baz'])
         loaded = loader.load_service_model('baz', type_name='service-2')
         self.assertEqual(loaded, ['loaded data'])
 
-    @mock.patch('os.path.isdir', mock.Mock(return_value=True))
-    def test_load_service_model_enforces_case(self):
-        class FakeLoader(object):
-            def load_file(self, name):
-                return ['loaded data']
-
-        loader = Loader(extra_search_paths=['foo'],
-                        file_loader=FakeLoader(),
-                        include_default_search_paths=False)
-        loader.determine_latest_version = mock.Mock(return_value='2015-03-01')
-        loader.list_available_services = mock.Mock(return_value=['baz'])
-
-        with self.assertRaises(ValidationError):
-            loader.load_service_model('BAZ', type_name='service-2')
-
     def test_create_loader_parses_data_path(self):
         search_path = os.pathsep.join(['foo', 'bar', 'baz'])
         loader = create_loader(search_path)
diff --git a/tests/unit/test_session.py b/tests/unit/test_session.py
index 457892916..5bb58e18f 100644
--- a/tests/unit/test_session.py
+++ b/tests/unit/test_session.py
@@ -449,5 +449,65 @@ class TestCreateClient(BaseSessionTest):
         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()
diff --git a/tests/unit/test_waiters.py b/tests/unit/test_waiters.py
index 7dd29a525..26ed7f275 100644
--- a/tests/unit/test_waiters.py
+++ b/tests/unit/test_waiters.py
@@ -565,20 +565,16 @@ class ServiceWaiterFunctionalTest(BaseEnvVar):
 
     def get_waiter_model(self, service, api_version=None):
         """Get the waiter model for the service."""
-        with mock.patch('botocore.loaders.Loader.list_available_services',
-                        return_value=[service]):
-            return WaiterModel(self.loader.load_service_model(
-                service, type_name='waiters-2', api_version=api_version))
+        return WaiterModel(self.loader.load_service_model(
+            service, type_name='waiters-2', api_version=api_version))
 
     def get_service_model(self, service, api_version=None):
         """Get the service model for the service."""
-        with mock.patch('botocore.loaders.Loader.list_available_services',
-                        return_value=[service]):
-            return ServiceModel(
-                self.loader.load_service_model(
-                    service, type_name='service-2', api_version=api_version),
-                service_name=service
-            )
+        return ServiceModel(
+            self.loader.load_service_model(
+                service, type_name='service-2', api_version=api_version),
+            service_name=service
+        )
 
 
 class CloudFrontWaitersTest(ServiceWaiterFunctionalTest):