2017-09-30 15:27:14 +00:00
|
|
|
import logging
|
2017-10-03 10:38:38 +00:00
|
|
|
import time
|
2017-11-11 22:59:38 +00:00
|
|
|
|
2017-11-12 12:42:27 +00:00
|
|
|
import unittest2
|
2017-09-30 15:27:14 +00:00
|
|
|
|
2017-09-16 09:57:30 +00:00
|
|
|
import mitogen.core
|
|
|
|
import mitogen.master
|
|
|
|
|
2017-09-30 15:27:14 +00:00
|
|
|
import testlib
|
|
|
|
|
2017-09-16 09:57:30 +00:00
|
|
|
|
|
|
|
class CrazyType(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-09-21 08:11:47 +00:00
|
|
|
def function_that_adds_numbers(x, y):
|
|
|
|
return x + y
|
|
|
|
|
|
|
|
|
2017-09-16 09:57:30 +00:00
|
|
|
def function_that_fails():
|
|
|
|
raise ValueError('exception text')
|
|
|
|
|
|
|
|
|
|
|
|
def func_with_bad_return_value():
|
|
|
|
return CrazyType()
|
|
|
|
|
|
|
|
|
2017-09-16 14:26:59 +00:00
|
|
|
def func_accepts_returns_context(context):
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2018-03-29 09:47:01 +00:00
|
|
|
def func_accepts_returns_sender(sender):
|
2018-03-29 09:58:48 +00:00
|
|
|
sender.send(123)
|
2018-03-29 09:47:01 +00:00
|
|
|
sender.close()
|
|
|
|
return sender
|
|
|
|
|
|
|
|
|
2017-09-30 15:27:14 +00:00
|
|
|
class CallFunctionTest(testlib.RouterMixin, testlib.TestCase):
|
2017-09-30 09:38:40 +00:00
|
|
|
def setUp(self):
|
|
|
|
super(CallFunctionTest, self).setUp()
|
2018-03-29 09:47:01 +00:00
|
|
|
self.local = self.router.fork()
|
2017-09-16 09:57:30 +00:00
|
|
|
|
2017-09-21 08:11:47 +00:00
|
|
|
def test_succeeds(self):
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertEqual(3, self.local.call(function_that_adds_numbers, 1, 2))
|
2017-09-21 08:11:47 +00:00
|
|
|
|
2017-09-16 10:04:55 +00:00
|
|
|
def test_crashes(self):
|
2017-09-30 15:27:14 +00:00
|
|
|
exc = self.assertRaises(mitogen.core.CallError,
|
|
|
|
lambda: self.local.call(function_that_fails))
|
2017-09-16 09:57:30 +00:00
|
|
|
|
2017-09-30 15:27:14 +00:00
|
|
|
s = str(exc)
|
2017-09-16 09:57:30 +00:00
|
|
|
etype, _, s = s.partition(': ')
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertEqual(etype, 'exceptions.ValueError')
|
2017-09-16 09:57:30 +00:00
|
|
|
|
|
|
|
msg, _, s = s.partition('\n')
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertEqual(msg, 'exception text')
|
2017-09-16 09:57:30 +00:00
|
|
|
|
|
|
|
# Traceback
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertGreater(len(s), 0)
|
2017-09-16 09:57:30 +00:00
|
|
|
|
|
|
|
def test_bad_return_value(self):
|
2017-09-30 15:27:14 +00:00
|
|
|
exc = self.assertRaises(mitogen.core.StreamError,
|
|
|
|
lambda: self.local.call(func_with_bad_return_value))
|
2018-04-01 00:23:13 +00:00
|
|
|
self.assertEquals(
|
|
|
|
exc[0],
|
|
|
|
"cannot unpickle '%s'/'CrazyType'" % (__name__,),
|
|
|
|
)
|
2017-09-16 09:57:30 +00:00
|
|
|
|
2017-10-03 10:24:55 +00:00
|
|
|
def test_aborted_on_local_context_disconnect(self):
|
|
|
|
stream = self.router._stream_by_id[self.local.context_id]
|
|
|
|
self.broker.stop_receive(stream)
|
|
|
|
recv = self.local.call_async(time.sleep, 120)
|
|
|
|
self.broker.defer(stream.on_disconnect, self.broker)
|
|
|
|
exc = self.assertRaises(mitogen.core.ChannelError,
|
|
|
|
lambda: recv.get())
|
|
|
|
self.assertEquals(exc[0], mitogen.core.ChannelError.local_msg)
|
2017-09-16 10:04:55 +00:00
|
|
|
|
|
|
|
def test_aborted_on_local_broker_shutdown(self):
|
2017-10-03 10:38:38 +00:00
|
|
|
stream = self.router._stream_by_id[self.local.context_id]
|
|
|
|
recv = self.local.call_async(time.sleep, 120)
|
2017-10-03 11:05:12 +00:00
|
|
|
time.sleep(0.05) # Ensure GIL is released
|
2017-10-03 10:38:38 +00:00
|
|
|
self.broker.shutdown()
|
|
|
|
exc = self.assertRaises(mitogen.core.ChannelError,
|
|
|
|
lambda: recv.get())
|
|
|
|
self.assertEquals(exc[0], mitogen.core.ChannelError.local_msg)
|
2017-09-16 14:26:59 +00:00
|
|
|
|
|
|
|
def test_accepts_returns_context(self):
|
|
|
|
context = self.local.call(func_accepts_returns_context, self.local)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIsNot(context, self.local)
|
|
|
|
self.assertEqual(context.context_id, self.local.context_id)
|
|
|
|
self.assertEqual(context.name, self.local.name)
|
2017-09-21 11:15:36 +00:00
|
|
|
|
2018-03-29 09:47:01 +00:00
|
|
|
def test_accepts_returns_sender(self):
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
sender = recv.to_sender()
|
|
|
|
sender2 = self.local.call(func_accepts_returns_sender, sender)
|
|
|
|
self.assertEquals(sender.context.context_id,
|
|
|
|
sender2.context.context_id)
|
|
|
|
self.assertEquals(sender.dst_handle, sender2.dst_handle)
|
|
|
|
self.assertEquals(123, recv.get().unpickle())
|
|
|
|
self.assertRaises(mitogen.core.ChannelError,
|
|
|
|
lambda: recv.get().unpickle())
|
|
|
|
|
2017-09-21 11:15:36 +00:00
|
|
|
|
2017-09-22 08:07:50 +00:00
|
|
|
if __name__ == '__main__':
|
2017-11-12 12:42:27 +00:00
|
|
|
unittest2.main()
|