mitogen/tests/econtext_test.py

128 lines
3.0 KiB
Python
Raw Normal View History

2014-01-10 11:42:57 +00:00
#!/usr/bin/env python2.5
"""
def DoStuff():
2014-01-10 11:49:17 +00:00
import time
file('/tmp/foobar', 'w').write(time.ctime())
2014-01-10 11:42:57 +00:00
localhost = pyrpc.SSHConnection('localhost')
localhost.Connect()
try:
2014-01-10 11:49:17 +00:00
ret = localhost.Evaluate(DoStuff)
2014-01-10 11:42:57 +00:00
except OSError, e:
2014-01-10 11:49:17 +00:00
2014-01-10 11:45:55 +00:00
Tests
2014-01-10 11:49:17 +00:00
- Test Channel objects to destruction.
- External contexts sometimes don't appear to die during a crash. This needs
tested to destruction.
- Test reconnecting to previously idle-killed contexts.
- Test remote context longevity to destruction. They should never stay
around after parent dies or disconnects.
2014-01-10 11:45:55 +00:00
2014-01-10 11:42:57 +00:00
"""
import sys
import unittest
import econtext
#
# Helper functions.
#
class GetModuleImportsTestCase(unittest.TestCase):
2014-01-10 11:49:17 +00:00
# This must be kept in sync with our actual imports.
IMPORTS = [
('econtext', 'econtext'),
('sys', 'PythonSystemModule'),
('sys', 'sys'),
('unittest', 'unittest')
]
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
def setUp(self):
global PythonSystemModule
import sys as PythonSystemModule
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
def tearDown(Self):
global PythonSystemModule
del PythonSystemModule
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
def testImports(self):
self.assertEqual(set(self.IMPORTS),
set(econtext.GetModuleImports(sys.modules[__name__])))
2014-01-10 11:42:57 +00:00
class BuildPartialModuleTestCase(unittest.TestCase):
2014-01-10 11:49:17 +00:00
def testNullModule(self):
"""Pass empty sequences; result should contain nothing but a hash bang line
and whitespace."""
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
lines = econtext.BuildPartialModule([], []).strip().split('\n')
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
self.assert_(lines[0].startswith('#!'))
self.assert_('import' not in lines[1:])
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
def testPassingMethodTypeFails(self):
"""Pass an instance method and ensure we refuse it."""
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
self.assertRaises(TypeError, econtext.BuildPartialModule,
[self.testPassingMethodTypeFails], [])
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
@staticmethod
def exampleStaticMethod():
pass
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
def testStaticMethodGetsUnwrapped(self):
"Ensure that @staticmethod decorators are stripped."
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
dct = {}
exec econtext.BuildPartialModule([self.exampleStaticMethod], []) in dct
self.assertFalse(isinstance(dct['exampleStaticMethod'], staticmethod))
2014-01-10 11:42:57 +00:00
#
# Streams
#
class StreamTestBase:
2014-01-10 11:49:17 +00:00
"""This defines rules that should remain true for all Stream subclasses. We
test in this manner to guard against a subclass breaking Stream's
polymorphism (e.g. overriding a method with the wrong prototype).
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
def testCommandLine(self):
print self.driver.command_line
"""
2014-01-10 11:42:57 +00:00
class SSHStreamTestCase(unittest.TestCase, StreamTestBase):
2014-01-10 11:49:17 +00:00
DRIVER_CLASS = econtext.SSHStream
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
def setUp(self):
# Stubs.
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
# Instance initialization.
self.stream = econtext.SSHStream('localhost', 'test-agent')
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
def tearDown(self):
pass
2014-01-10 11:42:57 +00:00
2014-01-10 11:49:17 +00:00
def testConstructor(self):
pass
2014-01-10 11:42:57 +00:00
class TCPStreamTestCase(unittest.TestCase, StreamTestBase):
2014-01-10 11:49:17 +00:00
pass
2014-01-10 11:42:57 +00:00
#
# Run the tests.
#
if __name__ == '__main__':
2014-01-10 11:49:17 +00:00
unittest.main()