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_stop_handles_actor_already_being_stopped(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock.actor_ref.stop.side_effect = pykka.ActorDeadError()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
self.mock.actor_ref.stop.assert_called_once_with(block=False)
def test_ask_nonblocking_fails_future_if_actor_is_stopped(actor_ref):
actor_ref.stop()
future = actor_ref.ask({'command': 'ping'}, block=False)
with pytest.raises(ActorDeadError) as exc_info:
future.get()
assert str(exc_info.value) == '{} not found'.format(actor_ref)
def test_messages_left_in_queue_after_actor_stops_receive_an_error(
runtime, actor_ref
):
event = runtime.event_class()
actor_ref.tell({'command': 'callback', 'callback': event.wait})
actor_ref.stop(block=False)
response = actor_ref.ask({'command': 'irrelevant'}, block=False)
event.set()
with pytest.raises(ActorDeadError):
response.get(timeout=0.5)
try:
data = self._sock.recv(4096)
except OSError as exc:
if exc.errno not in (errno.EWOULDBLOCK, errno.EINTR):
self.stop(f"Unexpected client error: {exc}")
return True
if not data:
self.disable_recv()
self.actor_ref.tell({"close": True})
return True
try:
self.actor_ref.tell({"received": data})
except pykka.ActorDeadError:
self.stop("Actor is dead.")
return True
if bookmarks:
doc = bookmarks.pop(0)
id = doc['identifier']
uri = '%s:%s' % (Extension.ext_name, id)
name = doc.get('title', id)
tracks = self.backend.library.lookup(uri).get()
if tracks:
playlists += [Playlist(uri=uri, name=name, tracks=tracks)]
else:
logger.warn('Skipping empty Archive bookmark %s', name)
self.future.load(bookmarks, playlists)
else:
logger.info('Loaded %d Archive bookmarks', len(playlists))
self.backend.playlists.playlists = playlists
backend.BackendListener.send('playlists_loaded')
except pykka.ActorDeadError:
self.stop()
except Exception as e:
logger.error('Error loading Archive bookmarks: %s', e)
def tell(self, message):
"""
Send message to actor without waiting for any response.
Will generally not block, but if the underlying queue is full it will
block until a free slot is available.
:param message: message to send
:type message: any
:raise: :exc:`pykka.ActorDeadError` if actor is not available
:return: nothing
"""
if not self.is_alive():
raise ActorDeadError('{} not found'.format(self))
self.actor_inbox.put(Envelope(message))
def _call_handler_filter(self, request, response, filter_chain):
try:
response = self._format_response(self._call_handler(request))
return self._call_next_filter(request, response, filter_chain)
except pykka.ActorDeadError as e:
logger.warning('Tried to communicate with dead actor.')
raise exceptions.MpdSystemError(e)
exception_value, self
)
)
self._stop()
ActorRegistry.stop_all()
while not self.actor_inbox.empty():
envelope = self.actor_inbox.get()
if envelope.reply_to is not None:
if isinstance(envelope.message, messages._ActorStop):
envelope.reply_to.set(None)
else:
envelope.reply_to.set_exception(
exc_info=(
ActorDeadError,
ActorDeadError(
'{} stopped before handling the message'.format(
self.actor_ref
)
),
None,
)
'{!r} in {}. Stopping all actors.'.format(
exception_value, self
)
)
self._stop()
ActorRegistry.stop_all()
while not self.actor_inbox.empty():
envelope = self.actor_inbox.get()
if envelope.reply_to is not None:
if isinstance(envelope.message, messages._ActorStop):
envelope.reply_to.set(None)
else:
envelope.reply_to.set_exception(
exc_info=(
ActorDeadError,
ActorDeadError(
'{} stopped before handling the message'.format(
self.actor_ref
)
),
None,
)
:param block: whether to block while waiting for a reply
:type block: boolean
:param timeout: seconds to wait before timeout if blocking
:type timeout: float or :class:`None`
:raise: :exc:`pykka.Timeout` if timeout is reached if blocking
:raise: any exception returned by the receiving actor if blocking
:return: :class:`pykka.Future`, or response if blocking
"""
future = self.actor_class._create_future()
try:
if not self.is_alive():
raise ActorDeadError('{} not found'.format(self))
except ActorDeadError:
future.set_exception()
else:
self.actor_inbox.put(Envelope(message, reply_to=future))
if block:
return future.get(timeout=timeout)
else:
return future