當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。