Exemplo n.º 1
0
 def test_exponential(self):
     r = Retrying(wait=tenacity.wait_exponential(max=100))
     self.assertEqual(r.wait(1, 0), 2)
     self.assertEqual(r.wait(2, 0), 4)
     self.assertEqual(r.wait(3, 0), 8)
     self.assertEqual(r.wait(4, 0), 16)
     self.assertEqual(r.wait(5, 0), 32)
     self.assertEqual(r.wait(6, 0), 64)
Exemplo n.º 2
0
 def test_exponential_with_max_wait_and_multiplier(self):
     r = Retrying(wait=tenacity.wait_exponential(max=50, multiplier=1))
     self.assertEqual(r.wait(1, 0), 2)
     self.assertEqual(r.wait(2, 0), 4)
     self.assertEqual(r.wait(3, 0), 8)
     self.assertEqual(r.wait(4, 0), 16)
     self.assertEqual(r.wait(5, 0), 32)
     self.assertEqual(r.wait(6, 0), 50)
     self.assertEqual(r.wait(7, 0), 50)
     self.assertEqual(r.wait(50, 0), 50)
Exemplo n.º 3
0
 def test_exponential_with_max_wait(self):
     r = Retrying(wait=tenacity.wait_exponential(max=40))
     self.assertEqual(r.wait(1, 0), 2)
     self.assertEqual(r.wait(2, 0), 4)
     self.assertEqual(r.wait(3, 0), 8)
     self.assertEqual(r.wait(4, 0), 16)
     self.assertEqual(r.wait(5, 0), 32)
     self.assertEqual(r.wait(6, 0), 40)
     self.assertEqual(r.wait(7, 0), 40)
     self.assertEqual(r.wait(50, 0), 40)
Exemplo n.º 4
0
    def test_random_sleep_without_min(self):
        r = Retrying(wait=tenacity.wait_random(max=2))
        times = set()
        times.add(r.wait(1, 6546))
        times.add(r.wait(1, 6546))
        times.add(r.wait(1, 6546))
        times.add(r.wait(1, 6546))

        # this is kind of non-deterministic...
        self.assertTrue(len(times) > 1)
        for t in times:
            self.assertTrue(t >= 0)
            self.assertTrue(t <= 2)
Exemplo n.º 5
0
 def test_wait_double_sum(self):
     r = Retrying(wait=tenacity.wait_random(0, 3) + tenacity.wait_fixed(5))
     # Test it a few time since it's random
     for i in six.moves.range(1000):
         w = r.wait(1, 5)
         self.assertLess(w, 8)
         self.assertGreaterEqual(w, 5)
Exemplo n.º 6
0
    def test_random_sleep(self):
        r = Retrying(wait=tenacity.wait_random(min=1, max=20))
        times = set()
        for x in six.moves.range(1000):
            times.add(r.wait(1, 6546))

        # this is kind of non-deterministic...
        self.assertTrue(len(times) > 1)
        for t in times:
            self.assertTrue(t >= 1)
            self.assertTrue(t < 20)
Exemplo n.º 7
0
 def test_wait_arbitrary_sum(self):
     r = Retrying(wait=sum([
         tenacity.wait_fixed(1),
         tenacity.wait_random(0, 3),
         tenacity.wait_fixed(5),
         tenacity.wait_none()
     ]))
     # Test it a few time since it's random
     for i in six.moves.range(1000):
         w = r.wait(1, 5)
         self.assertLess(w, 9)
         self.assertGreaterEqual(w, 6)
Exemplo n.º 8
0
    def test_wait_chain(self):
        r = Retrying(wait=tenacity.wait_chain(
            *[tenacity.wait_fixed(1) for i in six.moves.range(2)] +
            [tenacity.wait_fixed(4) for i in six.moves.range(2)] +
            [tenacity.wait_fixed(8) for i in six.moves.range(1)]))

        for i in six.moves.range(10):
            w = r.wait(i, 1)
            if i < 2:
                self._assert_range(w, 1, 2)
            elif i < 4:
                self._assert_range(w, 4, 5)
            else:
                self._assert_range(w, 8, 9)
Exemplo n.º 9
0
 def test_no_sleep(self):
     r = Retrying()
     self.assertEqual(0, r.wait(18, 9879))
Exemplo n.º 10
0
 def test_wait_func(self):
     r = Retrying(wait=lambda attempt, delay: attempt * delay)
     self.assertEqual(r.wait(1, 5), 5)
     self.assertEqual(r.wait(2, 11), 22)
     self.assertEqual(r.wait(10, 100), 1000)
Exemplo n.º 11
0
 def test_incrementing_sleep(self):
     r = Retrying(wait=tenacity.wait_incrementing(start=500, increment=100))
     self.assertEqual(500, r.wait(1, 6546))
     self.assertEqual(600, r.wait(2, 6546))
     self.assertEqual(700, r.wait(3, 6546))
Exemplo n.º 12
0
 def test_fixed_sleep(self):
     r = Retrying(wait=tenacity.wait_fixed(1))
     self.assertEqual(1, r.wait(12, 6546))