Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
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()
Ejemplo n.º 4
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
Ejemplo n.º 5
0
 def test_expires_overflow(self):
     tracker = Tracker()
     tracker.attempts = 15
     tracker.last_attempt = datetime.max
     assert tracker.expires() == datetime.max