本文整理汇总了Python中twisted.test.proto_helpers.MemoryReactor.getWriters方法的典型用法代码示例。如果您正苦于以下问题:Python MemoryReactor.getWriters方法的具体用法?Python MemoryReactor.getWriters怎么用?Python MemoryReactor.getWriters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.test.proto_helpers.MemoryReactor
的用法示例。
在下文中一共展示了MemoryReactor.getWriters方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_writers
# 需要导入模块: from twisted.test.proto_helpers import MemoryReactor [as 别名]
# 或者: from twisted.test.proto_helpers.MemoryReactor import getWriters [as 别名]
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(), [])
示例2: FileDescriptorRegistrationTests
# 需要导入模块: from twisted.test.proto_helpers import MemoryReactor [as 别名]
# 或者: from twisted.test.proto_helpers.MemoryReactor import getWriters [as 别名]
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,)))
#.........这里部分代码省略.........