def test_all_attempts_edge(self): backoff = Backoff() assert backoff.max_attempts == Backoff.max_attempts assert len(backoff.deltas) == 16 for edge in [-1, 0, 15, 16, 17]: assert backoff.delta(edge) >= backoff.zero assert backoff.remaining(edge, datetime.utcnow()) >= backoff.zero if edge > 0: assert backoff.expires(edge, datetime.utcnow()) >= datetime.utcnow() assert backoff.expired(edge, datetime.utcnow()) == False else: assert backoff.expires(edge, datetime.utcnow()) <= datetime.utcnow() assert backoff.expired(edge, datetime.utcnow())
def test_expired(self): backoff = Backoff() for attempt in range(backoff.max_attempts): now = datetime.utcnow() if attempt == 0: assert backoff.expired(0, now) == True continue expect_delta = timedelta(seconds=2**attempt) assert backoff.expired(attempt, datetime.utcnow()) == False assert backoff.expired(attempt, datetime.utcnow() - (expect_delta / 2)) == False assert backoff.expired(attempt, datetime.utcnow() - expect_delta) == True assert backoff.expired(attempt, datetime.utcnow() - (expect_delta * 2)) == True