Beispiel #1
0
def test_no_args(passes):
    """Use the decorator without passing any args or calling the decorator

    @retry
    def fn():
        #...
    """
    wrapped = retrace.retry(passes)
    assert wrapped() == 1
Beispiel #2
0
def test_no_args_instance(passes):
    """Use the decorator without passing any args or but call the decorator

    @retry()
    def fn():
        #...
    """
    wrapped = retrace.retry()(passes)
    assert wrapped() == 1
def test_validator_pass(passes):
    """Use the decorator without passing any args or calling the decorator

    @retry
    def fn():
        #...
    """
    wrapped = retrace.retry(validator=1)(passes)

    assert 1 == wrapped()
Beispiel #4
0
def test_limit_passed_first_time(passes):
    """Use the decorator and only retry on specific exceptions. The wrapped
    method raises a KeyboardInterrupt but we only want to retry on an Exception

    @retry()
    def fn():
        #...
    """
    wrapped = retrace.retry()(passes)
    assert wrapped() == 1
Beispiel #5
0
def test_limit_passed_first_time(passes):
    """Use the decorator and only retry on specific exceptions. The wrapped
    method raises a KeyboardInterrupt but we only want to retry on an Exception

    @retry()
    def fn():
        #...
    """
    wrapped = retrace.retry()(passes)
    assert wrapped() == 1
Beispiel #6
0
def test_limit_always_fails(fails):
    """Use the decorator and only retry on specific exceptions. The wrapped
    method raises a KeyboardInterrupt but we only want to retry on an Exception

    @retry()
    def fn():
        #...
    """
    wrapped = retrace.retry()(fails)
    with pytest.raises(retrace.LimitReached):
        wrapped(Exception)
Beispiel #7
0
def test_limit_always_fails(fails):
    """Use the decorator and only retry on specific exceptions. The wrapped
    method raises a KeyboardInterrupt but we only want to retry on an Exception

    @retry()
    def fn():
        #...
    """
    wrapped = retrace.retry()(fails)
    with pytest.raises(retrace.LimitReached):
        wrapped(Exception)
Beispiel #8
0
def test_raises(keyboard_interrupt):
    """Use the decorator and only retry on specific exceptions. The wrapped
    method raises a KeyboardInterrupt but we only want to retry on an Exception

    @retry()
    def fn():
        #...
    """
    wrapped = retrace.retry(on_exception=Exception)(keyboard_interrupt)
    with pytest.raises(KeyboardInterrupt):
        wrapped(KeyboardInterrupt())
def test_validator_limit_value(passes):
    """Use the decorator without passing any args or calling the decorator

    @retry
    def fn():
        #...
    """
    wrapped = retrace.retry(validator=2)(passes)

    with pytest.raises(retrace.LimitReached):
        wrapped()
Beispiel #10
0
def test_limit_fn(fails):

    start = time.time()
    count = [0]

    def limit_1_sec(attempt_number):
        """Create a limiter that allows as many calls as possible in 0.1s"""
        count[0] += 1
        if time.time() - start > 0.1:
            raise retrace.LimitReached()

    wrapped = retrace.retry(limit=limit_1_sec)(fails)

    with pytest.raises(retrace.LimitReached):
        wrapped()

    assert fails.call_count == count[0]
Beispiel #11
0
def test_limit_fn(fails):

    start = time.time()
    count = [0]

    def limit_1_sec(attempt_number):
        """Create a limiter that allows as many calls as possible in 0.1s"""
        count[0] += 1
        if time.time() - start > .1:
            raise retrace.LimitReached()

    wrapped = retrace.retry(limit=limit_1_sec)(fails)

    with pytest.raises(retrace.LimitReached):
        wrapped()

    assert fails.call_count == count[0]
Beispiel #12
0
def test_limit_class(fails):
    class LimitSeconds(object):
        def __init__(self, seconds):
            self.seconds = seconds
            self.count = 0
            self.start = None

        def __call__(self, attempt_number):
            """Create a limiter that allows as many calls as possible in 0.1s"""

            if self.start is None:
                self.start = time.time()

            self.count += 1
            if time.time() - self.start > self.seconds:
                raise retrace.LimitReached()

    limiter = LimitSeconds(0.1)
    wrapped = retrace.retry(limit=limiter)(fails)

    with pytest.raises(retrace.LimitReached):
        wrapped()

    assert fails.call_count == limiter.count
Beispiel #13
0
def test_limit_class(fails):
    class LimitSeconds(object):
        def __init__(self, seconds):
            self.seconds = seconds
            self.count = 0
            self.start = None

        def __call__(self, attempt_number):
            """Create a limiter that allows as many calls as possible in 0.1s"""

            if self.start is None:
                self.start = time.time()

            self.count += 1
            if time.time() - self.start > self.seconds:
                raise retrace.LimitReached()

    limiter = LimitSeconds(0.1)
    wrapped = retrace.retry(limit=limiter)(fails)

    with pytest.raises(retrace.LimitReached):
        wrapped()

    assert fails.call_count == limiter.count
Beispiel #14
0
def test_fails_4_times_and_hits_limit(fail_then_pass):
    mock, fail_then_pass = fail_then_pass
    wrapped = retrace.retry(limit=4)(fail_then_pass)
    with pytest.raises(retrace.LimitReached):
        wrapped()
    assert mock.call_count == 4
Beispiel #15
0
def test_fails_then_pass(fail_then_pass):
    mock, fail_then_pass = fail_then_pass
    wrapped = retrace.retry()(fail_then_pass)
    assert wrapped() == "PASS"
    assert mock.call_count == 5
Beispiel #16
0
def test_limit_reached_float(fails):
    with pytest.raises(retrace.LimitReached):
        wrapped = retrace.retry(interval=.1)(fails)
        wrapped(Exception)
Beispiel #17
0
def test_fails_4_times_and_hits_limit(fail_then_pass):
    mock, fail_then_pass = fail_then_pass
    wrapped = retrace.retry(limit=4)(fail_then_pass)
    with pytest.raises(retrace.LimitReached):
        wrapped()
    assert mock.call_count == 4
Beispiel #18
0
def test_fails_then_pass(fail_then_pass):
    mock, fail_then_pass = fail_then_pass
    wrapped = retrace.retry()(fail_then_pass)
    assert wrapped() == "PASS"
    assert mock.call_count == 5
Beispiel #19
0
def test_limit_passed(passes):
    wrapped = retrace.retry(interval=0.1)(passes)
    assert wrapped() == 1
Beispiel #20
0
def test_limit_passed(passes):
    wrapped = retrace.retry(interval=.1)(passes)
    assert wrapped() == 1
Beispiel #21
0
def test_limit_reached_float(fails):
    with pytest.raises(conftest.CustomException):
        wrapped = retrace.retry(interval=0.001)(fails)
        wrapped(Exception)