当前位置: 首页>>代码示例>>Python>>正文


Python MemoryReactor.getReaders方法代码示例

本文整理汇总了Python中twisted.test.proto_helpers.MemoryReactor.getReaders方法的典型用法代码示例。如果您正苦于以下问题:Python MemoryReactor.getReaders方法的具体用法?Python MemoryReactor.getReaders怎么用?Python MemoryReactor.getReaders使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twisted.test.proto_helpers.MemoryReactor的用法示例。


在下文中一共展示了MemoryReactor.getReaders方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_readers

# 需要导入模块: from twisted.test.proto_helpers import MemoryReactor [as 别名]
# 或者: from twisted.test.proto_helpers.MemoryReactor import getReaders [as 别名]
    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(), [])
开发者ID:0004c,项目名称:VTK,代码行数:17,代码来源:test_stringtransport.py

示例2: FileDescriptorRegistrationTests

# 需要导入模块: from twisted.test.proto_helpers import MemoryReactor [as 别名]
# 或者: from twisted.test.proto_helpers.MemoryReactor import getReaders [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,)))
#.........这里部分代码省略.........
开发者ID:dangra,项目名称:txtulip,代码行数:103,代码来源:test_eventloop.py


注:本文中的twisted.test.proto_helpers.MemoryReactor.getReaders方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。