本文整理汇总了Python中twisted.test.proto_helpers.MemoryReactor类的典型用法代码示例。如果您正苦于以下问题:Python MemoryReactor类的具体用法?Python MemoryReactor怎么用?Python MemoryReactor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MemoryReactor类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_listenDefaultHost
def test_listenDefaultHost(self):
"""
L{MemoryReactor.listenTCP}, L{MemoryReactor.listenSSL} and
L{MemoryReactor.listenUNIX} will return an L{IListeningPort} whose
C{getHost} method returns an L{IAddress}; C{listenTCP} and C{listenSSL}
will have a default host of C{'0.0.0.0'}, and a port that reflects the
value passed, and C{listenUNIX} will have a name that reflects the path
passed.
"""
memoryReactor = MemoryReactor()
for port in [memoryReactor.listenTCP(8242, Factory()), memoryReactor.listenSSL(8242, Factory(), None)]:
verifyObject(IListeningPort, port)
address = port.getHost()
verifyObject(IAddress, address)
self.assertEqual(address.host, "0.0.0.0")
self.assertEqual(address.port, 8242)
port = memoryReactor.listenUNIX(b"/path/to/socket", Factory())
verifyObject(IListeningPort, port)
address = port.getHost()
verifyObject(IAddress, address)
self.assertEqual(address.name, b"/path/to/socket")
示例2: test_connectDestination
def test_connectDestination(self):
"""
L{MemoryReactor.connectTCP}, L{MemoryReactor.connectSSL}, and
L{MemoryReactor.connectUNIX} will return an L{IConnector} whose
C{getDestination} method returns an L{IAddress} with attributes which
reflect the values passed.
"""
memoryReactor = MemoryReactor()
for connector in [
memoryReactor.connectTCP("test.example.com", 8321, ClientFactory()),
memoryReactor.connectSSL("test.example.com", 8321, ClientFactory(), None),
]:
verifyObject(IConnector, connector)
address = connector.getDestination()
verifyObject(IAddress, address)
self.assertEqual(address.host, "test.example.com")
self.assertEqual(address.port, 8321)
connector = memoryReactor.connectUNIX(b"/fake/path", ClientFactory())
verifyObject(IConnector, connector)
address = connector.getDestination()
verifyObject(IAddress, address)
self.assertEqual(address.name, b"/fake/path")
示例3: test_writers
def test_writers(self):
"""
Adding, removing, and listing writers works.
"""
writer = object()
reactor = MemoryReactor()
reactor.addWriter(writer)
reactor.addWriter(writer)
self.assertEqual(reactor.getWriters(), [writer])
reactor.removeWriter(writer)
self.assertEqual(reactor.getWriters(), [])
示例4: test_readers
def test_readers(self):
"""
Adding, removing, and listing readers works.
"""
reader = object()
reactor = MemoryReactor()
reactor.addReader(reader)
reactor.addReader(reader)
self.assertEqual(reactor.getReaders(), [reader])
reactor.removeReader(reader)
self.assertEqual(reactor.getReaders(), [])
示例5: __init__
def __init__(self):
MemoryReactor.__init__(self)
Clock.__init__(self)
self.spawnedProcesses = []
示例6: __init__
def __init__(self):
MemoryReactor.__init__(self)
self._triggers = {}
示例7: connectSSL
def connectSSL(self, *a, **kw):
MemoryReactor.connectSSL(self, *a, **kw)
connector = MemoryConnector()
self.connectors.append(connector)
return connector
示例8: __init__
def __init__(self):
MemoryReactor.__init__(self)
Clock.__init__(self)
self.connectors = []
示例9: __init__
def __init__(self):
MemoryReactor.__init__(self)
Clock.__init__(self)
示例10: setUp
def setUp(self):
self.reactor = MemoryReactor()
self.eventloop = TwistedEventLoop(self.reactor)
示例11: FileDescriptorRegistrationTests
class FileDescriptorRegistrationTests(TestCase):
"""
Tests for the add/remove_reader/writer API.
"""
def setUp(self):
self.reactor = MemoryReactor()
self.eventloop = TwistedEventLoop(self.reactor)
def test_reactor(self):
"""
FileDescriptors added to the reactor have a reference to the correct
reactor.
"""
self.eventloop.add_reader(123, lambda: None)
reader, = self.reactor.getReaders()
self.assertIs(reader.reactor, self.reactor)
def assert_descriptor_has_callbacks(self, fd, read_callback,
write_callback):
fd_wrappers = set(
[wrapper for wrapper in
self.reactor.getReaders() + self.reactor.getWriters()
if wrapper.fileno() == fd])
self.assertEqual(len(fd_wrappers), 1)
wrapper = fd_wrappers.pop()
self.assertEqual((wrapper.__class__, wrapper.fileno(),
wrapper._read_callback, wrapper._write_callback),
(_GenericFileDescriptor, fd, read_callback,
write_callback))
return wrapper
def test_add_reader_callback(self):
"""
For a new fd, the callback passed to add_reader is hooked up to the
Twisted FileDescriptor's doRead, and a no-op for doWrite.
"""
func = lambda: None
self.eventloop.add_reader(123, func, 1, 2)
self.eventloop.add_reader(125, func, 1)
self.assert_descriptor_has_callbacks(123, _Callable(func, (1, 2)),
_noop)
self.assert_descriptor_has_callbacks(125, _Callable(func, (1,)),
_noop)
def test_add_writer_callback(self):
"""
For a new fd, the callback passed to add_writer is hooked up to the
Twisted FileDescriptor's doWrite, and a no-op for doRead.
"""
func = lambda: None
self.eventloop.add_writer(123, func, 1, 2)
self.eventloop.add_writer(125, func, 5)
self.assert_descriptor_has_callbacks(123, _noop, _Callable(func, (1, 2)))
self.assert_descriptor_has_callbacks(125, _noop, _Callable(func, (5,)))
def test_remove_reader_callback(self):
"""
For a new fd that is only added with add_reader, the FileDescriptor for
the fd removed by remove_reader.
"""
self.eventloop.add_reader(123, lambda: None, 1, 2)
self.eventloop.add_reader(124, lambda: None)
self.eventloop.remove_reader(123)
self.assertEqual([f.fileno() for f in self.reactor.getReaders()], [124])
def test_remove_writer_callback(self):
"""
For a new fd that is only added with add_writer, the FileDescriptor for
the fd removed by remove_writer.
"""
self.eventloop.add_writer(123, lambda: None, 1, 2)
self.eventloop.add_writer(124, lambda: None)
self.eventloop.remove_writer(123)
self.assertEqual([f.fileno() for f in self.reactor.getWriters()], [124])
def test_remove_reader_twice(self):
"""
Calling remove_reader a second time has no effect.
"""
self.eventloop.add_reader(123, lambda: None, 1, 2)
self.eventloop.remove_reader(123)
self.eventloop.remove_reader(123)
def test_remove_writer_twice(self):
"""
Calling remove_writer a second time has no effect.
"""
self.eventloop.add_writer(123, lambda: None, 1, 2)
self.eventloop.remove_writer(123)
self.eventloop.remove_writer(123)
def test_add_reader_twice(self):
"""
Calling add_reader a second time overrides the first callback.
"""
self.eventloop.add_reader(123, lambda: None, 1, 2)
reader = next(iter(self.reactor.getReaders()))
f = lambda x: 2
self.eventloop.add_reader(123, f, 4)
self.assertEqual(reader._read_callback, _Callable(f, (4,)))
#.........这里部分代码省略.........
示例12: install
def install(self):
"""
Mock installation of L{MockReactor}.
"""
if self.hasInstalled:
raise error.ReactorAlreadyInstalledError(
"reactor already installed"
)
BaseReactor.install(self)
import sys
import twisted.internet
modules = sys.modules.copy()
modules["twisted.internet.reactor"] = self
self.testCase.patch(sys, "modules", self)
self.testCase.patch(twisted.internet, "reactor", self)
示例13: __init__
def __init__(self, testCase):
BaseReactor.__init__(self)
self.testCase = testCase