core: support Receiver.get(thread_dead=False)

For tests.
This commit is contained in:
David Wilson 2018-04-22 05:08:37 +01:00
parent 89fc842ca8
commit e63ae4768e
2 changed files with 30 additions and 20 deletions

View File

@ -441,10 +441,10 @@ class Receiver(object):
def empty(self): def empty(self):
return self._latch.empty() return self._latch.empty()
def get(self, timeout=None, block=True): def get(self, timeout=None, block=True, throw_dead=True):
_vv and IOLOG.debug('%r.get(timeout=%r, block=%r)', self, timeout, block) _vv and IOLOG.debug('%r.get(timeout=%r, block=%r)', self, timeout, block)
msg = self._latch.get(timeout=timeout, block=block) msg = self._latch.get(timeout=timeout, block=block)
if msg.is_dead: if msg.is_dead and throw_dead:
if msg.src_id == mitogen.context_id: if msg.src_id == mitogen.context_id:
raise ChannelError(ChannelError.local_msg) raise ChannelError(ChannelError.local_msg)
else: else:

View File

@ -223,40 +223,50 @@ class NoRouteTest(testlib.RouterMixin, testlib.TestCase):
# Verify sending a message to an invalid handle yields a dead message # Verify sending a message to an invalid handle yields a dead message
# from the target context. # from the target context.
l1 = self.router.fork() l1 = self.router.fork()
recv = l1.send_async(mitogen.core.Message(handle=999))
msg = recv.get(throw_dead=False)
self.assertEquals(msg.is_dead, True)
self.assertEquals(msg.src_id, l1.context_id)
recv = l1.send_async(mitogen.core.Message(handle=999)) recv = l1.send_async(mitogen.core.Message(handle=999))
e = self.assertRaises(mitogen.core.ChannelError, e = self.assertRaises(mitogen.core.ChannelError,
lambda: recv.get() lambda: recv.get())
)
self.assertEquals(e.args[0], mitogen.core.ChannelError.remote_msg) self.assertEquals(e.args[0], mitogen.core.ChannelError.remote_msg)
def test_totally_invalid_context_returns_dead(self): def test_totally_invalid_context_returns_dead(self):
recv = mitogen.core.Receiver(self.router) recv = mitogen.core.Receiver(self.router)
self.router.route( msg = mitogen.core.Message(
mitogen.core.Message( dst_id=1234,
dst_id=1234, handle=1234,
handle=1234, reply_to=recv.handle,
reply_to=recv.handle,
)
) )
self.router.route(msg)
rmsg = recv.get(throw_dead=False)
self.assertEquals(rmsg.is_dead, True)
self.assertEquals(rmsg.src_id, mitogen.context_id)
self.router.route(msg)
e = self.assertRaises(mitogen.core.ChannelError, e = self.assertRaises(mitogen.core.ChannelError,
lambda: recv.get() lambda: recv.get())
)
self.assertEquals(e.args[0], mitogen.core.ChannelError.local_msg) self.assertEquals(e.args[0], mitogen.core.ChannelError.local_msg)
def test_previously_alive_context_returns_dead(self): def test_previously_alive_context_returns_dead(self):
l1 = self.router.fork() l1 = self.router.fork()
l1.shutdown(wait=True) l1.shutdown(wait=True)
recv = mitogen.core.Receiver(self.router) recv = mitogen.core.Receiver(self.router)
self.router.route( msg = mitogen.core.Message(
mitogen.core.Message( dst_id=l1.context_id,
dst_id=l1.context_id, handle=mitogen.core.CALL_FUNCTION,
handle=mitogen.core.CALL_FUNCTION, reply_to=recv.handle,
reply_to=recv.handle,
)
) )
self.router.route(msg)
rmsg = recv.get(throw_dead=False)
self.assertEquals(rmsg.is_dead, True)
self.assertEquals(rmsg.src_id, mitogen.context_id)
self.router.route(msg)
e = self.assertRaises(mitogen.core.ChannelError, e = self.assertRaises(mitogen.core.ChannelError,
lambda: recv.get() lambda: recv.get())
)
self.assertEquals(e.args[0], mitogen.core.ChannelError.local_msg) self.assertEquals(e.args[0], mitogen.core.ChannelError.local_msg)