Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_waiters_that_cancel(self):
q = coros.queue()
def do_receive(q, evt):
eventlet.Timeout(0, RuntimeError())
try:
result = q.wait()
evt.send(result)
except RuntimeError:
evt.send('timed out')
evt = Event()
spawn(do_receive, q, evt)
self.assertEqual(evt.wait(), 'timed out')
q.send('hi')
self.assertEqual(q.wait(), 'hi')
def test_fail_fast_imap():
# A failing call...
failing_exception = Exception()
def failing_call():
raise failing_exception
# ...and an eventually successful call.
slow_call_returned = Event()
def slow_call():
sleep(5)
slow_call_returned.send() # pragma: no cover
def identity_fn(fn):
return fn()
calls = [slow_call, failing_call]
pool = GreenPool(2)
# fail_fast_imap fails as soon as the exception is raised
with pytest.raises(Exception) as raised_exc:
list(fail_fast_imap(pool, identity_fn, calls))
assert raised_exc.value == failing_exception
def test_send_rpc_multi_message_reply_ignores_all_but_last(get_connection):
queue_declared = Event()
def response_greenthread():
with get_connection() as conn:
with conn.channel() as chan:
queue = nova.get_topic_queue(
'test_rpc', 'test', channel=chan)
queue.declare()
queue_declared.send(True)
msg = ifirst(queue_iterator(queue, no_ack=True, timeout=2))
msgid, _, _, args = nova.parse_message(msg.payload)
exchange = nova.get_reply_exchange(msgid)
producer = Producer(chan, exchange=exchange, routing_key=msgid)
for _ in range(3):
self.topic_proxy[topic] = eventlet.queue.LightQueue(
CONF.rpc_zmq_topic_backlog)
self.sockets.append(out_sock)
# It takes some time for a pub socket to open,
# before we can have any faith in doing a send() to it.
if sock_type == zmq.PUB:
eventlet.sleep(.5)
waiter.send(True)
while(True):
data = self.topic_proxy[topic].get()
out_sock.send(data, copy=False)
wait_sock_creation = eventlet.event.Event()
eventlet.spawn(publisher, wait_sock_creation)
try:
wait_sock_creation.wait()
except RPCException:
LOG.error(_("Topic socket file creation failed."))
return
try:
self.topic_proxy[topic].put_nowait(data)
except eventlet.queue.Full:
LOG.error(_("Local per-topic backlog buffer full for topic "
"%(topic)s. Dropping message.") % {'topic': topic})
# proxy calls of the application through tpool
wsgi_application = tpool_wsgi(wsgi_application)
elif threads != 1:
from eventlet.green import socket
host, port = sock.getsockname()
access_log_file = config.get('access_log_file')
if access_log_file is not None:
access_log_file = open(access_log_file, 'a')
max_age = 0
if config.get('max_age'):
max_age = int(config.get('max_age'))
server_event = eventlet.event.Event()
# the status object wants to have a reference to the server object
if config.get('status_port'):
def send_server_to_status(server_event):
server = server_event.wait()
get_statusobj().server = server
eventlet.spawn(send_server_to_status, server_event)
http_version = config.get('no_keepalive') and 'HTTP/1.0' or 'HTTP/1.1'
try:
wsgi_args = (sock, wsgi_application)
wsgi_kwargs = {'log' : access_log_file, 'server_event' : server_event, 'max_http_version' : http_version}
if config.get('no_keepalive'):
wsgi_kwargs.update({'keepalive' : False})
if max_age:
wsgi_kwargs.update({'timeout_value' : True})
eventlet.with_timeout(max_age, eventlet.wsgi.server, *wsgi_args,
def _spawn(self):
"""Spawn a process and its watchers."""
self._is_running = True
self._pid = None
self._kill_event = eventlet.event.Event()
self._process, cmd = utils.create_process(self._cmd,
run_as_root=self.run_as_root)
self._watchers = []
for reader in (self._read_stdout, self._read_stderr):
# Pass the stop event directly to the greenthread to
# ensure that assignment of a new event to the instance
# attribute does not prevent the greenthread from using
# the original event.
watcher = eventlet.spawn(self._watch_process,
reader,
self._kill_event)
self._watchers.append(watcher)
def __init__(self, threads=1000):
self.tg = threadgroup.ThreadGroup(threads)
# signal that the service is done shutting itself down:
self._done = event.Event()
def start(self, initial_delay=None, periodic_interval_max=None):
self._running = True
done = event.Event()
def _inner():
if initial_delay:
greenthread.sleep(initial_delay)
try:
while self._running:
idle = self.f(*self.args, **self.kw)
if not self._running:
break
if periodic_interval_max is not None:
idle = min(idle, periodic_interval_max)
LOG.debug('Dynamic looping call %(func_name)r sleeping '
'for %(idle).02f seconds',
{'func_name': self.f, 'idle': idle})
def reset(self):
# NOTE(Fengqian): docs for Event.reset() recommend against using it
self._done = event.Event()
def restart(self):
self.stop()
self.done = event.Event()
for restart_service in self.services:
restart_service.reset()
self.tg.add_thread(self.run_service, restart_service, self.done)