예제 #1
0
 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
예제 #2
0
 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
예제 #3
0
 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)
예제 #4
0
    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
예제 #5
0
    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
예제 #6
0
    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
예제 #7
0
    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()