Пример #1
0
 async def test__listener_when_fails_passes(self, stub_chunk_event,
                                            mock_app):
     stream = hikari_test_helpers.stub_class(
         stateful_guild_chunker.ChunkStream,
         _filters=iterators.All(()),
         _active=False,
         _missing_chunks=[2, 3, 4, 5, 6, 7, 9],
         _queue=asyncio.Queue(),
         _app=mock_app,
     )
     stream.filter(lambda _: True)
     await stream._listener(stub_chunk_event)
     assert stream._missing_chunks == [2, 3, 4, 6, 7, 9]
     assert stream._queue.qsize() == 1
     assert stream._queue.get_nowait() is stub_chunk_event
     mock_app.dispatcher.unsubscribe.assert_not_called()
Пример #2
0
    def test_filter_for_inactive_stream(self):
        stream = hikari_test_helpers.stub_class(event_stream.EventStream,
                                                _filters=iterators.All(()),
                                                _active=False)
        first_pass = mock.Mock(attr=True)
        second_pass = mock.Mock(attr=True)
        first_fails = mock.Mock(attr=True)
        second_fail = mock.Mock(attr=False)

        def predicate(obj):
            return obj in (first_pass, second_pass)

        stream.filter(predicate, attr=True)

        assert stream._filters(first_pass) is True
        assert stream._filters(first_fails) is False
        assert stream._filters(second_pass) is True
        assert stream._filters(second_fail) is False
Пример #3
0
 async def test__listener_when_chunks_finished(self, stub_chunk_event,
                                               mock_app):
     stream = hikari_test_helpers.stub_class(
         stateful_guild_chunker.ChunkStream,
         _filters=iterators.All(()),
         _active=False,
         _missing_chunks=[5],
         _queue=asyncio.Queue(),
         _registered_listener=object(),
         _app=mock_app,
     )
     stream.filter(lambda _: True)
     await stream._listener(stub_chunk_event)
     assert stream._missing_chunks == []
     assert stream._queue.qsize() == 1
     assert stream._queue.get_nowait() is stub_chunk_event
     mock_app.dispatcher.unsubscribe.assert_called_once_with(
         shard_events.MemberChunkEvent, stream._registered_listener)
Пример #4
0
 def __init__(
     self,
     app: traits.BotAware,
     event_type: typing.Type[EventT],
     *,
     timeout: typing.Union[float, int, None],
     limit: typing.Optional[int] = None,
 ) -> None:
     self._app = app
     self._active = False
     self._event_type = event_type
     self._filters: iterators.All[EventT] = iterators.All(())
     # We accept `None` to represent unlimited here to be consistent with how `None` is already used to represent
     # unlimited for timeout in other places.
     self._queue: asyncio.Queue[EventT] = asyncio.Queue(limit or 0)
     self._registered_listener: typing.Optional[typing.Callable[
         [EventT], typing.Coroutine[typing.Any, typing.Any, None]]] = None
     # The registered wrapping function for the weak ref to this class's _listener method.
     self._timeout = timeout
Пример #5
0
    def test_filter_for_inactive_stream(self):
        stream = hikari_test_helpers.mock_class_namespace(
            event_stream.EventStream)(app=mock.Mock(),
                                      event_type=events.Event,
                                      timeout=1)
        stream._filters = iterators.All(())
        first_pass = mock.Mock(attr=True)
        second_pass = mock.Mock(attr=True)
        first_fails = mock.Mock(attr=True)
        second_fail = mock.Mock(attr=False)

        def predicate(obj):
            return obj in (first_pass, second_pass)

        stream.filter(predicate, attr=True)

        assert stream._filters(first_pass) is True
        assert stream._filters(first_fails) is False
        assert stream._filters(second_pass) is True
        assert stream._filters(second_fail) is False
Пример #6
0
 def __init__(
     self,
     event_manager: event_manager_.EventManager,
     event_type: typing.Type[event_manager_.EventT],
     *,
     timeout: typing.Union[float, int, None],
     limit: typing.Optional[int] = None,
 ) -> None:
     self._active = False
     self._event: typing.Optional[asyncio.Event] = None
     self._event_manager = event_manager
     self._event_type = event_type
     self._filters: iterators.All[event_manager_.EventT] = iterators.All(())
     self._limit = limit
     self._queue: typing.List[event_manager_.EventT] = []
     self._registered_listener: typing.Optional[typing.Callable[
         [event_manager_.EventT], typing.Coroutine[typing.Any, typing.Any,
                                                   None]]] = None
     # The registered wrapping function for the weak ref to this class's _listener method.
     self._timeout = timeout
Пример #7
0
    async def test_filter_handles_calls_while_active(self):
        stream = hikari_test_helpers.mock_class_namespace(
            event_manager_base.EventStream)(event_manager=mock.Mock(),
                                            event_type=base_events.Event,
                                            timeout=1)
        stream._filters = iterators.All(())
        first_pass = mock.Mock(attr=True)
        second_pass = mock.Mock(attr=True)
        first_fails = mock.Mock(attr=True)
        second_fail = mock.Mock(attr=False)
        await stream._listener(first_pass)
        await stream._listener(first_fails)
        await stream._listener(second_pass)
        await stream._listener(second_fail)

        def predicate(obj):
            return obj in (first_pass, second_pass)

        with stream:
            stream.filter(predicate, attr=True)

            assert await stream == [first_pass, second_pass]