def test_never_recovers(self): with self.assertRaises(TestException2): retry_with_backoff(self._throws_exception2, exc_list=(TestException1, TestException2), attempts=5, min_delay=0, max_delay=.1, args=["a", "b"], kwargs={"x": "X", "y": "Y"}) self.assertEqual(self.counter, 5)
def test_never_recovers(self): with self.assertRaises(TestException2): retry_with_backoff(self._throws_exception2, exc_list=(TestException1, TestException2), attempts=5, min_delay=0, max_delay=.1, args=['a', 'b'], kwargs={'x': 'X', 'y': 'Y'}) self.assertEqual(self.counter, 5)
def test_retry_exception(self): r = retry_with_backoff(self._works_second_attempt, attempts=5, min_delay=0, max_delay=.1, args=["a", "b"], kwargs={"x": "X", "y": "Y"}) self.assertEqual(r, ["a", "b", "X", "Y"]) self.assertEqual(self.counter, 2)
def test_function_works_no_retry(self): r = retry_with_backoff(self._works_immediately, attempts=2, min_delay=0, max_delay=.1, args=["a", "b"], kwargs={"x": "X", "y": "Y"}) self.assertEqual(r, ["a", "b", "X", "Y"]) self.assertEqual(self.counter, 1)
def test_retry_checker(self): def _throws_handled_exception(a, b, x=None, y=None): self.counter += 1 if self.counter == 2: return [a, b, x, y] raise TestException2("Broke.") def _throws_unhandled_exception(a, b, x=None, y=None): self.counter += 1 if self.counter == 2: return [a, b, x, y] raise TestException2("Invalid") def _check_for_broke_message(e): if "Broke." in e.message: return True return False r = retry_with_backoff(_throws_handled_exception, exc_list=(TestException2), retry_checker=_check_for_broke_message, attempts=5, min_delay=0, max_delay=.1, args=["a", "b"], kwargs={ "x": "X", "y": "Y" }) self.assertEqual(self.counter, 2) self.assertEqual(r, ["a", "b", "X", "Y"]) self.counter = 0 with self.assertRaises(TestException2): retry_with_backoff(_throws_unhandled_exception, exc_list=(TestException2), retry_checker=_check_for_broke_message, attempts=5, min_delay=0, max_delay=.1, args=["a", "b"], kwargs={ "x": "X", "y": "Y" }) self.assertEqual(self.counter, 1)
def test_retry_exception(self): r = retry_with_backoff(self._works_second_attempt, attempts=5, min_delay=0, max_delay=.1, args=['a', 'b'], kwargs={'x': 'X', 'y': 'Y'}) self.assertEqual(r, ['a', 'b', 'X', 'Y']) self.assertEqual(self.counter, 2)
def test_function_works_no_retry(self): r = retry_with_backoff(self._works_immediately, attempts=2, min_delay=0, max_delay=.1, args=['a', 'b'], kwargs={'x': 'X', 'y': 'Y'}) self.assertEqual(r, ['a', 'b', 'X', 'Y']) self.assertEqual(self.counter, 1)
def test_multiple_exceptions(self): r = retry_with_backoff(self._second_raises_exception2, exc_list=(TestException1, TestException2), attempts=5, min_delay=0, max_delay=.1, args=["a", "b"], kwargs={"x": "X", "y": "Y"}) self.assertEqual(r, ["a", "b", "X", "Y"]) self.assertEqual(self.counter, 2)
def test_multiple_exceptions(self): r = retry_with_backoff(self._second_raises_exception2, exc_list=(TestException1, TestException2), attempts=5, min_delay=0, max_delay=.1, args=['a', 'b'], kwargs={'x': 'X', 'y': 'Y'}) self.assertEqual(r, ['a', 'b', 'X', 'Y']) self.assertEqual(self.counter, 2)
def test_retry_checker(self): def _throws_handled_exception(a, b, x=None, y=None): self.counter += 1 if self.counter == 2: return [a, b, x, y] raise TestException2("Broke.") def _throws_unhandled_exception(a, b, x=None, y=None): self.counter += 1 if self.counter == 2: return [a, b, x, y] raise TestException2("Invalid") def _check_for_broke_message(e): if "Broke." in e.message: return True return False r = retry_with_backoff(_throws_handled_exception, exc_list=(TestException2), retry_checker=_check_for_broke_message, attempts=5, min_delay=0, max_delay=.1, args=["a", "b"], kwargs={"x": "X", "y": "Y"}) self.assertEqual(self.counter, 2) self.assertEqual(r, ["a", "b", "X", "Y"]) self.counter = 0 with self.assertRaises(TestException2): retry_with_backoff(_throws_unhandled_exception, exc_list=(TestException2), retry_checker=_check_for_broke_message, attempts=5, min_delay=0, max_delay=.1, args=["a", "b"], kwargs={"x": "X", "y": "Y"}) self.assertEqual(self.counter, 1)