Python tenacity.wait_fixed() Examples

The following are 30 code examples of tenacity.wait_fixed(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tenacity , or try the search function .
Example #1
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_before_attempts(self):
        TestBeforeAfterAttempts._attempt_number = 0

        def _before(retry_state):
            TestBeforeAfterAttempts._attempt_number = \
                retry_state.attempt_number

        @retry(wait=tenacity.wait_fixed(1),
               stop=tenacity.stop_after_attempt(1),
               before=_before)
        def _test_before():
            pass

        _test_before()

        self.assertTrue(TestBeforeAfterAttempts._attempt_number == 1) 
Example #2
Source File: test_etcd3.py    From python-etcd3 with Apache License 2.0 6 votes vote down vote up
def etcd(self):
        endpoint = os.environ.get('PYTHON_ETCD_HTTP_URL')
        timeout = 5
        if endpoint:
            url = urlparse(endpoint)
            with etcd3.client(host=url.hostname,
                              port=url.port,
                              timeout=timeout) as client:
                yield client
        else:
            with etcd3.client() as client:
                yield client

        @retry(wait=wait_fixed(2), stop=stop_after_attempt(3))
        def delete_keys_definitely():
            # clean up after fixture goes out of scope
            etcdctl('del', '--prefix', '/')
            out = etcdctl('get', '--prefix', '/')
            assert 'kvs' not in out

        delete_keys_definitely() 
Example #3
Source File: __init__.py    From aodh with Apache License 2.0 6 votes vote down vote up
def get_connection_from_config(conf):
    retries = conf.database.max_retries
    url = conf.database.connection
    connection_scheme = urlparse.urlparse(url).scheme
    LOG.debug('looking for %(name)r driver in %(namespace)r',
              {'name': connection_scheme, 'namespace': _NAMESPACE})
    mgr = driver.DriverManager(_NAMESPACE, connection_scheme)

    @tenacity.retry(
        wait=tenacity.wait_fixed(conf.database.retry_interval),
        stop=tenacity.stop_after_attempt(retries if retries >= 0 else 5),
        reraise=True)
    def _get_connection():
        """Return an open connection to the database."""
        return mgr.driver(conf, url)

    return _get_connection() 
Example #4
Source File: shell.py    From vaultlocker with Apache License 2.0 6 votes vote down vote up
def _do_it_with_persistence(func, args, config):
    """Exec func with retries based on provided cli flags

    :param: func: function to attempt to execute
    :param: args: argparser generated cli arguments
    :param: config: configparser object of vaultlocker config
    """
    @tenacity.retry(
        wait=tenacity.wait_fixed(1),
        reraise=True,
        stop=(
            tenacity.stop_after_delay(args.retry) if args.retry > 0
            else tenacity.stop_after_attempt(1)
            ),
        retry=(
            tenacity.retry_if_exception(hvac.exceptions.VaultNotInitialized) |
            tenacity.retry_if_exception(hvac.exceptions.VaultDown)
            )
        )
    def _do_it():
        client = _vault_client(config)
        func(args, client, config)
    _do_it() 
Example #5
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_reraise_timeout_from_retry_error(self):
        calls = []

        @retry(wait=tenacity.wait_fixed(0.1),
               stop=tenacity.stop_after_attempt(2),
               retry=lambda retry_state: True)
        def _mock_fn():
            calls.append('x')

        def _reraised_mock_fn():
            try:
                _mock_fn()
            except tenacity.RetryError as retry_err:
                retry_err.reraise()

        self.assertRaises(tenacity.RetryError, _reraised_mock_fn)
        self.assertEqual(2, len(calls)) 
Example #6
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_reraise_from_retry_error(self):
        calls = []

        @retry(wait=tenacity.wait_fixed(0.1),
               stop=tenacity.stop_after_attempt(2))
        def _raise_key_error():
            calls.append('x')
            raise KeyError("Bad key")

        def _reraised_key_error():
            try:
                _raise_key_error()
            except tenacity.RetryError as retry_err:
                retry_err.reraise()

        self.assertRaises(KeyError, _reraised_key_error)
        self.assertEqual(2, len(calls)) 
Example #7
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_before_sleep_log_returns(self, exc_info=False):
        thing = NoneReturnUntilAfterCount(2)
        logger = logging.getLogger(self.id())
        logger.propagate = False
        logger.setLevel(logging.INFO)
        handler = CapturingHandler()
        logger.addHandler(handler)
        try:
            _before_sleep = tenacity.before_sleep_log(logger,
                                                      logging.INFO,
                                                      exc_info=exc_info)
            _retry = tenacity.retry_if_result(lambda result: result is None)
            retrying = Retrying(wait=tenacity.wait_fixed(0.01),
                                stop=tenacity.stop_after_attempt(3),
                                retry=_retry, before_sleep=_before_sleep)
            retrying.call(thing.go)
        finally:
            logger.removeHandler(handler)

        etalon_re = r'^Retrying .* in 0\.01 seconds as it returned None\.$'
        self.assertEqual(len(handler.records), 2)
        fmt = logging.Formatter().format
        self.assertRegexpMatches(fmt(handler.records[0]), etalon_re)
        self.assertRegexpMatches(fmt(handler.records[1]), etalon_re) 
Example #8
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_before_sleep_backward_compat(self):
        def _before_sleep(retry_obj, sleep, last_result):
            self.assertGreater(sleep, 0)
            _before_sleep.attempt_number = \
                retry_obj.statistics['attempt_number']
        _before_sleep.attempt_number = 0

        @retry(wait=tenacity.wait_fixed(0.01),
               stop=tenacity.stop_after_attempt(3),
               before_sleep=_before_sleep)
        def _test_before_sleep():
            if _before_sleep.attempt_number < 2:
                raise Exception("testing before_sleep_attempts handler")

        with reports_deprecation_warning():
            _test_before_sleep()
        self.assertEqual(_before_sleep.attempt_number, 2) 
Example #9
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_after_attempts(self):
        TestBeforeAfterAttempts._attempt_number = 0

        def _after(retry_state):
            TestBeforeAfterAttempts._attempt_number = \
                retry_state.attempt_number

        @retry(wait=tenacity.wait_fixed(0.1),
               stop=tenacity.stop_after_attempt(3),
               after=_after)
        def _test_after():
            if TestBeforeAfterAttempts._attempt_number < 2:
                raise Exception("testing after_attempts handler")
            else:
                pass

        _test_after()

        self.assertTrue(TestBeforeAfterAttempts._attempt_number == 2) 
Example #10
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_wait_chain_multiple_invocations(self):
        sleep_intervals = []
        r = Retrying(
            sleep=sleep_intervals.append,
            wait=tenacity.wait_chain(*[
                tenacity.wait_fixed(i + 1) for i in six.moves.range(3)
            ]),
            stop=tenacity.stop_after_attempt(5),
            retry=tenacity.retry_if_result(lambda x: x == 1),
        )

        @r.wraps
        def always_return_1():
            return 1

        self.assertRaises(tenacity.RetryError, always_return_1)
        self.assertEqual(sleep_intervals, [1.0, 2.0, 3.0, 3.0])
        sleep_intervals[:] = []

        # Clear and restart retrying.
        self.assertRaises(tenacity.RetryError, always_return_1)
        self.assertEqual(sleep_intervals, [1.0, 2.0, 3.0, 3.0])
        sleep_intervals[:] = [] 
Example #11
Source File: shippable_api.py    From ansibullbot with GNU General Public License v3.0 6 votes vote down vote up
def _fetch(self, url, verb='get', **kwargs):
        """return response or None in case of failure, try twice"""
        @retry(stop=stop_after_attempt(2), wait=wait_fixed(2))
        def _inner_fetch(verb='get'):
            headers = {
                'Authorization': 'apiToken %s' % C.DEFAULT_SHIPPABLE_TOKEN
            }

            logging.info(u'%s %s' % (verb, url))
            http_method = getattr(requests, verb)
            resp = http_method(url, headers=headers, **kwargs)
            logging.info(u'shippable status code: %s' % resp.status_code)
            logging.info(u'shippable reason: %s' % resp.reason)

            if resp.status_code not in [200, 302, 400]:
                logging.error(u'RC: %s', resp.status_code)
                raise TryAgain

            return resp

        try:
            logging.debug(u'%s' % url)
            return _inner_fetch(verb=verb)
        except RetryError as e:
            logging.error(e) 
Example #12
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_retry_child_class_with_override_backward_compat(self):

        class MyStop(tenacity.stop_after_attempt):
            def __init__(self):
                super(MyStop, self).__init__(1)

            def __call__(self, attempt_number, seconds_since_start):
                return super(MyStop, self).__call__(
                    attempt_number, seconds_since_start)
        retrying = Retrying(wait=tenacity.wait_fixed(0.01),
                            stop=MyStop())

        def failing():
            raise NotImplementedError()
        with pytest.raises(RetryError):
            retrying.call(failing) 
Example #13
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_before_sleep_log_raises_with_exc_info(self):
        thing = NoIOErrorAfterCount(2)
        logger = logging.getLogger(self.id())
        logger.propagate = False
        logger.setLevel(logging.INFO)
        handler = CapturingHandler()
        logger.addHandler(handler)
        try:
            _before_sleep = tenacity.before_sleep_log(logger,
                                                      logging.INFO,
                                                      exc_info=True)
            retrying = Retrying(wait=tenacity.wait_fixed(0.01),
                                stop=tenacity.stop_after_attempt(3),
                                before_sleep=_before_sleep)
            retrying.call(thing.go)
        finally:
            logger.removeHandler(handler)

        etalon_re = re.compile(r"^Retrying .* in 0\.01 seconds as it raised "
                               r"(IO|OS)Error: Hi there, I'm an IOError\.{0}"
                               r"Traceback \(most recent call last\):{0}"
                               r".*$".format(os.linesep),
                               flags=re.MULTILINE)
        self.assertEqual(len(handler.records), 2)
        fmt = logging.Formatter().format
        self.assertRegexpMatches(fmt(handler.records[0]), etalon_re)
        self.assertRegexpMatches(fmt(handler.records[1]), etalon_re) 
Example #14
Source File: test_functions.py    From qinling with Apache License 2.0 5 votes vote down vote up
def test_detach(self):
        """Admin only operation."""
        function_id = self.create_function(self.python_zip_file)
        resp, _ = self.client.create_execution(
            function_id, input='{"name": "Qinling"}'
        )
        self.assertEqual(201, resp.status)

        resp, body = self.admin_client.get_function_workers(function_id)
        self.assertEqual(200, resp.status)
        self.assertEqual(1, len(body['workers']))

        # Detach function
        resp, _ = self.admin_client.detach_function(function_id)
        self.assertEqual(202, resp.status)

        def _assert_workers():
            resp, body = self.admin_client.get_function_workers(function_id)
            self.assertEqual(200, resp.status)
            self.assertEqual(0, len(body['workers']))

        r = tenacity.Retrying(
            wait=tenacity.wait_fixed(1),
            stop=tenacity.stop_after_attempt(5),
            retry=tenacity.retry_if_exception_type(AssertionError)
        )
        r.call(_assert_workers) 
Example #15
Source File: test_function_versions.py    From qinling with Apache License 2.0 5 votes vote down vote up
def test_detach(self):
        """Admin only operation."""
        function_id = self.create_function()
        version = self.create_function_version(function_id)

        # Create execution to allocate worker
        resp, _ = self.client.create_execution(
            function_id, input='{"name": "Qinling"}', version=version
        )
        self.assertEqual(201, resp.status)

        resp, body = self.admin_client.get_function_workers(function_id,
                                                            version=version)
        self.assertEqual(200, resp.status)
        self.assertEqual(1, len(body['workers']))

        # Detach function version from workers
        resp, _ = self.admin_client.detach_function(function_id,
                                                    version=version)
        self.assertEqual(202, resp.status)

        def _assert_workers():
            resp, body = self.admin_client.get_function_workers(
                function_id,
                version=version
            )
            self.assertEqual(200, resp.status)
            self.assertEqual(0, len(body['workers']))

        r = tenacity.Retrying(
            wait=tenacity.wait_fixed(1),
            stop=tenacity.stop_after_attempt(5),
            retry=tenacity.retry_if_exception_type(AssertionError)
        )
        r.call(_assert_workers) 
Example #16
Source File: __init__.py    From networking-generic-switch with Apache License 2.0 5 votes vote down vote up
def _get_connection(self):
        """Context manager providing a netmiko SSH connection object.

        This function hides the complexities of gracefully handling retrying
        failed connection attempts.
        """
        retry_exc_types = (paramiko.SSHException, EOFError)

        # Use tenacity to handle retrying.
        @tenacity.retry(
            # Log a message after each failed attempt.
            after=tenacity.after_log(LOG, logging.DEBUG),
            # Reraise exceptions if our final attempt fails.
            reraise=True,
            # Retry on SSH connection errors.
            retry=tenacity.retry_if_exception_type(retry_exc_types),
            # Stop after the configured timeout.
            stop=tenacity.stop_after_delay(
                int(self.ngs_config['ngs_ssh_connect_timeout'])),
            # Wait for the configured interval between attempts.
            wait=tenacity.wait_fixed(
                int(self.ngs_config['ngs_ssh_connect_interval'])),
        )
        def _create_connection():
            return netmiko.ConnectHandler(**self.config)

        # First, create a connection.
        try:
            net_connect = _create_connection()
        except tenacity.RetryError as e:
            LOG.error("Reached maximum SSH connection attempts, not retrying")
            raise exc.GenericSwitchNetmikoConnectError(
                config=device_utils.sanitise_config(self.config), error=e)
        except Exception as e:
            LOG.error("Unexpected exception during SSH connection")
            raise exc.GenericSwitchNetmikoConnectError(
                config=device_utils.sanitise_config(self.config), error=e)

        # Now yield the connection to the caller.
        with net_connect:
            yield net_connect 
Example #17
Source File: __init__.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def get_connection_from_config():
    retries = CONF.database.max_retries
    url = CONF.database.connection

    try:
        # TOTO(iafek): check why this call randomly fails
        connection_scheme = urlparse.urlparse(url).scheme
        LOG.debug('looking for %(name)r driver in %(namespace)r',
                  {'name': connection_scheme, 'namespace': _NAMESPACE})
        mgr = driver.DriverManager(_NAMESPACE, connection_scheme)

    except Exception:
        LOG.exception('Failed to get scheme %s.' % url)
        return None

    @tenacity.retry(
        wait=tenacity.wait_fixed(CONF.database.retry_interval),
        stop=tenacity.stop_after_attempt(retries),
        after=tenacity.after_log(LOG, log.WARN),
        reraise=True)
    def _get_connection():
        """Return an open connection to the database."""
        conn = mgr.driver(url)
        session = conn._engine_facade.get_session()
        session.execute('SELECT 1;')
        return conn

    return _get_connection() 
Example #18
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_reraise_by_default(self):
        calls = []

        @retry(wait=tenacity.wait_fixed(0.1),
               stop=tenacity.stop_after_attempt(2),
               reraise=True)
        def _reraised_by_default():
            calls.append('x')
            raise KeyError("Bad key")

        self.assertRaises(KeyError, _reraised_by_default)
        self.assertEqual(2, len(calls)) 
Example #19
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_wait_triple_sum(self):
        r = Retrying(wait=tenacity.wait_fixed(1) + 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, 9)
            self.assertGreaterEqual(w, 6) 
Example #20
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_before_sleep_backward_compat_method(self):
        self.slept = 0

        @retry(wait=tenacity.wait_fixed(0.01),
               stop=tenacity.stop_after_attempt(3),
               before_sleep=self._before_sleep)
        def _test_before_sleep():
            raise Exception("testing before_sleep_attempts handler")

        try:
            _test_before_sleep()
        except tenacity.RetryError:
            pass

        self.assertEqual(self.slept, 2) 
Example #21
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_fixed_sleep(self):
        r = Retrying(wait=tenacity.wait_fixed(1))
        self.assertEqual(1, r.wait(12, 6546)) 
Example #22
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_before_sleep(self):
        def _before_sleep(retry_state):
            self.assertGreater(retry_state.next_action.sleep, 0)
            _before_sleep.attempt_number = retry_state.attempt_number

        @retry(wait=tenacity.wait_fixed(0.01),
               stop=tenacity.stop_after_attempt(3),
               before_sleep=_before_sleep)
        def _test_before_sleep():
            if _before_sleep.attempt_number < 2:
                raise Exception("testing before_sleep_attempts handler")

        _test_before_sleep()
        self.assertEqual(_before_sleep.attempt_number, 2) 
Example #23
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_wait_combine(self):
        r = Retrying(wait=tenacity.wait_combine(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) 
Example #24
Source File: retry_write_file_tenacity_module.py    From Mastering-Python-Design-Patterns-Second-Edition with MIT License 5 votes vote down vote up
def create_file(filename, after_delay=5):
    time.sleep(after_delay)

    with open(filename, 'w') as f:
        f.write('A file creation test')

#@tenacity.retry(wait=tenacity.wait_fixed(2)) 
Example #25
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_retry_function_object(self):
        """Test that six.wraps doesn't cause problems with callable objects.

        It raises an error upon trying to wrap it in Py2, because __name__
        attribute is missing. It's fixed in Py3 but was never backported.
        """
        class Hello(object):
            def __call__(self):
                return "Hello"
        retrying = Retrying(wait=tenacity.wait_fixed(0.01),
                            stop=tenacity.stop_after_attempt(3))
        h = retrying.wraps(Hello())
        self.assertEqual(h(), "Hello") 
Example #26
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_retry_with(self):
        start = current_time_ms()
        result = _retryable_test_with_wait.retry_with(
            wait=tenacity.wait_fixed(0.1))(NoneReturnUntilAfterCount(5))
        t = current_time_ms() - start
        self.assertGreaterEqual(t, 500)
        self.assertTrue(result) 
Example #27
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_wait_class_backward_compatibility(self):
        """Ensure builtin objects accept both old and new parameters."""
        waitobj = tenacity.wait_fixed(5)
        self.assertEqual(waitobj(1, 0.1), 5)
        self.assertEqual(
            waitobj(1, 0.1, tenacity.Future.construct(1, 1, False)), 5)
        retry_state = make_retry_state(123, 456)
        self.assertEqual(retry_state.attempt_number, 123)
        self.assertEqual(retry_state.seconds_since_start, 456)
        self.assertEqual(waitobj(retry_state=retry_state), 5) 
Example #28
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
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) 
Example #29
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
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, 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) 
Example #30
Source File: health_manager.py    From senlin with Apache License 2.0 4 votes vote down vote up
def run_health_check(self, ctx, node):
        """Routine to check a node status from a url and recovery if necessary

        :param node: The node to be checked.
        :returns: True if node is healthy. False otherwise.
        """

        max_unhealthy_retry = self.params['poll_url_retry_limit']
        retry_interval = self.params['poll_url_retry_interval']

        def _return_last_value(retry_state):
            return retry_state.outcome.result()

        @tenacity.retry(
            retry=tenacity.retry_if_result(lambda x: x is False),
            wait=tenacity.wait_fixed(retry_interval),
            retry_error_callback=_return_last_value,
            stop=tenacity.stop_after_attempt(max_unhealthy_retry)
        )
        def _poll_url_with_retry(url):
            return self._poll_url(url, node)

        try:
            if node.status != consts.NS_ACTIVE:
                LOG.info("%s for %s: node is not in ACTIVE state, so skip "
                         "poll url",
                         consts.POLL_URL_PASS, node.name)
                return True

            url_template = self.params['poll_url']
            url = self._expand_url_template(url_template, node)

            # If health check returns True, return True to mark node as
            # healthy. Else return True to mark node as healthy if we are still
            # within the node's grace period to allow the node to warm-up.
            # Return False to mark the node as unhealthy if we are outside the
            # grace period.

            return (_poll_url_with_retry(url) or
                    self._node_within_grace_period(node))
        except Exception as ex:
            LOG.warning(
                "%s for %s: Ignoring error on poll URL: %s",
                consts.POLL_URL_PASS, node.name, ex
            )

            # treat node as healthy when an exception is encountered
            return True