File: t1_retry.py

package info (click to toggle)
s3ql 5.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,416 kB
  • sloc: python: 19,027; cpp: 408; makefile: 147; sh: 133
file content (67 lines) | stat: -rwxr-xr-x 1,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
'''
t1_retry.py - this file is part of S3QL.

Copyright © 2008 Nikolaus Rath <Nikolaus@rath.org>

This work can be distributed under the terms of the GNU GPLv3.
'''

if __name__ == '__main__':
    import sys

    import pytest

    sys.exit(pytest.main([__file__] + sys.argv[1:]))

import logging

from pytest_checklogs import assert_logs

from s3ql.backends.common import retry


class TemporaryProblem(Exception):
    pass


class NthAttempt:
    def __init__(self, succeed_on=3):
        self.count = 0
        self.succeed_on = succeed_on

    @staticmethod
    def is_temp_failure(exc):
        return isinstance(exc, TemporaryProblem)

    @retry
    def do_stuff(self):
        if self.count == self.succeed_on:
            return True
        self.count += 1
        raise TemporaryProblem()

    @retry
    def test_is_retry(self, is_retry=False):
        assert is_retry == (self.count != 0)
        if self.count == self.succeed_on:
            return True
        self.count += 1
        raise TemporaryProblem()


def test_retry():
    inst = NthAttempt(3)

    assert inst.do_stuff()


def test_is_retry():
    inst = NthAttempt(3)
    assert inst.test_is_retry()


def test_logging():
    inst = NthAttempt(6)
    with assert_logs(r'^Encountered %s \(%s\), retrying ', count=2, level=logging.WARNING):
        inst.do_stuff()