Repository: ansible/awx
File: awx/main/tests/unit/utils/test_redis.py (Python)

Code before:
        assert retry._retries == settings.REDIS_RETRY_COUNT == 3
        assert isinstance(backoff, ExponentialBackoff)
        assert backoff._base == 0.5
        assert backoff._cap == 1.0
        assert BusyLoadingError in retry_errors
        assert ConnectionError in retry_errors
        assert TimeoutError in retry_errors

        # Test async client has same config
        client_async = get_redis_client_async()
        retry_async = client_async.connection_pool.connection_kwargs['retry']
        backoff_async = retry_async._backoff
        retry_errors_async = client_async.connection_pool.connection_kwargs['retry_on_error']

        assert retry_async._retries == settings.REDIS_RETRY_COUNT
        assert backoff_async._base == 0.5
        assert backoff_async._cap == 1.0
        assert ConnectionError in retry_errors_async

    @override_settings(REDIS_RETRY_COUNT=5)
    def test_override_settings_applied_to_client(self):
        """Verify override_settings changes are applied to client object."""
        from importlib import reload
        import awx.main.utils.redis as redis_module

        reload(redis_module)
        from awx.main.utils.redis import get_redis_client as get_client_new

        client = get_client_new()
        retry = client.connection_pool.connection_kwargs['retry']

        # Assert provided value (5) matches value on object
        assert retry._retries == 5


Review comment (bug):
I don't think you should need this. `get_client_new` should call `_get_redis_pool_kwargs` and that should call `settings.REDIS_RETRY_COUNT` which is a `getattr` on the Django `LazySettings` object. If `override_settings` doesn't take effect in this case, then it failed at its job.

I want to remove the reload line and get all the imports from the top of the file. Something went off the rails in the debugging, and we need to rein that back in to sanity.

Code after:
        assert retry._retries == settings.REDIS_RETRY_COUNT == 3
        assert isinstance(backoff, ExponentialBackoff)
        assert backoff._base == settings.REDIS_BACKOFF_BASE == 0.5
        assert backoff._cap == settings.REDIS_BACKOFF_CAP == 1.0
        assert BusyLoadingError in retry_errors
        assert ConnectionError in retry_errors
        assert TimeoutError in retry_errors

        # Test async client has same config
        client_async = get_redis_client_async()
        retry_async = client_async.connection_pool.connection_kwargs['retry']
        backoff_async = retry_async._backoff
        retry_errors_async = client_async.connection_pool.connection_kwargs['retry_on_error']

        assert retry_async._retries == settings.REDIS_RETRY_COUNT
        assert backoff_async._base == settings.REDIS_BACKOFF_BASE
        assert backoff_async._cap == settings.REDIS_BACKOFF_CAP
        assert ConnectionError in retry_errors_async

    @override_settings(REDIS_RETRY_COUNT=5)
    def test_override_settings_applied_to_client(self):
        """Verify override_settings changes are applied to client object."""
        client = get_redis_client()
        retry = client.connection_pool.connection_kwargs['retry']

        assert retry._retries == 5

    @override_settings(REDIS_BACKOFF_CAP=2.0, REDIS_BACKOFF_BASE=1.0)
    def test_override_backoff_settings_applied_to_client(self):
        """Verify override_settings for backoff parameters are applied to client object."""
        client = get_redis_client()
        retry = client.connection_pool.connection_kwargs['retry']
        backoff = retry._backoff

        # Assert provided values match values on object
        assert backoff._cap == 2.0
        assert backoff._base == 1.0