def test_init_backoff(self): b = Backoff() tracker = Tracker(b) tracker2 = Tracker() assert tracker.backoff == b assert tracker2.backoff != b assert tracker.attempts == 0 assert tracker.last_attempt is None
def test_remaining(self): tracker = Tracker() for attempt in range(tracker.backoff.max_attempts): now = datetime.utcnow() if attempt == 0: assert tracker.remaining() == timedelta() continue tracker.attempt() expect_delta = timedelta(seconds=2**attempt) assert tracker.remaining() > timedelta() assert tracker.remaining() < expect_delta tracker.last_attempt = now - expect_delta assert tracker.remaining() == timedelta() tracker.last_attempt = now + expect_delta assert tracker.remaining() > expect_delta
def test_delta(self): tracker = Tracker() for attempt in range(tracker.backoff.max_attempts): if attempt == 0: assert tracker.delta() == timedelta() continue tracker.attempt() assert tracker.delta() == tracker.backoff.deltas[attempt] assert tracker.delta() == timedelta(seconds=2**attempt) # Should cap at max_attempts to highest delta tracker.attempts = tracker.backoff.max_attempts + 20 assert tracker.delta() == timedelta( seconds=2**tracker.backoff.max_attempts)
def test_elapsed(self): tracker = Tracker() assert tracker.elapsed() == Backoff.zero tracker.attempt() sleep_delta = timedelta(0, 0, 0, 1) sleep((sleep_delta * 2).total_seconds()) assert tracker.elapsed() > sleep_delta
def test_reset(self): now = datetime.utcnow() tracker = Tracker() tracker.attempt() assert tracker.attempts == 1 assert tracker.last_attempt > now tracker.reset() assert tracker.attempts == 0 assert tracker.last_attempt is None
def test_wait(self): tracker = Tracker() sleep_delta = timedelta(0, 0, 0, 5) for attempt in range(tracker.backoff.max_attempts): now = datetime.utcnow() if attempt == 0: # Wait with 0 attempts should always be instant, 500 microsec buffer # here for when that carbon black is going hard at 100% tracker.wait() assert datetime.utcnow() < (now + timedelta(0, 0, 500)) continue # Every attempt we expect a (5 millisec) sleep when calling wait tracker.attempt() delta = timedelta(seconds=2**attempt) tracker.last_attempt = (now - delta) + sleep_delta expect = now - delta tracker.wait() assert datetime.utcnow() > expect
def test_expired(self): tracker = Tracker() for attempt in range(tracker.backoff.max_attempts): if attempt == 0: assert tracker.expired() == True continue tracker.attempt() expect_delta = timedelta(seconds=2**attempt) tracker.last_attempt = datetime.utcnow() assert tracker.expired() == False tracker.last_attempt = datetime.utcnow() - (expect_delta / 2) assert tracker.expired() == False tracker.last_attempt = datetime.utcnow() - expect_delta assert tracker.expired() == True tracker.last_attempt = datetime.utcnow() - (expect_delta * 2) assert tracker.expired() == True
def test_expires_overflow(self): tracker = Tracker() tracker.attempts = 15 tracker.last_attempt = datetime.max assert tracker.expires() == datetime.max
def test_expires(self): tracker = Tracker() for attempt in range(tracker.backoff.max_attempts): now = datetime.utcnow() if attempt == 0: assert tracker.expires() < datetime.utcnow() continue tracker.attempt() expect_delta = timedelta(seconds=2**attempt) assert tracker.expires() > datetime.utcnow() tracker.last_attempt = now - expect_delta assert tracker.expires() < datetime.utcnow() tracker.last_attempt = now - (expect_delta * 2) assert tracker.expires() < datetime.utcnow() tracker.last_attempt = now + expect_delta assert tracker.expires() > datetime.utcnow() tracker.last_attempt = now + (expect_delta * 2) assert tracker.expires() > datetime.utcnow()
def test_str(self): assert str(Tracker()) == 'Tracker(attempts=0, remaining=0:00:00)'
def test_init(self): tracker = Tracker() assert tracker.attempts == 0 assert tracker.last_attempt is None