2017-10-03 10:38:38 +00:00
|
|
|
import time
|
2017-11-11 22:59:38 +00:00
|
|
|
|
2017-09-16 09:57:30 +00:00
|
|
|
import mitogen.core
|
2018-09-09 17:51:03 +00:00
|
|
|
import mitogen.parent
|
2019-01-23 12:44:08 +00:00
|
|
|
from mitogen.core import str_partition
|
2017-09-16 09:57:30 +00:00
|
|
|
|
2017-09-30 15:27:14 +00:00
|
|
|
import testlib
|
2018-06-26 01:17:50 +00:00
|
|
|
import plain_old_module
|
2018-04-17 16:40:45 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-09-08 18:19:14 +00:00
|
|
|
def function_that_fails(s=''):
|
|
|
|
raise plain_old_module.MyError('exception text'+s)
|
2017-09-16 09:57:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def func_with_bad_return_value():
|
|
|
|
return CrazyType()
|
|
|
|
|
|
|
|
|
2018-09-08 18:19:14 +00:00
|
|
|
def func_returns_arg(context):
|
2017-09-16 14:26:59 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-09-05 21:41:05 +00:00
|
|
|
class TargetClass:
|
|
|
|
|
|
|
|
offset = 100
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def add_numbers_with_offset(cls, x, y):
|
|
|
|
return cls.offset + x + y
|
|
|
|
|
|
|
|
|
2017-09-30 15:27:14 +00:00
|
|
|
class CallFunctionTest(testlib.RouterMixin, testlib.TestCase):
|
2018-09-05 21:41:05 +00:00
|
|
|
|
2017-09-30 09:38:40 +00:00
|
|
|
def setUp(self):
|
|
|
|
super(CallFunctionTest, self).setUp()
|
2019-01-23 12:44:08 +00:00
|
|
|
self.local = self.router.local()
|
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
|
|
|
|
2018-09-05 21:41:05 +00:00
|
|
|
def test_succeeds_class_method(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self.local.call(TargetClass.add_numbers_with_offset, 1, 2),
|
|
|
|
103,
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2019-01-23 12:44:08 +00:00
|
|
|
s = mitogen.core.to_text(exc)
|
|
|
|
etype, _, s = str_partition(s, u': ')
|
|
|
|
self.assertEqual(etype, u'plain_old_module.MyError')
|
2017-09-16 09:57:30 +00:00
|
|
|
|
2019-01-23 12:44:08 +00:00
|
|
|
msg, _, s = str_partition(s, u'\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))
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual(
|
2018-04-17 16:40:45 +00:00
|
|
|
exc.args[0],
|
2018-04-01 00:23:13 +00:00
|
|
|
"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())
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual(exc.args[0], self.router.respondent_disconnect_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()
|
2019-01-19 06:19:55 +00:00
|
|
|
self.broker_shutdown = True
|
2017-10-03 10:38:38 +00:00
|
|
|
exc = self.assertRaises(mitogen.core.ChannelError,
|
|
|
|
lambda: recv.get())
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual(exc.args[0], self.router.respondent_disconnect_msg)
|
2017-09-16 14:26:59 +00:00
|
|
|
|
|
|
|
def test_accepts_returns_context(self):
|
2018-09-08 18:19:14 +00:00
|
|
|
context = self.local.call(func_returns_arg, self.local)
|
2018-10-26 09:54:22 +00:00
|
|
|
# Unpickling now deduplicates Context instances.
|
|
|
|
self.assertIs(context, self.local)
|
2017-11-11 23:08:08 +00:00
|
|
|
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)
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual(sender.context.context_id,
|
2018-03-29 09:47:01 +00:00
|
|
|
sender2.context.context_id)
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual(sender.dst_handle, sender2.dst_handle)
|
|
|
|
self.assertEqual(123, recv.get().unpickle())
|
2018-03-29 09:47:01 +00:00
|
|
|
self.assertRaises(mitogen.core.ChannelError,
|
|
|
|
lambda: recv.get().unpickle())
|
|
|
|
|
2017-09-21 11:15:36 +00:00
|
|
|
|
2018-10-26 08:14:39 +00:00
|
|
|
class CallChainTest(testlib.RouterMixin, testlib.TestCase):
|
2018-09-08 18:19:14 +00:00
|
|
|
# Verify mitogen_chain functionality.
|
2018-09-09 17:51:03 +00:00
|
|
|
klass = mitogen.parent.CallChain
|
2018-09-08 18:19:14 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-10-26 09:54:22 +00:00
|
|
|
super(CallChainTest, self).setUp()
|
2019-01-23 12:44:08 +00:00
|
|
|
self.local = self.router.local()
|
2018-09-08 18:19:14 +00:00
|
|
|
|
|
|
|
def test_subsequent_calls_produce_same_error(self):
|
2018-09-09 17:51:03 +00:00
|
|
|
chain = self.klass(self.local, pipelined=True)
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual('xx', chain.call(func_returns_arg, 'xx'))
|
2018-09-09 17:51:03 +00:00
|
|
|
chain.call_no_reply(function_that_fails, 'x1')
|
2018-09-08 18:19:14 +00:00
|
|
|
e1 = self.assertRaises(mitogen.core.CallError,
|
2018-09-09 17:51:03 +00:00
|
|
|
lambda: chain.call(function_that_fails, 'x2'))
|
2018-09-08 18:19:14 +00:00
|
|
|
e2 = self.assertRaises(mitogen.core.CallError,
|
2018-09-09 17:51:03 +00:00
|
|
|
lambda: chain.call(func_returns_arg, 'x3'))
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual(str(e1), str(e2))
|
2018-09-08 18:19:14 +00:00
|
|
|
|
|
|
|
def test_unrelated_overlapping_failed_chains(self):
|
2018-09-09 17:51:03 +00:00
|
|
|
c1 = self.klass(self.local, pipelined=True)
|
|
|
|
c2 = self.klass(self.local, pipelined=True)
|
|
|
|
c1.call_no_reply(function_that_fails, 'c1')
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual('yes', c2.call(func_returns_arg, 'yes'))
|
2018-09-08 18:19:14 +00:00
|
|
|
self.assertRaises(mitogen.core.CallError,
|
2018-09-09 17:51:03 +00:00
|
|
|
lambda: c1.call(func_returns_arg, 'yes'))
|
2018-09-08 18:19:14 +00:00
|
|
|
|
2018-09-09 17:51:03 +00:00
|
|
|
def test_reset(self):
|
|
|
|
c1 = self.klass(self.local, pipelined=True)
|
|
|
|
c1.call_no_reply(function_that_fails, 'x1')
|
2018-09-08 18:32:20 +00:00
|
|
|
e1 = self.assertRaises(mitogen.core.CallError,
|
2018-09-09 17:51:03 +00:00
|
|
|
lambda: c1.call(function_that_fails, 'x2'))
|
|
|
|
c1.reset()
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual('x3', c1.call(func_returns_arg, 'x3'))
|
2018-09-08 18:32:20 +00:00
|
|
|
|
2018-09-08 18:19:14 +00:00
|
|
|
|
2018-12-10 01:59:01 +00:00
|
|
|
class UnsupportedCallablesTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
# Verify mitogen_chain functionality.
|
|
|
|
klass = mitogen.parent.CallChain
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(UnsupportedCallablesTest, self).setUp()
|
2019-01-23 12:44:08 +00:00
|
|
|
self.local = self.router.local()
|
2018-12-10 01:59:01 +00:00
|
|
|
|
|
|
|
def test_closures_unsuppored(self):
|
|
|
|
a = 1
|
|
|
|
closure = lambda: a
|
|
|
|
e = self.assertRaises(TypeError,
|
|
|
|
lambda: self.local.call(closure))
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual(e.args[0], self.klass.closures_msg)
|
2018-12-10 01:59:01 +00:00
|
|
|
|
|
|
|
def test_lambda_unsupported(self):
|
|
|
|
lam = lambda: None
|
|
|
|
e = self.assertRaises(TypeError,
|
|
|
|
lambda: self.local.call(lam))
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual(e.args[0], self.klass.lambda_msg)
|
2018-12-10 01:59:01 +00:00
|
|
|
|
|
|
|
def test_instance_method_unsupported(self):
|
|
|
|
class X:
|
|
|
|
def x(): pass
|
|
|
|
e = self.assertRaises(TypeError,
|
|
|
|
lambda: self.local.call(X().x))
|
2022-04-21 18:23:43 +00:00
|
|
|
self.assertEqual(e.args[0], self.klass.method_msg)
|