# Path: test/mock_connection.py
# class MockResponse:
#     def __init__(self, status_code, json=None):
#         self.ok = status_code < 400
#         self.status_code = status_code
#         self._json = json
# 
#     def json(self):
#         return self._json
# 
# Path: qarnot/_retry.py
# def with_retry(http_request_func):
#     def _with_retry(self, *args, **kwargs):
#         retry = self._retry_count
#         last_chance = False
# 
#         while True:
#             try:
#                 ret = http_request_func(self, *args, **kwargs)
#             except ConnectionError:
#                 if last_chance:
#                     raise
# 
#             if ret.ok:
#                 return ret
#             if ret.status_code == 401:
#                 raise UnauthorizedException()
#             # Do not retry on error except for rate limiting errors.
#             if 400 <= ret.status_code <= 499 and ret.status_code != 429:
#                 return ret
#             if last_chance:
#                 return ret
# 
#             if retry > 1:
#                 retry -= 1
#                 time.sleep(self._retry_wait * (self._retry_count - retry))
#             else:
#                 last_chance = True
# 
#     return _with_retry


from .mock_connection import MockResponse
from qarnot._retry import with_retry
    def __init__(self, responses):
        self._retry_count = 2
        self._retry_wait = 0

        self.calls = 0
        self._responses = responses

    @with_retry
    def get(self, *args, **kwargs):
        self.calls += 1

        if len(self._responses) > 0:
            resp = self._responses[0]
            self._responses = self._responses[1:]
            return resp

        return MockResponse(200)


class TestRetry:
    def test_failing_all_the_way(self):
        conn = MockConnection(
            [MockResponse(429)] * 4
        )
        resp = conn.get()
        assert resp.status_code == 429
        assert conn.calls == 3

    def test_eventually_succeeding(self):
        conn = MockConnection(

# Path: test/mock_connection.py
# class MockResponse:
#     def __init__(self, status_code, json=None):
#         self.ok = status_code < 400
#         self.status_code = status_code
#         self._json = json
# 
#     def json(self):
#         return self._json
# 
# Path: qarnot/_retry.py
# def with_retry(http_request_func):
#     def _with_retry(self, *args, **kwargs):
#         retry = self._retry_count
#         last_chance = False
# 
#         while True:
#             try:
#                 ret = http_request_func(self, *args, **kwargs)
#             except ConnectionError:
#                 if last_chance:
#                     raise
# 
#             if ret.ok:
#                 return ret
#             if ret.status_code == 401:
#                 raise UnauthorizedException()
#             # Do not retry on error except for rate limiting errors.
#             if 400 <= ret.status_code <= 499 and ret.status_code != 429:
#                 return ret
#             if last_chance:
#                 return ret
# 
#             if retry > 1:
#                 retry -= 1
#                 time.sleep(self._retry_wait * (self._retry_count - retry))
#             else:
#                 last_chance = True
# 
#     return _with_retry


# Path: test/test_retry.py
from .mock_connection import MockResponse
from qarnot._retry import with_retry

    def __init__(self, responses):
        self._retry_count = 2
        self._retry_wait = 0

        self.calls = 0
        self._responses = responses

    @with_retry
    def get(self, *args, **kwargs):
        self.calls += 1

        if len(self._responses) > 0:
            resp = self._responses[0]
            self._responses = self._responses[1:]
            return resp

        return MockResponse(200)


class TestRetry:
    def test_failing_all_the_way(self):
        conn = MockConnection(
            [MockResponse(429)] * 4
        )
        resp = conn.get()
        assert resp.status_code == 429
        assert conn.calls == 3

    def test_eventually_succeeding(self):
        conn = MockConnection(

                [MockResponse(429)] * 2 + [MockResponse(200, json={"message": "hello"})]