Exemplo n.º 1
0
    def __init__(self, eventloop: Optional[AbstractEventLoop] = None):
        if eventloop is None:
            _eventloop: AbstractEventLoop = get_event_loop()
        else:
            _eventloop = eventloop

        # On Python 3.8+, asyncio.get_event_loop() on
        # Windows was changed to return a ProactorEventLoop
        # unless the loop policy has been changed.
        if sys.platform == "win32":
            from asyncio import ProactorEventLoop

            if isinstance(_eventloop, ProactorEventLoop):
                raise TypeError(
                    f"ProactorEventLoop is not supported, got: {_eventloop}")

        self._asyncioEventloop: AbstractEventLoop = _eventloop
        self._writers: Dict[Type[FileDescriptor], int] = {}
        self._readers: Dict[Type[FileDescriptor], int] = {}
        self._continuousPolling = _ContinuousPolling(self)

        self._scheduledAt = None
        self._timerHandle = None

        super().__init__()
Exemplo n.º 2
0
 def test_removeUnknown(self):
     """
     Removing unknown readers and writers silently does nothing.
     """
     poller = _ContinuousPolling(Clock())
     poller.removeWriter(object())
     poller.removeReader(object())
Exemplo n.º 3
0
 def test_getWriters(self):
     """
     L{_ContinuousPolling.getWriters} returns a list of the write
     descriptors.
     """
     poller = _ContinuousPolling(Clock())
     writer = object()
     poller.addWriter(writer)
     self.assertIn(writer, poller.getWriters())
Exemplo n.º 4
0
 def test_getReaders(self):
     """
     L{_ContinuousPolling.getReaders} returns a list of the read
     descriptors.
     """
     poller = _ContinuousPolling(Clock())
     reader = object()
     poller.addReader(reader)
     self.assertIn(reader, poller.getReaders())
Exemplo n.º 5
0
    def __init__(self, eventloop=None):

        if eventloop is None:
            eventloop = new_event_loop()

        self._asyncioEventloop = eventloop
        self._writers = {}
        self._readers = {}
        self._delayedCalls = set()
        self._continuousPolling = _ContinuousPolling(self)
        super().__init__()
Exemplo n.º 6
0
 def test_removeWriter(self):
     """
     Removing a writer stops the C{LoopingCall}.
     """
     poller = _ContinuousPolling(Clock())
     writer = object()
     poller.addWriter(writer)
     poller.removeWriter(writer)
     self.assertIsNone(poller._loop)
     self.assertEqual(poller._reactor.getDelayedCalls(), [])
     self.assertFalse(poller.isWriting(writer))
Exemplo n.º 7
0
    def __init__(self, eventloop=None):

        if eventloop is None:
            eventloop = new_event_loop()

        self._asyncioEventloop = eventloop
        self._writers = {}
        self._readers = {}
        self._delayedCalls = set()
        self._continuousPolling = _ContinuousPolling(self)
        super().__init__()
Exemplo n.º 8
0
 def test_connectionLostOnWrite(self):
     """
     If a C{doWrite} returns a value indicating disconnection,
     C{connectionLost} is called on it.
     """
     reactor = Clock()
     poller = _ContinuousPolling(reactor)
     desc = Descriptor()
     desc.doWrite = lambda: ConnectionDone()
     poller.addWriter(desc)
     self.assertEqual(desc.events, [])
     reactor.advance(0.001)
     self.assertEqual(desc.events, ["lost"])
Exemplo n.º 9
0
    def __init__(self, eventloop=None):

        if eventloop is None:
            eventloop = get_event_loop()

        self._asyncioEventloop = eventloop
        self._writers = {}
        self._readers = {}
        self._continuousPolling = _ContinuousPolling(self)

        self._scheduledAt = None
        self._timerHandle = None

        super().__init__()
Exemplo n.º 10
0
 def test_addWriter(self):
     """
     Adding a writer when there was previously no writer starts up a
     C{LoopingCall}.
     """
     poller = _ContinuousPolling(Clock())
     self.assertIsNone(poller._loop)
     writer = object()
     self.assertFalse(poller.isWriting(writer))
     poller.addWriter(writer)
     self.assertIsNotNone(poller._loop)
     self.assertTrue(poller._loop.running)
     self.assertIs(poller._loop.clock, poller._reactor)
     self.assertTrue(poller.isWriting(writer))
Exemplo n.º 11
0
 def __init__(self):
     """
     Initialize epoll object, file descriptor tracking dictionaries, and the
     base class.
     """
     # Create the poller we're going to use.  The 1024 here is just a hint
     # to the kernel, it is not a hard maximum.  After Linux 2.6.8, the size
     # argument is completely ignored.
     self._poller = epoll(1024)
     self._reads = set()
     self._writes = set()
     self._selectables = {}
     self._continuousPolling = posixbase._ContinuousPolling(self)
     posixbase.PosixReactorBase.__init__(self)
Exemplo n.º 12
0
 def __init__(self):
     """
     Initialize epoll object, file descriptor tracking dictionaries, and the
     base class.
     """
     # Create the poller we're going to use.  The 1024 here is just a hint
     # to the kernel, it is not a hard maximum.  After Linux 2.6.8, the size
     # argument is completely ignored.
     self._poller = epoll(1024)
     self._reads = set()
     self._writes = set()
     self._selectables = {}
     self._continuousPolling = posixbase._ContinuousPolling(self)
     posixbase.PosixReactorBase.__init__(self)
Exemplo n.º 13
0
 def test_writerPolling(self):
     """
     Adding a writer causes its C{doWrite} to be called every 1
     milliseconds.
     """
     reactor = Clock()
     poller = _ContinuousPolling(reactor)
     desc = Descriptor()
     poller.addWriter(desc)
     self.assertEqual(desc.events, [])
     reactor.advance(0.001)
     self.assertEqual(desc.events, ["write"])
     reactor.advance(0.001)
     self.assertEqual(desc.events, ["write", "write"])
     reactor.advance(0.001)
     self.assertEqual(desc.events, ["write", "write", "write"])
Exemplo n.º 14
0
 def test_removeAll(self):
     """
     L{_ContinuousPolling.removeAll} removes all descriptors and returns
     the readers and writers.
     """
     poller = _ContinuousPolling(Clock())
     reader = object()
     writer = object()
     both = object()
     poller.addReader(reader)
     poller.addReader(both)
     poller.addWriter(writer)
     poller.addWriter(both)
     removed = poller.removeAll()
     self.assertEqual(poller.getReaders(), [])
     self.assertEqual(poller.getWriters(), [])
     self.assertEqual(len(removed), 3)
     self.assertEqual(set(removed), set([reader, writer, both]))
Exemplo n.º 15
0
 def test_multipleReadersAndWriters(self):
     """
     Adding multiple readers and writers results in a single
     C{LoopingCall}.
     """
     poller = _ContinuousPolling(Clock())
     writer = object()
     poller.addWriter(writer)
     self.assertIsNotNone(poller._loop)
     poller.addWriter(object())
     self.assertIsNotNone(poller._loop)
     poller.addReader(object())
     self.assertIsNotNone(poller._loop)
     poller.addReader(object())
     poller.removeWriter(writer)
     self.assertIsNotNone(poller._loop)
     self.assertTrue(poller._loop.running)
     self.assertEqual(len(poller._reactor.getDelayedCalls()), 1)
Exemplo n.º 16
0
    def __init__(self, eventloop: Optional[SelectorEventLoop] = None):
        if eventloop is None:
            _eventloop = get_event_loop()  # type: AbstractEventLoop
        else:
            _eventloop = eventloop

        # On Python 3.8+, asyncio.get_event_loop() on
        # Windows was changed to return a ProactorEventLoop
        # unless the loop policy has been changed.
        if not isinstance(_eventloop, SelectorEventLoop):
            raise TypeError(
                "SelectorEventLoop required, instead got: {}".format(_eventloop)
            )

        self._asyncioEventloop = _eventloop  # type: SelectorEventLoop
        self._writers = {}  # type: Dict[Type[FileDescriptor], int]
        self._readers = {}  # type: Dict[Type[FileDescriptor], int]
        self._continuousPolling = _ContinuousPolling(self)

        self._scheduledAt = None
        self._timerHandle = None

        super().__init__()