From e63ae4768e3cf03c8ef0dfe46f117d8346c380e1 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 22 Apr 2018 05:08:37 +0100 Subject: [PATCH] core: support Receiver.get(thread_dead=False) For tests. --- mitogen/core.py | 4 ++-- tests/router_test.py | 46 +++++++++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/mitogen/core.py b/mitogen/core.py index c57f84cb..4a48dac0 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -441,10 +441,10 @@ class Receiver(object): def empty(self): 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) 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: raise ChannelError(ChannelError.local_msg) else: diff --git a/tests/router_test.py b/tests/router_test.py index 09c54245..b32c5e4b 100644 --- a/tests/router_test.py +++ b/tests/router_test.py @@ -223,40 +223,50 @@ class NoRouteTest(testlib.RouterMixin, testlib.TestCase): # Verify sending a message to an invalid handle yields a dead message # from the target context. 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)) e = self.assertRaises(mitogen.core.ChannelError, - lambda: recv.get() - ) + lambda: recv.get()) self.assertEquals(e.args[0], mitogen.core.ChannelError.remote_msg) def test_totally_invalid_context_returns_dead(self): recv = mitogen.core.Receiver(self.router) - self.router.route( - mitogen.core.Message( - dst_id=1234, - handle=1234, - reply_to=recv.handle, - ) + msg = mitogen.core.Message( + dst_id=1234, + handle=1234, + 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, - lambda: recv.get() - ) + lambda: recv.get()) self.assertEquals(e.args[0], mitogen.core.ChannelError.local_msg) def test_previously_alive_context_returns_dead(self): l1 = self.router.fork() l1.shutdown(wait=True) recv = mitogen.core.Receiver(self.router) - self.router.route( - mitogen.core.Message( - dst_id=l1.context_id, - handle=mitogen.core.CALL_FUNCTION, - reply_to=recv.handle, - ) + msg = mitogen.core.Message( + dst_id=l1.context_id, + handle=mitogen.core.CALL_FUNCTION, + 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, - lambda: recv.get() - ) + lambda: recv.get()) self.assertEquals(e.args[0], mitogen.core.ChannelError.local_msg)