2017-09-15 06:24:41 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2019-01-30 03:46:21 +00:00
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
|
2017-11-12 12:42:27 +00:00
|
|
|
import unittest2
|
2019-01-30 03:46:21 +00:00
|
|
|
import mock
|
2017-09-15 06:24:41 +00:00
|
|
|
|
2018-09-30 20:37:15 +00:00
|
|
|
import mitogen.core
|
2019-01-30 03:46:21 +00:00
|
|
|
import mitogen.parent
|
2017-09-15 06:24:41 +00:00
|
|
|
import mitogen.master
|
|
|
|
import mitogen.utils
|
2019-01-23 12:44:08 +00:00
|
|
|
from mitogen.core import b
|
2017-09-15 06:24:41 +00:00
|
|
|
|
2018-11-03 13:28:37 +00:00
|
|
|
import testlib
|
|
|
|
|
2017-09-15 06:24:41 +00:00
|
|
|
|
2017-09-21 18:59:53 +00:00
|
|
|
def func0(router):
|
|
|
|
return router
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
|
2017-09-21 18:59:53 +00:00
|
|
|
@mitogen.utils.with_router
|
|
|
|
def func(router):
|
|
|
|
return router
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
|
2018-11-03 13:28:37 +00:00
|
|
|
class RunWithRouterTest(testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
# test_shutdown_on_exception
|
|
|
|
# test_shutdown_on_success
|
|
|
|
|
|
|
|
def test_run_with_broker(self):
|
2017-09-21 18:59:53 +00:00
|
|
|
router = mitogen.utils.run_with_router(func0)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIsInstance(router, mitogen.master.Router)
|
2017-09-21 18:59:53 +00:00
|
|
|
self.assertFalse(router.broker._thread.isAlive())
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
|
2018-11-03 13:28:37 +00:00
|
|
|
class WithRouterTest(testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
def test_with_broker(self):
|
2017-09-21 18:59:53 +00:00
|
|
|
router = func()
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIsInstance(router, mitogen.master.Router)
|
2017-09-21 18:59:53 +00:00
|
|
|
self.assertFalse(router.broker._thread.isAlive())
|
2017-10-02 09:06:36 +00:00
|
|
|
|
|
|
|
|
2018-09-30 20:37:15 +00:00
|
|
|
class Dict(dict): pass
|
|
|
|
class List(list): pass
|
|
|
|
class Tuple(tuple): pass
|
|
|
|
class Unicode(mitogen.core.UnicodeType): pass
|
|
|
|
class Bytes(mitogen.core.BytesType): pass
|
|
|
|
|
|
|
|
|
2018-11-03 13:28:37 +00:00
|
|
|
class CastTest(testlib.TestCase):
|
2018-09-30 20:37:15 +00:00
|
|
|
def test_dict(self):
|
|
|
|
self.assertEqual(type(mitogen.utils.cast({})), dict)
|
|
|
|
self.assertEqual(type(mitogen.utils.cast(Dict())), dict)
|
|
|
|
|
|
|
|
def test_nested_dict(self):
|
|
|
|
specimen = mitogen.utils.cast(Dict({'k': Dict({'k2': 'v2'})}))
|
|
|
|
self.assertEqual(type(specimen), dict)
|
|
|
|
self.assertEqual(type(specimen['k']), dict)
|
|
|
|
|
|
|
|
def test_list(self):
|
|
|
|
self.assertEqual(type(mitogen.utils.cast([])), list)
|
|
|
|
self.assertEqual(type(mitogen.utils.cast(List())), list)
|
|
|
|
|
|
|
|
def test_nested_list(self):
|
|
|
|
specimen = mitogen.utils.cast(List((0, 1, List((None,)))))
|
|
|
|
self.assertEqual(type(specimen), list)
|
|
|
|
self.assertEqual(type(specimen[2]), list)
|
|
|
|
|
|
|
|
def test_tuple(self):
|
|
|
|
self.assertEqual(type(mitogen.utils.cast(())), list)
|
|
|
|
self.assertEqual(type(mitogen.utils.cast(Tuple())), list)
|
|
|
|
|
|
|
|
def test_nested_tuple(self):
|
|
|
|
specimen = mitogen.utils.cast(Tuple((0, 1, Tuple((None,)))))
|
|
|
|
self.assertEqual(type(specimen), list)
|
|
|
|
self.assertEqual(type(specimen[2]), list)
|
|
|
|
|
|
|
|
def assertUnchanged(self, v):
|
|
|
|
self.assertIs(mitogen.utils.cast(v), v)
|
|
|
|
|
|
|
|
def test_passthrough(self):
|
|
|
|
self.assertUnchanged(0)
|
|
|
|
self.assertUnchanged(0.0)
|
|
|
|
self.assertUnchanged(float('inf'))
|
|
|
|
self.assertUnchanged(True)
|
|
|
|
self.assertUnchanged(False)
|
|
|
|
self.assertUnchanged(None)
|
|
|
|
|
|
|
|
def test_unicode(self):
|
|
|
|
self.assertEqual(type(mitogen.utils.cast(u'')), mitogen.core.UnicodeType)
|
|
|
|
self.assertEqual(type(mitogen.utils.cast(Unicode())), mitogen.core.UnicodeType)
|
|
|
|
|
|
|
|
def test_bytes(self):
|
2019-01-23 12:44:08 +00:00
|
|
|
self.assertEqual(type(mitogen.utils.cast(b(''))), mitogen.core.BytesType)
|
2018-09-30 20:37:15 +00:00
|
|
|
self.assertEqual(type(mitogen.utils.cast(Bytes())), mitogen.core.BytesType)
|
|
|
|
|
|
|
|
def test_unknown(self):
|
|
|
|
self.assertRaises(TypeError, mitogen.utils.cast, set())
|
|
|
|
self.assertRaises(TypeError, mitogen.utils.cast, 4j)
|
|
|
|
|
|
|
|
|
2017-10-02 09:06:36 +00:00
|
|
|
if __name__ == '__main__':
|
2017-11-12 12:42:27 +00:00
|
|
|
unittest2.main()
|