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_expires(self): backoff = Backoff() for attempt in range(backoff.max_attempts): now = datetime.utcnow() if attempt == 0: assert backoff.expires(attempt, now) < datetime.utcnow() continue expect_delta = timedelta(seconds=2**attempt) assert backoff.expires(attempt, now) > datetime.utcnow() assert backoff.expires(attempt, now - expect_delta) < datetime.utcnow() assert backoff.expires(attempt, now - (expect_delta * 2)) < datetime.utcnow() assert backoff.expires(attempt, now + expect_delta) > datetime.utcnow() assert backoff.expires(attempt, now + (expect_delta * 2)) > datetime.utcnow()
def test_expires_overflow(self): backoff = Backoff() assert backoff.expires(15, datetime.max) == datetime.max assert backoff.expires(16, datetime.max) == datetime.max