From ae03d5f4f1d9b396d80da34e0280fa7e67221853 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sat, 19 Jan 2019 22:42:28 +0000 Subject: [PATCH] issue #446: basic tests for Receiver._on_receive(). --- tests/receiver_test.py | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/receiver_test.py b/tests/receiver_test.py index 75f61b40..550b4525 100644 --- a/tests/receiver_test.py +++ b/tests/receiver_test.py @@ -76,5 +76,54 @@ class CloseTest(testlib.RouterMixin, testlib.TestCase): t.join() +class OnReceiveTest(testlib.RouterMixin, testlib.TestCase): + # Verify behaviour of _on_receive dead message handling. A dead message + # should unregister the receiver and wake all threads. + + def wait(self, latch, wait_recv): + try: + latch.put(wait_recv.get()) + except Exception: + latch.put(sys.exc_info()[1]) + + def test_sender_closes_one_thread(self): + latch = mitogen.core.Latch() + wait_recv = mitogen.core.Receiver(self.router) + t = threading.Thread(target=lambda: self.wait(latch, wait_recv)) + t.start() + sender = wait_recv.to_sender() + sender.close() + def throw(): + raise latch.get() + t.join() + e = self.assertRaises(mitogen.core.ChannelError, throw) + self.assertEquals(e.args[0], sender.explicit_close_msg) + + @unittest2.skip(reason=( + 'Unclear if a asingle dead message received from remote should ' + 'cause all threads to wake up.' + )) + def test_sender_closes_all_threads(self): + latch = mitogen.core.Latch() + wait_recv = mitogen.core.Receiver(self.router) + ts = [ + threading.Thread(target=lambda: self.wait(latch, wait_recv)) + for x in range(5) + ] + for t in ts: + t.start() + sender = wait_recv.to_sender() + sender.close() + def throw(): + raise latch.get() + for x in range(5): + e = self.assertRaises(mitogen.core.ChannelError, throw) + self.assertEquals(e.args[0], mitogen.core.Receiver.closed_msg) + for t in ts: + t.join() + + # TODO: what happens to a Select subscribed to the receiver in this case? + + if __name__ == '__main__': unittest2.main()