コード例 #1
0
ファイル: test_retry.py プロジェクト: afcarl/Henson
def test_callback_prevents_others(test_app, coroutine):
    """Test that the callback blocks other callbacks."""
    test_app.settings['RETRY_CALLBACK'] = coroutine
    retry.Retry(test_app)

    with pytest.raises(Abort):
        for cb in test_app._callbacks['error']:
            yield from cb(test_app, {}, retry.RetryableException())
コード例 #2
0
def test_callback_insertion(test_app, coroutine):
    """Test that the callback is properly registered."""
    # Add an error callback before registering Retry.
    @test_app.error
    async def original_callback(*args):
        pass

    # Register Retry.
    test_app.settings['RETRY_CALLBACK'] = coroutine
    retry.Retry(test_app)

    assert test_app._callbacks['error'][0] is retry._retry
コード例 #3
0
async def test_delay(monkeypatch, test_app, coroutine):
    """Test that retry delays."""
    sleep_called = False

    test_app.settings['RETRY_CALLBACK'] = coroutine
    test_app.settings['RETRY_DELAY'] = 1
    retry.Retry(test_app)

    async def sleep(duration):
        nonlocal sleep_called
        sleep_called = True

    monkeypatch.setattr(asyncio, 'sleep', sleep)

    with suppress(Abort):
        await retry._retry(test_app, {}, retry.RetryableException())

    assert sleep_called
コード例 #4
0
async def test_get_settings(test_app: Application, coroutine: Coroutine,
                            exc: Exception, excs, expected: bool):
    """Test that _get_settings is grabbing the proper settings working."""
    test_app.settings['RETRY_CALLBACK'] = coroutine
    test_app.settings['RETRY_BACKOFF'] = 1.1
    test_app.settings['RETRY_EXCEPTIONS'] = excs
    test_app.settings['RETRY_OVERRIDES'] = {
        OSError: {
            'RETRY_BACKOFF': 1.05,
        },
        TestRetryChildAException: {
            'RETRY_BACKOFF': 2.05,
        },
        TestRetryGrandchildException: {},
        TestRetryChildBException: {
            'RETRY_BACKOFF': 3.05,
        },
    }
    retry.Retry(test_app)

    assert retry._get_settings(test_app, exc)['RETRY_BACKOFF'] == expected
コード例 #5
0
async def test_merge_override_settings(test_app, coroutine, exception, backoff,
                                       callback, delay, threshold, timeout,
                                       request):
    """Test that overrides are working."""
    test_app.settings['RETRY_CALLBACK'] = coroutine
    test_app.settings['RETRY_EXCEPTIONS'] = (IOError, FileNotFoundError, Abort)
    test_app.settings['RETRY_OVERRIDES'] = {
        IOError: {
            'RETRY_BACKOFF': 1.05,
        },
        FileNotFoundError: {
            'RETRY_TIMEOUT': 21600,  # 6 hours
        },
        Abort: {
            'RETRY_THRESHOLD': 2,
        },
        retry.RetryableException: {},
    }
    retry.Retry(test_app)

    # Check to see if the callback fixture exists, else, default to None
    try:
        callback_arg = request.getfixturevalue(callback)
    except pytest.FixtureLookupError:
        callback_arg = None

    assert test_app.settings['RETRY_OVERRIDES'][exception].get(
        'RETRY_BACKOFF', None) == backoff
    assert test_app.settings['RETRY_OVERRIDES'][exception].get(
        'RETRY_CALLBACK', None) == callback_arg
    assert test_app.settings['RETRY_OVERRIDES'][exception].get(
        'RETRY_DELAY', None) == delay
    assert test_app.settings['RETRY_OVERRIDES'][exception].get(
        'RETRY_THRESHOLD', None) == threshold
    assert test_app.settings['RETRY_OVERRIDES'][exception].get(
        'RETRY_TIMEOUT', None) == timeout