Ejemplo n.º 1
0
 def test_delta(self):
     backoff = Backoff()
     for attempt in range(backoff.max_attempts):
         if attempt == 0:
             assert backoff.delta(attempt) == timedelta()
             continue
         assert backoff.delta(attempt) == backoff.deltas[attempt]
         assert backoff.delta(attempt) == timedelta(seconds=2**attempt)
     # Should cap at max_attempts to highest delta
     assert backoff.delta(backoff.max_attempts +
                          20) == timedelta(seconds=2**backoff.max_attempts)
Ejemplo n.º 2
0
 def test_init_deltas(self):
     count = 30
     backoff = Backoff(count, [
         timedelta(seconds=2**attempt if attempt > 0 else 0)
         for attempt in range(count + 1)
     ])
     for attempt in range(count):
         if attempt == 0:
             assert backoff.delta(attempt) == timedelta()
             continue
         assert backoff.delta(attempt) == backoff.deltas[attempt]
         assert backoff.delta(attempt) == timedelta(seconds=2**attempt)
Ejemplo n.º 3
0
 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())